{"record":{"id":"563dcf2b078f56a3","repo":"tracel-ai/burn","slug":"other-reduction-is-not-supported","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/cosine_embedding.rs","lineNumber":98,"sourceCode":"    ///\n    /// - input1: ``[batch_size, embedding_dim]``\n    /// - input2: ``[batch_size, embedding_dim]``\n    /// - target: ``[batch_size]`` with values 1 or -1\n    ///\n    /// # Returns\n    ///\n    /// Loss tensor of shape ``[1]``\n    pub fn forward(\n        &self,\n        input1: Tensor<2>,\n        input2: Tensor<2>,\n        target: Tensor<1, Int>,\n    ) -> Tensor<1> {\n        let tensor = self.forward_no_reduction(input1, input2, target);\n        match &self.reduction {\n            Reduction::Mean | Reduction::Auto => tensor.mean(),\n            Reduction::Sum => tensor.sum(),\n            other => panic!(\"{other:?} reduction is not supported\"),\n        }\n    }\n\n    /// Compute loss without applying reduction.\n    ///\n    /// # Arguments\n    ///\n    /// * `input1` - First input tensor of shape ``[batch_size, embedding_dim]``\n    /// * `input2` - Second input tensor of shape ``[batch_size, embedding_dim]``\n    /// * `target` - Target tensor of shape ``[batch_size]`` with values 1 or -1\n    ///\n    /// # Returns\n    ///\n    /// Tensor of per-element losses with shape ``[batch_size]``\n    pub fn forward_no_reduction(\n        &self,\n        input1: Tensor<2>,\n        input2: Tensor<2>,","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-nn/src/loss/cosine_embedding.rs#L80-L116","documentation":"CosineEmbeddingLoss::forward only supports Mean, Auto, and Sum reductions. Any other Reduction variant (e.g. None) passed to forward hits a catch-all arm that panics. To get per-element losses, use forward_no_reduction instead.","triggerScenarios":"Calling CosineEmbeddingLoss::forward(input1, input2, target) with reduction set to Reduction::None or any variant other than Mean/Auto/Sum.","commonSituations":"Copying reduction config from PyTorch (where 'none' is valid for losses) into burn; constructing Reduction from user config/CLI where the enum has more variants than this loss supports; blindly forwarding a shared Reduction from a config struct.","solutions":["Use Reduction::Mean, Reduction::Sum, or Reduction::Auto when calling forward.","Use CosineEmbeddingLoss::forward_no_reduction(input1, input2, target) to obtain unreduced per-element losses and reduce manually.","Validate/normalize the reduction setting at config-load time before constructing/invoking the loss."],"exampleFix":"// before\nlet loss = criterion.forward(x1, x2, target, Reduction::None); // panics\n// after\nlet per_elem = criterion.forward_no_reduction(x1, x2, target);\nlet loss = per_elem.mean(); // or handle per-element directly","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 cosine embedding: {other:?}\")),\n    }\n}","typeGuard":"fn is_supported_reduction(r: &Reduction) -> bool {\n    matches!(r, Reduction::Mean | Reduction::Auto | Reduction::Sum)\n}","tryCatchPattern":"// burn panics rather than returning Result; isolate in catch_unwind if needed\nlet result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(||\n    criterion.forward(x1, x2, target, reduction.clone())));","preventionTips":["Only pass Reduction::Mean | Auto | Sum to burn loss forwards.","Use forward_no_reduction plus manual reduction whenever you need Reduction::None semantics.","Clamp reduction settings in your training config to the supported set at load time.","Add a unit test asserting your config's reduction is supported before training runs."],"tags":["rust","burn","loss","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"}