Skip to content
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

Upgraded OnnxRuntime to v1.0 and Google Protobuf to 3.10.1 #4416

Merged
merged 7 commits into from
Nov 5, 2019
Merged
4 changes: 2 additions & 2 deletions build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@

<!-- Other/Non-Core Product Dependencies -->
<PropertyGroup>
<GoogleProtobufPackageVersion>3.5.1</GoogleProtobufPackageVersion>
<GoogleProtobufPackageVersion>3.10.1</GoogleProtobufPackageVersion>
<LightGBMPackageVersion>2.2.3</LightGBMPackageVersion>
<MicrosoftExtensionsPackageVersion>2.1.0</MicrosoftExtensionsPackageVersion>
<MicrosoftMLOnnxRuntimePackageVersion>0.5.1</MicrosoftMLOnnxRuntimePackageVersion>
<MicrosoftMLOnnxRuntimePackageVersion>1.0.0</MicrosoftMLOnnxRuntimePackageVersion>
<MlNetMklDepsPackageVersion>0.0.0.9</MlNetMklDepsPackageVersion>
<ParquetDotNetPackageVersion>2.1.3</ParquetDotNetPackageVersion>
<SystemDrawingCommonPackageVersion>4.5.0</SystemDrawingCommonPackageVersion>
Expand Down
1,503 changes: 1,346 additions & 157 deletions src/Microsoft.ML.OnnxConverter/OnnxMl.cs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions src/Microsoft.ML.OnnxConverter/OnnxUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ private static TypeProto MakeType(TypeProto typeProto, TensorProto.Types.DataTyp
if (typeProto.TensorType == null)
typeProto.TensorType = new TypeProto.Types.Tensor();

typeProto.TensorType.ElemType = dataType;
typeProto.TensorType.ElemType = (int)dataType;
if (dims != null)
{
for (int index = 0; index < dims.Count; index++)
Expand Down Expand Up @@ -304,7 +304,7 @@ public static ModelProto MakeModel(List<NodeProto> nodes, string producerName, s
model.ProducerVersion = producerVersion;
model.IrVersion = (long)OnnxCSharpToProtoWrapper.Version.IrVersion;
model.ModelVersion = modelVersion;
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "ai.onnx.ml", Version = 1 });
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "ai.onnx.ml", Version = 2 });
model.OpsetImport.Add(new OperatorSetIdProto() { Domain = "", Version = 9 });
model.Graph = new GraphProto();
var graph = model.Graph;
Expand Down Expand Up @@ -386,7 +386,7 @@ public static TensorProto MakeInt64(string name, long value)
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.Int64;
tensor.DataType = (int)TensorProto.Types.DataType.Int64;
tensor.Int64Data.Add(value);
return tensor;
}
Expand All @@ -396,7 +396,7 @@ public static TensorProto MakeInt64s(string name, IEnumerable<long> values, IEnu
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.Int64;
tensor.DataType = (int)TensorProto.Types.DataType.Int64;
tensor.Int64Data.AddRange(values);
if (dims != null)
tensor.Dims.AddRange(dims);
Expand All @@ -410,7 +410,7 @@ public static TensorProto MakeFloat(string name, float value)
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.Float;
tensor.DataType = (int)TensorProto.Types.DataType.Float;
tensor.FloatData.Add(value);
return tensor;
}
Expand All @@ -420,7 +420,7 @@ public static TensorProto MakeFloats(string name, IEnumerable<float> values, IEn
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.Float;
tensor.DataType = (int)TensorProto.Types.DataType.Float;
tensor.FloatData.AddRange(values);
if (dims != null)
tensor.Dims.AddRange(dims);
Expand All @@ -434,7 +434,7 @@ public static TensorProto MakeString(string name, string value)
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.String;
tensor.DataType = (int)TensorProto.Types.DataType.String;
tensor.StringData.Add(StringToByteString(value));
return tensor;
}
Expand All @@ -444,7 +444,7 @@ public static TensorProto MakeStrings(string name, IEnumerable<string> values, I
{
var tensor = new TensorProto();
tensor.Name = name;
tensor.DataType = TensorProto.Types.DataType.String;
tensor.DataType = (int)TensorProto.Types.DataType.String;
tensor.StringData.AddRange(StringToByteString(values));
if (dims != null)
tensor.Dims.AddRange(dims);
Expand Down
4 changes: 2 additions & 2 deletions src/Microsoft.ML.OnnxTransformer/OnnxTransform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ private Delegate MakeTensorGetter<T>(DataViewRow input, int iinfo, INamedOnnxVal
{
UpdateCacheIfNeeded(input.Position, srcNamedValueGetters, activeOutputColNames, outputCacher);
var namedOnnxValue = outputCacher.Outputs[_parent.Outputs[iinfo]];
var tensor = namedOnnxValue.AsTensor<T>() as System.Numerics.Tensors.DenseTensor<T>;
var tensor = namedOnnxValue.AsTensor<T>() as Microsoft.ML.OnnxRuntime.Tensors.DenseTensor<T>;
if (tensor == null)
throw Host.Except($"Output column {namedOnnxValue.Name} doesn't contain a DenseTensor of expected type {typeof(T)}");
var editor = VBufferEditor.Create(ref dst, (int)tensor.Length);
Expand All @@ -528,7 +528,7 @@ private Delegate MakeStringTensorGetter(DataViewRow input, int iinfo, INamedOnnx
{
UpdateCacheIfNeeded(input.Position, srcNamedValueGetters, activeOutputColNames, outputCacher);
var namedOnnxValue = outputCacher.Outputs[_parent.Outputs[iinfo]];
var tensor = namedOnnxValue.AsTensor<string>() as System.Numerics.Tensors.DenseTensor<string>;
var tensor = namedOnnxValue.AsTensor<string>() as Microsoft.ML.OnnxRuntime.Tensors.DenseTensor<string>;
if (tensor == null)
throw Host.Except($"Output column {namedOnnxValue.Name} doesn't contain a DenseTensor of expected type {typeof(string)}");

Expand Down
17 changes: 10 additions & 7 deletions src/Microsoft.ML.OnnxTransformer/OnnxTypeParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,27 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics.Tensors;
using Microsoft.ML.Data;
using Microsoft.ML.Internal.Utilities;
using Microsoft.ML.Model.OnnxConverter;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;

using Microsoft.ML.Runtime;

namespace Microsoft.ML.Transforms.Onnx
{
internal static class OnnxTypeParser
{
/// <summary>
/// Derive the corresponding <see cref="Type"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
/// Derive the corresponding <see cref="Type"/> for ONNX tensor's element type specified by <paramref name="elementType"/>.
/// The corresponding <see cref="Type"/> should match the type system in ONNXRuntime's C# APIs.
/// This function is used when determining the corresponding <see cref="Type"/> of <see cref="OnnxCSharpToProtoWrapper.TypeProto"/>.
/// </summary>
/// <param name="dataType">ONNX's tensor element type.</param>
public static Type GetNativeScalarType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
/// <param name="elementType">ONNX's tensor element type.</param>
public static Type GetNativeScalarType(int elementType)
{
var dataType = (OnnxCSharpToProtoWrapper.TensorProto.Types.DataType)elementType;
Type scalarType = null;
switch (dataType)
{
Expand Down Expand Up @@ -106,11 +108,12 @@ public static Type GetNativeType(OnnxCSharpToProtoWrapper.TypeProto typeProto)
}

/// <summary>
/// Derive the corresponding <see cref="DataViewType"/> for ONNX tensor's element type specified by <paramref name="dataType"/>.
/// Derive the corresponding <see cref="DataViewType"/> for ONNX tensor's element type specified by <paramref name="elementType"/>.
/// </summary>
/// <param name="dataType">ONNX's tensor element type.</param>
public static DataViewType GetScalarDataViewType(OnnxCSharpToProtoWrapper.TensorProto.Types.DataType dataType)
/// <param name="elementType">ONNX's tensor element type.</param>
public static DataViewType GetScalarDataViewType(int elementType)
{
var dataType = (OnnxCSharpToProtoWrapper.TensorProto.Types.DataType)elementType;
DataViewType scalarType = null;
switch (dataType)
{
Expand Down
29 changes: 13 additions & 16 deletions src/Microsoft.ML.OnnxTransformer/OnnxUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics.Tensors;
using Microsoft.ML.Data;
using Microsoft.ML.Model.OnnxConverter;
using Microsoft.ML.OnnxRuntime;
using Microsoft.ML.OnnxRuntime.Tensors;
using Microsoft.ML.Runtime;
using OnnxShape = System.Collections.Generic.List<int>;

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

if (gpuDeviceId != null)
{
try
{
_session = new InferenceSession(modelFile,
SessionOptions.MakeSessionOptionWithCudaProvider(gpuDeviceId.Value));
}
catch (OnnxRuntimeException)
{
if (fallbackToCpu)
_session = new InferenceSession(modelFile);
else
// if called from OnnxTranform, is caught and rethrown.
throw;
}
// The onnxruntime v1.0 currently does not support running on the GPU on all of ML.NET's supported platforms.
// This code path will be re-enabled when there is appropriate support in onnxruntime
throw new NotSupportedException("Running Onnx models on a GPU is temporarily not supported!");
}
else
{
Expand Down Expand Up @@ -411,7 +401,9 @@ internal sealed class OnnxUtils
typeof(Int64),
typeof(UInt16),
typeof(UInt32),
typeof(UInt64)
typeof(UInt64),
typeof(ReadOnlyMemory<Char>),
typeof(Boolean)
};
private static Dictionary<Type, InternalDataKind> _typeToKindMap=
new Dictionary<Type, InternalDataKind>
Expand Down Expand Up @@ -439,7 +431,12 @@ public static NamedOnnxValue CreateScalarNamedOnnxValue<T>(string name, T data)
{
if (!_onnxTypeMap.Contains(typeof(T)))
throw new NotImplementedException($"Not implemented type {typeof(T)}");
return NamedOnnxValue.CreateFromTensor<T>(name, new DenseTensor<T>(new T[] { data }, new int[] { 1 }));

if (typeof(T) == typeof(ReadOnlyMemory<char>))
{
return NamedOnnxValue.CreateFromTensor<string>(name, new DenseTensor<string>(new string[] { data.ToString() }, new int[] { 1, 1 }, false));
}
return NamedOnnxValue.CreateFromTensor<T>(name, new DenseTensor<T>(new T[] { data }, new int[] { 1, 1 }));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"irVersion": "3",
"irVersion": "6",
"producerName": "ML.NET",
"producerVersion": "##VERSION##",
"domain": "machinelearning.dotnet",
Expand Down Expand Up @@ -421,7 +421,7 @@
"name": "F1",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -439,7 +439,7 @@
"name": "F2",
"type": {
"tensorType": {
"elemType": "STRING",
"elemType": 8,
"shape": {
"dim": [
{
Expand All @@ -459,7 +459,7 @@
"name": "PredictedLabel0",
"type": {
"tensorType": {
"elemType": "BOOL",
"elemType": 9,
"shape": {
"dim": [
{
Expand All @@ -477,7 +477,7 @@
"name": "Score0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -495,7 +495,7 @@
"name": "Probability0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -515,7 +515,7 @@
"name": "F20",
"type": {
"tensorType": {
"elemType": "UINT32",
"elemType": 12,
"shape": {
"dim": [
{
Expand All @@ -533,7 +533,7 @@
"name": "F21",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -551,7 +551,7 @@
"name": "F22",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -569,7 +569,7 @@
"name": "Features",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -587,7 +587,7 @@
"name": "Features0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -606,7 +606,7 @@
"opsetImport": [
{
"domain": "ai.onnx.ml",
"version": "1"
"version": "2"
},
{
"version": "9"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"irVersion": "3",
"irVersion": "6",
"producerName": "ML.NET",
"producerVersion": "##VERSION##",
"domain": "machinelearning.dotnet",
Expand Down Expand Up @@ -392,7 +392,7 @@
"name": "FeatureVector",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -410,7 +410,7 @@
"name": "Target",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -430,7 +430,7 @@
"name": "FeatureVector1",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -448,7 +448,7 @@
"name": "Target0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -466,7 +466,7 @@
"name": "Score0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -486,7 +486,7 @@
"name": "FeatureVector0",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -504,7 +504,7 @@
"name": "Score",
"type": {
"tensorType": {
"elemType": "FLOAT",
"elemType": 1,
"shape": {
"dim": [
{
Expand All @@ -523,7 +523,7 @@
"opsetImport": [
{
"domain": "ai.onnx.ml",
"version": "1"
"version": "2"
},
{
"version": "9"
Expand Down
Loading