@@ -43,9 +43,15 @@ namespace Microsoft.ML.Transforms
4343 /// </summary>
4444 /// <remarks>
4545 /// <p>Supports inferencing of models in 1.2 and 1.3 format, using the
46- /// <a href='https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime/'>Microsoft.ML.OnnxRuntime</a> library
46+ /// <a href='https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime/'>Microsoft.ML.OnnxRuntime</a> library.
4747 /// </p>
48- /// <p>The inputs and outputs of the onnx models must of of Tensors. Sequence and Maps are not yet supported.</p>
48+ /// <p>Models are scored on CPU by default. If GPU execution is needed (optional), install
49+ /// <a href='https://developer.nvidia.com/cuda-downloads'>CUDA 10.0 Toolkit</a>
50+ /// and
51+ /// <a href='https://developer.nvidia.com/cudnn'>cuDNN</a>
52+ /// , and set the parameter 'gpuDeviceId' to a valid non-negative integer. Typical device ID values are 0 or 1.
53+ /// </p>
54+ /// <p>The inputs and outputs of the ONNX models must be Tensor type. Sequence and Maps are not yet supported.</p>
4955 /// <p>Visit https://github.com/onnx/models to see a list of readily available models to get started with.</p>
5056 /// <p>Refer to http://onnx.ai' for more information about ONNX.</p>
5157 /// </remarks>
@@ -61,6 +67,12 @@ public sealed class Arguments : TransformInputBase
6167
6268 [ Argument ( ArgumentType . Multiple | ArgumentType . Required , HelpText = "Name of the output column." , SortOrder = 2 ) ]
6369 public string [ ] OutputColumns ;
70+
71+ [ Argument ( ArgumentType . AtMostOnce | ArgumentType . Required , HelpText = "GPU device id to run on (e.g. 0,1,..). Null for CPU. Requires CUDA 10.0." , SortOrder = 3 ) ]
72+ public int ? GpuDeviceId = null ;
73+
74+ [ Argument ( ArgumentType . AtMostOnce | ArgumentType . Required , HelpText = "If true, resumes execution on CPU upon GPU error. If false, will raise the GPU execption." , SortOrder = 4 ) ]
75+ public bool FallbackToCpu = false ;
6476 }
6577
6678 private readonly Arguments _args ;
@@ -88,15 +100,27 @@ private static VersionInfo GetVersionInfo()
88100 loaderAssemblyName : typeof ( OnnxTransform ) . Assembly . FullName ) ;
89101 }
90102
91- public static IDataTransform Create ( IHostEnvironment env , IDataView input , string modelFile )
103+ public static IDataTransform Create ( IHostEnvironment env , IDataView input , string modelFile , int ? gpuDeviceId = null , bool fallbackToCpu = false )
92104 {
93- var args = new Arguments { ModelFile = modelFile , InputColumns = new string [ ] { } , OutputColumns = new string [ ] { } } ;
105+ var args = new Arguments {
106+ ModelFile = modelFile ,
107+ InputColumns = new string [ ] { } ,
108+ OutputColumns = new string [ ] { } ,
109+ GpuDeviceId = gpuDeviceId ,
110+ FallbackToCpu = fallbackToCpu } ;
111+
94112 return Create ( env , args , input ) ;
95113 }
96114
97- public static IDataTransform Create ( IHostEnvironment env , IDataView input , string modelFile , string [ ] inputColumns , string [ ] outputColumns )
115+ public static IDataTransform Create ( IHostEnvironment env , IDataView input , string modelFile , string [ ] inputColumns , string [ ] outputColumns , int ? gpuDeviceId = null , bool fallbackToCpu = false )
98116 {
99- var args = new Arguments { ModelFile = modelFile , InputColumns = inputColumns , OutputColumns = outputColumns } ;
117+ var args = new Arguments {
118+ ModelFile = modelFile ,
119+ InputColumns = inputColumns ,
120+ OutputColumns = outputColumns ,
121+ GpuDeviceId = gpuDeviceId ,
122+ FallbackToCpu = fallbackToCpu } ;
123+
100124 return Create ( env , args , input ) ;
101125 }
102126
@@ -156,14 +180,21 @@ private OnnxTransform(IHostEnvironment env, Arguments args, byte[] modelBytes =
156180 foreach ( var col in args . OutputColumns )
157181 Host . CheckNonWhiteSpace ( col , nameof ( args . OutputColumns ) ) ;
158182
159- if ( modelBytes == null )
183+ try
184+ {
185+ if ( modelBytes == null )
186+ {
187+ Host . CheckNonWhiteSpace ( args . ModelFile , nameof ( args . ModelFile ) ) ;
188+ Host . CheckUserArg ( File . Exists ( args . ModelFile ) , nameof ( args . ModelFile ) ) ;
189+ Model = new OnnxModel ( args . ModelFile , args . GpuDeviceId , args . FallbackToCpu ) ;
190+ }
191+ else
192+ Model = OnnxModel . CreateFromBytes ( modelBytes , args . GpuDeviceId , args . FallbackToCpu ) ;
193+ }
194+ catch ( OnnxRuntimeException e )
160195 {
161- Host . CheckNonWhiteSpace ( args . ModelFile , nameof ( args . ModelFile ) ) ;
162- Host . CheckUserArg ( File . Exists ( args . ModelFile ) , nameof ( args . ModelFile ) ) ;
163- Model = new OnnxModel ( args . ModelFile ) ;
196+ throw Host . Except ( e , $ "Error initializing model :{ e . ToString ( ) } ") ;
164197 }
165- else
166- Model = OnnxModel . CreateFromBytes ( modelBytes ) ;
167198
168199 var modelInfo = Model . ModelInfo ;
169200 Inputs = ( args . InputColumns . Count ( ) == 0 ) ? Model . InputNames . ToArray ( ) : args . InputColumns ;
@@ -184,18 +215,68 @@ private OnnxTransform(IHostEnvironment env, Arguments args, byte[] modelBytes =
184215 _args = args ;
185216 }
186217
187- public OnnxTransform ( IHostEnvironment env , string modelFile )
188- : this ( env , new Arguments ( ) { ModelFile = modelFile , InputColumns = new string [ ] { } , OutputColumns = new string [ ] { } } )
218+ /// <summary>
219+ /// Transform for scoring ONNX models. Input data column names/types must exactly match
220+ /// all model input names. All possible output columns are generated, with names/types
221+ /// specified by model.
222+ /// </summary>
223+ /// <param name="env">The environment to use.</param>
224+ /// <param name="modelFile">Model file path.</param>
225+ /// <param name="gpuDeviceId">Optional GPU device ID to run execution on. Null for CPU.</param>
226+ /// <param name="fallbackToCpu">If GPU error, raise exception or fallback to CPU.</param>
227+ public OnnxTransform ( IHostEnvironment env , string modelFile , int ? gpuDeviceId = null , bool fallbackToCpu = false )
228+ : this ( env , new Arguments ( )
229+ {
230+ ModelFile = modelFile ,
231+ InputColumns = new string [ ] { } ,
232+ OutputColumns = new string [ ] { } ,
233+ GpuDeviceId = gpuDeviceId ,
234+ FallbackToCpu = fallbackToCpu
235+ } )
189236 {
190237 }
191238
192- public OnnxTransform ( IHostEnvironment env , string modelFile , string inputColumn , string outputColumn )
193- : this ( env , new Arguments ( ) { ModelFile = modelFile , InputColumns = new [ ] { inputColumn } , OutputColumns = new [ ] { outputColumn } } )
239+ /// <summary>
240+ /// Transform for scoring ONNX models. Input data column name/type must exactly match
241+ /// the model specification. Only 1 output column is generated.
242+ /// </summary>
243+ /// <param name="env">The environment to use.</param>
244+ /// <param name="modelFile">Model file path.</param>
245+ /// <param name="inputColumn">The name of the input data column. Must match model input name.</param>
246+ /// <param name="outputColumn">The output columns to generate. Names must match model specifications. Data types are inferred from model.</param>
247+ /// <param name="gpuDeviceId">Optional GPU device ID to run execution on. Null for CPU.</param>
248+ /// <param name="fallbackToCpu">If GPU error, raise exception or fallback to CPU.</param>
249+ public OnnxTransform ( IHostEnvironment env , string modelFile , string inputColumn , string outputColumn , int ? gpuDeviceId = null , bool fallbackToCpu = false )
250+ : this ( env , new Arguments ( )
251+ {
252+ ModelFile = modelFile ,
253+ InputColumns = new [ ] { inputColumn } ,
254+ OutputColumns = new [ ] { outputColumn } ,
255+ GpuDeviceId = gpuDeviceId ,
256+ FallbackToCpu = fallbackToCpu
257+ } )
194258 {
195259 }
196260
197- public OnnxTransform ( IHostEnvironment env , string modelFile , string [ ] inputColumns , string [ ] outputColumns )
198- : this ( env , new Arguments ( ) { ModelFile = modelFile , InputColumns = inputColumns , OutputColumns = outputColumns } )
261+ /// <summary>
262+ /// Transform for scoring ONNX models. Input data column names/types must exactly match
263+ /// all model input names. Only the output columns specified will be generated.
264+ /// </summary>
265+ /// <param name="env">The environment to use.</param>
266+ /// <param name="modelFile">Model file path.</param>
267+ /// <param name="inputColumns">The name of the input data columns. Must match model's input names.</param>
268+ /// <param name="outputColumns">The output columns to generate. Names must match model specifications. Data types are inferred from model.</param>
269+ /// <param name="gpuDeviceId">Optional GPU device ID to run execution on. Null for CPU.</param>
270+ /// <param name="fallbackToCpu">If GPU error, raise exception or fallback to CPU.</param>
271+ public OnnxTransform ( IHostEnvironment env , string modelFile , string [ ] inputColumns , string [ ] outputColumns , int ? gpuDeviceId = null , bool fallbackToCpu = false )
272+ : this ( env , new Arguments ( )
273+ {
274+ ModelFile = modelFile ,
275+ InputColumns = inputColumns ,
276+ OutputColumns = outputColumns ,
277+ GpuDeviceId = gpuDeviceId ,
278+ FallbackToCpu = fallbackToCpu
279+ } )
199280 {
200281 }
201282
0 commit comments