Skip to content

Added Onnx ValueToKey and KeyToValue support for more int types #4889

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 4 commits into from
Feb 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
17 changes: 11 additions & 6 deletions src/Microsoft.ML.Data/Transforms/KeyToValue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -505,30 +505,35 @@ public override bool SaveOnnx(OnnxContext ctx, string srcVariableName, string ds
// Onnx expects the input keys to be int64s. But the input data can come from an ML.NET node that
// may output a uint32. So cast it here to ensure that the data is treated correctly
opType = "Cast";
var castNodeOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, "CastNodeOutput", true);
var castNodeOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, "CastNodeOutput");
var castNode = ctx.CreateNode(opType, srcVariableName, castNodeOutput, ctx.GetNodeName(opType), "");
var t = InternalDataKindExtensions.ToInternalDataKind(DataKind.Int64).ToType();
castNode.AddAttribute("to", t);

var labelEncoderOutput = dstVariableName;
var labelEncoderInput = srcVariableName;
if (TypeOutput == NumberDataViewType.Double || TypeOutput == NumberDataViewType.Int64)
labelEncoderOutput = ctx.AddIntermediateVariable(TypeOutput, "CastNodeOutput", true);
if (TypeOutput == NumberDataViewType.Double)
labelEncoderOutput = ctx.AddIntermediateVariable(NumberDataViewType.Single, "CastNodeOutput");
else if (TypeOutput == NumberDataViewType.Int64 || TypeOutput == NumberDataViewType.UInt16 ||
TypeOutput == NumberDataViewType.Int32 || TypeOutput == NumberDataViewType.Int16 ||
TypeOutput == NumberDataViewType.UInt64 || TypeOutput == NumberDataViewType.UInt32)
labelEncoderOutput = ctx.AddIntermediateVariable(TextDataViewType.Instance, "CastNodeOutput");

opType = "LabelEncoder";
var node = ctx.CreateNode(opType, castNodeOutput, labelEncoderOutput, ctx.GetNodeName(opType));
var keys = Array.ConvertAll<int, long>(Enumerable.Range(1, _values.Length).ToArray(), item => Convert.ToInt64(item));
node.AddAttribute("keys_int64s", keys);

if (TypeOutput == NumberDataViewType.Int64)
if (TypeOutput == NumberDataViewType.Int64 || TypeOutput == NumberDataViewType.Int32 ||
TypeOutput == NumberDataViewType.Int16 || TypeOutput == NumberDataViewType.UInt64 ||
TypeOutput == NumberDataViewType.UInt32 || TypeOutput == NumberDataViewType.UInt16)
{
// LabelEncoder doesn't support mapping int64 -> int64, so values are converted to strings and later cast back to Int64s
string[] values = Array.ConvertAll<TValue, string>(_values.GetValues().ToArray(), item => Convert.ToString(item));
node.AddAttribute("values_strings", values);
opType = "Cast";
castNode = ctx.CreateNode(opType, labelEncoderOutput, dstVariableName, ctx.GetNodeName(opType), "");
t = InternalDataKindExtensions.ToInternalDataKind(DataKind.Int64).ToType();
castNode.AddAttribute("to", t);
castNode.AddAttribute("to", TypeOutput.RawType);
}
else if (TypeOutput == NumberDataViewType.Single)
{
Expand Down
56 changes: 41 additions & 15 deletions src/Microsoft.ML.Data/Transforms/ValueToKeyMappingTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -781,47 +781,73 @@ private IEnumerable<T> GetTermsAndIds<T>(int iinfo, out long[] termIds)
return termValues;
}

private void CastInputToString<T>(OnnxContext ctx, out OnnxNode node, out long[] termIds, string srcVariableName, int iinfo,
string opType, string labelEncoderOutput)
{
var castOutput = ctx.AddIntermediateVariable(TextDataViewType.Instance, "castOutput");
var castNode = ctx.CreateNode("Cast", srcVariableName, castOutput, ctx.GetNodeName("Cast"), "");
var t = InternalDataKindExtensions.ToInternalDataKind(DataKind.String).ToType();
castNode.AddAttribute("to", t);
node = ctx.CreateNode(opType, castOutput, labelEncoderOutput, ctx.GetNodeName(opType));
var terms = GetTermsAndIds<T>(iinfo, out termIds);
node.AddAttribute("keys_strings", terms.Select(item => item.ToString()));
}

private bool SaveAsOnnxCore(OnnxContext ctx, int iinfo, ColInfo info, string srcVariableName, string dstVariableName)
{
OnnxNode node;
long[] termIds;
string opType = "LabelEncoder";
OnnxNode castNode;
var labelEncoderOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, "LabelEncoderOutput", true);
var labelEncoderOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, "LabelEncoderOutput");

