tracel-ai/burn · error

{other:?} reduction is not supported

Error message

{other:?} reduction is not supported

What it means

Reduction-enum exhaustiveness guard in MarginRankingLoss::forward: only Mean/Auto and Sum are handled; passing any other `Reduction` variant panics. The failing input is the unsupported reduction argument.

Source

Thrown at crates/burn-nn/src/loss/margin_ranking.rs:88

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

    /// Compute the loss element-wise for the inputs and target.
    ///
    /// # Shapes
    ///
    /// - first: [...dims]
    /// - second: [...dims]
    /// - target: [...dims]
    /// - output: [...dims]
    pub fn forward_no_reduction<const D: usize>(
        &self,
        first: Tensor<D>,
        second: Tensor<D>,
        target: Tensor<D>,
    ) -> Tensor<D> {
        // -y * (x1 - x2) + margin, then clamp negatives to zero via relu.

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Use Reduction::Mean, Reduction::Sum, or Reduction::Auto.
  2. Use MarginRankingLoss::forward_no_reduction and apply your own reduction or per-pair weighting.
  3. Whitelist valid reduction variants when parsing loss configuration.

Example fix

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

Prevention

When it happens

Trigger: Calling MarginRankingLoss::forward(first, second, target, reduction) with Reduction::None or any unsupported variant.

Common situations: Porting PyTorch MarginRankingLoss(reduction='none') pipelines; config-driven reduction values not checked for this loss; assuming parity with frameworks that accept 'none'.

Related errors


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