{"record":{"id":"1e8a84a1ab5dee38","repo":"tracel-ai/burn","slug":"tensors-are-not-broadcastable-along-dimension","errorCode":null,"errorMessage":"Tensors are not broadcastable along dimension {}","messagePattern":"Tensors are not broadcastable along dimension (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/matmul.rs","lineNumber":211,"sourceCode":"    let shape_rhs = rhs.shape();\n    let ndims = shape_lhs.num_dims();\n\n    // Broadcast the shapes except along dim\n    let mut broadcast_shape = vec![0; ndims];\n    for i in 0..ndims {\n        if i == dim {\n            broadcast_shape[i] = shape_lhs[i]; // already checked to be 3\n        } else {\n            let l = shape_lhs[i];\n            let r = shape_rhs[i];\n            if l == r {\n                broadcast_shape[i] = l;\n            } else if l == 1 {\n                broadcast_shape[i] = r;\n            } else if r == 1 {\n                broadcast_shape[i] = l;\n            } else {\n                panic!(\"Tensors are not broadcastable along dimension {}\", i);\n            }\n        }\n    }\n\n    // Broadcast lhs and rhs\n    let lhs_broadcast = if shape_lhs == broadcast_shape.as_slice() {\n        lhs\n    } else {\n        NdArrayOps::expand(lhs, Shape::from(broadcast_shape.clone()))\n    };\n    let rhs_broadcast = if shape_rhs == broadcast_shape.as_slice() {\n        rhs\n    } else {\n        NdArrayOps::expand(rhs, Shape::from(broadcast_shape.clone()))\n    };\n\n    // Now, move dim to the last dimension\n    let mut perm = (0..ndims).collect::<Vec<_>>();","sourceCodeStart":193,"sourceCodeEnd":229,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/matmul.rs#L193-L229","documentation":"Broadcast-check panic in the ndarray backend's batched cross product: along every dimension other than the batch dim (dim == 3's axis), the two operand shapes must be equal or one must be 1; otherwise they cannot be broadcast for the batched matmul and this panic fires. The failing input is a pair of tensors with incompatible non-batch dimensions.","triggerScenarios":"Calling tensor.cross(other, dim) with tensors whose shapes differ in a leading dimension where both sizes are >1, e.g. [2,3] x [4,3].","commonSituations":"Cross products of vectors from different batch sizes; mixing single vector [3] with batched vectors [N,3] without reshaping.","solutions":["Make both tensors the same rank and shape before cross, e.g. unsqueeze the single vector to [1,3] so it broadcasts.","Reshape/expand one tensor so all leading dims match or equal 1.","Verify both inputs come from the same batched source."],"exampleFix":"// before: a [3], b [4,3]\nlet c = a.cross(b, -1); // panic along dim 0\n// after\nlet a2 = a.unsqueeze().expand([4, 3]); // or a.unsqueeze::<2>() broadcast\nlet c = a2.cross(b, -1);","handlingStrategy":"validation","validationCode":"fn ensure_cross_shapes(a: &[usize], b: &[usize]) {\n    assert_eq!(a.len(), b.len(), \"cross requires same rank\");\n    for (l, r) in a.iter().zip(b) {\n        assert!(l == r || *l == 1 || *r == 1, \"cross dims {l} vs {r} not broadcastable\");\n    }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Unsqueeze single vectors before cross with batched vectors.","Keep cross-product inputs from the same batched source.","Assert shapes equal before geometric ops."],"tags":["rust","burn-ndarray","cross","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"}