-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Remove ColumnType.RawKind usages Round 1. #2143
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -295,56 +295,40 @@ public static ModelArgs GetModelArgs(ColumnType type, string colName, | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contracts.CheckNonEmpty(colName, nameof(colName)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
TensorProto.Types.DataType dataType = TensorProto.Types.DataType.Undefined; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DataKind rawKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Type rawType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (type is VectorType vectorType) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rawKind = vectorType.ItemType.RawKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (type is KeyType keyType) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rawKind = keyType.RawKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rawType = vectorType.ItemType.RawType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rawType = type.RawType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
if (rawType == typeof(bool)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Unrelated to your changes, I wander if this is correct. @wschin? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wondering the same thing when I made the change... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This probably is caused by the limited types accepted by old ONNX operators implemented in RS4. As ONNXRuntime supports much more ops than dark-age implementations, we should revise our type mappings and create more tests. This is a reasonable setting but it can be improved in another PR. In reply to: 247951442 [](ancestors = 247951442) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we have an existing bug to make this better - #1198 |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(ReadOnlyMemory<char>)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.String; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(sbyte)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(byte)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(short)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(ushort)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(int)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(uint)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @wschin - this one looks incorrect as well. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears both BL and U4 mapped these values when this repo was created - machinelearning/src/Microsoft.ML.Data/Model/Onnx/OnnxUtils.cs Lines 296 to 328 in f0e639a
In reply to: 248108591 [](ancestors = 248108591) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we always translate uint to ONNX Int64, this should be fine. In reply to: 248112875 [](ancestors = 248112875) |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(long)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(ulong)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(float)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else if (rawType == typeof(double)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Double; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
else | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
rawKind = type.RawKind; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
switch (rawKind) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.BL: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.TX: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.String; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.I1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.U1: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint8; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.I2: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.U2: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint16; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.I4: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int32; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.U4: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.I8: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Int64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.U8: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Uint64; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.R4: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Float; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
case DataKind.R8: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
dataType = TensorProto.Types.DataType.Double; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
default: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
string msg = "Unsupported type: DataKind " + rawKind.ToString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contracts.Check(false, msg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
break; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
string msg = "Unsupported type: " + type.ToString(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contracts.Check(false, msg); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
string name = colName; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -134,7 +134,7 @@ private static Type GetTypeOrNull(SchemaShape.Column col) | |
|
||
if (col.IsKey) | ||
{ | ||
Type physType = StaticKind(col.ItemType.RawKind); | ||
Type physType = GetPhysicalType(col.ItemType); | ||
Contracts.Assert(physType == typeof(byte) || physType == typeof(ushort) | ||
|| physType == typeof(uint) || physType == typeof(ulong)); | ||
var keyType = typeof(Key<>).MakeGenericType(physType); | ||
|
@@ -158,7 +158,7 @@ private static Type GetTypeOrNull(SchemaShape.Column col) | |
|
||
if (col.ItemType is PrimitiveType pt) | ||
{ | ||
Type physType = StaticKind(pt.RawKind); | ||
Type physType = GetPhysicalType(pt); | ||
// Though I am unaware of any existing instances, it is theoretically possible for a | ||
// primitive type to exist, have the same data kind as one of the existing types, and yet | ||
// not be one of the built in types. (For example, an outside analogy to the key types.) For this | ||
|
@@ -266,7 +266,7 @@ private static Type GetTypeOrNull(Schema.Column col) | |
|
||
if (t is KeyType kt) | ||
{ | ||
Type physType = StaticKind(kt.RawKind); | ||
Type physType = GetPhysicalType(kt); | ||
Contracts.Assert(physType == typeof(byte) || physType == typeof(ushort) | ||
|| physType == typeof(uint) || physType == typeof(ulong)); | ||
var keyType = kt.Count > 0 ? typeof(Key<>) : typeof(VarKey<>); | ||
|
@@ -302,7 +302,7 @@ private static Type GetTypeOrNull(Schema.Column col) | |
|
||
if (t is PrimitiveType pt) | ||
{ | ||
Type physType = StaticKind(pt.RawKind); | ||
Type physType = GetPhysicalType(pt); | ||
// Though I am unaware of any existing instances, it is theoretically possible for a | ||
// primitive type to exist, have the same data kind as one of the existing types, and yet | ||
// not be one of the built in types. (For example, an outside analogy to the key types.) For this | ||
|
@@ -327,34 +327,22 @@ private static Type GetTypeOrNull(Schema.Column col) | |
/// type for communicating text. | ||
/// </summary> | ||
/// <returns>The basic type used to represent an item type in the static pipeline</returns> | ||
private static Type StaticKind(DataKind kind) | ||
private static Type GetPhysicalType(ColumnType columnType) | ||
{ | ||
switch (kind) | ||
switch (columnType) | ||
{ | ||
// The default kind is reserved for unknown types. | ||
case default(DataKind): return null; | ||
case DataKind.I1: return typeof(sbyte); | ||
case DataKind.I2: return typeof(short); | ||
case DataKind.I4: return typeof(int); | ||
case DataKind.I8: return typeof(long); | ||
|
||
case DataKind.U1: return typeof(byte); | ||
case DataKind.U2: return typeof(ushort); | ||
case DataKind.U4: return typeof(uint); | ||
case DataKind.U8: return typeof(ulong); | ||
case DataKind.U16: return typeof(RowId); | ||
|
||
case DataKind.R4: return typeof(float); | ||
case DataKind.R8: return typeof(double); | ||
case DataKind.BL: return typeof(bool); | ||
|
||
case DataKind.Text: return typeof(string); | ||
case DataKind.TimeSpan: return typeof(TimeSpan); | ||
case DataKind.DateTime: return typeof(DateTime); | ||
case DataKind.DateTimeZone: return typeof(DateTimeOffset); | ||
case NumberType numberType: | ||
case KeyType keyType: | ||
case TimeSpanType timeSpanType: | ||
case DateTimeType dateTimeType: | ||
case DateTimeOffsetType dateTimeOffsetType: | ||
case BoolType boolType: | ||
return columnType.RawType; | ||
case TextType textType: | ||
return typeof(string); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. FYI this style of change you have here is more what I'd be more comfortable with in the the |
||
|
||
default: | ||
throw Contracts.ExceptParam(nameof(kind), $"Unrecognized type '{kind}'"); | ||
return null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
curious, why change to return null, rather than throw? If an invalid type gets passed, would it be better to throw? #Resolved There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was trying to preserve the first line's behavior:
When DataKind is I assume the existing In reply to: 247782795 [](ancestors = 247782795) |
||
} | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -901,16 +901,14 @@ public void SaveAsOnnx(OnnxContext ctx) | |
|
||
private bool SaveAsOnnxCore(OnnxContext ctx, int iinfo, ColInfo info, string srcVariableName, string dstVariableName) | ||
{ | ||
DataKind rawKind; | ||
Type rawType; | ||
var type = _infos[iinfo].TypeSrc; | ||
if (type is VectorType vectorType) | ||
rawKind = vectorType.ItemType.RawKind; | ||
else if (type is KeyType keyType) | ||
rawKind = keyType.RawKind; | ||
rawType = vectorType.ItemType.RawType; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Placeholder note, once your other PR goes in you could probably benefit from your new There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, that will simplify this code. #Resolved |
||
else | ||
rawKind = type.RawKind; | ||
rawType = type.RawType; | ||
|
||
if (rawKind != DataKind.R4) | ||
if (rawType != typeof(float)) | ||
return false; | ||
|
||
string opType = "Imputer"; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -843,7 +843,7 @@ internal static bool IsColumnTypeValid(ColumnType type) | |
if (!(vectorType.ItemType is KeyType itemKeyType)) | ||
return false; | ||
// Can only accept key types that can be converted to U4. | ||
if (itemKeyType.Count == 0 && itemKeyType.RawKind > DataKind.U4) | ||
if (itemKeyType.Count == 0 && !NgramUtils.IsValidNgramRawType(itemKeyType.RawType)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If I read the comment, it says, However, if I then go and read this I would have expected just making sure it isn't a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was just converting the existing logic: I can change that method to assume that In reply to: 248037058 [](ancestors = 248037058) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool, thanks @eerhardt. I'm not sure why the original code was written this was -- it's certainly a very long winded way of writing "is U8." :) There is a competition, or used to be a competition, to see who could write the most obfuscated code possible, as I recall. (Not part of this codebase, to be clear, just in a university somewhere if I recall correctly, or a newsgroup or something?) Maybe the author was a participant. In reply to: 248110407 [](ancestors = 248110407,248037058) |
||
return false; | ||
return true; | ||
} | ||
|
@@ -855,7 +855,7 @@ internal static bool IsSchemaColumnValid(SchemaShape.Column col) | |
if (!col.IsKey) | ||
return false; | ||
// Can only accept key types that can be converted to U4. | ||
if (col.ItemType.RawKind > DataKind.U4) | ||
if (!NgramUtils.IsValidNgramRawType(col.ItemType.RawType)) | ||
return false; | ||
return true; | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure operating over
RawType
is an appropriate alternative toRawKind
, in this case. I might prefer that we operate over the exactColumnType
implementations, since that is more descriptive of what we're trying to do I think.To give a concrete example, I see the code below is conflating
uint
as being identical, even thought the same .NET type is used for both rawuint
s and actual key types... same .NET type, very different information being encoded. This might be a sign that the entire function is screwed up (I see @sfilipi found what appears to be a bug below with booleans, so that's well within the realm of possibility), but I'm a little concerned that this is the first thing I see. #ClosedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I agree. In this case we are mapping from ML.NET's
ColumnType
to Onnx'sTensorProto.Types.DataType
. It doesn't really matter if we are using aKeyType
orNumberType.U4
. In either case, we need to get aTensorProto.Types.DataType.UInt32
. Actually - what would we return forKeyType
? We wouldn't want to hard-code it to justTensorProto.Types.DataType.UInt32
, would we? What if it was a KeyType that was usingUInt16
?In reply to: 248033212 [](ancestors = 248033212)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmmm. I see your point, Fair enough thanks @eerhardt.
In reply to: 248109614 [](ancestors = 248109614,248033212)