{"record":{"id":"22d1fbf2775fba35","repo":"tracel-ai/burn","slug":"dimensions-are-incompatible-for-matrix-multiplicat","errorCode":null,"errorMessage":"Dimensions are incompatible for matrix multiplication: LHS columns ({}) != ({})","messagePattern":"Dimensions are incompatible for matrix multiplication: LHS columns \\((.+?)\\) != \\((.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-ndarray/src/ops/matmul.rs","lineNumber":127,"sourceCode":"/// * If the matrix multiplication dimensions (last 2) are incompatible.\n/// * If any other dimension is not the same for both tensors, or equal to 1. (Any dimension where\n///   one dim is equal to 1 is broadcast.)\nfn output_shape(lsh: &[usize], rsh: &[usize]) -> (Shape, Strides, Strides, Strides) {\n    let ndims = lsh.num_dims();\n    if ndims < 2 {\n        panic!(\n            \"Matrix multiplication requires an array with at least 2 dimensions. Got Rank {}\",\n            ndims\n        );\n    }\n\n    // Fetch matrix dimensions and check compatibility.\n    let l_rows = lsh[ndims - 2];\n    let l_cols = lsh[ndims - 1];\n    let r_rows = rsh[ndims - 2];\n    let r_cols = rsh[ndims - 1];\n    if l_cols != r_rows {\n        panic!(\n            \"Dimensions are incompatible for matrix multiplication: LHS columns ({}) != ({})\",\n            l_cols, r_rows\n        );\n    }\n    // Set matrix dimensions of the output shape.\n    let mut osh = vec![0; ndims];\n    osh[ndims - 2] = l_rows;\n    osh[ndims - 1] = r_cols;\n\n    // Set other array dimensions, broadcasting as necessary.\n    // Compute the strides inline.\n    let mut cur_l_stride: usize = 1;\n    let mut cur_r_stride: usize = 1;\n    let mut cur_o_stride: usize = 1;\n    let mut l_strides = Vec::with_capacity(ndims - 2);\n    let mut r_strides = Vec::with_capacity(ndims - 2);\n    let mut o_strides = Vec::with_capacity(ndims - 2);\n    for i in (0..ndims - 2).rev() {","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-ndarray/src/ops/matmul.rs#L109-L145","documentation":"For matrix multiplication the LHS's last dimension (columns) must equal the RHS's second-to-last dimension (rows). output_shape checks this when computing the result shape and panics when they differ.","triggerScenarios":"Calling tensor.matmul(other) where lhs.shape()[last] != rhs.shape()[-2], e.g. matmul of [3,4] by [3,4] or [2,3] by [4,5] without transposing.","commonSituations":"Forgetting to transpose the weight matrix; mismatched feature counts between layers (in_features vs out_features); loading weights from a checkpoint with transposed layout.","solutions":["Transpose the RHS: rhs.transpose() so its rows match the LHS columns.","Fix the shapes so inner dimensions match (e.g. Linear layer weights should be [in_features, out_features] or transposed per the layer convention).","Print both shapes before matmul and adjust reshape/permute accordingly.","Check layer definitions/configs for swapped in/out feature sizes."],"exampleFix":"// before: lhs [2,3], rhs [4,5]\nlet y = lhs.matmul(rhs); // panic 3 != 4\n// after\nlet y = lhs.matmul(rhs.transpose()); // rhs now [5,4] -> still needs 3==5; correct fix:\n// ensure rhs has shape [3,5], e.g. rhs = weight.transpose() when weight is [5,3]","handlingStrategy":"validation","validationCode":"fn ensure_matmul_inner_dims(lsh: &[usize], rsh: &[usize]) {\n    assert!(lsh[lsh.len()-1] == rsh[rsh.len()-2],\n        \"matmul inner dims differ: {} vs {}\", lsh[lsh.len()-1], rsh[rsh.len()-2]);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Transposing weights once at load time avoids repeated transpose fixes.","Verify Linear layer weight layouts (in_features x out_features) match backend convention.","Log shapes of weights and activations at layer wiring time.","Unit-test layer shapes with tiny tensors before full runs."],"tags":["rust","burn-ndarray","matmul","shape"],"backgroundTag":"matmul-dimension-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"}