Skip to content
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

Implement ONNX conversion for TypeConverting transform #4155

Merged
merged 6 commits into from
Sep 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/Microsoft.ML.Data/Transforms/TypeConverting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private bool SaveAsOnnxCore(OnnxContext ctx, int iinfo, string srcVariableName,
}

/// <summary>
/// Estimator for <see cref="KeyToVectorMappingTransformer"/>. Converts the underlying input column type to a new type.
/// Estimator for <see cref="TypeConvertingTransformer"/>. Converts the underlying input column type to a new type.
/// The input and output column types need to be compatible.
/// <see cref="PrimitiveDataViewType"/>
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Microsoft.ML.OnnxConverter/OnnxUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ private static TensorProto.Types.DataType ConvertToTensorProtoType(Type rawType)
else if (rawType == typeof(int))
dataType = TensorProto.Types.DataType.Int32;
else if (rawType == typeof(uint))
dataType = TensorProto.Types.DataType.Int64;
dataType = TensorProto.Types.DataType.Uint32;
else if (rawType == typeof(long))
dataType = TensorProto.Types.DataType.Int64;
else if (rawType == typeof(ulong))
Expand Down
155 changes: 155 additions & 0 deletions test/Microsoft.ML.Tests/OnnxConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,83 @@ public void WordEmbeddingsTest()
Done();
}

[Fact]
public void OnnxTypeConversionTest()
{
var mlContext = new MLContext(seed: 1);
string filePath = GetDataPath("type-conversion.txt");

// These are the supported conversions
// ML.NET does not allow any conversions between signed and unsigned numeric types
// Onnx does not seem to support casting a string to any type
// Though the onnx docs claim support for byte and sbyte,
// CreateNamedOnnxValue in OnnxUtils.cs throws a NotImplementedException for those two
DataKind[,] supportedConversions = new[,]
{
{ DataKind.Int16, DataKind.Int16},
{ DataKind.Int16, DataKind.Int32},
{ DataKind.Int16, DataKind.Int64},
{ DataKind.Int16, DataKind.Single},
{ DataKind.Int16, DataKind.Double},
{ DataKind.UInt16, DataKind.UInt16},
{ DataKind.UInt16, DataKind.UInt32},
{ DataKind.UInt16, DataKind.UInt64},
{ DataKind.UInt16, DataKind.Single},
{ DataKind.UInt16, DataKind.Double},
{ DataKind.Int32, DataKind.Int16},
{ DataKind.Int32, DataKind.Int32},
{ DataKind.Int32, DataKind.Int64},
{ DataKind.Int32, DataKind.Single},
{ DataKind.Int32, DataKind.Double},
{ DataKind.Int64, DataKind.Int16},
{ DataKind.Int64, DataKind.Int32},
{ DataKind.Int64, DataKind.Int64},
{ DataKind.Int64, DataKind.Single},
{ DataKind.Int64, DataKind.Double},
{ DataKind.UInt64, DataKind.UInt16},
{ DataKind.UInt64, DataKind.UInt32},
{ DataKind.UInt64, DataKind.UInt64},
{ DataKind.UInt64, DataKind.Single},
{ DataKind.UInt64, DataKind.Double},
{ DataKind.Single, DataKind.Single},
{ DataKind.Single, DataKind.Double},
{ DataKind.Double, DataKind.Single},
{ DataKind.Double, DataKind.Double}
};

for (int i = 0; i < supportedConversions.GetLength(0); i++)
{
var fromKind = supportedConversions[i, 0];
var toKind = supportedConversions[i, 1];

TextLoader.Column[] columns = new []
{
new TextLoader.Column("Value", fromKind, 0, 0)
};
var dataView = mlContext.Data.LoadFromTextFile(filePath, columns);
harishsk marked this conversation as resolved.
Show resolved Hide resolved

var pipeline = mlContext.Transforms.Conversion.ConvertType("ValueConverted", "Value", outputKind: toKind);
var model = pipeline.Fit(dataView);
var mlnetResult = model.Transform(dataView);

var onnxModel = mlContext.Model.ConvertToOnnxProtobuf(model, dataView);
var onnxFileName = "typeconversion.onnx";
var onnxModelPath = GetOutputPath(onnxFileName);
SaveOnnxModel(onnxModel, onnxModelPath, null);

if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && Environment.Is64BitProcess)
{
string[] inputNames = onnxModel.Graph.Input.Select(valueInfoProto => valueInfoProto.Name).ToArray();
string[] outputNames = onnxModel.Graph.Output.Select(valueInfoProto => valueInfoProto.Name).ToArray();
var onnxEstimator = mlContext.Transforms.ApplyOnnxModel(outputNames, inputNames, onnxModelPath);
var onnxTransformer = onnxEstimator.Fit(dataView);
var onnxResult = onnxTransformer.Transform(dataView);

CompareResults("ValueConverted", "ValueConverted0", mlnetResult, onnxResult);
}
}
}

