{"record":{"id":"5945012c03e1924c","repo":"tracel-ai/burn","slug":"matmul-unsupported-dtype","errorCode":null,"errorMessage":"matmul: unsupported dtype {:?}","messagePattern":"matmul: unsupported dtype (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/matmul.rs","lineNumber":104,"sourceCode":"    let lhs_shape = lhs.layout().shape();\n    let rhs_shape = rhs.layout().shape();\n    let lhs_rank = lhs_shape.num_dims();\n    let rhs_rank = rhs_shape.num_dims();\n\n    assert!(lhs_rank >= 2, \"matmul requires at least 2D tensors\");\n    assert!(rhs_rank >= 2, \"matmul requires at least 2D tensors\");\n\n    // Check inner dimensions match: lhs[..., M, K] x rhs[..., K, N]\n    let k_lhs = lhs_shape[lhs_rank - 1];\n    let k_rhs = rhs_shape[rhs_rank - 2];\n    assert_eq!(k_lhs, k_rhs, \"matmul: inner dimensions must match\");\n\n    match lhs.dtype() {\n        DType::F32 => matmul_gemm::<f32>(lhs, rhs),\n        DType::F64 => matmul_gemm::<f64>(lhs, rhs),\n        DType::F16 => matmul_gemm::<f16>(lhs, rhs),\n        DType::BF16 => matmul_bf16(lhs, rhs),\n        _ => panic!(\"matmul: unsupported dtype {:?}\", lhs.dtype()),\n    }\n}\n\n/// Extract 2D matrix strides from a tensor layout.\n/// Returns (row_stride, col_stride) for the last two dimensions.\nfn get_2d_strides(layout: &Layout) -> (isize, isize) {\n    let strides = layout.strides();\n    let ndim = strides.len();\n    let row_stride = strides[ndim - 2];\n    let col_stride = strides[ndim - 1];\n    (row_stride, col_stride)\n}\n\n/// Compute broadcast batch dimensions for batched matmul.\n/// Returns (broadcast_shape, lhs_strides, rhs_strides) where strides map\n/// output batch index to input batch offset (in matrices).\nfn broadcast_batch_dims(\n    lhs_batch: &[usize],","sourceCodeStart":86,"sourceCodeEnd":122,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/matmul.rs#L86-L122","documentation":"The public matmul entry point dispatches on the lhs tensor dtype and supports F32, F64, F16 and BF16 only. Any other dtype (integer dtypes, Bool, etc.) hits the panic arm. Integer matmul exists as a separate int_matmul function.","triggerScenarios":"Calling Tensor::matmul on a Flex tensor with an integer dtype (I64/I32/U8/...) or Bool instead of a float dtype.","commonSituations":"Matmul on one-hot/boolean masks without casting; embedding-index tensors (int64) accidentally fed into a matmul; PyTorch port where torch.matmul handled int inputs; quantized (int8) weights used directly without a dequantize step.","solutions":["Cast the operands to a float dtype before matmul: a.cast::<f32>().matmul(b.cast::<f32>()).","For true integer matmul, use the int tensor API (int_matmul supports I32/I64).","Dequantize int8 weights to float before the linear layer.","Assert both operands are float dtype at the model forward boundary."],"exampleFix":"// before\nlet y = indices.matmul(weights); // indices are I64 -> panic\n// after\nlet y = indices.cast::<f32>().matmul(weights); // or use int_matmul for integer results","handlingStrategy":"validation","validationCode":"assert!(lhs.dtype().is_float() && rhs.dtype().is_float(), \"matmul requires float tensors, got {:?} and {:?}\", lhs.dtype(), rhs.dtype());","typeGuard":"fn is_float_tensor(t: &FlexTensor) -> bool { matches!(t.dtype(), DType::F32 | DType::F64 | DType::F16 | DType::BF16) }","tryCatchPattern":null,"preventionTips":["Cast integer operands to f32/f64 before matmul","Use int_matmul for integer workloads","Dequantize int8 weights to float before linear layers","Assert operand dtypes at model forward boundaries"],"tags":["rust","burn","matmul","dtype"],"backgroundTag":"unsupported-dtype-for-op","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"}