{"record":{"id":"333e9932ec8fe779","repo":"huggingface/candle","slug":"unexpected-rank-for-got-expected","errorCode":null,"errorMessage":"unexpected rank for {}, got {:?}, expected {:?}","messagePattern":"unexpected rank for (.+?), got (.+?), expected (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":289,"sourceCode":"\n        let tensor = match values.get(&input.name) {\n            None => bail!(\"missing input {}\", input.name),\n            Some(tensor) => tensor,\n        };\n        let dt = match DataType::try_from(tensor_type.elem_type) {\n            Ok(dt) => match dtype(dt) {\n                Some(dt) => dt,\n                None => {\n                    bail!(\"unsupported 'value' data-type {dt:?} for {}\", input.name)\n                }\n            },\n            type_ => bail!(\"unsupported input type {type_:?}\"),\n        };\n        match &tensor_type.shape {\n            None => continue,\n            Some(shape) => {\n                if shape.dim.len() != tensor.rank() {\n                    bail!(\n                        \"unexpected rank for {}, got {:?}, expected {:?}\",\n                        input.name,\n                        shape.dim,\n                        tensor.shape()\n                    )\n                }\n                for (idx, (d, &dim)) in shape.dim.iter().zip(tensor.dims().iter()).enumerate() {\n                    match &d.value {\n                        Some(onnx::tensor_shape_proto::dimension::Value::DimValue(v)) => {\n                            if *v as usize != dim {\n                                bail!(\n                                    \"unexpected dim {idx} for {}, got {:?}, expected {:?}\",\n                                    input.name,\n                                    shape.dim,\n                                    tensor.shape()\n                                )\n                            }\n                        }","sourceCodeStart":271,"sourceCodeEnd":307,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L271-L307","documentation":"simple_eval_ validates each supplied input tensor against the declared shape in graph.input.value_info. If the tensor's rank (number of dimensions) differs from the number of dims declared in the ONNX TensorShapeProto, this error is thrown, printing the declared dims and the actual tensor shape.","triggerScenarios":"Calling simple_eval with an input tensor whose .rank() != shape.dim.len() for a graph input that declares a fixed-rank shape — e.g. passing a [B,S] tensor where [B,S,H] is declared, or omitting/adding a batch dimension.","commonSituations":"Forgetting the batch dimension (model expects [batch, seq], caller passes [seq]); squeezing/unsqueezing differences between PyTorch export and candle tensors; dynamic axes exported as fixed dims.","solutions":["Compare the declared shape (printed in the error) with your tensor's shape and reshape (unsqueeze/squeeze) before eval.","If the model supports dynamic axes, re-export with dynamic_axes so rank checks match flexible usage.","Fix off-by-one on batch: wrap the tensor in an explicit batch dimension of 1 when needed.","Add pre-eval shape assertions in your app mirroring the model's input metadata."],"exampleFix":"// before\nlet x = Tensor::new(vec![...], &dev)?; // rank 2\n// after\nlet x = Tensor::new(vec![vec![...]], &dev)?.unsqueeze(0)?; // rank 3 as declared","handlingStrategy":"validation","validationCode":"fn ranks_match(model: &onnx::ModelProto, inputs: &HashMap<String, Value>) -> bool {\n    model.graph.as_ref().map_or(true, |g| g.input.iter().all(|i| {\n        match (inputs.get(&i.name), &i.r#type.value) {\n            (Some(t), Some(onnx::type_proto::Value::TensorType(tt))) => match &tt.shape {\n                Some(s) => s.dim.len() == t.rank(),\n                None => true,\n            },\n            _ => true,\n        }\n    }))\n}","typeGuard":null,"tryCatchPattern":"match simple_eval(&model, inputs) {\n    Err(e) if e.to_string().starts_with(\"unexpected rank for\") => {\n        anyhow::bail!(\"reshape inputs to match the declared rank (see error for expected dims)\")\n    }\n    r => r?,\n}","preventionTips":["Match declared rank exactly, including batch dim (use unsqueeze(0) for single samples)","Re-export with dynamic_axes if input shapes vary","Mirror the exporter's input spec in your preprocessing code","Assert tensor.rank() against model metadata in tests"],"tags":["onnx","shape-mismatch","eval","input-validation"],"backgroundTag":"shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}