tracel-ai/burn · error

Should be float, got quantized

Error message

Should be float, got quantized

What it means

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.

Source

Thrown at crates/burn-dispatch/src/tensor.rs:43

    /// Int tensor handle.
    Int(B::IntTensorPrimitive),
    /// Bool tensor handle.
    Bool(B::BoolTensorPrimitive),
    /// Quantized tensor handle.
    Quantized(B::QuantizedTensorPrimitive),
    #[cfg(feature = "autodiff")]
    /// Autodiff float tensor handle.
    Autodiff(FloatTensor<Autodiff<B>>),
}

impl<B: Backend> BackendTensor<B> {
    /// Returns the inner float tensor primitive.
    pub fn float(self) -> B::FloatTensorPrimitive {
        match self {
            BackendTensor::Float(tensor) => tensor,
            BackendTensor::Int(_) => panic!("Should be float, got int"),
            BackendTensor::Bool(_) => panic!("Should be float, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be float, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be float, got autodiff"),
        }
    }
    /// Returns the inner float tensor primitive.
    pub fn as_float(&self) -> &B::FloatTensorPrimitive {
        match self {
            BackendTensor::Float(tensor) => tensor,
            BackendTensor::Int(_) => panic!("Should be float, got int"),
            BackendTensor::Bool(_) => panic!("Should be float, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be float, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be float, got autodiff"),
        }
    }

    /// Returns the inner int tensor primitive.
    pub fn int(self) -> B::IntTensorPrimitive {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Route quantized tensors through quantized-aware ops (e.g., q_matmul / QFloat paths) instead of .float().
  2. If a float tensor is truly required, dequantize explicitly via the backend's dequantize op, then call .float().
  3. Match on the BackendTensor variant and handle Quantized separately in generic code.
  4. Keep quantized execution confined to quantized model configs so quantized primitives don't reach float-only call sites.

Example fix

// before
let f = qtensor.float(); // panics: Quantized
// after
let f = match qtensor {
    BackendTensor::Quantized(q) => BackendTensor::Float(dequantize::<B>(q)).float(),
    t => t.float(),
};
Defensive patterns

Strategy: type-guard

Validate before calling

// before calling .float()
if let BackendTensor::Quantized(_) = &tensor {
    // dequantize first or route to quantized ops
}

Type guard

fn as_non_quantized<B: Backend>(t: BackendTensor<B>) -> Option<BackendTensor<B>> {
    match t {
        BackendTensor::Quantized(_) => None,
        other => Some(other),
    }
}

Try / catch

// guard before extraction
let f = match tensor {
    BackendTensor::Quantized(q) => BackendTensor::Float(dequantize::<B>(q)),
    BackendTensor::Float(f) => BackendTensor::Float(f),
    other => bail!("unsupported dtype for float extraction: {other:?}"),
};

Prevention

When it happens

Trigger: 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.

Common situations: 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().

Related errors


AI-assisted analysis of tracel-ai/burn@d16f7ba2ed (2026-09-05). Data as JSON: /api/errors/319fa8090f37c39c. Report an issue: GitHub.