huggingface/candle · error

Reshape: -1 cannot be inferred when another dimension is zer

Error message

Reshape: -1 cannot be inferred when another dimension is zero

What it means

Reshape infers -1 by dividing the input element count by the product of the known dims. If another dim in the target shape is 0 (resolved from the input or allowzero), that product is 0 and the -1 dimension has no unique value, so candle-onnx bails out instead of guessing.

Source

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

                // has to be in the product that -1 is inferred against.
                let mut resolved: Vec<Option<usize>> = Vec::with_capacity(input1.len());
                for (idx, &v) in input1.iter().enumerate() {
                    resolved.push(match v {
                        -1 => None,
                        0 if allowzero => Some(0),
                        0 => Some(input0.dim(idx)?),
                        v if v > 0 => Some(v as usize),
                        v => bail!("Reshape: invalid dimension {v} in target shape"),
                    });
                }
                let known: usize = resolved.iter().flatten().product();
                let input1 = resolved
                    .into_iter()
                    .map(|d| match d {
                        Some(d) => Ok(d),
                        // A -1 has no unique value when the rest of the volume is zero.
                        None if known == 0 => {
                            bail!("Reshape: -1 cannot be inferred when another dimension is zero")
                        }
                        None => Ok(input0.elem_count() / known),
                    })
                    .collect::<Result<Vec<usize>>>()?;
                let output = input0.reshape(input1)?;
                values.insert(node.output[0].clone(), output);
            }
            "LogSoftmax" => {
                let input = get(&node.input[0])?;
                let output = match get_attr_opt::<i64>(node, "axis")? {
                    None => candle_nn::ops::softmax_last_dim(input)?,
                    Some(&axis) => {
                        let axis = input.normalize_axis(axis)?;
                        candle_nn::ops::log_softmax(input, axis)?
                    }
                };
                values.insert(node.output[0].clone(), output);
            }

View on GitHub (pinned to d5fee525bf)

Solutions

  1. Remove the -1 and specify all dimensions explicitly, e.g. [0, 128] instead of [0, -1]
  2. Replace the 0 with the concrete dimension so the product is non-zero and -1 can be inferred
  3. Check the Reshape node's allowzero attribute; with allowzero=0 a literal 0 copies the input dim — restructure so the copied dim is known before inference

Example fix

// before: target shape [0, -1] with a zero dim
let shape = vec![0i64, -1];
// after: fully specify or drop the -1
let shape = vec![0i64, 128];
Defensive patterns

Strategy: validation

Validate before calling

fn validate_reshape_inferable(shape: &[i64]) -> Result<(), String> {
    if shape.contains(&-1) && shape.contains(&0) {
        Err("shape contains both -1 and 0; -1 is not inferable".into())
    } else { Ok(()) }
}

Type guard

fn is_inferable_shape(shape: &[i64]) -> bool { !(shape.contains(&-1) && shape.contains(&0)) }

Try / catch

match simple_eval(&model, inputs) {
    Ok(v) => v,
    Err(e) if e.to_string().contains("-1 cannot be inferred") => {
        eprintln!("replace 0 with a concrete dim: {}", e); Default::default()
    }
    Err(e) => return Err(e.into()),
}

Prevention

When it happens

Trigger: simple_eval_ Reshape with a target shape containing both -1 and a 0 (and allowzero semantics making the 0 resolve to 0), e.g. [0, -1].

Common situations: Models mixing ONNX ≤12 semantics (0 = copy input dim) with allowzero=1 models; exporters emitting [0, -1] shapes; manual shape edits.

Related errors


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