[C#] enable string-typed FixedBufferOnnxValue in input (#4178)

This commit is contained in:
Yulong Wang 2020-06-16 11:06:11 -07:00 committed by GitHub
parent 189fb60ef9
commit 12367a6b11
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 14 deletions

View file

@ -31,16 +31,9 @@ namespace Microsoft.ML.OnnxRuntime
/// <returns></returns>
public static FixedBufferOnnxValue CreateFromTensor<T>(Tensor<T> value)
{
if (value is Tensor<string>)
{
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);
}

View file

@ -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);
}
}

View file

@ -1056,12 +1056,41 @@ namespace Microsoft.ML.OnnxRuntime.Tests
public void TestCreateFixedBufferOnnxValueFromStringTensor()
{
var tensor = new DenseTensor<string>(new string[] { "a", "b" }, new int[] { 1, 2 });
using (var value = FixedBufferOnnxValue.CreateFromTensor(tensor)) { }
}
Assert.Throws<ArgumentException>("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<string>(new string[] { "a", "b", "c", "d", "e" }, new int[] { 1, 5 });
var tensorB = new DenseTensor<string>(new string[] { "v", "w", "x", "y", "z" }, new int[] { 1, 5 });
var tensorC = new DenseTensor<string>(new string[] { "i", "j", "k", "l", "m" }, new int[] { 1, 5 });
var tensorD = new DenseTensor<string>(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<NotSupportedException>(() =>
{
// NamedOnnxValue inputs
session.Run(new[] { NamedOnnxValue.CreateFromTensor("input", tensorB) }, new[] { "output" }, new[] { b });
});
Assert.Throws<NotSupportedException>(() =>
{
// both FixedBufferOnnxValue for inputs and outputs
session.Run(new[] { "input" }, new[] { c }, new[] { "output" }, new[] { d });
});
}
}
}
[Fact]