tracel-ai/burn · error

Expected quantized handle, got {}

Error message

Expected quantized handle, got {}

What it means

`BackendIr::quantized_tensor` for the Flex backend unwraps a `TensorHandle` expecting `HandleKind::Quantized`. Any other handle kind causes a panic with 'Expected quantized handle, got <name>'. Quantized inference ops therefore can never silently receive float/int/bool handles.

Source

Thrown at crates/burn-flex/src/backend.rs:197

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

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

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

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

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

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

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

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Quantize the input first so the kernel produces a `HandleKind::Quantized` handle before invoking quantized ops.
  2. Fix the op registration so quantized ops map to kernels returning quantized handles.
  3. Match on the handle kind at the call site and route non-quantized handles to the regular (float) op path.

Example fix

// before
let q = BackendIr::quantized_tensor(handle); // panics: got float
// after
let q = match handle.handle {
    HandleKind::Quantized(t) => t,
    HandleKind::Float(f) => f.quantize(params), // quantize before use
    other => panic!("unexpected handle {}", other.name()),
};
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(handle.handle, HandleKind::Quantized(_)) { /* quantize first or use float path */ }

Type guard

fn as_quantized_handle(t: HandleKind<Flex>) -> Option<FlexQTensor> {
    match t { HandleKind::Quantized(q) => Some(q), _ => None }
}

Try / catch

let result = std::panic::catch_unwind(AssertUnwindSafe(|| Flex::quantized_tensor(handle.clone())));
match result {
    Ok(q) => use_quantized(q),
    Err(_) => eprintln!("handle was not quantized"),
}

Prevention

When it happens

Trigger: Calling `quantized_tensor()` on a handle produced by a non-quantized kernel — e.g. a quantized conv/matmul was lowered to a plain float kernel, or a float tensor was passed where a pre-quantized handle was required.

Common situations: Quantized model pipelines where packing/quantization kernels were skipped or mis-registered, or calling quantized ops on tensors that were never quantized (missing quantize step).

Related errors


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