{"record":{"id":"d2905e08860c7771","repo":"dotnet/machinelearning","slug":"only-1-unknown-dimension-is-allowed","errorCode":null,"errorMessage":"Only 1 unknown dimension is allowed","messagePattern":"Only 1 unknown dimension is allowed","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs","lineNumber":526,"sourceCode":"            {\n\n                _parent = parent;\n                _inputColIndices = new int[_parent.Inputs.Length];\n                _inputTensorShapes = new OnnxShape[_parent.Inputs.Length];\n                _inputOnnxTypes = new Type[_parent.Inputs.Length];\n\n                var model = _parent.Model;\n                for (int i = 0; i < _parent.Inputs.Length; i++)\n                {\n                    var inputNodeInfo = model.ModelInfo.GetInput(_parent.Inputs[i]);\n\n                    var shape = inputNodeInfo.Shape;\n\n                    var inputShape = AdjustDimensions(inputNodeInfo.Shape);\n\n                    // Only allow a single unkown size dimension\n                    if (inputShape.Where(x => x == 0).Count() > 1)\n                        throw new ArgumentOutOfRangeException(_parent.Inputs[i], \"Only 1 unknown dimension is allowed\");\n\n                    _inputTensorShapes[i] = inputShape.ToList();\n                    _inputOnnxTypes[i] = inputNodeInfo.TypeInOnnxRuntime;\n\n                    var col = inputSchema.GetColumnOrNull(_parent.Inputs[i]);\n                    if (!col.HasValue)\n                        throw Host.ExceptSchemaMismatch(nameof(inputSchema), \"input\", _parent.Inputs[i]);\n\n                    _inputColIndices[i] = col.Value.Index;\n\n                    var type = inputSchema[_inputColIndices[i]].Type;\n                    var vectorType = type as VectorDataViewType;\n\n                    var itemType = type.GetItemType();\n                    var nodeItemType = inputNodeInfo.DataViewType.GetItemType();\n                    if (itemType != nodeItemType)\n                    {\n                        // If the ONNX model input node expects a type that mismatches with the type of the input IDataView column that is provided","sourceCodeStart":508,"sourceCodeEnd":544,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs#L508-L544","documentation":"OnnxTransform validates each model input's tensor shape at bind time. ONNX models may declare dimensions as 0 (unknown/dynamic), but ML.NET only supports exactly one dynamic dimension per input tensor; if more than one dimension equals 0 it throws ArgumentOutOfRangeException naming the offending input column. This keeps the inferred IDataView column shape concrete.","triggerScenarios":"Calling MLContext.Transforms.ApplyOnnxModel (or the OnnxScoringEstimator) with a model whose input node declares two or more symbolic/unknown dimensions (shape entries equal to 0) for any single input, e.g. a model with input shape [0, 0, H, W].","commonSituations":"Using dynamic-batch ONNX models exported from PyTorch/TensorFlow where both batch and a spatial axis are marked dynamic; exporting models with variable sequence length and variable batch; using a model trained/exported elsewhere with fully dynamic axes.","solutions":["Re-export the model fixing all but one dimension as static (e.g. batch dynamic only): torch.onnx.export with dynamic_axes limited to one axis, or set fixed values in ONNX export axes.","Post-process the ONNX model to hard-code the extra dynamic dims (onnx package: load, set dim_param to a fixed value via tensor.shape, save).","Reshape/pad data so only one dimension varies and pin the others in the model.","Use a different model variant with static input sizes."],"exampleFix":"// before: export with multiple dynamic axes\ntorch.onnx.export(model, x, 'm.onnx', dynamic_axes={'input': {0: 'batch', 1: 'seq'}})\n// after: only one dynamic axis\ntorch.onnx.export(model, x, 'm.onnx', dynamic_axes={'input': {0: 'batch'}})","handlingStrategy":"validation","validationCode":"// C#: inspect model inputs before ApplyOnnxModel\nusing var session = new InferenceSession(modelPath);\nforeach (var input in session.InputMetadata)\n{\n    int unk = input.Value.Dimensions.Count(d => d == -1 || d == 0);\n    if (unk > 1)\n        throw new InvalidOperationException($\"{input.Key} has {unk} dynamic dims; re-export with at most 1\");\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Export ONNX models with only one dynamic axis (usually batch).","Run a schema smoke-test (pipeline.GetOutputSchema) before long training runs.","Pin model export settings in CI so dynamic axes don't regress.","Document each model's fixed input dimensions next to the model file."],"tags":["onnx","machine-learning","argument-out-of-range","tensor-shape"],"backgroundTag":"tensor-shape-mismatch","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}