{"record":{"id":"b7a6c4c85a844d8d","repo":"huggingface/candle","slug":"reshape-1-cannot-be-inferred-when-another-dimens","errorCode":null,"errorMessage":"Reshape: -1 cannot be inferred when another dimension is zero","messagePattern":"Reshape: -1 cannot be inferred when another dimension is zero","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":427,"sourceCode":"                // has to be in the product that -1 is inferred against.\n                let mut resolved: Vec<Option<usize>> = Vec::with_capacity(input1.len());\n                for (idx, &v) in input1.iter().enumerate() {\n                    resolved.push(match v {\n                        -1 => None,\n                        0 if allowzero => Some(0),\n                        0 => Some(input0.dim(idx)?),\n                        v if v > 0 => Some(v as usize),\n                        v => bail!(\"Reshape: invalid dimension {v} in target shape\"),\n                    });\n                }\n                let known: usize = resolved.iter().flatten().product();\n                let input1 = resolved\n                    .into_iter()\n                    .map(|d| match d {\n                        Some(d) => Ok(d),\n                        // A -1 has no unique value when the rest of the volume is zero.\n                        None if known == 0 => {\n                            bail!(\"Reshape: -1 cannot be inferred when another dimension is zero\")\n                        }\n                        None => Ok(input0.elem_count() / known),\n                    })\n                    .collect::<Result<Vec<usize>>>()?;\n                let output = input0.reshape(input1)?;\n                values.insert(node.output[0].clone(), output);\n            }\n            \"LogSoftmax\" => {\n                let input = get(&node.input[0])?;\n                let output = match get_attr_opt::<i64>(node, \"axis\")? {\n                    None => candle_nn::ops::softmax_last_dim(input)?,\n                    Some(&axis) => {\n                        let axis = input.normalize_axis(axis)?;\n                        candle_nn::ops::log_softmax(input, axis)?\n                    }\n                };\n                values.insert(node.output[0].clone(), output);\n            }","sourceCodeStart":409,"sourceCodeEnd":445,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L409-L445","documentation":"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.","triggerScenarios":"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].","commonSituations":"Models mixing ONNX ≤12 semantics (0 = copy input dim) with allowzero=1 models; exporters emitting [0, -1] shapes; manual shape edits.","solutions":["Remove the -1 and specify all dimensions explicitly, e.g. [0, 128] instead of [0, -1]","Replace the 0 with the concrete dimension so the product is non-zero and -1 can be inferred","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"],"exampleFix":"// before: target shape [0, -1] with a zero dim\nlet shape = vec![0i64, -1];\n// after: fully specify or drop the -1\nlet shape = vec![0i64, 128];","handlingStrategy":"validation","validationCode":"fn validate_reshape_inferable(shape: &[i64]) -> Result<(), String> {\n    if shape.contains(&-1) && shape.contains(&0) {\n        Err(\"shape contains both -1 and 0; -1 is not inferable\".into())\n    } else { Ok(()) }\n}","typeGuard":"fn is_inferable_shape(shape: &[i64]) -> bool { !(shape.contains(&-1) && shape.contains(&0)) }","tryCatchPattern":"match simple_eval(&model, inputs) {\n    Ok(v) => v,\n    Err(e) if e.to_string().contains(\"-1 cannot be inferred\") => {\n        eprintln!(\"replace 0 with a concrete dim: {}\", e); Default::default()\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Never combine -1 with 0 in a Reshape target shape","Check the allowzero attribute when 0 appears in shapes","Use ONNX official shape inference to validate reshape nodes","Avoid mixing pre-ONNX-12 (0=copy) and allowzero semantics"],"tags":["onnx","reshape","shape-inference"],"backgroundTag":"invalid-reshape-shape","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}