diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/FixedBufferOnnxValue.cs b/csharp/src/Microsoft.ML.OnnxRuntime/FixedBufferOnnxValue.cs index 92aab02a2f..206e87d035 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/FixedBufferOnnxValue.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/FixedBufferOnnxValue.cs @@ -31,16 +31,9 @@ namespace Microsoft.ML.OnnxRuntime /// public static FixedBufferOnnxValue CreateFromTensor(Tensor value) { - if (value is Tensor) - { - throw new ArgumentException("Only numeric tensors can be used to create FixedBufferOnnxValue.", nameof(value)); - } - NativeOnnxValueHelper.CreateNativeOnnxValue(value, out IntPtr onnxValue, out MemoryHandle pinnedMemoryHandle, out OnnxValueType onnxValueType, out TensorElementType elementType); - Debug.Assert( - onnxValueType == OnnxValueType.ONNX_TYPE_TENSOR && elementType != TensorElementType.String, - "the value should always be a numeric tensor"); + Debug.Assert(onnxValueType == OnnxValueType.ONNX_TYPE_TENSOR, "the value should always be a tensor"); return new FixedBufferOnnxValue(pinnedMemoryHandle, onnxValue, onnxValueType, elementType); } diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs b/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs index e03a70e4d9..cd3145a4c3 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs @@ -366,6 +366,11 @@ namespace Microsoft.ML.OnnxRuntime int outputIndex = 0; foreach (var output in outputValues) { + if (output.ElementType == TensorElementType.String) + { + throw new NotSupportedException("Using string type FixedBufferOnnxValue in outputs is not supported."); + } + outputValuesArray[outputIndex] = output.Value; outputIndex++; @@ -556,6 +561,11 @@ namespace Microsoft.ML.OnnxRuntime int outputIndex = 0; foreach (var output in outputValues) { + if (output.ElementType == TensorElementType.String) + { + throw new NotSupportedException("Using string type FixedBufferOnnxValue in outputs is not supported."); + } + outputValuesArray[outputIndex] = output.Value; outputIndex++; @@ -695,7 +705,7 @@ namespace Microsoft.ML.OnnxRuntime IntPtr nameHandle = IntPtr.Zero; string str = null; - IntPtr status = NativeMethods.OrtSessionEndProfiling(_nativeHandle, + IntPtr status = NativeMethods.OrtSessionEndProfiling(_nativeHandle, NativeMemoryAllocator.DefaultInstance.Handle, out nameHandle); @@ -708,7 +718,7 @@ namespace Microsoft.ML.OnnxRuntime { if (nameHandle != IntPtr.Zero) { - NativeMemoryAllocator.DefaultInstance.FreeMemory(nameHandle); + NativeMemoryAllocator.DefaultInstance.FreeMemory(nameHandle); } } diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index 1a5c2e94cb..38a70d923d 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -1056,12 +1056,41 @@ namespace Microsoft.ML.OnnxRuntime.Tests public void TestCreateFixedBufferOnnxValueFromStringTensor() { var tensor = new DenseTensor(new string[] { "a", "b" }, new int[] { 1, 2 }); + using (var value = FixedBufferOnnxValue.CreateFromTensor(tensor)) { } + } - Assert.Throws("value", () => + [Fact] + public void TestReusingStringFixedBufferOnnxValue() + { + string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "test_types_STRING.pb"); + using (var session = new InferenceSession(modelPath)) { - // cannot create from string tensor - FixedBufferOnnxValue.CreateFromTensor(tensor); - }); + var tensorA = new DenseTensor(new string[] { "a", "b", "c", "d", "e" }, new int[] { 1, 5 }); + var tensorB = new DenseTensor(new string[] { "v", "w", "x", "y", "z" }, new int[] { 1, 5 }); + var tensorC = new DenseTensor(new string[] { "i", "j", "k", "l", "m" }, new int[] { 1, 5 }); + var tensorD = new DenseTensor(new string[] { "i", "j", "k", "l", "m" }, new int[] { 1, 5 }); + using (FixedBufferOnnxValue a = FixedBufferOnnxValue.CreateFromTensor(tensorA), + b = FixedBufferOnnxValue.CreateFromTensor(tensorB), + c = FixedBufferOnnxValue.CreateFromTensor(tensorC), + d = FixedBufferOnnxValue.CreateFromTensor(tensorD)) + { + // OK to use string type FixedBufferOnnxValue only in input + session.Run(new[] { "input" }, new[] { a }); + + // Cannot use string type FixedBufferOnnxValue in output + Assert.Throws(() => + { + // NamedOnnxValue inputs + session.Run(new[] { NamedOnnxValue.CreateFromTensor("input", tensorB) }, new[] { "output" }, new[] { b }); + }); + Assert.Throws(() => + { + // both FixedBufferOnnxValue for inputs and outputs + session.Run(new[] { "input" }, new[] { c }, new[] { "output" }, new[] { d }); + }); + } + + } } [Fact]