{"record":{"id":"ad8c49b0e4597335","repo":"tracel-ai/burn","slug":"scheme-does-not-take-a-per-tensor-scale","errorCode":null,"errorMessage":"{scheme:?} does not take a per-tensor scale","messagePattern":"(.+?) does not take a per-tensor scale","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-std/src/tensor/quantization.rs","lineNumber":259,"sourceCode":"        };\n        let scale_bytes = encode_scales(scales, scheme.scale_dtype());\n        bytes.extend_from_byte_slice_aligned(scale_bytes.as_slice(), QPARAM_ALIGN);\n\n        // Last, so a reader can peel it off the end before the block scales it normalizes.\n        match (global_scale_dtype(&scheme), global) {\n            (Some(dtype), Some(global)) => {\n                // Encoding the per-tensor scale narrower would round it, and the block scales were\n                // normalized against the unrounded one.\n                assert_eq!(\n                    dtype,\n                    ScaleDtype::F32,\n                    \"a two-level scheme stores its per-tensor scale as f32, got {scheme:?}\"\n                );\n                let global_bytes = encode_scales(&[global], dtype);\n                bytes.extend_from_byte_slice_aligned(global_bytes.as_slice(), QPARAM_ALIGN);\n            }\n            (Some(_), None) => panic!(\"{scheme:?} requires a per-tensor scale\"),\n            (None, Some(_)) => panic!(\"{scheme:?} does not take a per-tensor scale\"),\n            (None, None) => {}\n        }\n\n        Self {\n            bytes,\n            scheme,\n            shape,\n        }\n    }\n\n    /// The number of quantized elements.\n    pub fn num_elements(&self) -> usize {\n        self.shape.num_elements()\n    }\n\n    /// Returns the int8 quantized values with the quantization parameters.\n    pub fn into_vec_i8(self) -> (Vec<i8>, DecodedScales) {\n        let scheme = self.scheme;","sourceCodeStart":241,"sourceCodeEnd":277,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-std/src/tensor/quantization.rs#L241-L277","documentation":"The mirror case of the previous error: when building quantization parameters, the given scheme does not accept a per-tensor scale (blockwise/two-level schemes carry their own scales inside the encoded bytes), but one was supplied. The API refuses the inconsistent combination with a panic because the extra scale cannot be represented for that scheme.","triggerScenarios":"Passing `Some(scale)` as the per-tensor scale to `QParams`/qparams `new` while `scheme` is a blockwise/two-level scheme (e.g. QBlockwise variants) that stores scales per-block in the byte payload.","commonSituations":"Reusing param-construction code written for per-tensor schemes with a blockwise scheme; exporting a model quantized blockwise and also attaching a leftover global scale; misreading scheme enums when switching between symmetric per-tensor and blockwise quantization.","solutions":["Pass `None` for the per-tensor scale when the scheme is blockwise/two-level.","Choose the per-tensor scheme variant if you genuinely have (and need) a single global f32 scale.","Strip the global scale before serialization, or use burn's high-level quantize API to encode scales in the correct layout."],"exampleFix":"// before\nlet params = QuantizationParameters::from_bytes(bytes, blockwise_scheme, Some(scale)); // panics\n// after\nlet params = QuantizationParameters::from_bytes(bytes, blockwise_scheme, None);","handlingStrategy":"validation","validationCode":"fn validate_qparams(scheme: QuantizationScheme, scale: Option<f32>) -> Result<(), String> {\n    if scheme.is_blockwise() && scale.is_some() {\n        return Err(format!(\"{scheme:?} does not take a per-tensor scale\"));\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"// panic-based constructor; validate arguments first\nlet result = std::panic::catch_unwind(|| QuantizationParameters::from_bytes(bytes, scheme, scale));","preventionTips":["Pass None for the scale with blockwise/two-level schemes","Don't reuse per-tensor param code for blockwise schemes without review","Document which scheme each serialized artifact was quantized with"],"tags":["rust","panic","quantization","validation"],"backgroundTag":"incompatible-quantization-scheme","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"}