|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Collections.Generic; |
| 7 | +using Microsoft.ML.Data; |
| 8 | +using Microsoft.ML.Internal.Utilities; |
| 9 | + |
| 10 | +namespace Microsoft.ML.Transforms.Onnx |
| 11 | +{ |
| 12 | + /// <summary> |
| 13 | + /// The corresponding <see cref="DataViewSchema.Column.Type"/> of ONNX's map type in <see cref="IDataView"/>'s type system. |
| 14 | + /// In other words, if an ONNX model produces a map, a column in <see cref="IDataView"/> may be typed to <see cref="OnnxMapType"/>. |
| 15 | + /// Its underlying type is <see cref="IDictionary{TKey, TValue}"/>, where the generic type "TKey" and "TValue" are the input arguments of |
| 16 | + /// <see cref="OnnxMapType.OnnxMapType(Type,Type)"/>. |
| 17 | + /// </summary> |
| 18 | + public sealed class OnnxMapType : StructuredDataViewType |
| 19 | + { |
| 20 | + /// <summary> |
| 21 | + /// Create the corresponding <see cref="DataViewType"/> for ONNX map. |
| 22 | + /// </summary> |
| 23 | + /// <param name="keyType">Key type of the associated ONNX map.</param> |
| 24 | + /// <param name="valueType">Value type of the associated ONNX map.</param> |
| 25 | + public OnnxMapType(Type keyType, Type valueType) : base(typeof(IDictionary<,>).MakeGenericType(keyType, valueType)) |
| 26 | + { |
| 27 | + DataViewTypeManager.Register(this, RawType, new[] { new OnnxMapTypeAttribute(keyType, valueType) }); |
| 28 | + } |
| 29 | + |
| 30 | + public override bool Equals(DataViewType other) |
| 31 | + { |
| 32 | + if (other is OnnxMapType) |
| 33 | + return RawType == other.RawType; |
| 34 | + else |
| 35 | + return false; |
| 36 | + } |
| 37 | + |
| 38 | + public override int GetHashCode() |
| 39 | + { |
| 40 | + return RawType.GetHashCode(); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// To declare <see cref="OnnxMapType"/> column in <see cref="IDataView"/> as a field |
| 46 | + /// in a <see langword="class"/>, the associated field should be marked with <see cref="OnnxMapTypeAttribute"/>. |
| 47 | + /// Its uses are similar to those of <see cref="VectorTypeAttribute"/> and other <see langword="class"/>es derived |
| 48 | + /// from <see cref="DataViewTypeAttribute"/>. |
| 49 | + /// </summary> |
| 50 | + public sealed class OnnxMapTypeAttribute : DataViewTypeAttribute |
| 51 | + { |
| 52 | + private Type _keyType; |
| 53 | + private Type _valueType; |
| 54 | + |
| 55 | + /// <summary> |
| 56 | + /// Create a map (aka dictionary) type. |
| 57 | + /// </summary> |
| 58 | + public OnnxMapTypeAttribute() |
| 59 | + { |
| 60 | + } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Create a map (aka dictionary) type. A map is a collection of key-value |
| 64 | + /// pairs. <paramref name="keyType"/> specifies the type of keys and <paramref name="valueType"/> |
| 65 | + /// is the type of values. |
| 66 | + /// </summary> |
| 67 | + public OnnxMapTypeAttribute(Type keyType, Type valueType) |
| 68 | + { |
| 69 | + _keyType = keyType; |
| 70 | + _valueType = valueType; |
| 71 | + } |
| 72 | + |
| 73 | + /// <summary> |
| 74 | + /// Map types with the same key type and the same value type should be equal. |
| 75 | + /// </summary> |
| 76 | + public override bool Equals(DataViewTypeAttribute other) |
| 77 | + { |
| 78 | + if (other is OnnxMapTypeAttribute otherSequence) |
| 79 | + return _keyType.Equals(otherSequence._keyType) && _valueType.Equals(otherSequence._valueType); |
| 80 | + return false; |
| 81 | + } |
| 82 | + |
| 83 | + /// <summary> |
| 84 | + /// Produce the same hash code for map types with the same key type and the same value type. |
| 85 | + /// </summary> |
| 86 | + public override int GetHashCode() |
| 87 | + { |
| 88 | + return Hashing.CombineHash(_keyType.GetHashCode(), _valueType.GetHashCode()); |
| 89 | + } |
| 90 | + |
| 91 | + /// <summary> |
| 92 | + /// An implementation of <see cref="DataViewTypeAttribute.Register"/>. |
| 93 | + /// </summary> |
| 94 | + public override void Register() |
| 95 | + { |
| 96 | + var enumerableType = typeof(IDictionary<,>); |
| 97 | + var type = enumerableType.MakeGenericType(_keyType, _valueType); |
| 98 | + DataViewTypeManager.Register(new OnnxMapType(_keyType, _valueType), type, new[] { this }); |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments