{"record":{"id":"3a872b7fd93a3b23","repo":"tracel-ai/burn","slug":"incompatible-shapes-for-broadcasting-and","errorCode":null,"errorMessage":"Incompatible shapes for broadcasting: {:?} and {:?}","messagePattern":"Incompatible shapes for broadcasting: (.+?) and (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/base.rs","lineNumber":824,"sourceCode":"        let lhs_dim = if i < lhs_shape.len() {\n            lhs_shape[lhs_shape.len() - 1 - i]\n        } else {\n            1\n        };\n        let rhs_dim = if i < rhs_shape.len() {\n            rhs_shape[rhs_shape.len() - 1 - i]\n        } else {\n            1\n        };\n\n        if lhs_dim == rhs_dim {\n            broadcast_shape[ndims - 1 - i] = lhs_dim;\n        } else if lhs_dim == 1 {\n            broadcast_shape[ndims - 1 - i] = rhs_dim;\n        } else if rhs_dim == 1 {\n            broadcast_shape[ndims - 1 - i] = lhs_dim;\n        } else {\n            panic!(\n                \"Incompatible shapes for broadcasting: {:?} and {:?}\",\n                lhs_shape, rhs_shape\n            );\n        }\n    }\n\n    // Create IxDyn from broadcast shape\n    let broadcast_dim = ndarray::IxDyn(&broadcast_shape);\n\n    // Broadcast both arrays\n    let lhs_broadcast = lhs\n        .broadcast(broadcast_dim.clone())\n        .expect(\"Failed to broadcast lhs\");\n    let rhs_broadcast = rhs\n        .broadcast(broadcast_dim)\n        .expect(\"Failed to broadcast rhs\");\n\n    (lhs_broadcast, rhs_broadcast)","sourceCodeStart":806,"sourceCodeEnd":842,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/base.rs#L806-L842","documentation":"When preparing a binary elementwise op, both operands are broadcast to a common shape. Broadcasting fails when, at some trailing-aligned dimension, neither side is 1 and the two dimensions differ. The backend panics with the two incompatible shapes.","triggerScenarios":"Applying elementwise ops (remainder, equal, greater, greater_equal, lower_equal, lower) on tensors whose shapes cannot be broadcast, e.g. [3, 4] vs [5, 4] or [3, 4] vs [2].","commonSituations":"Comparing tensors of different batch sizes; comparing a [N] tensor with a [M, N] tensor expecting implicit prepending broadcast (not supported the same way); dimension count errors from squeeze/unsqueeze omissions.","solutions":["Reshape/unsqueeze the smaller tensor so its dimensions align with broadcast rules (trailing dimensions must be equal or 1).","Expand one operand explicitly to the target shape before the op.","Fix the pipeline so both operands are produced with compatible shapes.","Print lhs.dims() and rhs.dims() and verify each trailing dimension pair is equal or one of them is 1."],"exampleFix":"// before\nlet a = Tensor::<_,_,NdArray>::zeros([3, 4], &device);\nlet b = Tensor::zeros([5, 4], &device);\nlet c = a.greater(b);\n// after\nlet b = Tensor::zeros([1, 4], &device).repeat(0, 3); // or fix dims to [3,4]\nlet c = a.greater(b);","handlingStrategy":"validation","validationCode":"fn can_broadcast(a: &[usize], b: &[usize]) -> bool {\n    a.iter().rev().zip(b.iter().rev()).all(|(x, y)| x == y || *x == 1 || *y == 1)\n}\nassert!(can_broadcast(&lhs.dims(), &rhs.dims()));","typeGuard":"fn can_broadcast(a: &[usize], b: &[usize]) -> bool {\n    a.iter().rev().zip(b.iter().rev()).all(|(x, y)| x == y || *x == 1 || *y == 1)\n}","tryCatchPattern":null,"preventionTips":["Follow broadcasting rules: trailing dims equal or 1","unsqueeze/reshape to align ranks before elementwise ops","Add shape assertions at pipeline boundaries"],"tags":["rust","burn","broadcasting","shape-mismatch"],"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"}