{"record":{"id":"77027a852a18bbe7","repo":"tracel-ai/burn","slug":"broadcast-shape-incompatible-dimensions-and","errorCode":null,"errorMessage":"broadcast_shape: incompatible dimensions {} and {} at position {}","messagePattern":"broadcast_shape: incompatible dimensions (.+?) and (.+?) at position (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/expand.rs","lineNumber":38,"sourceCode":"        let lhs_dim = if lhs_idx >= 0 {\n            lhs[lhs_idx as usize]\n        } else {\n            1\n        };\n        let rhs_dim = if rhs_idx >= 0 {\n            rhs[rhs_idx as usize]\n        } else {\n            1\n        };\n\n        if lhs_dim == rhs_dim {\n            *out = lhs_dim;\n        } else if lhs_dim == 1 {\n            *out = rhs_dim;\n        } else if rhs_dim == 1 {\n            *out = lhs_dim;\n        } else {\n            panic!(\n                \"broadcast_shape: incompatible dimensions {} and {} at position {}\",\n                lhs_dim, rhs_dim, i\n            );\n        }\n    }\n\n    Shape::from(result)\n}\n\n/// Broadcast two tensors to the same shape for binary operations.\npub fn broadcast_binary(lhs: FlexTensor, rhs: FlexTensor) -> (FlexTensor, FlexTensor) {\n    let lhs_shape = lhs.layout().shape().clone();\n    let rhs_shape = rhs.layout().shape().clone();\n\n    if lhs_shape == rhs_shape {\n        return (lhs, rhs);\n    }\n","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/expand.rs#L20-L56","documentation":"broadcast_shape computes the output shape for a broadcasting binary op. When two non-1 dimensions at the same position differ, broadcasting is impossible and the function panics with both dim sizes and the position.","triggerScenarios":"Calling any broadcasting binary op (through broadcast_binary) with two tensors whose shapes have mismatched non-1 dimensions at some position, e.g. [2,3] vs [4,3] or [8] vs [6].","commonSituations":"Matrix-with-vector shape mistakes (batch dim mismatch), off-by-one rank handling, mixing tensors from differently-shaped intermediate results, forgetting to reshape/expand before an elementwise op.","solutions":["Check shapes before the op: for each aligned (from the right) position, dims must be equal or one must be 1","Reshape/expand one tensor to a compatible shape before the binary op","Fix upstream shape computations so the operands are produced with compatible shapes"],"exampleFix":"// before\nlet c = broadcast_binary(a /* [4,3] */, b /* [8,3] */);\n// after\nlet b2 = b.reshape([4, 3]); // or fix upstream so shapes align\nlet c = broadcast_binary(a, b2);","handlingStrategy":"validation","validationCode":"fn broadcastable(l: &[usize], r: &[usize]) -> bool {\n    let n = l.len().max(r.len());\n    (0..n).all(|i| {\n        let a = if i < l.len() { l[l.len() - 1 - i] } else { 1 };\n        let b = if i < r.len() { r[r.len() - 1 - i] } else { 1 };\n        a == b || a == 1 || b == 1\n    })\n}\nassert!(broadcastable(&a.shape().dims, &b.shape().dims), \"shapes {:?} and {:?} are not broadcastable\", a.shape().dims, b.shape().dims);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Log both operand shapes before every broadcasting binary op in debug builds","Reshape/expand explicitly instead of relying on implicit broadcasting for tricky ranks","Right-align shapes mentally when mixing ranks (e.g. [B] with [B,N])","Centralize shape assertions in a helper used by all elementwise ops"],"tags":["rust","burn","shape","broadcast","panic"],"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"}