Skip to content

Commit 7b2478c

Browse files
authored
Upgraded OnnxRuntime to v1.0 and Google Protobuf to 3.10.1 (#4416)
* Upgraded OnnxRuntime to v1.0 and Google Protobuf to 3.10.1 * Fixed version number to 1.0.0 * Changed type checking for ReadOnlyMemory<char> and changed Char to char * Fixed string handling in CreateScalarNamedOnnxValue from code review comments * Disabled GPU codepath for running Onnx models until it is better supported in onnxruntime * Upgraded baseline files to match the new ORT version * Adjusted accuracy range for onnx test
1 parent 4f5d6cf commit 7b2478c

File tree

17 files changed

+1482
-293
lines changed

17 files changed

+1482
-293
lines changed

build/Dependencies.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313

1414
<!-- Other/Non-Core Product Dependencies -->
1515
<PropertyGroup>
16-
<GoogleProtobufPackageVersion>3.5.1</GoogleProtobufPackageVersion>
16+
<GoogleProtobufPackageVersion>3.10.1</GoogleProtobufPackageVersion>
1717
<LightGBMPackageVersion>2.2.3</LightGBMPackageVersion>
1818
<MicrosoftExtensionsPackageVersion>2.1.0</MicrosoftExtensionsPackageVersion>
19-
<MicrosoftMLOnnxRuntimePackageVersion>0.5.1</MicrosoftMLOnnxRuntimePackageVersion>
19+
<MicrosoftMLOnnxRuntimePackageVersion>1.0.0</MicrosoftMLOnnxRuntimePackageVersion>
2020
<MlNetMklDepsPackageVersion>0.0.0.9</MlNetMklDepsPackageVersion>
2121
<ParquetDotNetPackageVersion>2.1.3</ParquetDotNetPackageVersion>
2222
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>

src/Microsoft.ML.OnnxConverter/OnnxMl.cs

Lines changed: 1346 additions & 157 deletions
Large diffs are not rendered by default.

src/Microsoft.ML.OnnxConverter/OnnxUtils.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ private static TypeProto MakeType(TypeProto typeProto, TensorProto.Types.DataTyp
2626
if (typeProto.TensorType == null)
2727
typeProto.TensorType = new TypeProto.Types.Tensor();
2828

29-
typeProto.TensorType.ElemType = dataType;
29+
typeProto.TensorType.ElemType = (int)dataType;
3030
if (dims != null)
3131
{
3232
for (int index = 0; index < dims.Count; index++)
@@ -304,7 +304,7 @@ public static ModelProto MakeModel(List<NodeProto> nodes, string producerName, s
304304
model.ProducerVersion = producerVersion;
305305
model.IrVersion = (long)OnnxCSharpToProtoWrapper.Version.IrVersion;
306306
model.ModelVersion = modelVersion;
307-
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "ai.onnx.ml", Version = 1 });
307+
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "ai.onnx.ml", Version = 2 });
308308
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "", Version = 9 });
309309
model.Graph = new GraphProto();
310310
var graph = model.Graph;
@@ -386,7 +386,7 @@ public static TensorProto MakeInt64(string name, long value)
386386
{
387387
var tensor = new TensorProto();
388388
tensor.Name = name;
389-
tensor.DataType = TensorProto.Types.DataType.Int64;
389+
tensor.DataType = (int)TensorProto.Types.DataType.Int64;
390390
tensor.Int64Data.Add(value);
391391
return tensor;
392392
}
@@ -396,7 +396,7 @@ public static TensorProto MakeInt64s(string name, IEnumerable<long> values, IEnu
396396
{
397397
var tensor = new TensorProto();
398398
tensor.Name = name;
399-
tensor.DataType = TensorProto.Types.DataType.Int64;
399+
tensor.DataType = (int)TensorProto.Types.DataType.Int64;
400400
tensor.Int64Data.AddRange(values);
401401
if (dims != null)
402402
tensor.Dims.AddRange(dims);
@@ -410,7 +410,7 @@ public static TensorProto MakeFloat(string name, float value)
410410
{
411411
var tensor = new TensorProto();
412412
tensor.Name = name;
413-
tensor.DataType = TensorProto.Types.DataType.Float;
413+
tensor.DataType = (int)TensorProto.Types.DataType.Float;
414414
tensor.FloatData.Add(value);
415415
return tensor;
416416
}
@@ -420,7 +420,7 @@ public static TensorProto MakeFloats(string name, IEnumerable<float> values, IEn
420420
{
421421
var tensor = new TensorProto();
422422
tensor.Name = name;
423-
tensor.DataType = TensorProto.Types.DataType.Float;
423+
tensor.DataType = (int)TensorProto.Types.DataType.Float;
424424
tensor.FloatData.AddRange(values);
425425
if (dims != null)
426426
tensor.Dims.AddRange(dims);
@@ -434,7 +434,7 @@ public static TensorProto MakeString(string name, string value)
434434
{
435435
var tensor = new TensorProto();
436436
tensor.Name = name;
437-
tensor.DataType = TensorProto.Types.DataType.String;
437+
tensor.DataType = (int)TensorProto.Types.DataType.String;
438438
tensor.StringData.Add(StringToByteString(value));
439439
return tensor;
440440
}
@@ -444,7 +444,7 @@ public static TensorProto MakeStrings(string name, IEnumerable<string> values, I
444444
{
445445
var tensor = new TensorProto();
446446
tensor.Name = name;
447-
tensor.DataType = TensorProto.Types.DataType.String;
447+
tensor.DataType = (int)TensorProto.Types.DataType.String;
448448
tensor.StringData.AddRange(StringToByteString(values));
449449
if (dims != null)
450450
tensor.Dims.AddRange(dims);

src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ private Delegate MakeTensorGetter<T>(DataViewRow input, int iinfo, INamedOnnxVal
510510
{
511511
UpdateCacheIfNeeded(input.Position, srcNamedValueGetters, activeOutputColNames, outputCacher);
512512
var namedOnnxValue = outputCacher.Outputs[_parent.Outputs[iinfo]];
513-
var tensor = namedOnnxValue.AsTensor<T>() as System.Numerics.Tensors.DenseTensor<T>;
513+
var tensor = namedOnnxValue.AsTensor<T>() as Microsoft.ML.OnnxRuntime.Tensors.DenseTensor<T>;
514514
if (tensor == null)
515515
throw Host.Except($"Output column {namedOnnxValue.Name} doesn't contain a DenseTensor of expected type {typeof(T)}");
516516
var editor = VBufferEditor.Create(ref dst, (int)tensor.Length);
@@ -528,7 +528,7 @@ private Delegate MakeStringTensorGetter(DataViewRow input, int iinfo, INamedOnnx
528528
{
529529
UpdateCacheIfNeeded(input.Position, srcNamedValueGetters, activeOutputColNames, outputCacher);
530530
var namedOnnxValue = outputCacher.Outputs[_parent.Outputs[iinfo]];
531-
var tensor = namedOnnxValue.AsTensor<string>() as System.Numerics.Tensors.DenseTensor<string>;
531+
var tensor = namedOnnxValue.AsTensor<string>() as Microsoft.ML.OnnxRuntime.Tensors.DenseTensor<string>;
532532
if (tensor == null)
533533
throw Host.Except($"Output column {namedOnnxValue.Name} doesn't contain a DenseTensor of expected type {typeof(string)}");
534534

src/Microsoft.ML.OnnxTransformer/OnnxTypeParser.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,27 @@
55
using System;
66
using System.Collections.Generic;
77
using System.Linq;
8-
using System.Numerics.Tensors;
98
using Microsoft.ML.Data;
109
using Microsoft.ML.Internal.Utilities;
1110
using Microsoft.ML.Model.OnnxConverter;
1211
using Microsoft.ML.OnnxRuntime;
12+
using Microsoft.ML.OnnxRuntime.Tensors;
13+
1314
using Microsoft.ML.Runtime;
1415

1516
namespace Microsoft.ML.Transforms.Onnx
1617
{
1718
internal static class OnnxTypeParser
1819
{
1920
/// <summary>
20-
/// Derive the corresponding <see cref="Type"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
21+
/// Derive the corresponding <see cref="Type"/> for ONNX tensor's element type specified by <paramref name="elementType"/>.
2122
/// The corresponding <see cref="Type"/> should match the type system in ONNXRuntime's C# APIs.
2223
/// This function is used when determining the corresponding <see cref="Type"/> of <see cref="OnnxCSharpToProtoWrapper.TypeProto"/>.
2324
/// </summary>
24-
/// <param name="dataType">ONNX's tensor element type.</param>
25-
public static Type GetNativeScalarType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
25+
/// <param name="elementType">ONNX's tensor element type.</param>
26+
public static Type GetNativeScalarType(int elementType)
2627
{
28+
var dataType = (OnnxCSharpToProtoWrapper.TensorProto.Types.DataType)elementType;
2729
Type scalarType = null;
2830
switch (dataType)
2931
{
@@ -106,11 +108,12 @@ public static Type GetNativeType(OnnxCSharpToProtoWrapper.TypeProto typeProto)
106108
}
107109

108110
/// <summary>
109-
/// Derive the corresponding <see cref="DataViewType"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
111+
/// Derive the corresponding <see cref="DataViewType"/> for ONNX tensor's element type specified by <paramref name="elementType"/>.
110112
/// </summary>
111-
/// <param name="dataType">ONNX's tensor element type.</param>
112-
public static DataViewType GetScalarDataViewType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
113+
/// <param name="elementType">ONNX's tensor element type.</param>
114+
public static DataViewType GetScalarDataViewType(int elementType)
113115
{
116+
var dataType = (OnnxCSharpToProtoWrapper.TensorProto.Types.DataType)elementType;
114117
DataViewType scalarType = null;
115118
switch (dataType)
116119
{

src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
using System.Collections.Generic;
77
using System.IO;
88
using System.Linq;
9-
using System.Numerics.Tensors;
109
using Microsoft.ML.Data;
1110
using Microsoft.ML.Model.OnnxConverter;
1211
using Microsoft.ML.OnnxRuntime;
12+
using Microsoft.ML.OnnxRuntime.Tensors;
1313
using Microsoft.ML.Runtime;
1414
using OnnxShape = System.Collections.Generic.List<int>;
1515

@@ -159,19 +159,9 @@ public OnnxModel(string modelFile, int? gpuDeviceId = null, bool fallbackToCpu =
159159

160160
if (gpuDeviceId != null)
161161
{
162-
try
163-
{
164-
_session = new InferenceSession(modelFile,
165-
SessionOptions.MakeSessionOptionWithCudaProvider(gpuDeviceId.Value));
166-
}
167-
catch (OnnxRuntimeException)
168-
{
169-
if (fallbackToCpu)
170-
_session = new InferenceSession(modelFile);
171-
else
172-
// if called from OnnxTranform, is caught and rethrown.
173-
throw;
174-
}
162+
// The onnxruntime v1.0 currently does not support running on the GPU on all of ML.NET's supported platforms.
163+
// This code path will be re-enabled when there is appropriate support in onnxruntime
164+
throw new NotSupportedException("Running Onnx models on a GPU is temporarily not supported!");
175165
}
176166
else
177167
{
@@ -411,7 +401,9 @@ internal sealed class OnnxUtils
411401
typeof(Int64),
412402
typeof(UInt16),
413403
typeof(UInt32),
414-
typeof(UInt64)
404+
typeof(UInt64),
405+
typeof(ReadOnlyMemory<Char>),
406+
typeof(Boolean)
415407
};
416408
private static Dictionary<Type, InternalDataKind> _typeToKindMap=
417409
new Dictionary<Type, InternalDataKind>
@@ -439,7 +431,12 @@ public static NamedOnnxValue CreateScalarNamedOnnxValue<T>(string name, T data)
439431
{
440432
if (!_onnxTypeMap.Contains(typeof(T)))
441433
throw new NotImplementedException($"Not implemented type {typeof(T)}");
442-
return NamedOnnxValue.CreateFromTensor<T>(name, new DenseTensor<T>(new T[] { data }, new int[] { 1 }));
434+
435+
if (typeof(T) == typeof(ReadOnlyMemory<char>))
436+
{
437+
return NamedOnnxValue.CreateFromTensor<string>(name, new DenseTensor<string>(new string[] { data.ToString() }, new int[] { 1, 1 }, false));
438+
}
439+
return NamedOnnxValue.CreateFromTensor<T>(name, new DenseTensor<T>(new T[] { data }, new int[] { 1, 1 }));
443440
}
444441

445442
/// <summary>

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/ExcludeVariablesInOnnxConversion.txt

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"irVersion": "3",
2+
"irVersion": "6",
33
"producerName": "ML.NET",
44
"producerVersion": "##VERSION##",
55
"domain": "machinelearning.dotnet",
@@ -421,7 +421,7 @@
421421
"name": "F1",
422422
"type": {
423423
"tensorType": {
424-
"elemType": "FLOAT",
424+
"elemType": 1,
425425
"shape": {
426426
"dim": [
427427
{
@@ -439,7 +439,7 @@
439439
"name": "F2",
440440
"type": {
441441
"tensorType": {
442-
"elemType": "STRING",
442+
"elemType": 8,
443443
"shape": {
444444
"dim": [
445445
{
@@ -459,7 +459,7 @@
459459
"name": "PredictedLabel0",
460460
"type": {
461461
"tensorType": {
462-
"elemType": "BOOL",
462+
"elemType": 9,
463463
"shape": {
464464
"dim": [
465465
{
@@ -477,7 +477,7 @@
477477
"name": "Score0",
478478
"type": {
479479
"tensorType": {
480-
"elemType": "FLOAT",
480+
"elemType": 1,
481481
"shape": {
482482
"dim": [
483483
{
@@ -495,7 +495,7 @@
495495
"name": "Probability0",
496496
"type": {
497497
"tensorType": {
498-
"elemType": "FLOAT",
498+
"elemType": 1,
499499
"shape": {
500500
"dim": [
501501
{
@@ -515,7 +515,7 @@
515515
"name": "F20",
516516
"type": {
517517
"tensorType": {
518-
"elemType": "UINT32",
518+
"elemType": 12,
519519
"shape": {
520520
"dim": [
521521
{
@@ -533,7 +533,7 @@
533533
"name": "F21",
534534
"type": {
535535
"tensorType": {
536-
"elemType": "FLOAT",
536+
"elemType": 1,
537537
"shape": {
538538
"dim": [
539539
{
@@ -551,7 +551,7 @@
551551
"name": "F22",
552552
"type": {
553553
"tensorType": {
554-
"elemType": "FLOAT",
554+
"elemType": 1,
555555
"shape": {
556556
"dim": [
557557
{
@@ -569,7 +569,7 @@
569569
"name": "Features",
570570
"type": {
571571
"tensorType": {
572-
"elemType": "FLOAT",
572+
"elemType": 1,
573573
"shape": {
574574
"dim": [
575575
{
@@ -587,7 +587,7 @@
587587
"name": "Features0",
588588
"type": {
589589
"tensorType": {
590-
"elemType": "FLOAT",
590+
"elemType": 1,
591591
"shape": {
592592
"dim": [
593593
{
@@ -606,7 +606,7 @@
606606
"opsetImport": [
607607
{
608608
"domain": "ai.onnx.ml",
609-
"version": "1"
609+
"version": "2"
610610
},
611611
{
612612
"version": "9"

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/LightGbmBinaryClassificationOnnxConversionTest.txt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"irVersion": "3",
2+
"irVersion": "6",
33
"producerName": "ML.NET",
44
"producerVersion": "##VERSION##",
55
"domain": "machinelearning.dotnet",
@@ -392,7 +392,7 @@
392392
"name": "FeatureVector",
393393
"type": {
394394
"tensorType": {
395-
"elemType": "FLOAT",
395+
"elemType": 1,
396396
"shape": {
397397
"dim": [
398398
{
@@ -410,7 +410,7 @@
410410
"name": "Target",
411411
"type": {
412412
"tensorType": {
413-
"elemType": "FLOAT",
413+
"elemType": 1,
414414
"shape": {
415415
"dim": [
416416
{
@@ -430,7 +430,7 @@
430430
"name": "FeatureVector1",
431431
"type": {
432432
"tensorType": {
433-
"elemType": "FLOAT",
433+
"elemType": 1,
434434
"shape": {
435435
"dim": [
436436
{
@@ -448,7 +448,7 @@
448448
"name": "Target0",
449449
"type": {
450450
"tensorType": {
451-
"elemType": "FLOAT",
451+
"elemType": 1,
452452
"shape": {
453453
"dim": [
454454
{
@@ -466,7 +466,7 @@
466466
"name": "Score0",
467467
"type": {
468468
"tensorType": {
469-
"elemType": "FLOAT",
469+
"elemType": 1,
470470
"shape": {
471471
"dim": [
472472
{
@@ -486,7 +486,7 @@
486486
"name": "FeatureVector0",
487487
"type": {
488488
"tensorType": {
489-
"elemType": "FLOAT",
489+
"elemType": 1,
490490
"shape": {
491491
"dim": [
492492
{
@@ -504,7 +504,7 @@
504504
"name": "Score",
505505
"type": {
506506
"tensorType": {
507-
"elemType": "FLOAT",
507+
"elemType": 1,
508508
"shape": {
509509
"dim": [
510510
{
@@ -523,7 +523,7 @@
523523
"opsetImport": [
524524
{
525525
"domain": "ai.onnx.ml",
526-
"version": "1"
526+
"version": "2"
527527
},
528528
{
529529
"version": "9"

0 commit comments

Comments
 (0)