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

Support DisposableNamedOnnxValue inputs in c# Run() #3175

Merged
merged 13 commits into from
Mar 24, 2020
7 changes: 7 additions & 0 deletions csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,13 @@ public IDisposableReadOnlyCollection<DisposableNamedOnnxValue> Run(IReadOnlyColl
int inputIndex = 0;
foreach (var input in inputs)
{
// guard against accidental DisposableNamedOnnxValue inputs flowing through
// as the code is not equipped to handle that
if (input is DisposableNamedOnnxValue)
{
throw new NotSupportedException("Input of type 'DisposableNamedOnnxValue' is not supported");
}
hariharans29 marked this conversation as resolved.
Show resolved Hide resolved

inputNames[inputIndex] = input.Name;

// create Tensor from the input if feasible, else throw notsupported exception for now
Expand Down
34 changes: 34 additions & 0 deletions csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,40 @@ private void TestModelInputBOOL()
}
}

[Fact]
private void TestReusingRunOutput()
{
// model takes 1x5 input of fixed type, echoes back
string modelPath = Path.Combine(Directory.GetCurrentDirectory(), "test_types_BOOL.pb");
using (var session = new InferenceSession(modelPath))
{
var container = new List<NamedOnnxValue>();
var tensorIn = new DenseTensor<bool>(new bool[] { true, false, true, false, true }, new int[] { 1, 5 });
var nov = NamedOnnxValue.CreateFromTensor("input", tensorIn);
container.Add(nov);
var res = session.Run(container);

bool succeeded = false;

try
{
// We feed in the output of one Run() as the input to another Run()
// This causes problems as Run() tries to pin underlying managed memory of
// DisposableNamedOnnxValue.
// Run() should fail with a user friendly error message.
session.Run(res);
}

catch(System.NotSupportedException e)
{
Assert.True(e.Message.Contains("Input of type 'DisposableNamedOnnxValue' is not supported"));
succeeded = true;
}

Assert.True(succeeded);
}
}

[Fact]
private void TestModelInputINT32()
{
Expand Down