{"record":{"id":"f2184e003eb764ea","repo":"tracel-ai/burn","slug":"broadcast-arguments-must-be-greater-than-the-numbe","errorCode":null,"errorMessage":"Broadcast arguments must be greater than the number of dimensions! got {}, need at least {}","messagePattern":"Broadcast arguments must be greater than the number of dimensions! got (.+?), need at least (.+?)","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/api/base.rs","lineNumber":3375,"sourceCode":"}\n\n/// Trait used for broadcast arguments.\npub trait BroadcastArgs<const D1: usize, const D2: usize> {\n    /// Converts to a shape.\n    fn into_shape(self, shape: &Shape) -> Shape;\n}\n\nimpl<const D1: usize, const D2: usize> BroadcastArgs<D1, D2> for Shape {\n    fn into_shape(self, _shape: &Shape) -> Shape {\n        self\n    }\n}\n\nimpl<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                }","sourceCodeStart":3357,"sourceCodeEnd":3393,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/api/base.rs#L3357-L3393","documentation":"When a shape-like array ([E; D2] of AsIndex) is converted into a broadcast shape via BroadcastArgs::into_shape, the target array must have at least as many entries as the tensor has dimensions (D2 >= D1). Broadcasting aligns dimensions from the right; a shorter argument list is invalid, so Burn panics with the actual vs. required length. This is a shape-specification bug in the caller's reshape/expand call.","triggerScenarios":"Calling reshape/expand-style APIs (e.g. tensor.reshape(...) taking broadcast args) with an array shorter than the tensor's rank D1; hardcoding a small shape literal like [1, 32] against a 4-D tensor; using a const-generic D2 smaller than D1 from a generic function.","commonSituations":"Porting PyTorch view/expand code where -1 semantics differ and fewer dims were passed; writing layer code where the tensor rank changed (added batch/channel dims) but the broadcast literal didn't; miscasting a slice of the shape array.","solutions":["Pad the broadcast argument list with 1s (or -1 to keep dims) on the left so its length is at least the tensor's rank","Use shape.num_dims() or D1 at the call site to build the array with the right const size","Replace the literal with a computed Shape/expand target derived from the tensor's current dims","If ranks vary generically, use APIs accepting Shape or slices rather than fixed-size arrays"],"exampleFix":"// before, x is [B, C, H, W] (D1 = 4)\nlet y = x.reshape([1, -1]); // got 2, need at least 4 -> panic\n// after\nlet y = x.reshape([1, 1, 1, -1]); // length 4 >= rank 4","handlingStrategy":"validation","validationCode":"// Check the broadcast arg length against the tensor rank before reshaping\nlet args_len = args.len();\nlet rank = x.shape().num_dims();\nassert!(args_len >= rank, \"broadcast args ({args_len}) must be >= rank ({rank})\");","typeGuard":null,"tryCatchPattern":"// The API panics rather than returning Result; validate lengths beforehand:\nif args.len() < x.shape().num_dims() { args = pad_with_ones_left(args, x.shape().num_dims()); }","preventionTips":["Left-pad broadcast/reshape literals with 1s (or -1) to match the tensor rank","Derive target shapes from x.dims()/Shape instead of hardcoded short literals","When ranks change (new batch/channel dims), update every reshape/expand site","In generic code, tie D2 >= D1 via types rather than runtime literals"],"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"}