tracel-ai/burn · error

any_float: unsupported dtype {:?}

Error message

any_float: unsupported dtype {:?}

What it means

Dtype-dispatch exhaustiveness panic: `any_float` (any non-zero over a float tensor) only handles F32/F64/F16/BF16; any other dtype reaching the float any-op triggers the panic, implying a kernel-dispatch bug that routed a non-float tensor into the float reduction.

Source

Thrown at crates/burn-flex/src/ops/comparison.rs:854

    let result: Vec<u8> = data
        .iter()
        .map(|&a| if a != rhs_val { 1 } else { 0 })
        .collect();
    make_bool_tensor(result, shape, out_dtype)
}

// ============================================================================
// any / all operations
// ============================================================================

/// Check if any element is non-zero (float tensors).
pub fn any_float(tensor: FlexTensor, out_dtype: BoolDType) -> FlexTensor {
    let has_any = match tensor.dtype() {
        DType::F32 => iter_elements::<f32>(&tensor).any(|x| x != 0.0),
        DType::F64 => iter_elements::<f64>(&tensor).any(|x| x != 0.0),
        DType::F16 => iter_elements::<f16>(&tensor).any(|x: f16| x.to_f32() != 0.0),
        DType::BF16 => iter_elements::<bf16>(&tensor).any(|x: bf16| x.to_f32() != 0.0),
        _ => panic!("any_float: unsupported dtype {:?}", tensor.dtype()),
    };
    bool_scalar(has_any, out_dtype)
}

/// Check if any element along a dimension is non-zero (float tensors).
pub fn any_float_dim(tensor: FlexTensor, dim: usize, out_dtype: BoolDType) -> FlexTensor {
    reduce_bool_dim(&tensor, dim, false, |a, b| a || b, out_dtype)
}

/// Check if all elements are non-zero (float tensors).
pub fn all_float(tensor: FlexTensor, out_dtype: BoolDType) -> FlexTensor {
    let all = match tensor.dtype() {
        DType::F32 => iter_elements::<f32>(&tensor).all(|x| x != 0.0),
        DType::F64 => iter_elements::<f64>(&tensor).all(|x| x != 0.0),
        DType::F16 => iter_elements::<f16>(&tensor).all(|x: f16| x.to_f32() != 0.0),
        DType::BF16 => iter_elements::<bf16>(&tensor).all(|x: bf16| x.to_f32() != 0.0),
        _ => panic!("all_float: unsupported dtype {:?}", tensor.dtype()),
    };

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Check tensor.dtype() is a float variant before calling any_float; route int tensors to any_int
  2. Cast the tensor to F32 first if float semantics are acceptable
  3. Adjust upstream casts so the tensor stays float until after the reduction

Example fix

// before
let has_any = any_float(int_tensor, BoolDType::Native);
// after
let has_any = any_int(int_tensor, BoolDType::Native);
Defensive patterns

Strategy: type-guard

Validate before calling

if !matches!(t.dtype(), DType::F32|DType::F64|DType::F16|DType::BF16) { /* route to any_int or cast */ }

Type guard

fn is_float_dtype(d: &DType) -> bool {
    matches!(d, DType::F32|DType::F64|DType::F16|DType::BF16)
}

Prevention

When it happens

Trigger: Calling the public any_float(tensor, out_dtype) with an integer (I64/I32/U8/...) or Bool tensor instead of a float tensor.

Common situations: Calling any_float generically on tensors of unknown dtype; a pipeline where the tensor was silently converted to an int dtype (e.g. after a cast or quantization) before the reduction.

Related errors


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