tracel-ai/burn · error

Scalar not supported for {dtype:?}

Error message

Scalar not supported for {dtype:?}

What it means

`Scalar::new` in burn-ir converts an `Element` value into the variant matching the target dtype. It covers int, uint, and bool dtypes (float is handled in the earlier match arms); any dtype outside these — e.g. quantized or exotic dtypes — reaches the catch-all `unimplemented!("Scalar not supported for {dtype:?}")`.

Source

Thrown at crates/burn-ir/src/scalar.rs:39

            ScalarIr::UInt(x) => x.hash(state),
            ScalarIr::Bool(x) => x.hash(state),
        }
    }
}

impl ScalarIr {
    /// Creates a scalar with the specified data type.
    pub fn new<E: ElementConversion>(value: E, dtype: &DType) -> Self {
        if dtype.is_float() {
            Self::Float(value.elem())
        } else if dtype.is_int() {
            Self::Int(value.elem())
        } else if dtype.is_uint() {
            Self::UInt(value.elem())
        } else if dtype.is_bool() {
            Self::Bool(value.elem())
        } else {
            unimplemented!("Scalar not supported for {dtype:?}")
        }
    }

    /// Converts and returns the converted element.
    pub fn elem<E: Element>(self) -> E {
        match self {
            ScalarIr::Float(x) => x.elem(),
            ScalarIr::Int(x) => x.elem(),
            ScalarIr::UInt(x) => x.elem(),
            ScalarIr::Bool(x) => x.elem(),
        }
    }
}

// The enums are similar, but both types have different roles:
// - `Scalar`: runtime literal value
// - `ScalarIr`: serializable literal representation (used for IR)
impl From<Scalar> for ScalarIr {

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Only create Scalar values for dtypes supported by burn-ir (float/int/uint/bool)
  2. Convert the value to a supported dtype before wrapping it in a Scalar
  3. Extend the match in `Scalar::new` if your fork introduces a new dtype
  4. Check the dtype of the operation parameter that produces the scalar

Example fix

// before
let scalar = Scalar::new(DType::QInt8, value);
// after
let scalar = Scalar::new(DType::I32, value.to_i32());
Defensive patterns

Strategy: validation

Validate before calling

fn supported_for_scalar(dtype: DType) -> bool {
    dtype.is_float() || dtype.is_int() || dtype.is_uint() || dtype.is_bool()
}
if !supported_for_scalar(dtype) {
    // convert or reject before Scalar::new
}

Type guard

fn scalar_supported(dtype: DType) -> bool {
    dtype.is_float() || dtype.is_int() || dtype.is_uint() || dtype.is_bool()
}

Try / catch

// unimplemented! panics are not catchable; pre-check instead
if !scalar_supported(dtype) {
    return Err(format!("Scalar not supported for {dtype:?}"));
}
let s = Scalar::new(dtype, value);

Prevention

When it happens

Trigger: Constructing `Scalar::new(dtype, value)` with a dtype that is not a supported float/int/uint/bool type, e.g. when serializing a scalar operation parameterized by an unsupported dtype.

Common situations: Custom/extended dtypes (quantized, flex-specific dtypes) flowing into the IR's scalar representation; plugins or new backends introducing dtypes the IR doesn't model yet.

Related errors


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