tracel-ai/burn · error

{other:?} reduction is not supported

Error message

{other:?} reduction is not supported

What it means

Reduction-enum exhaustiveness guard in MultiMarginLoss::forward: supports Mean/Auto and Sum only; any other `Reduction` value panics when passed to forward, indicating an unsupported reduction variant.

Source

Thrown at crates/burn-nn/src/loss/multi_margin.rs:100

    ///
    /// `Reduction::Auto` behaves as `Reduction::Mean`.
    ///
    /// # Shapes
    ///
    /// - input:  `[batch_size, num_classes]`
    /// - target: `[batch_size]` (class indices in `0..num_classes`)
    /// - output: `[1]`
    pub fn forward(
        &self,
        input: Tensor<2>,
        target: Tensor<1, Int>,
        reduction: Reduction,
    ) -> Tensor<1> {
        let loss = self.forward_no_reduction(input, target);
        match reduction {
            Reduction::Mean | Reduction::Auto => loss.mean(),
            Reduction::Sum => loss.sum(),
            other => panic!("{other:?} reduction is not supported"),
        }
    }

    /// Compute the loss for each sample, without reducing.
    ///
    /// # Shapes
    ///
    /// - input:  `[batch_size, num_classes]`
    /// - target: `[batch_size]` (class indices in `0..num_classes`)
    /// - output: `[batch_size]`
    pub fn forward_no_reduction(&self, input: Tensor<2>, target: Tensor<1, Int>) -> Tensor<1> {
        let [batch_size, num_classes] = input.dims();
        let target_indices = target.reshape([batch_size, 1]);

        // Score of the correct class per sample: [batch_size, 1].
        let correct = input.clone().gather(1, target_indices);

        // Sum over ALL classes of max(0, margin - x[y] + x[i]) ^ p: [batch_size, 1].

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use Reduction::Mean, Reduction::Sum, or Reduction::Auto.
  2. Use MultiMarginLoss::forward_no_reduction and reduce manually if per-sample values are needed.
  3. Validate/map the reduction in the config layer before invoking the loss.

Example fix

// before
let loss = criterion.forward(input, target, Reduction::None); // panics
// after
let elem = criterion.forward_no_reduction(input, target);
let loss = elem.mean();
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 multi margin: {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, reduction.clone())));

Prevention

When it happens

Trigger: Calling MultiMarginLoss::forward(input, target, reduction) with Reduction::None or any non-Mean/Auto/Sum variant.

Common situations: Porting PyTorch MultiMarginLoss(reduction='none'); a single reduction setting shared across multiple losses where this one is stricter; stale enum values from config files.

Related errors


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