{"record":{"id":"d173c367321cd4a4","repo":"tracel-ai/burn","slug":"dimensions-differ-and-cannot-be-broadcasted","errorCode":null,"errorMessage":"Dimensions differ and cannot be broadcasted.","messagePattern":"Dimensions differ and cannot be broadcasted\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/matmul.rs","lineNumber":166,"sourceCode":"\n        // Compatible dimensions are:\n        //   1. Both dimensions are equal.\n        //   2. One of the dimensions is equal to 1.\n        let o_dim: usize;\n        if l_dim == r_dim {\n            o_dim = l_dim; // both dimensions are equal\n            l_strides.push(cur_l_stride);\n            r_strides.push(cur_r_stride);\n        } else if l_dim == 1 {\n            o_dim = r_dim; // broadcast the left\n            l_strides.push(0);\n            r_strides.push(cur_r_stride);\n        } else if r_dim == 1 {\n            o_dim = l_dim; // broadcast the right\n            l_strides.push(cur_l_stride);\n            r_strides.push(0);\n        } else {\n            panic!(\"Dimensions differ and cannot be broadcasted.\");\n        }\n        osh[i] = o_dim;\n        o_strides.push(cur_o_stride);\n        cur_o_stride *= o_dim;\n\n        cur_l_stride *= l_dim;\n        cur_r_stride *= r_dim;\n    }\n    l_strides.reverse();\n    r_strides.reverse();\n    o_strides.reverse();\n\n    (\n        Shape::from(osh),\n        Strides::new(l_strides),\n        Strides::new(r_strides),\n        Strides::new(o_strides),\n    )","sourceCodeStart":148,"sourceCodeEnd":184,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/matmul.rs#L148-L184","documentation":"output_shape supports batched/broadcast matmul, but a leading (batch) dimension pair can only differ if one of them is 1 (broadcastable). If both dimensions are non-1 and unequal, broadcasting is impossible and the backend panics.","triggerScenarios":"matmul of tensors with batch shapes like [2,3,4] x [5,4,6] where dim0 is 2 vs 5 (neither is 1); calling matmul with mismatched batch or channel counts.","commonSituations":"Batch-size mismatch between two pipeline branches; mixing per-sample and batched tensors; concatenating datasets with different batch sizes.","solutions":["Ensure batch dimensions match, or make one side 1 to enable broadcasting (e.g. reshape to [1,3,4]).","Use expand/repeat on the smaller tensor to match the larger batch shape explicitly.","Align data pipeline batch sizes before matmul.","Check that the tensors come from compatible batched sources (same batch dim)."],"exampleFix":"// before: [2,3,4] x [5,4,6]\nlet y = a.matmul(b); // panic\n// after\nlet b2 = b.reshape([1, 5, 4, 6]); // or fix a's batch dim to 5\nlet y = a.reshape([1, 2, 3, 4]).matmul(b2); // dims of size 1 broadcast","handlingStrategy":"validation","validationCode":"fn ensure_broadcastable_batch_dims(lsh: &[usize], rsh: &[usize]) {\n    for (l, r) in lsh[..lsh.len()-2].iter().zip(&rsh[..rsh.len()-2]) {\n        assert!(l == r || *l == 1 || *r == 1, \"batch dims {l} vs {r} not broadcastable\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Fix batch sizes across the whole training/eval pipeline.","Use size-1 leading dims when you intend broadcasting.","Expand explicitly instead of relying on implicit broadcast in complex graphs.","Assert batch dims equal at merge points of multi-branch models."],"tags":["rust","burn-ndarray","matmul","broadcast"],"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"}