{"record":{"id":"319fa8090f37c39c","repo":"tracel-ai/burn","slug":"should-be-float-got-quantized","errorCode":null,"errorMessage":"Should be float, got quantized","messagePattern":"Should be float, got quantized","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dispatch/src/tensor.rs","lineNumber":43,"sourceCode":"    /// Int tensor handle.\n    Int(B::IntTensorPrimitive),\n    /// Bool tensor handle.\n    Bool(B::BoolTensorPrimitive),\n    /// Quantized tensor handle.\n    Quantized(B::QuantizedTensorPrimitive),\n    #[cfg(feature = \"autodiff\")]\n    /// Autodiff float tensor handle.\n    Autodiff(FloatTensor<Autodiff<B>>),\n}\n\nimpl<B: Backend> BackendTensor<B> {\n    /// Returns the inner float tensor primitive.\n    pub fn float(self) -> B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n    /// Returns the inner float tensor primitive.\n    pub fn as_float(&self) -> &B::FloatTensorPrimitive {\n        match self {\n            BackendTensor::Float(tensor) => tensor,\n            BackendTensor::Int(_) => panic!(\"Should be float, got int\"),\n            BackendTensor::Bool(_) => panic!(\"Should be float, got bool\"),\n            BackendTensor::Quantized(_) => panic!(\"Should be float, got quantized\"),\n            #[cfg(feature = \"autodiff\")]\n            BackendTensor::Autodiff(_) => panic!(\"Should be float, got autodiff\"),\n        }\n    }\n\n    /// Returns the inner int tensor primitive.\n    pub fn int(self) -> B::IntTensorPrimitive {","sourceCodeStart":25,"sourceCodeEnd":61,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dispatch/src/tensor.rs#L25-L61","documentation":"BackendTensor::float() panics with this message when called on a Quantized tensor. Quantized tensors (TensorPrimitive::QFloat wrapped as BackendTensor::Quantized) are not plain float primitives; extracting them as float would silently dequantize or corrupt semantics, so burn panics instead.","triggerScenarios":"Calling BackendTensor::float() on a tensor holding BackendTensor::Quantized(_), e.g., quantized model outputs or QFloat tensors from quantized backends flowing into float-only APIs.","commonSituations":"Mixing quantized inference models with code written for float tensors; refactors that moved code under a generic BackendTensor abstraction where quantized variants now reach .float().","solutions":["Route quantized tensors through quantized-aware ops (e.g., q_matmul / QFloat paths) instead of .float().","If a float tensor is truly required, dequantize explicitly via the backend's dequantize op, then call .float().","Match on the BackendTensor variant and handle Quantized separately in generic code.","Keep quantized execution confined to quantized model configs so quantized primitives don't reach float-only call sites."],"exampleFix":"// before\nlet f = qtensor.float(); // panics: Quantized\n// after\nlet f = match qtensor {\n    BackendTensor::Quantized(q) => BackendTensor::Float(dequantize::<B>(q)).float(),\n    t => t.float(),\n};","handlingStrategy":"type-guard","validationCode":"// before calling .float()\nif let BackendTensor::Quantized(_) = &tensor {\n    // dequantize first or route to quantized ops\n}","typeGuard":"fn as_non_quantized<B: Backend>(t: BackendTensor<B>) -> Option<BackendTensor<B>> {\n    match t {\n        BackendTensor::Quantized(_) => None,\n        other => Some(other),\n    }\n}","tryCatchPattern":"// guard before extraction\nlet f = match tensor {\n    BackendTensor::Quantized(q) => BackendTensor::Float(dequantize::<B>(q)),\n    BackendTensor::Float(f) => BackendTensor::Float(f),\n    other => bail!(\"unsupported dtype for float extraction: {other:?}\"),\n};","preventionTips":["Keep quantized tensors within quantized op paths; never feed them to float-only code.","Dequantize explicitly at the boundary between quantized and float code.","Add CI tests that exercise quantized models through generic tensor code."],"tags":["rust","dtype","quantization","tensor","panic"],"backgroundTag":"tensor-dtype-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"}