Skip to content

Fixed up dimensions to create known sized vectors #4867

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/Microsoft.ML.OnnxTransformer/OnnxTypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,16 @@ public static IEnumerable<int> GetTensorDims(Microsoft.ML.Model.OnnxConverter.On
var dimValue = GetDimValue(d);
dims.Add(dimValue);
}

// In ONNX, the first dimension refers to the batch size. If that is set to -1, it means OnnxRuntime can do inferencing in batches on
// multiple rows at once. In ML.NET, a vector is considered to be of known size if the dimensions are all greater than zero
// Leaving the batch size at -1 causes all Onnx vectors to be considered to be of unknown size. Therefore, if the first dimension is -1,
// we need to fix up the shape. But GetDimValue above converts any dimension < 0 to be 0. We need that behavior for dimensions other than
// the first dimension. So we check only the first dimension here and fix it up. (The '<=' comparison below is there to make sure that
// this holds even if the behavior of GetDimValue changes).
if ((dims.Count > 0) && (dims[0] <= 0))
dims[0] = 1;

return dims;
}

Expand Down