{"record":{"id":"59672cc26b1fd85d","repo":"tracel-ai/burn","slug":"cannot-reshape-a-block-quantized-tensor-when-the-r","errorCode":null,"errorMessage":"Cannot reshape a block-quantized tensor when the reshape requires recomputing the buffer.","messagePattern":"Cannot reshape a block-quantized tensor when the reshape requires recomputing the buffer\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-cubecl/src/ops/base.rs","lineNumber":465,"sourceCode":"        }\n        // Any action to recompute\n        (ReshapeAction::Recompute, _) | (_, ReshapeAction::Recompute) => {\n            // Rewriting the buffer would have to repack values that share a\n            // storage element; a metadata-only reshape leaves the packing alone.\n            if !is_unsqueeze\n                && matches!(\n                    scheme.value,\n                    QuantValue::Q4S | QuantValue::Q4F | QuantValue::Q2S | QuantValue::Q2F\n                )\n            {\n                todo!(\n                    \"Reshape with sub-byte values is not supported when the buffer must be recomputed\"\n                )\n            }\n\n            if scheme.block_size().is_some() && shape_scales.num_elements() > 1 {\n                // Original block boundaries no longer align with the layout, would have to be recomputed\n                unimplemented!(\n                    \"Cannot reshape a block-quantized tensor when the reshape requires recomputing the buffer.\"\n                );\n            }\n\n            tensor = kernel::into_contiguous(tensor);\n            *tensor.meta = Metadata::new(shape, contiguous_strides(&shape_values));\n\n            let qparams = tensor.qparams.as_mut().unwrap();\n\n            let strides = contiguous_strides(&shape_scales);\n            qparams.scales.metadata = Metadata::new(shape_scales, strides);\n        }\n        (ReshapeAction::NoChange, ReshapeAction::NoChange) => {}\n    }\n\n    tensor\n}\n","sourceCodeStart":447,"sourceCodeEnd":483,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-cubecl/src/ops/base.rs#L447-L483","documentation":"Some reshapes require materializing a new contiguous buffer; for block-quantized tensors with multiple scale blocks, the original block boundaries no longer align with the new layout, so scales would have to be recomputed. Since this is not implemented, q_reshape panics instead of silently corrupting the scales.","triggerScenarios":"Calling reshape on a block-quantized tensor (block_size set, scales.num_elements() > 1) whose new shape requires the buffer to be recomputed (non-trivial stride change), including sub-byte packed values needing buffer recomputation.","commonSituations":"Reshaping non-contiguous or transposed quantized tensors; calling ops that internally reshape before kernels requiring contiguity.","solutions":["Call tensor.clone().into_contiguous() (dequantize then requantize) before reshaping","Avoid reshaping block-quantized tensors; restructure code so shapes stay fixed across quantized ops","Use non-block (per-tensor) quantization where reshaping is needed","Upgrade burn to check if scale recomputation on reshape was implemented"],"exampleFix":"// before\nlet y = x.transpose(0, 1).reshape([rows, cols]); // block-quant -> panic\n// after\nlet y = x.dequantize().transpose(0, 1).reshape([rows, cols]); // then requantize if needed","handlingStrategy":"validation","validationCode":"fn reshape_needs_recompute(tensor_non_contiguous: bool, scheme: &QuantScheme, scales_elems: usize) -> bool {\n    tensor_non_contiguous && scheme.block_size().is_some() && scales_elems > 1\n}","typeGuard":"fn reshape_safe(t: &CubeTensor) -> bool {\n    t.scheme.block_size().is_none() || t.scales_shape().num_elements() <= 1 || t.is_contiguous()\n}","tryCatchPattern":"// Guard:\nif reshape_safe(&t) { t.reshape(new_shape) } else { t.dequantize().reshape(new_shape) }","preventionTips":["Make block-quantized tensors contiguous (or dequantize) before reshaping","Avoid reshaping after transposes on quantized tensors","Prefer per-tensor quantization when reshapes are common"],"tags":["quantization","reshape","block-quantization","unimplemented"],"backgroundTag":"unsupported-quantized-reshape","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"}