tracel-ai/burn · error

Expected quantized handle, got {}

Error message

Expected quantized handle, got {}

What it means

`NdArray::quantized_tensor` (BackendIr) panics when the TensorHandle's `HandleKind` is not `HandleKind::Quantized` — the runtime tried to materialize a quantized tensor primitive from a Float, Int, or Bool handle. This conversion is on the path for quantized ops (dq/qdq), so it means a quantized-typed TensorIr received a non-quantized resource.

Source

Thrown at crates/burn-ndarray/src/backend.rs:158

    fn int_tensor(handle: TensorHandle<Self::Handle>) -> IntTensor<Self> {
        match handle.handle {
            HandleKind::Int(handle) => handle,
            _ => panic!("Expected int handle, got {}", handle.handle.name()),
        }
    }

    fn bool_tensor(handle: TensorHandle<Self::Handle>) -> BoolTensor<Self> {
        match handle.handle {
            HandleKind::Bool(handle) => handle,
            _ => panic!("Expected bool handle, got {}", handle.handle.name()),
        }
    }

    fn quantized_tensor(handle: TensorHandle<Self::Handle>) -> QuantizedTensor<Self> {
        match handle.handle {
            HandleKind::Quantized(handle) => handle,
            _ => panic!("Expected quantized handle, got {}", handle.handle.name()),
        }
    }

    fn float_tensor_handle(tensor: FloatTensor<Self>) -> Self::Handle {
        HandleKind::Float(tensor)
    }

    fn int_tensor_handle(tensor: IntTensor<Self>) -> Self::Handle {
        HandleKind::Int(tensor)
    }

    fn bool_tensor_handle(tensor: BoolTensor<Self>) -> Self::Handle {
        HandleKind::Bool(tensor)
    }

    fn quantized_tensor_handle(tensor: QuantizedTensor<Self>) -> Self::Handle {
        HandleKind::Quantized(tensor)
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Quantize the tensor before the quantized op (use the backend's quantize/dequantize ops) so a Quantized handle exists.
  2. Fix the TensorKind in the TensorIr/description so it matches the handle actually stored.
  3. Use the correct accessor (`float_tensor`/`int_tensor`) for non-quantized handles in custom backend code.
  4. Check the model-import graph for ops whose outputs were declared quantized but produced plain tensors.

Example fix

// before
let out = NdArray::quantized_tensor(float_handle); // panics
// after
let q = NdArray::quantize(float_tensor, &scheme, &qparams);
let out = NdArray::quantized_tensor(handle_of(q));
Defensive patterns

Strategy: type-guard

Validate before calling

fn ensure_quantized(t: &TensorIr) -> Result<(), String> {
    (t.kind == TensorKind::Quantized)
        .then_some(())
        .ok_or_else(|| format!("expected quantized tensor, got kind {:?}", t.kind))
}

Type guard

fn as_quantized_handle(h: HandleKind<NdArray>) -> Option<QArray> {
    match h { HandleKind::Quantized(q) => Some(q), _ => None }
}

Try / catch

let q = std::panic::catch_unwind(|| NdArray::quantized_tensor(th))
    .map_err(|_| anyhow!("handle is not quantized; expected {}", th.handle.name()))?;

Prevention

When it happens

Trigger: Calling `NdArray::quantized_tensor(handle)` (or `get_quantized_tensor`) with a handle registered as Float, Int, or Bool; e.g. running a quantized op on a tensor that was never quantized.

Common situations: Quantized model import where a tensor skipped its quantize step; mismatch between the declared quantized TensorKind and the actual stored handle in custom IR/graph code; accidental use of quantized APIs on regular tensors.

Related errors


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