{"record":{"id":"55b57f3c19d06f6f","repo":"tracel-ai/burn","slug":"scalar-not-supported-for-dtype-55b57f","errorCode":null,"errorMessage":"Scalar not supported for {dtype:?}","messagePattern":"Scalar not supported for (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-std/src/element/scalar.rs","lineNumber":41,"sourceCode":"    /// # Note\n    /// [`QFloat`](DType::QFloat) scalars are represented as float for element-wise operations.\n    pub fn new<E: ElementConversion>(value: E, dtype: &DType) -> Self {\n        if dtype.is_float() | matches!(dtype, &DType::QFloat(_)) {\n            Self::Float(value.elem())\n        } else if dtype.is_int() {\n            Self::Int(value.elem())\n        } else if dtype.is_uint() {\n            Self::UInt(value.elem())\n        } else if dtype.is_bool() {\n            match dtype {\n                DType::Bool(BoolStore::Native) => Self::Bool(value.elem()),\n                DType::Bool(BoolStore::U8) | DType::Bool(BoolStore::U32) => {\n                    Self::UInt(value.elem())\n                }\n                _ => unreachable!(),\n            }\n        } else {\n            unimplemented!(\"Scalar not supported for {dtype:?}\")\n        }\n    }\n\n    /// Converts and returns the converted element.\n    pub fn elem<E: Element>(self) -> E {\n        match self {\n            Self::Float(x) => x.elem(),\n            Self::Int(x) => x.elem(),\n            Self::UInt(x) => x.elem(),\n            Self::Bool(x) => x.elem(),\n        }\n    }\n\n    /// Returns the exact integer value, if valid.\n    pub fn try_as_integer(&self) -> Option<Self> {\n        match self {\n            Scalar::Float(x) => (x.floor() == *x).then(|| Self::Int(x.to_i64().unwrap())),\n            Scalar::Int(_) | Scalar::UInt(_) => Some(*self),","sourceCodeStart":23,"sourceCodeEnd":59,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-std/src/element/scalar.rs#L23-L59","documentation":"Scalar::new (crates/burn-std/src/element/scalar.rs:25-43) converts a value into a Scalar matching a target DType. It handles float (including QFloat), int, uint, and bool dtypes; any other DType — i.e. one that is none of float/int/uint/bool — falls into the final `unimplemented!(\"Scalar not supported for {dtype:?}\")` panic. In practice this fires when the dtype is a quantized type not classified as QFloat-compatible for scalars or another non-scalar-representable dtype.","triggerScenarios":"Calling `Scalar::new(value, &dtype)` with a dtype for which `is_float()`, `is_int()`, `is_uint()`, and `is_bool()` are all false — e.g. a quantized DType variant not covered by the QFloat match arm (dependent on the DType enum in this version).","commonSituations":"Passing a quantized dtype (DType::QFloat with an unusual scheme) or a newly added dtype into scalar-creating APIs like tensor elementwise ops with a scalar argument (e.g. `tensor + scalar` dispatching through Scalar::new).","solutions":["Dequantize the tensor to a plain float dtype before applying scalar operations.","Pass scalars only to tensors with standard float/int/uint/bool dtypes.","If you control the code, extend Scalar::new to map the dtype (e.g. treat QFloat like f32 per the documented note).","Upgrade burn if the missing dtype coverage was fixed upstream."],"exampleFix":"// before\nlet s = Scalar::new(2.0f32, &DType::QFloat(scheme)); // may panic depending on scheme\n// after\nlet f_tensor = q_tensor.dequantize(); // DType::F32\nlet s = Scalar::new(2.0f32, &f_tensor.dtype());","handlingStrategy":"validation","validationCode":"fn scalar_supported(dtype: &DType) -> bool {\n    dtype.is_float()\n        || dtype.is_int()\n        || dtype.is_uint()\n        || dtype.is_bool()\n        || matches!(dtype, DType::QFloat(_))\n}\nassert!(scalar_supported(&dtype), \"Scalar::new unsupported for {dtype:?}\");","typeGuard":"fn accepts_scalar(dtype: &DType) -> bool {\n    !matches!(dtype, DType::QFloat(_)) || true // narrow: only standard dtypes are safe in all versions\n}","tryCatchPattern":"// Scalar::new panics; validate dtype before scalar ops\nif scalar_supported(&tensor.dtype()) {\n    let out = tensor + 2.0f32;\n} else {\n    let out = tensor.dequantize() + 2.0f32;\n}","preventionTips":["Apply scalar operations only on float/int/uint/bool (or dequantized) tensors.","Dequantize QFloat tensors before scalar arithmetic unless your burn version supports QFloat scalars.","Check dtype with is_float/is_int/is_uint/is_bool before building a Scalar."],"tags":["rust","burn","scalar","dtype","unimplemented"],"backgroundTag":"unsupported-dtype-panic","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"}