huggingface/candle · error

unsupported 'to' value {dt:?} for cast {}

Error message

unsupported 'to' value {dt:?} for cast {}

What it means

This error is thrown when evaluating an ONNX Cast node whose 'to' attribute is an integer that candle-onnx cannot map to a supported candle DType. DataType::try_from either fails (the attribute is not a valid onnx DataType) or the mapped dtype has no candle equivalent (dtype() returns None). The library only supports casts to tensor element types it can represent.

Source

Thrown at candle-onnx/src/eval.rs:1121

                    AttributeType::Tensor => {
                        let t = value.t.as_ref().unwrap();
                        get_tensor(t, &node.name)?
                    }
                    rtype => bail!("unsupported 'value' type {rtype:?} for {}", node.name),
                };

                values.insert(node.output[0].clone(), output);
            }
            // https://github.com/onnx/onnx/blob/main/docs/Operators.md#Cast
            "Cast" => {
                let input = get(&node.input[0])?;
                let dt: i64 = *get_attr(node, "to")?;
                let dtype = match DataType::try_from(dt as i32) {
                    Ok(DataType::Int32) => DType::I64,
                    Ok(dt) => match dtype(dt) {
                        Some(dt) => dt,
                        None => {
                            bail!("unsupported 'to' value {dt:?} for cast {}", node.name)
                        }
                    },
                    Err(_) => {
                        bail!("unsupported 'to' value {dt:?} for cast {}", node.name)
                    }
                };
                let output = input.to_dtype(dtype)?;
                values.insert(node.output[0].clone(), output);
            }
            // https://github.com/onnx/onnx/blob/main/docs/Operators.md#CumSum
            "CumSum" => {
                let exclusive = get_attr_opt::<i64>(node, "exclusive")?
                    .copied()
                    .unwrap_or(0);
                let reverse = get_attr_opt::<i64>(node, "reverse")?.copied().unwrap_or(0);
                if exclusive != 0 {
                    bail!("only exclusive == 0 is supported in CumSum")
                }

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Inspect the Cast node's 'to' attribute (e.g. with Netron or onnx python tools) and identify the unsupported dtype
  2. Pre-process the model to replace the cast with a supported dtype (e.g. via onnx python: convert the node's 'to' to int32/int64/float32)
  3. Export the model with an opset/dtype configuration candle-onnx supports (avoid bfloat16/float8/string casts)
  4. Convert the unsupported dtype conversion in the surrounding code outside the ONNX graph
  5. File/patch candle-onnx to add the missing DataType -> DType mapping in eval.rs

Example fix

// before (python, export-side)
cast_node.attr['to'] = onnx.TensorProto.BFLOAT16
// after
cast_node.attr['to'] = onnx.TensorProto.FLOAT
Defensive patterns

Strategy: validation

Validate before calling

fn is_supported_cast_to(to: i64) -> bool {
    match onnx_pb::DataType::try_from(to as i32) {
        Ok(onnx_pb::DataType::Int32) => true,
        Ok(dt) => candle_onnx::eval::dtype(dt).is_some(),
        Err(_) => false,
    }
}
// assert every Cast node in the graph passes before eval

Type guard

fn cast_dtype_supported(to: i64) -> Option<bool> {
    onnx_pb::DataType::try_from(to as i32).ok().map(|dt| {
        matches!(dt, onnx_pb::DataType::Int32) || candle_onnx::eval::dtype(dt).is_some()
    })
}

Prevention

When it happens

Trigger: Evaluating a model containing a Cast node whose 'to' attribute is an invalid or unrepresentable onnx DataType enum value (e.g. DataType::Int32 is special-cased, but other types like STRING/BFLOAT16 variants without a candle mapping will fail).

Common situations: Running an ONNX model exported with newer opset/dtype support (e.g. float8, bfloat16, string casts) than candle-onnx implements; a malformed or hand-written Protobuf with a bogus 'to' attribute.

Related errors


AI-assisted analysis of huggingface/candle@d5fee525bf (2026-09-02). Data as JSON: /api/errors/33ddb5af0e7af8ac. Report an issue: GitHub.