tracel-ai/burn · error

Expected quantized dtype, got {:?}

Error message

Expected quantized dtype, got {:?}

What it means

`q_from_data` in burn-flex requires `TensorData` whose `dtype` is `DType::QFloat(scheme)`, which carries the quantization scheme (mode, symmetrical, scale). Any plain float/int dtype means the data is not quantized, so the backend panics instead of silently quantizing.

Source

Thrown at crates/burn-flex/src/ops/qtensor.rs:46

    );
    blocks
}

/// The largest magnitude in each block of `values`, laid out as `blocks`.
fn block_max_abs(values: &[f32], blocks: &BlockLayout) -> Vec<f32> {
    let mut peaks = alloc::vec![0.0f32; blocks.num_blocks()];
    for (index, &x) in values.iter().enumerate() {
        let peak = &mut peaks[blocks.block_of(index)];
        *peak = peak.max(x.abs());
    }
    peaks
}

impl QTensorOps<Flex> for Flex {
    fn q_from_data(data: TensorData, _device: &Device<Flex>) -> QuantizedTensor<Flex> {
        let scheme = match data.dtype {
            DType::QFloat(scheme) => scheme,
            _ => panic!("Expected quantized dtype, got {:?}", data.dtype),
        };

        let shape = data.shape.clone();

        let q_bytes = QuantizedBytes {
            shape: shape.clone(),
            bytes: data.into_bytes(),
            scheme,
        };

        let (values, qparams) = q_bytes.into_vec_i8();
        let tensor_data = TensorData::new(values, shape);
        let tensor = FlexTensor::from_data(tensor_data);

        // Use native storage since we've unpacked to i8
        let scheme = scheme.with_store(QuantStore::Native);

        FlexQTensor::new(tensor, scheme, qparams.block, qparams.global)

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Construct TensorData with a quantized dtype: use `TensorData::quantized::<YourDType>(values, shape, scheme)` or attach the QFloat scheme before calling q_from_data.
  2. If the data is meant to be plain float, use the regular (non-quantized) tensor constructor instead.
  3. Verify the source of the data exports the quantization scheme (scale/mode); re-export if missing.

Example fix

// before
let q = QTensor::from_data(TensorData::from(values_f32).convert::<bf16>());
// after
let data = TensorData::quantized::<bf16>(values, shape, QuantScheme::default());
let q = QTensor::from_data(data);
Defensive patterns

Strategy: type-guard

Validate before calling

assert!(matches!(data.dtype, DType::QFloat(_)), "q_from_data requires quantized TensorData, got {:?}", data.dtype);

Type guard

fn as_quantized_dtype(d: DType) -> Option<QuantScheme> {
    match d { DType::QFloat(s) => Some(s), _ => None }
}

Prevention

When it happens

Trigger: Calling `Tensor::from_data`/`q_from_data` (QuantizedTensor creation) with TensorData produced from plain f32/f16 buffers, or data loaded from a file that was not exported with quantization metadata.

Common situations: Loading quantized model weights from a format that stores scales separately, yielding plain float data; calling the quantized tensor constructor instead of the regular float constructor by mistake; migrating between burn versions where quantization metadata handling changed.

Related errors


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