tracel-ai/burn · error

float_cast: unsupported target dtype {:?}

Error message

float_cast: unsupported target dtype {:?}

What it means

float_cast's target-side match materializes the f64 values into F32, F64, F16 or BF16 buffers; any other target dtype hits the panic arm. The target dtype requested is not a supported float dtype for this cast path on the flex backend.

Source

Thrown at crates/burn-flex/src/ops/float.rs:875

                let result: Vec<f32> = f64_values.iter().map(|&v| v as f32).collect();
                let bytes = Bytes::from_elems(result);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F32)
            }
            DType::F64 => {
                let bytes = Bytes::from_elems(f64_values);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F64)
            }
            DType::F16 => {
                let result: Vec<f16> = f64_values.iter().map(|&v| f16::from_f64(v)).collect();
                let bytes = Bytes::from_elems(result);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::F16)
            }
            DType::BF16 => {
                let result: Vec<bf16> = f64_values.iter().map(|&v| bf16::from_f64(v)).collect();
                let bytes = Bytes::from_elems(result);
                FlexTensor::new(bytes, Layout::contiguous(shape), DType::BF16)
            }
            _ => panic!("float_cast: unsupported target dtype {:?}", target_dtype),
        }
    }

    fn float_exp(tensor: FloatTensor<Flex>) -> FloatTensor<Flex> {
        unary::exp(tensor)
    }

    fn float_log(tensor: FloatTensor<Flex>) -> FloatTensor<Flex> {
        unary::log(tensor)
    }

    fn float_log1p(tensor: FloatTensor<Flex>) -> FloatTensor<Flex> {
        unary::log1p(tensor)
    }

    fn float_powf(lhs: FloatTensor<Flex>, rhs: FloatTensor<Flex>) -> FloatTensor<Flex> {
        binary_op(lhs, rhs, |a: f32, b| a.powf(b), |a: f64, b| a.powf(b), None)
    }

View on GitHub (pinned to d16f7ba2ed)

Solutions

  1. Cast to an integer via the integer cast path after the float cast, or use int_cast on the correct tensor kind.
  2. Request a supported float target (F32/F64/F16/BF16) and convert to the final dtype in a second, kind-correct step.
  3. Review the DType value being requested; construct targets from FloatDType variants, not raw DType.
  4. Extend float_cast's target match in crates/burn-flex/src/ops/float.rs if new float targets are added upstream.

Example fix

// before
let idx = probs.to_dtype(DType::I64); // unsupported target on float cast
// after
let rounded = probs.to_dtype(burn::tensor::FloatDType::F32);
let idx = rounded.cast::<burn::tensor::i64>(); // kind-correct int cast
Defensive patterns

Strategy: type-guard

Validate before calling

let target: FloatDType = FloatDType::F32; // only F32/F64/F16/BF16 are valid float targets
assert!(matches!(target, FloatDType::F32 | FloatDType::F64 | FloatDType::F16 | FloatDType::BF16));

Type guard

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

Try / catch

// Validate the requested target before casting:
if is_supported_float_target(&target_dtype) { let out = tensor.to_dtype(target_dtype); } else { /* route to int/bool cast */ }

Prevention

When it happens

Trigger: Calling tensor.to_dtype(float_cast) on burn-flex where the TARGET dtype is not F32/F64/F16/BF16, e.g. requesting Int or Bool output from the float cast entry point.

Common situations: Converting float tensors to integers for indexing (e.g. argmax results, quantization indices) through the wrong cast path; constructing a DType manually and passing an unexpected variant; API migrations where to_dtype targets changed.

Related errors


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