huggingface/candle · error

attribute {} of type TENSOR has a negative dimension, which

Error message

attribute {} of type TENSOR has a negative dimension, which is unsupported

What it means

ONNX permits negative dimensions as symbolic/unknown markers in some contexts, but candle requires concrete usize dimensions. When any dimension of a TENSOR attribute is negative, the library bails because it cannot build a candle shape from it.

Source

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

                "attribute {} of type TENSOR was an invalid data_type number {}",
                attr.name,
                tensor_proto.data_type
            ),
        };

        let dtype = match dtype(data_type) {
            Some(value) => value,
            None => bail!(
                "attribute {} of type TENSOR has an unsupported data_type {}",
                attr.name,
                data_type.as_str_name()
            ),
        };

        let mut dims = Vec::with_capacity(tensor_proto.dims.len());
        for dim in &tensor_proto.dims {
            if dim < &0 {
                bail!(
                    "attribute {} of type TENSOR has a negative dimension, which is unsupported",
                    attr.name
                )
            }
            dims.push(*dim as usize)
        }

        Tensor::from_raw_buffer(&tensor_proto.raw_data, dtype, &dims, &Device::Cpu)
    }
}

fn get_attr_<'a>(node: &'a onnx::NodeProto, name: &str) -> Result<&'a onnx::AttributeProto> {
    match node.attribute.iter().find(|attr| attr.name == name) {
        None => {
            bail!(
                "cannot find the '{name}' attribute in '{}' for {}",
                node.op_type,
                node.name

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Re-export the model with static, fully-specified shapes so no dimension is -1.
  2. Fix the tensor's dims in the model file (e.g. via Python onnx) to concrete non-negative values.
  3. If the attribute is truly dynamic, handle it outside candle-onnx or via a graph input instead of a tensor attribute.
  4. Use onnx shape inference / make_dim_param_fixed tools to specialize dynamic dims before loading.
Defensive patterns

Strategy: validation

Validate before calling

for d in &t.dims {
    assert!(*d >= 0, "tensor '{name}' has negative dim {d}");
}

Type guard

fn all_dims_non_negative(t: &onnx::TensorProto) -> bool {
    t.dims.iter().all(|d| *d >= 0)
}

Try / catch

match get_attr::<Tensor>(node, name) {
    Ok(v) => v,
    Err(e) if e.to_string().contains("negative dimension") => return Err(e.into()),
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: Evaluating an ONNX model whose tensor attribute contains a negative dim value (symbolic unknown encoded as -1) via simple_eval_ / get_attr::<Tensor>.

Common situations: Models exported with dynamic shapes where an attribute tensor kept a -1 sentinel dimension; malformed exporters producing negative dims; hand-edited models.

Related errors


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