private void CreateDummyExamplesToMakeComplierHappy()
{
var dummyExample = new BreastCancerFeatureVector() { Features = null };
Expand All @@ -609,6 +686,84 @@ private void CreateDummyExamplesToMakeComplierHappy()
var dummyExample3 = new SmallSentimentExample() { Tokens = null };
}

private void CompareResults(string leftColumnName, string rightColumnName, IDataView left, IDataView right)
{
var leftColumn = left.Schema[leftColumnName];
var rightColumn = right.Schema[rightColumnName];
var leftType = leftColumn.Type.GetItemType();
var rightType = rightColumn.Type.GetItemType();
Assert.Equal(leftType, rightType);

if (leftType == NumberDataViewType.SByte)
CompareSelectedVectorColumns<sbyte>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Byte)
CompareSelectedVectorColumns<byte>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Int16)
CompareSelectedVectorColumns<short>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.UInt16)
CompareSelectedVectorColumns<ushort>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Int32)
CompareSelectedVectorColumns<int>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.UInt32)
CompareSelectedVectorColumns<uint>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Int64)
CompareSelectedVectorColumns<long>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.UInt64)
CompareSelectedVectorColumns<ulong>(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Single)
CompareSelectedR4VectorColumns(leftColumnName, rightColumnName, left, right);
else if (leftType == NumberDataViewType.Double)
CompareSelectedVectorColumns<double>(leftColumnName, rightColumnName, left, right);
}

private void CompareSelectedVectorColumns<T>(string leftColumnName, string rightColumnName, IDataView left, IDataView right)
{
var leftColumn = left.Schema[leftColumnName];
var rightColumn = right.Schema[rightColumnName];

using (var expectedCursor = left.GetRowCursor(leftColumn))
using (var actualCursor = right.GetRowCursor(rightColumn))
{
VBuffer<T> expected = default;
VBuffer<T> actual = default;
var expectedGetter = expectedCursor.GetGetter<VBuffer<T>>(leftColumn);
var actualGetter = actualCursor.GetGetter<VBuffer<T>>(rightColumn);
while (expectedCursor.MoveNext() && actualCursor.MoveNext())
{
expectedGetter(ref expected);
actualGetter(ref actual);

Assert.Equal(expected.Length, actual.Length);
for (int i = 0; i < expected.Length; ++i)
Assert.Equal(expected.GetItemOrDefault(i), actual.GetItemOrDefault(i));
}
}
}

private void CompareSelectedR8VectorColumns(string leftColumnName, string rightColumnName, IDataView left, IDataView right, int precision = 6)
{
var leftColumn = left.Schema[leftColumnName];
var rightColumn = right.Schema[rightColumnName];

using (var expectedCursor = left.GetRowCursor(leftColumn))
using (var actualCursor = right.GetRowCursor(rightColumn))
{
VBuffer<double> expected = default;
VBuffer<double> actual = default;
var expectedGetter = expectedCursor.GetGetter<VBuffer<double>>(leftColumn);
var actualGetter = actualCursor.GetGetter<VBuffer<double>>(rightColumn);
while (expectedCursor.MoveNext() && actualCursor.MoveNext())
{
expectedGetter(ref expected);
actualGetter(ref actual);

Assert.Equal(expected.Length, actual.Length);
for (int i = 0; i < expected.Length; ++i)
Assert.Equal(expected.GetItemOrDefault(i), actual.GetItemOrDefault(i), precision);
}
}
}

private void CompareSelectedR4VectorColumns(string leftColumnName, string rightColumnName, IDataView left, IDataView right, int precision = 6)
{
var leftColumn = left.Schema[leftColumnName];
Expand Down
1 change: 1 addition & 0 deletions test/data/type-conversion.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3