{"record":{"id":"c17436f25c959081","repo":"tracel-ai/burn","slug":"other-reduction-is-not-supported-c17436","errorCode":null,"errorMessage":"{other:?} reduction is not supported","messagePattern":"(.+?) reduction is not supported","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-nn/src/loss/ctc.rs","lineNumber":203,"sourceCode":"        &self,\n        log_probs: Tensor<3>,\n        targets: Tensor<2, Int>,\n        input_lengths: Tensor<1, Int>,\n        target_lengths: Tensor<1, Int>,\n        reduction: Reduction,\n    ) -> Tensor<1> {\n        let ctc_loss_tensor =\n            self.forward(log_probs, targets, input_lengths, target_lengths.clone());\n\n        match reduction {\n            Reduction::Auto | Reduction::Mean => {\n                // Following PyTorch's behavior where the output losses are divided\n                // by the target lengths and then the mean over the batch is taken\n                let target_lengths_float = target_lengths.float();\n                ctc_loss_tensor.div(target_lengths_float).mean()\n            }\n            Reduction::Sum => ctc_loss_tensor.sum(),\n            other => panic!(\"{other:?} reduction is not supported\"),\n        }\n    }\n\n    /// Checks the per-element length invariants required by the alpha\n    /// recursion. These require reading the length tensors from the device,\n    /// so the checks are gated behind `cfg(debug_assertions)` to avoid the\n    /// device-to-host sync in release builds.\n    ///\n    /// Validated:\n    /// - `target_lengths[i] >= 0`\n    /// - `target_lengths[i] <= max_target_len`\n    /// - `input_lengths[i] >= target_lengths[i]`\n    /// - `input_lengths[i] <= max_input_length`\n    #[allow(unused_variables)]\n    fn length_assertions(\n        &self,\n        input_lengths: Tensor<1, Int>,\n        target_lengths: Tensor<1, Int>,","sourceCodeStart":185,"sourceCodeEnd":221,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-nn/src/loss/ctc.rs#L185-L221","documentation":"CtcLoss::forward_with_reduction supports only Mean, Auto, and Sum reductions; any other Reduction variant panics with this message. The CTC loss otherwise follows PyTorch's behavior of dividing per-sample losses by target lengths before averaging.","triggerScenarios":"Calling CtcLoss::forward_with_reduction with reduction = Reduction::None (or any non-Mean/Auto/Sum variant).","commonSituations":"Porting PyTorch CTCLoss(reduction='none') code to burn; passing a user-configured Reduction through without validating supported variants; expecting per-sequence unreduced CTC losses via forward with None.","solutions":["Use Reduction::Mean, Reduction::Sum, or Reduction::Auto.","If per-sequence (unreduced) losses are needed, check whether the module exposes a no-reduction variant and use it, otherwise compute per-sample inside a loop or patch/extend the loss.","Normalize the reduction in your config layer to only the supported variants before invoking the loss."],"exampleFix":"// before\nlet loss = ctc.forward_with_reduction(logits, targets, input_lengths, target_lengths, Reduction::None);\n// after\nlet loss = ctc.forward_with_reduction(logits, targets, input_lengths, target_lengths, Reduction::Mean);","handlingStrategy":"validation","validationCode":"fn ensure_supported(r: &Reduction) -> Result<(), String> {\n    match r {\n        Reduction::Mean | Reduction::Auto | Reduction::Sum => Ok(()),\n        other => Err(format!(\"unsupported reduction for CTC loss: {other:?}\")),\n    }\n}","typeGuard":"fn is_supported_reduction(r: &Reduction) -> bool {\n    matches!(r, Reduction::Mean | Reduction::Auto | Reduction::Sum)\n}","tryCatchPattern":"let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(||\n    ctc.forward_with_reduction(logits, targets, in_lens, tgt_lens, reduction.clone())));","preventionTips":["Restrict CTC loss config to Mean/Auto/Sum.","For per-sequence losses, use a no-reduction path or compute manually instead of Reduction::None.","Validate reduction in config parsing, not at call time.","Add tests covering every reduction value your config can produce."],"tags":["rust","burn","loss","ctc","reduction"],"backgroundTag":"unsupported-reduction","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}