-
Notifications
You must be signed in to change notification settings - Fork 1.9k
ONNXTransform Upgrade to Enable Non-tensor Types #3881
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
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
59faeda
Bump version
wschin ba5c2df
Minor polishment
wschin 55d8c12
revert name changes
wschin 4880ecd
Save Progress
wschin 15f8aad
Add test
wschin 2873d19
Polish code
wschin e6d710a
Merge branch 'master' into ot-upgrade
wschin 6d72ee4
Fix build by using the latest test package
wschin 76a7d0d
Add cast if ONNXRuntime produces string tensor
wschin c6bb688
Implement IDisposable
wschin fc3cc90
Add doc strings
wschin 6933250
Merge branch 'master' into ot-upgrade
wschin cf0fd06
Merge branch 'master' into ot-upgrade
wschin 75f8ff0
Use ONNXFact instead of Fact
wschin 9e539b6
Address comments
wschin d3764bf
Address comments
wschin 984315d
Add a copy of OnnxMl.cs
wschin de6741c
Referencing instead of copying
wschin 2b592d4
Fix path
wschin 6f13b96
Add a smoke test for map type
wschin 513903f
Move public classes to new files
wschin d5a379d
Merge branch 'master' into ot-upgrade
wschin da3fd48
Fix a test
wschin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ML.Data; | ||
using Microsoft.ML.Internal.Utilities; | ||
|
||
namespace Microsoft.ML.Transforms.Onnx | ||
{ | ||
/// <summary> | ||
/// The corresponding <see cref="DataViewSchema.Column.Type"/> of ONNX's map type in <see cref="IDataView"/>'s type system. | ||
/// In other words, if an ONNX model produces a map, a column in <see cref="IDataView"/> may be typed to <see cref="OnnxMapType"/>. | ||
/// Its underlying type is <see cref="IDictionary{TKey, TValue}"/>, where the generic type "TKey" and "TValue" are the input arguments of | ||
/// <see cref="OnnxMapType.OnnxMapType(Type,Type)"/>. | ||
/// </summary> | ||
public sealed class OnnxMapType : StructuredDataViewType | ||
{ | ||
/// <summary> | ||
/// Create the corresponding <see cref="DataViewType"/> for ONNX map. | ||
/// </summary> | ||
/// <param name="keyType">Key type of the associated ONNX map.</param> | ||
/// <param name="valueType">Value type of the associated ONNX map.</param> | ||
public OnnxMapType(Type keyType, Type valueType) : base(typeof(IDictionary<,>).MakeGenericType(keyType, valueType)) | ||
{ | ||
DataViewTypeManager.Register(this, RawType, new[] { new OnnxMapTypeAttribute(keyType, valueType) }); | ||
} | ||
|
||
public override bool Equals(DataViewType other) | ||
{ | ||
if (other is OnnxMapType) | ||
return RawType == other.RawType; | ||
else | ||
return false; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return RawType.GetHashCode(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// To declare <see cref="OnnxMapType"/> column in <see cref="IDataView"/> as a field | ||
/// in a <see langword="class"/>, the associated field should be marked with <see cref="OnnxMapTypeAttribute"/>. | ||
/// Its uses are similar to those of <see cref="VectorTypeAttribute"/> and other <see langword="class"/>es derived | ||
/// from <see cref="DataViewTypeAttribute"/>. | ||
/// </summary> | ||
public sealed class OnnxMapTypeAttribute : DataViewTypeAttribute | ||
{ | ||
private Type _keyType; | ||
private Type _valueType; | ||
|
||
/// <summary> | ||
/// Create a map (aka dictionary) type. | ||
/// </summary> | ||
public OnnxMapTypeAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Create a map (aka dictionary) type. A map is a collection of key-value | ||
/// pairs. <paramref name="keyType"/> specifies the type of keys and <paramref name="valueType"/> | ||
/// is the type of values. | ||
/// </summary> | ||
public OnnxMapTypeAttribute(Type keyType, Type valueType) | ||
{ | ||
_keyType = keyType; | ||
_valueType = valueType; | ||
} | ||
|
||
/// <summary> | ||
/// Map types with the same key type and the same value type should be equal. | ||
/// </summary> | ||
public override bool Equals(DataViewTypeAttribute other) | ||
{ | ||
if (other is OnnxMapTypeAttribute otherSequence) | ||
return _keyType.Equals(otherSequence._keyType) && _valueType.Equals(otherSequence._valueType); | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Produce the same hash code for map types with the same key type and the same value type. | ||
/// </summary> | ||
public override int GetHashCode() | ||
{ | ||
return Hashing.CombineHash(_keyType.GetHashCode(), _valueType.GetHashCode()); | ||
} | ||
|
||
/// <summary> | ||
/// An implementation of <see cref="DataViewTypeAttribute.Register"/>. | ||
/// </summary> | ||
public override void Register() | ||
{ | ||
var enumerableType = typeof(IDictionary<,>); | ||
var type = enumerableType.MakeGenericType(_keyType, _valueType); | ||
DataViewTypeManager.Register(new OnnxMapType(_keyType, _valueType), type, new[] { this }); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Transforms.Onnx | ||
{ | ||
/// <summary> | ||
/// The corresponding <see cref="DataViewSchema.Column.Type"/> of ONNX's sequence type in <see cref="IDataView"/>'s type system. | ||
/// In other words, if an ONNX model produces a sequence, a column in <see cref="IDataView"/> may be typed to <see cref="OnnxSequenceType"/>. | ||
/// Its underlying type is <see cref="IEnumerable{T}"/>, where the generic type "T" is the input argument of | ||
/// <see cref="OnnxSequenceType.OnnxSequenceType(Type)"/>. | ||
/// </summary> | ||
public sealed class OnnxSequenceType : StructuredDataViewType | ||
{ | ||
private static Type MakeNativeType(Type elementType) | ||
{ | ||
var enumerableTypeInfo = typeof(IEnumerable<>); | ||
var enumerableType = enumerableTypeInfo.MakeGenericType(elementType); | ||
return enumerableType; | ||
} | ||
|
||
/// <summary> | ||
/// Create the corresponding <see cref="DataViewType"/> for ONNX sequence. | ||
/// </summary> | ||
/// <param name="elementType">The element type of a sequence.</param> | ||
public OnnxSequenceType(Type elementType) : base(MakeNativeType(elementType)) | ||
{ | ||
DataViewTypeManager.Register(this, RawType, new[] { new OnnxSequenceTypeAttribute(elementType) }); | ||
} | ||
|
||
public override bool Equals(DataViewType other) | ||
{ | ||
if (other is OnnxSequenceType) | ||
return RawType == other.RawType; | ||
else | ||
return false; | ||
} | ||
|
||
public override int GetHashCode() | ||
{ | ||
return RawType.GetHashCode(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// To declare <see cref="OnnxSequenceType"/> column in <see cref="IDataView"/> as a field | ||
/// in a <see langword="class"/>, the associated field should be marked with <see cref="OnnxSequenceTypeAttribute"/>. | ||
/// Its uses are similar to those of <see cref="VectorTypeAttribute"/> and other <see langword="class"/>es derived | ||
/// from <see cref="DataViewTypeAttribute"/>. | ||
/// </summary> | ||
public sealed class OnnxSequenceTypeAttribute : DataViewTypeAttribute | ||
{ | ||
private Type _elemType; | ||
|
||
/// <summary> | ||
/// Create a sequence type. | ||
/// </summary> | ||
public OnnxSequenceTypeAttribute() | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Create a <paramref name="elemType"/>-sequence type. | ||
/// </summary> | ||
public OnnxSequenceTypeAttribute(Type elemType) | ||
{ | ||
_elemType = elemType; | ||
} | ||
|
||
/// <summary> | ||
/// Sequence types with the same element type should be equal. | ||
/// </summary> | ||
public override bool Equals(DataViewTypeAttribute other) | ||
{ | ||
if (other is OnnxSequenceTypeAttribute otherSequence) | ||
return _elemType.Equals(otherSequence._elemType); | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Produce the same hash code for sequence types with the same element type. | ||
/// </summary> | ||
public override int GetHashCode() | ||
{ | ||
return _elemType.GetHashCode(); | ||
} | ||
|
||
/// <summary> | ||
/// An implementation of <see cref="DataViewTypeAttribute.Register"/>. | ||
/// </summary> | ||
public override void Register() | ||
{ | ||
var enumerableType = typeof(IEnumerable<>); | ||
var type = enumerableType.MakeGenericType(_elemType); | ||
DataViewTypeManager.Register(new OnnxSequenceType(_elemType), type, new[] { this }); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.