tracel-ai/burn · error

Should be int, got quantized

Error message

Should be int, got quantized

What it means

BackendTensor::int() was called on a Quantized tensor handle. Quantized primitives live in their own variant and cannot be returned as an int primitive, so the code panics. A quantized tensor reached a code path that only handles integer tensors.

Source

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

    /// 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 {
        match self {
            BackendTensor::Int(tensor) => tensor,
            BackendTensor::Float(_) => panic!("Should be int, got float"),
            BackendTensor::Bool(_) => panic!("Should be int, got bool"),
            BackendTensor::Quantized(_) => panic!("Should be int, got quantized"),
            #[cfg(feature = "autodiff")]
            BackendTensor::Autodiff(_) => panic!("Should be int, got autodiff"),
        }
    }

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

    /// Returns the inner quantized tensor primitive.

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Dequantize the tensor before integer/float computation, or route it through the quantized op path (quantized())
  2. Match the Quantized variant explicitly in kernels rather than calling int()
  3. Check the producing op: quantized ops must output through quantized() consumers
  4. Validate dtype/kind via TensorMetadata at the dispatch boundary

Example fix

// before
let value = handle.int(); // panics: Quantized variant
// after
let value = match handle {
    BackendTensor::Quantized(_) => handle.quantized(), // use the quantized path
    _ => handle.int(),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if matches!(handle, BackendTensor::Quantized(_)) {
    let q = handle.quantized(); // use quantized path, not int()
}

Type guard

fn is_quantized<B: BackendTypes>(t: &BackendTensor<B>) -> bool {
    matches!(t, BackendTensor::Quantized(_))
}

Prevention

When it happens

Trigger: Calling int() on a quantized tensor (int8/int4 quantized weights/activations); feeding quantized inference tensors into ops implemented for plain int primitives; forgetting to dequantize or treat the quantized variant separately.

Common situations: Quantized model runs where an op lacks a quantized kernel and falls into the int path; mixing quantized weights with int buffers in custom code; post-training-quantization pipelines that assume quantized tensors are plain ints.

Related errors


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