|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Runtime.InteropServices; |
| 4 | +using System.Security; |
| 5 | +using System.Text; |
| 6 | +using Microsoft.Win32.SafeHandles; |
| 7 | + |
| 8 | +namespace Microsoft.ML.Featurizers |
| 9 | +{ |
| 10 | + internal enum FitResult : byte |
| 11 | + { |
| 12 | + Complete = 1, |
| 13 | + Continue = 2, |
| 14 | + ResetAndContinue = 3 |
| 15 | + } |
| 16 | + |
| 17 | + // Not all these types are currently supported. These are taken directly from the Native code implementation. |
| 18 | + internal enum TypeId : uint |
| 19 | + { |
| 20 | + // Enumeration values are in the following format: |
| 21 | + // |
| 22 | + // 0xVTTTXXXX |
| 23 | + // ^^^^^^^^ |
| 24 | + // || |- Id |
| 25 | + // ||- Number of trailing types |
| 26 | + // |- Has trailing types |
| 27 | + // |
| 28 | + String = 1, |
| 29 | + SByte = 2, |
| 30 | + Short = 3, |
| 31 | + Int = 4, |
| 32 | + Long = 5, |
| 33 | + Byte = 6, |
| 34 | + UShort = 7, |
| 35 | + UInt = 8, |
| 36 | + ULong = 9, |
| 37 | + Float16 = 10, |
| 38 | + Float32 = 11, |
| 39 | + Double = 12, |
| 40 | + Complex64 = 13, |
| 41 | + Complex128 = 14, |
| 42 | + BFloat16 = 15, |
| 43 | + Bool = 16, |
| 44 | + Timepoint = 17, |
| 45 | + Duration = 18, |
| 46 | + |
| 47 | + LastStaticValue = 19, |
| 48 | + |
| 49 | + // The following values have N number of trailing types |
| 50 | + Tensor = 0x1001 | LastStaticValue + 1, |
| 51 | + SparseTensor = 0x1001 | LastStaticValue + 2, |
| 52 | + Tabular = 0x1001 | LastStaticValue + 3, |
| 53 | + |
| 54 | + Nullable = 0x1001 | LastStaticValue + 4, |
| 55 | + Vector = 0x1001 | LastStaticValue + 5, |
| 56 | + MapId = 0x1002 | LastStaticValue + 6 |
| 57 | + }; |
| 58 | + |
| 59 | + // Is a struct mirroring the native struct. |
| 60 | + // I used to pass binary data between ML.NET and the native code. |
| 61 | + [StructLayout(LayoutKind.Sequential, Pack = 1)] |
| 62 | + internal unsafe struct NativeBinaryArchiveData |
| 63 | + { |
| 64 | + public byte* Data; |
| 65 | + public IntPtr DataSize; |
| 66 | + } |
| 67 | + |
| 68 | + #region SafeHandles |
| 69 | + |
| 70 | + // Safe handle that frees the memory for a native error returned to ML.NET. |
| 71 | + internal class ErrorInfoSafeHandle : SafeHandleZeroOrMinusOneIsInvalid |
| 72 | + { |
| 73 | + [DllImport("Featurizers", EntryPoint = "DestroyErrorInfo", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
| 74 | + private static extern bool DestroyErrorInfo(IntPtr error); |
| 75 | + |
| 76 | + public ErrorInfoSafeHandle(IntPtr handle) : base(true) |
| 77 | + { |
| 78 | + SetHandle(handle); |
| 79 | + } |
| 80 | + |
| 81 | + protected override bool ReleaseHandle() |
| 82 | + { |
| 83 | + return DestroyErrorInfo(handle); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // Safe handle that frees the memory for errors strings return from the native code to ML.NET. |
| 88 | + internal class ErrorInfoStringSafeHandle : SafeHandleZeroOrMinusOneIsInvalid |
| 89 | + { |
| 90 | + [DllImport("Featurizers", EntryPoint = "DestroyErrorInfoString", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
| 91 | + private static extern bool DestroyErrorInfoString(IntPtr errorString, IntPtr errorStringSize); |
| 92 | + |
| 93 | + private IntPtr _length; |
| 94 | + public ErrorInfoStringSafeHandle(IntPtr handle, IntPtr length) : base(true) |
| 95 | + { |
| 96 | + SetHandle(handle); |
| 97 | + _length = length; |
| 98 | + } |
| 99 | + |
| 100 | + protected override bool ReleaseHandle() |
| 101 | + { |
| 102 | + return DestroyErrorInfoString(handle, _length); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Safe handle that frees the memory for the transformed data. |
| 107 | + // Is called automatically after each call to transform. |
| 108 | + internal delegate bool DestroyTransformedDataNative(IntPtr output, IntPtr outputSize, out IntPtr errorHandle); |
| 109 | + internal class TransformedDataSafeHandle : SafeHandleZeroOrMinusOneIsInvalid |
| 110 | + { |
| 111 | + private DestroyTransformedDataNative _destroySaveDataHandler; |
| 112 | + private IntPtr _dataSize; |
| 113 | + |
| 114 | + public TransformedDataSafeHandle(IntPtr handle, IntPtr dataSize, DestroyTransformedDataNative destroyCppTransformerEstimator) : base(true) |
| 115 | + { |
| 116 | + SetHandle(handle); |
| 117 | + _dataSize = dataSize; |
| 118 | + _destroySaveDataHandler = destroyCppTransformerEstimator; |
| 119 | + } |
| 120 | + |
| 121 | + protected override bool ReleaseHandle() |
| 122 | + { |
| 123 | + // Not sure what to do with error stuff here. There shoudln't ever be one though. |
| 124 | + return _destroySaveDataHandler(handle, _dataSize, out IntPtr errorHandle); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // Safe handle that frees the memory for a native estimator or transformer. |
| 129 | + // Is called automatically at the end of life for a transformer or estimator. |
| 130 | + internal delegate bool DestroyNativeTransformerEstimator(IntPtr estimator, out IntPtr errorHandle); |
| 131 | + internal class TransformerEstimatorSafeHandle : SafeHandleZeroOrMinusOneIsInvalid |
| 132 | + { |
| 133 | + private DestroyNativeTransformerEstimator _destroyNativeTransformerEstimator; |
| 134 | + public TransformerEstimatorSafeHandle(IntPtr handle, DestroyNativeTransformerEstimator destroyNativeTransformerEstimator) : base(true) |
| 135 | + { |
| 136 | + SetHandle(handle); |
| 137 | + _destroyNativeTransformerEstimator = destroyNativeTransformerEstimator; |
| 138 | + } |
| 139 | + |
| 140 | + protected override bool ReleaseHandle() |
| 141 | + { |
| 142 | + // Not sure what to do with error stuff here. There shouldn't ever be one though. |
| 143 | + return _destroyNativeTransformerEstimator(handle, out IntPtr errorHandle); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Safe handle that frees the memory for the internal state of a native transformer. |
| 148 | + // Is called automatically after we save the model. |
| 149 | + internal delegate bool DestroyTransformerSaveData(IntPtr buffer, IntPtr bufferSize, out IntPtr errorHandle); |
| 150 | + internal class SaveDataSafeHandle : SafeHandleZeroOrMinusOneIsInvalid |
| 151 | + { |
| 152 | + private readonly IntPtr _dataSize; |
| 153 | + |
| 154 | + [DllImport("Featurizers", EntryPoint = "DestroyTransformerSaveData", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
| 155 | + private static extern bool DestroyTransformerSaveDataNative(IntPtr buffer, IntPtr bufferSize, out IntPtr error); |
| 156 | + |
| 157 | + public SaveDataSafeHandle(IntPtr handle, IntPtr dataSize) : base(true) |
| 158 | + { |
| 159 | + SetHandle(handle); |
| 160 | + _dataSize = dataSize; |
| 161 | + } |
| 162 | + |
| 163 | + protected override bool ReleaseHandle() |
| 164 | + { |
| 165 | + // Not sure what to do with error stuff here. There shoudln't ever be one though. |
| 166 | + return DestroyTransformerSaveDataNative(handle, _dataSize, out _); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + #endregion |
| 171 | + |
| 172 | + // Static extension classes with Common methods used in multiple featurizers |
| 173 | + internal static class CommonExtensions |
| 174 | + { |
| 175 | + [DllImport("Featurizers", EntryPoint = "GetErrorInfoString", CallingConvention = CallingConvention.Cdecl), SuppressUnmanagedCodeSecurity] |
| 176 | + private static extern bool GetErrorInfoString(IntPtr error, out IntPtr errorHandleString, out IntPtr errorHandleStringSize); |
| 177 | + |
| 178 | + internal static string GetErrorDetailsAndFreeNativeMemory(IntPtr errorHandle) |
| 179 | + { |
| 180 | + using (var error = new ErrorInfoSafeHandle(errorHandle)) |
| 181 | + { |
| 182 | + GetErrorInfoString(errorHandle, out IntPtr errorHandleString, out IntPtr errorHandleStringSize); |
| 183 | + using (var errorString = new ErrorInfoStringSafeHandle(errorHandleString, errorHandleStringSize)) |
| 184 | + { |
| 185 | + byte[] buffer = new byte[errorHandleStringSize.ToInt32()]; |
| 186 | + Marshal.Copy(errorHandleString, buffer, 0, buffer.Length); |
| 187 | + |
| 188 | + return Encoding.UTF8.GetString(buffer); |
| 189 | + } |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + internal static TypeId GetNativeTypeIdFromType(this Type type) |
| 194 | + { |
| 195 | + if (type == typeof(sbyte)) |
| 196 | + return TypeId.SByte; |
| 197 | + else if (type == typeof(short)) |
| 198 | + return TypeId.Short; |
| 199 | + else if (type == typeof(int)) |
| 200 | + return TypeId.Int; |
| 201 | + else if (type == typeof(long)) |
| 202 | + return TypeId.Long; |
| 203 | + else if (type == typeof(byte)) |
| 204 | + return TypeId.Byte; |
| 205 | + else if (type == typeof(ushort)) |
| 206 | + return TypeId.UShort; |
| 207 | + else if (type == typeof(uint)) |
| 208 | + return TypeId.UInt; |
| 209 | + else if (type == typeof(ulong)) |
| 210 | + return TypeId.ULong; |
| 211 | + else if (type == typeof(float)) |
| 212 | + return TypeId.Float32; |
| 213 | + else if (type == typeof(double)) |
| 214 | + return TypeId.Double; |
| 215 | + else if (type == typeof(bool)) |
| 216 | + return TypeId.Bool; |
| 217 | + else if (type == typeof(ReadOnlyMemory<char>)) |
| 218 | + return TypeId.String; |
| 219 | + |
| 220 | + throw new InvalidOperationException($"Unsupported type {type}"); |
| 221 | + } |
| 222 | + } |
| 223 | +} |
0 commit comments