{"record":{"id":"3aa8b2ee9a56ac47","repo":"tracel-ai/burn","slug":"broadcast-arguments-must-be-positive-or-1-got","errorCode":null,"errorMessage":"Broadcast arguments must be positive or -1! Got {}","messagePattern":"Broadcast arguments must be positive or -1! Got (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/api/base.rs","lineNumber":3389,"sourceCode":"impl<const D1: usize, const D2: usize, E: AsIndex> BroadcastArgs<D1, D2> for [E; D2] {\n    // Passing -1 as the size for a dimension means not changing the size of that dimension.\n    fn into_shape(self, shape: &Shape) -> Shape {\n        if self.len() < shape.num_dims() {\n            panic!(\n                \"Broadcast arguments must be greater than the number of dimensions! got {}, need at least {}\",\n                self.len(),\n                shape.num_dims()\n            );\n        }\n\n        // Zip the two shapes in reverse order and replace -1 with the actual dimension value.\n        let new_shape: Vec<_> = self\n            .iter()\n            .rev()\n            .map(|x| {\n                let primitive = x.as_index();\n                if primitive < -1 || primitive == 0 {\n                    panic!(\n                        \"Broadcast arguments must be positive or -1! Got {}\",\n                        primitive\n                    );\n                }\n                primitive\n            })\n            .zip(shape.iter().rev().chain(repeat(&0)).take(self.len())) // Pad the original shape with 0s\n            .map(|(x, &y)| if x == -1 { y } else { x as usize })\n            .collect::<Vec<_>>()\n            .into_iter()\n            .rev()\n            .collect();\n\n        if new_shape.contains(&0) {\n            panic!(\n                \"Cannot substitute -1 for a non-existing dimension! Got {:?}\",\n                new_shape\n            );","sourceCodeStart":3371,"sourceCodeEnd":3407,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/api/base.rs#L3371-L3407","documentation":"In BroadcastArgs::into_shape, each broadcast dimension may be a positive size or -1 (meaning 'keep this dimension unchanged'); 0 or anything less than -1 is invalid. Burn panics with the offending value when iterating the arguments right-to-left and finding primitive < -1 or primitive == 0. A zero-sized broadcast dim is almost always a computed value that collapsed (e.g. an empty batch or a division yielding 0).","triggerScenarios":"Passing 0 as a dimension size in a reshape/broadcast arg array; passing -2 or lower (typo or sign error); computing a dim size arithmetically and getting 0 (e.g. len // something); using i64/i32 indices where a negative sentinel other than -1 was produced.","commonSituations":"Dynamic batch sizes hitting 0 on an empty batch; porting numpy semantics where 0 is legal but -1-only semantics apply in Burn; off-by-one or negation bugs in generated shape code.","solutions":["Validate dimension sizes before calling: reject 0 and values < -1, mapping 'keep' semantics to -1 explicitly","Fix the computation producing the dim size (guard against empty inputs, clamp to >= 1 where a real dim is required)","Replace the invalid literal with -1 if the intent was to keep the existing dimension","Clamp/derive the target dims from tensor.dims() instead of hand-computed values"],"exampleFix":"// before\nlet y = x.reshape([0, -1]); // 0 is invalid -> panic\n// after\nlet y = x.reshape([-1, -1]); // -1 keeps the existing dim, or use a positive size","handlingStrategy":"validation","validationCode":"// Reject invalid broadcast dims before calling the API\nfn valid_dim(d: i64) -> bool { d == -1 || d > 0 }\nassert!(args.iter().all(|d| valid_dim(d.as_index())), \"dims must be > 0 or exactly -1\");","typeGuard":null,"tryCatchPattern":"// Panic API; sanitize inputs first:\nlet args: Vec<_> = args.into_iter().map(|d| if d == 0 { 1 } else { d }).collect(); // example sanitization","preventionTips":["Never pass 0 as a dimension size in broadcast/reshape args; use -1 to keep a dim","Guard computed dim sizes against 0 (empty batches, integer division truncation)","Avoid negative sentinels other than -1; -2 or lower is invalid","Validate user- or config-supplied shapes at the boundary"],"tags":["tensor","shape","broadcast","panic","burn"],"backgroundTag":"broadcast-shape-mismatch","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"}