tracel-ai/burn · error

{other:?} reduction is not supported

Error message

{other:?} reduction is not supported

What it means

GaussianNllLoss::forward accepts only Mean, Auto, and Sum reductions and panics on any other variant. It computes the element-wise loss via forward_no_reduction and then reduces; unknown reductions are rejected with this panic.

Source

Thrown at crates/burn-nn/src/loss/gaussian_nll.rs:91

    ///
    /// # Shapes
    ///
    /// - input:  `[...dims]` (predicted mean)
    /// - target: `[...dims]`
    /// - var:    `[...dims]` (predicted variance, positive)
    /// - output: `[1]`
    pub fn forward<const D: usize>(
        &self,
        input: Tensor<D>,
        target: Tensor<D>,
        var: Tensor<D>,
        reduction: Reduction,
    ) -> Tensor<1> {
        let loss = self.forward_no_reduction(input, target, var);
        match reduction {
            Reduction::Mean | Reduction::Auto => loss.mean(),
            Reduction::Sum => loss.sum(),
            other => panic!("{other:?} reduction is not supported"),
        }
    }

    /// Compute the loss element-wise.
    ///
    /// # Shapes
    ///
    /// - input:  `[...dims]` (predicted mean)
    /// - target: `[...dims]`
    /// - var:    `[...dims]` (predicted variance, positive)
    /// - output: `[...dims]`
    pub fn forward_no_reduction<const D: usize>(
        &self,
        input: Tensor<D>,
        target: Tensor<D>,
        var: Tensor<D>,
    ) -> Tensor<D> {
        // Clamp the variance for numerical stability.

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Pass Reduction::Mean, Reduction::Sum, or Reduction::Auto.
  2. Use forward_no_reduction to get the element-wise loss and apply your own reduction.
  3. Sanitize reduction values at config parsing time with a whitelist of supported variants.

Example fix

// before
let loss = criterion.forward(pred, target, var, Reduction::None); // panics
// after
let elem = criterion.forward_no_reduction(pred, target, var);
let loss = elem.sum();
Defensive patterns

Strategy: validation

Validate before calling

fn ensure_supported(r: &Reduction) -> Result<(), String> {
    match r {
        Reduction::Mean | Reduction::Auto | Reduction::Sum => Ok(()),
        other => Err(format!("unsupported reduction for gaussian NLL: {other:?}")),
    }
}

Type guard

fn is_supported_reduction(r: &Reduction) -> bool {
    matches!(r, Reduction::Mean | Reduction::Auto | Reduction::Sum)
}

Try / catch

let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(||
    criterion.forward(input, target, var, reduction.clone())));

Prevention

When it happens

Trigger: Calling GaussianNllLoss::forward(input, target, var, reduction) where reduction is Reduction::None or any variant outside Mean/Auto/Sum.

Common situations: Translating PyTorch GaussianNLLLoss(reduction='none') usage to burn; a shared LossReduction config field flowing into this loss; typos or stale enum values from older burn versions.

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/e1391fe517663b12. Report an issue: GitHub.