{"record":{"id":"84d4ce93a107f259","repo":"tracel-ai/burn","slug":"cannot-reshape-packed-tensor-inner-dimension-i","errorCode":null,"errorMessage":"Cannot reshape packed tensor: inner dimension {} is not aligned with packing factor {num_quants}","messagePattern":"Cannot reshape packed tensor: inner dimension (.+?) is not aligned with packing factor (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-cubecl/src/ops/base.rs","lineNumber":349,"sourceCode":"\n    out\n}\n\n/// Reshape a jit tensor to a new shape\npub fn q_reshape(mut tensor: CubeTensor, shape: Shape) -> CubeTensor {\n    let scheme = tensor.scheme();\n    let curr_shape = tensor.meta.shape();\n\n    let shape_values = match scheme.store {\n        QuantStore::Native => shape.clone(),\n        QuantStore::PackedNative(packed_dim) | QuantStore::PackedU32(packed_dim) => {\n            let rank = shape.num_dims();\n            let mut shape = shape.clone();\n            let packed_d = rank - packed_dim - 1;\n            let num_quants = scheme.num_quants();\n\n            if !shape[packed_d].is_multiple_of(num_quants) {\n                unimplemented!(\n                    \"Cannot reshape packed tensor: inner dimension {} is not aligned with packing factor {num_quants}\",\n                    shape[packed_d]\n                );\n            }\n\n            shape[packed_d] = shape[packed_d].div_ceil(num_quants);\n            shape\n        }\n    };\n\n    let (values, scales) = tensor.quantized_handles().unwrap();\n    let analysis_values = reshape_analysis(\n        values.meta.shape(),\n        Some(values.meta.strides()),\n        &shape_values,\n    );\n    let action_values =\n        analysis_values.action(values.meta.shape(), values.meta.strides(), &shape_values);","sourceCodeStart":331,"sourceCodeEnd":367,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-cubecl/src/ops/base.rs#L331-L367","documentation":"q_reshape reshapes a packed (sub-byte) quantized tensor by dividing the packed dimension size by the packing factor. If the packed dimension's length is not a multiple of the packing factor, the packed values cannot be re-packed losslessly and the op panics with this unimplemented!.","triggerScenarios":"Calling tensor.reshape() on a quantized tensor with packing (e.g. 2-bit packed into u8) where shape[packed_dim] % num_quants != 0, e.g. reshaping a dim of size 7 with a packing factor of 4.","commonSituations":"Reshaping quantized tensors produced from data whose last dimension was padded to non-multiple sizes; dynamic shapes computed at runtime that drift from multiples of the packing factor.","solutions":["Pad or recompute the tensor shape so the packed dimension is a multiple of the packing factor before reshaping","Dequantize, reshape, and re-quantize the tensor","Choose a quantization scheme without sub-byte packing (plain int8) if reshaping is required","Upgrade burn to check whether unpacked reshape support was added"],"exampleFix":"// before\nt.reshape([7, 32]); // packed dim 7 not multiple of 4 -> panic\n// after\nt.reshape([8, 32]); // aligned with packing factor 4","handlingStrategy":"validation","validationCode":"fn reshape_packed_ok(dim: usize, num_quants: usize) -> bool {\n    dim.is_multiple_of(num_quants)\n}","typeGuard":"fn is_packed_aligned(shape: &[usize], packed_dim: usize, scheme: &QuantScheme) -> bool {\n    shape[shape.len() - packed_dim - 1] % scheme.num_quants() == 0\n}","tryCatchPattern":"// unimplemented! panics; validate first:\nif is_packed_aligned(&new_shape, packed_dim, &scheme) { t.reshape(new_shape) } else { t.dequantize().reshape(new_shape) }","preventionTips":["Keep packed dimensions as multiples of the packing factor","Pad shapes before quantizing if reshaping is planned","Prefer int8 (unpacked) schemes for reshape-heavy workloads"],"tags":["quantization","reshape","packing","unimplemented"],"backgroundTag":"reshape-not-multiple-of-packing-factor","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"}