if (info.TypeSrc.GetItemType().Equals(TextDataViewType.Instance))
var type = info.TypeSrc.GetItemType();
if (type.Equals(TextDataViewType.Instance))
{
node = ctx.CreateNode(opType, srcVariableName, labelEncoderOutput, ctx.GetNodeName(opType));
var terms = GetTermsAndIds<ReadOnlyMemory<char>>(iinfo, out termIds);
node.AddAttribute("keys_strings", terms);
}
else if (info.TypeSrc.GetItemType().Equals(NumberDataViewType.Single))
else if (type.Equals(NumberDataViewType.Single))
{
node = ctx.CreateNode(opType, srcVariableName, labelEncoderOutput, ctx.GetNodeName(opType));
var terms = GetTermsAndIds<float>(iinfo, out termIds);
node.AddAttribute("keys_floats", terms);
}
else if (info.TypeSrc.GetItemType().Equals(NumberDataViewType.Double))
else if (type.Equals(NumberDataViewType.Double))
{
// LabelEncoder doesn't support double tensors, so values are cast to floats
var castOutput = ctx.AddIntermediateVariable(NumberDataViewType.Single, "castOutput", true);
castNode = ctx.CreateNode("Cast", srcVariableName, castOutput, ctx.GetNodeName(opType), "");
var castOutput = ctx.AddIntermediateVariable(NumberDataViewType.Single, "castOutput");
castNode = ctx.CreateNode("Cast", srcVariableName, castOutput, ctx.GetNodeName("Cast"), "");
var t = InternalDataKindExtensions.ToInternalDataKind(DataKind.Single).ToType();
castNode.AddAttribute("to", t);
node = ctx.CreateNode(opType, castOutput, labelEncoderOutput, ctx.GetNodeName(opType));
var terms = GetTermsAndIds<double>(iinfo, out termIds);
node.AddAttribute("keys_floats", terms);
}
else if (info.TypeSrc.GetItemType().Equals(NumberDataViewType.Int64))
else if (type.Equals(NumberDataViewType.Int64))
{
// LabelEncoder doesn't support mapping int64 -> int64, so values are cast to strings
var castOutput = ctx.AddIntermediateVariable(TextDataViewType.Instance, "castOutput", true);
castNode = ctx.CreateNode("Cast", srcVariableName, castOutput, ctx.GetNodeName(opType), "");
var t = InternalDataKindExtensions.ToInternalDataKind(DataKind.String).ToType();
castNode.AddAttribute("to", t);
node = ctx.CreateNode(opType, castOutput, labelEncoderOutput, ctx.GetNodeName(opType));
var terms = GetTermsAndIds<long>(iinfo, out termIds);
node.AddAttribute("keys_strings", terms.Select(item => item.ToString()));
CastInputToString<Int64>(ctx, out node, out termIds ,srcVariableName, iinfo, opType, labelEncoderOutput );
}
else if (type.Equals(NumberDataViewType.Int32))
{
CastInputToString<Int32>(ctx, out node, out termIds, srcVariableName, iinfo, opType, labelEncoderOutput);
}
else if (type.Equals(NumberDataViewType.Int16))
{
CastInputToString<Int16>(ctx, out node, out termIds, srcVariableName, iinfo, opType, labelEncoderOutput);
}
else if (type.Equals(NumberDataViewType.UInt64))
{
CastInputToString<UInt64>(ctx, out node, out termIds, srcVariableName, iinfo, opType, labelEncoderOutput);
}
else if (type.Equals(NumberDataViewType.UInt32))
{
CastInputToString<UInt32>(ctx, out node, out termIds, srcVariableName, iinfo, opType, labelEncoderOutput);
}
else if (type.Equals(NumberDataViewType.UInt16))
{
CastInputToString<UInt16>(ctx, out node, out termIds, srcVariableName, iinfo, opType, labelEncoderOutput);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,24 @@
}
}
},
{
"name": "LabelEncoderOutput",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "F21",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,24 @@
}
}
},
{
"name": "LabelEncoderOutput",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "F21",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,24 @@
}
}
},
{
"name": "LabelEncoderOutput",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "F21",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,24 @@
}
}
},
{
"name": "LabelEncoderOutput",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "PredictedLabel",
"type": {
Expand Down
10 changes: 10 additions & 0 deletions test/Microsoft.ML.Tests/OnnxConversionTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1202,6 +1202,11 @@ public void IndicateMissingValuesOnnxConversionTest()
[Theory]
[InlineData(DataKind.Single)]
[InlineData(DataKind.Int64)]
[InlineData(DataKind.Int32)]
[InlineData(DataKind.Int16)]
[InlineData(DataKind.UInt64)]
[InlineData(DataKind.UInt32)]
[InlineData(DataKind.UInt16)]
[InlineData(DataKind.Double)]
[InlineData(DataKind.String)]
public void ValueToKeyMappingOnnxConversionTest(DataKind valueType)
Expand Down Expand Up @@ -1237,6 +1242,11 @@ public void ValueToKeyMappingOnnxConversionTest(DataKind valueType)
[Theory]
[InlineData(DataKind.Single)]
[InlineData(DataKind.Int64)]
[InlineData(DataKind.Int32)]
[InlineData(DataKind.Int16)]
[InlineData(DataKind.UInt64)]
[InlineData(DataKind.UInt32)]
[InlineData(DataKind.UInt16)]
[InlineData(DataKind.Double)]
[InlineData(DataKind.String)]
public void KeyToValueMappingOnnxConversionTest(DataKind valueType)
Expand Down