From 1a183784a8d79c8b7fe19184f43e82945ae3084c Mon Sep 17 00:00:00 2001 From: Hariharan Seshadri Date: Mon, 18 May 2020 11:10:13 -0700 Subject: [PATCH] Fix C# layer in the way it handles sequences (#3965) * Fix C# layer in the way it handles sequence of tensors * Revert comment --- .../DisposableNamedOnnxValue.cs | 1 - .../InferenceTest.cs | 60 ++++++++++++++++++- csharp/testdata/test_sequence_tensors.onnx | 18 ++++++ 3 files changed, 77 insertions(+), 2 deletions(-) create mode 100644 csharp/testdata/test_sequence_tensors.onnx diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs b/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs index 1a8979016a..08cf3dc681 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/DisposableNamedOnnxValue.cs @@ -209,7 +209,6 @@ namespace Microsoft.ML.OnnxRuntime IntPtr nativeOnnxValueSeq; NativeApiStatus.VerifySuccess(NativeMethods.OrtGetValue(nativeOnnxValue, i, allocator, out nativeOnnxValueSeq)); sequence.Add(CreateFromOnnxValue(string.Empty, nativeOnnxValueSeq, allocator)); - NativeMethods.OrtReleaseValue(nativeOnnxValueSeq); } return new DisposableNamedOnnxValue(name, sequence, OnnxValueType.ONNX_TYPE_SEQUENCE, TensorElementType.DataTypeMax, null); diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index c634f235e5..278db87266 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -576,7 +576,6 @@ namespace Microsoft.ML.OnnxRuntime.Tests } } - [Theory] [MemberData(nameof(GetModelsForTest))] [MemberData(nameof(GetSkippedModelForTest), Skip = "Skipped due to Error, please fix the error and enable the test")] @@ -1427,6 +1426,65 @@ namespace Microsoft.ML.OnnxRuntime.Tests Assert.Equal(0.40904793, map["1"], 6); Assert.Equal(0.33156919, map["2"], 6); } + + } + } + + [Fact] + private void TestModelSequenceOfTensors() + { + + string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "test_sequence_tensors.onnx"); + + using (var session = new InferenceSession(modelPath)) + { + var outMeta = session.OutputMetadata; + Assert.Equal(OnnxValueType.ONNX_TYPE_SEQUENCE, outMeta["output_sequence"].OnnxValueType); + + var container = new List(); + var firstInputTensor = new DenseTensor(new Int64[] { 1, 2, 3, 4, 5, 6 }, new int[] { 2, 3 }); + var secondInputTensor = new DenseTensor(new Int64[] { 7, 8, 9, 10, 11, 12 }, new int[] { 2, 3 }); + + var firstNov = NamedOnnxValue.CreateFromTensor("tensor1", firstInputTensor); + var secondNov = NamedOnnxValue.CreateFromTensor("tensor2", secondInputTensor); + + container.Add(firstNov); + container.Add(secondNov); + + using (var outputs = session.Run(container)) + { + // output is a sequence + // try-cast to an sequence of NOV + var outNode = outputs.ElementAtOrDefault(0); + Assert.Equal("output_sequence", outNode.Name); + + // try-cast to an sequence of NOV + var seq = outNode.AsEnumerable(); + + // make sure that the sequence holds only 2 elements (tensors) + Assert.True(seq.Count() == 2); + + // try-cast the elements in sequence to tensor type + var firstTensorInOuputSequence = seq.First().AsTensor(); + var secondTensorInOuputSequence = seq.Last().AsTensor(); + + // make sure the tensors in the output sequence hold the correct values + Assert.True(firstTensorInOuputSequence.GetValue(0) == 1); + Assert.True(firstTensorInOuputSequence.GetValue(1) == 2); + Assert.True(firstTensorInOuputSequence.GetValue(2) == 3); + Assert.True(firstTensorInOuputSequence.GetValue(3) == 4); + Assert.True(firstTensorInOuputSequence.GetValue(4) == 5); + Assert.True(firstTensorInOuputSequence.GetValue(5) == 6); + + Assert.True(secondTensorInOuputSequence.GetValue(0) == 7); + Assert.True(secondTensorInOuputSequence.GetValue(1) == 8); + Assert.True(secondTensorInOuputSequence.GetValue(2) == 9); + Assert.True(secondTensorInOuputSequence.GetValue(3) == 10); + Assert.True(secondTensorInOuputSequence.GetValue(4) == 11); + Assert.True(secondTensorInOuputSequence.GetValue(5) == 12); + + + } } } diff --git a/csharp/testdata/test_sequence_tensors.onnx b/csharp/testdata/test_sequence_tensors.onnx new file mode 100644 index 0000000000..5fdd0aaa78 --- /dev/null +++ b/csharp/testdata/test_sequence_tensors.onnx @@ -0,0 +1,18 @@ + onnx-example:¡ +6 +tensor1 +tensor2output_sequence"SequenceConstruct +test-modelZ +tensor1 +  + +Z +tensor2 +  + +b% +output_sequence" + +  + +B \ No newline at end of file