{"record":{"id":"bc8c75dce97879e4","repo":"huggingface/candle","slug":"unexpected-dim-idx-for-got-expected","errorCode":null,"errorMessage":"unexpected dim {idx} for {}, got {:?}, expected {:?}","messagePattern":"unexpected dim (.+?) for (.+?), got (.+?), expected (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"candle-onnx/src/eval.rs","lineNumber":300,"sourceCode":"            },\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                        }\n                        // We do not check equality constraints for the DimParam dimensions for now.\n                        Some(onnx::tensor_shape_proto::dimension::Value::DimParam(_)) | None => (),\n                    }\n                }\n            }\n        };\n        if dt != tensor.dtype() {\n            bail!(\n                \"unexpected dtype for {}, got {:?}, expected {dt:?}\",\n                input.name,\n                tensor.dtype()","sourceCodeStart":282,"sourceCodeEnd":318,"githubUrl":"https://github.com/huggingface/candle/blob/d5fee525bfde3273eb7c9b75fd2bc4937be867ca/candle-onnx/src/eval.rs#L282-L318","documentation":"After rank validation, simple_eval_ compares each dimension pairwise. When a declared dim is a concrete DimValue and the corresponding tensor dim differs, this error is thrown with the index, the full declared shape, and the actual tensor shape. Dimensions declared as DimParam (symbolic) or unknown are skipped.","triggerScenarios":"simple_eval with an input tensor whose concrete dimension violates a fixed declared dim — e.g. seq_len 128 tensor for a model fixed at seq_len 512, or batch size 2 where the model declared batch 1.","commonSituations":"Variable-length inputs fed to a model exported with fixed sequence length; batch-size mismatch against a static-batch export; resized/spatial inputs not matching exported image size.","solutions":["Pad/truncate/reshape the input so each concrete dimension matches the declared shape shown in the error.","Re-export the model with dynamic_axes for the dimensions you need to vary (batch, sequence length).","Check preprocessing (tokenizer max_length, image resize) matches the export configuration.","Split inputs into batches matching the exported static batch size, or re-export for multiple sizes."],"exampleFix":"// before\nlet ids = tokenizer.encode(text, None)?; // arbitrary length\n// after\nlet ids = tokenizer.encode(text, Some(512))?; // match declared fixed seq len 512\nlet ids = pad_to_len(&ids, 512);","handlingStrategy":"validation","validationCode":"fn dims_match(model: &onnx::ModelProto, inputs: &HashMap<String, Value>) -> Vec<String> {\n    let mut bad = vec![];\n    if let Some(g) = &model.graph {\n        for i in &g.input {\n            if let (Some(t), Some(onnx::type_proto::Value::TensorType(tt))) =\n                (inputs.get(&i.name), &i.r#type.value)\n            {\n                if let Some(s) = &tt.shape {\n                    for (idx, (d, &dim)) in s.dim.iter().zip(t.dims()).enumerate() {\n                        if let Some(onnx::tensor_shape_proto::dimension::Value::DimValue(v)) = &d.value {\n                            if *v as usize != dim { bad.push(format!(\"{} dim {idx}: want {v}, got {dim}\", i.name)); }\n                        }\n                    }\n                }\n            }\n        }\n    }\n    bad\n}","typeGuard":null,"tryCatchPattern":"match simple_eval(&model, inputs) {\n    Err(e) if e.to_string().starts_with(\"unexpected dim\") => {\n        anyhow::bail!(\"pad/truncate/resize input to the declared shape shown in the error\")\n    }\n    r => r?,\n}","preventionTips":["Pin tokenizer max_length and image sizes to the exported static dims","Export dynamic_axes for batch/sequence if shapes vary","Batch inputs to the exported static batch size","Pre-validate every concrete dim against model metadata before eval"],"tags":["onnx","shape-mismatch","dimension","eval"],"backgroundTag":"shape-mismatch","analyzedSha":"d5fee525bfde3273eb7c9b75fd2bc4937be867ca","analyzedAt":"2026-09-02T00:15:47.023Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-09T06:17:21.866Z"}