Skip to content

Fixed onnx export for key types other than uint #5160

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 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions src/Microsoft.ML.Data/Transforms/Hashing.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1359,7 +1359,7 @@ private bool SaveAsOnnxCore(OnnxContext ctx, int iinfo, string srcVariable, stri
OnnxNode murmurNode;
OnnxNode isZeroNode;

var srcType = _srcTypes[iinfo].GetItemType();
var srcType = _srcTypes[iinfo].GetItemType().RawType;
if (_parent._columns[iinfo].Combine)
return false;

Expand All @@ -1386,9 +1386,9 @@ private bool SaveAsOnnxCore(OnnxContext ctx, int iinfo, string srcVariable, stri
}

// Since these numeric types are not supported by Onnxruntime, we cast them to UInt32.
if (srcType == NumberDataViewType.UInt16 || srcType == NumberDataViewType.Int16 ||
srcType == NumberDataViewType.SByte || srcType == NumberDataViewType.Byte ||
srcType == BooleanDataViewType.Instance)
if (srcType == typeof(ushort) || srcType == typeof(short) ||
srcType == typeof(sbyte) || srcType == typeof(byte) ||
srcType == typeof(bool))
{
castOutput = ctx.AddIntermediateVariable(NumberDataViewType.UInt32, "CastOutput", true);
castNode = ctx.CreateNode("Cast", srcVariable, castOutput, ctx.GetNodeName(opType), "");
Expand Down
7 changes: 5 additions & 2 deletions src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -389,13 +389,16 @@ public Mapper(OnnxTransformer parent, DataViewSchema inputSchema) :
if (vectorType != null && vectorType.Size == 0)
throw Host.Except($"Variable length input columns not supported");

if (type.GetItemType() != inputNodeInfo.DataViewType.GetItemType())
var itemType = type.GetItemType();
var nodeItemType = inputNodeInfo.DataViewType.GetItemType();
if (itemType != nodeItemType)
{
// If the ONNX model input node expects a type that mismatches with the type of the input IDataView column that is provided
// then throw an exception.
// This is done except in the case where the ONNX model input node expects a UInt32 but the input column is actually KeyDataViewType
// This is done to support a corner case originated in NimbusML. For more info, see: https://github.com/microsoft/NimbusML/issues/426
if (!(type.GetItemType() is KeyDataViewType && inputNodeInfo.DataViewType.GetItemType().RawType == typeof(UInt32)))
var isKeyType = itemType is KeyDataViewType;
if (!isKeyType || itemType.RawType != nodeItemType.RawType)
throw Host.ExceptSchemaMismatch(nameof(inputSchema), "input", _parent.Inputs[i], inputNodeInfo.DataViewType.GetItemType().ToString(), type.ToString());
}

Expand Down
2 changes: 1 addition & 1 deletion test/Microsoft.ML.Tests/OnnxConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ private class HashData
[Theory]
[CombinatorialData]
public void MurmurHashKeyTest(
[CombinatorialValues(/*DataKind.Byte, DataKind.UInt16, */DataKind.UInt32/*, DataKind.UInt64*/)]DataKind keyType)
[CombinatorialValues(DataKind.Byte, DataKind.UInt16, DataKind.UInt32, DataKind.UInt64)]DataKind keyType)
{
var dataFile = DeleteOutputPath("KeysToOnnx.txt");
File.WriteAllLines(dataFile,
Expand Down