onnxruntime/csharp/src/Microsoft.ML.OnnxRuntime/Exceptions.shared.cs
Dmitri Smirnov 322237f482
[C#] Implement OrtValue APIs (#16206)
### Description

Expose `OrtValue` class API as first-class citizen.
Make it simular with C++ API.
Enable safe direct native memory access.
Make string tensor manipulation more efficient.
Avoid intermediate structures such as `NamedOnnxValue`,
`DisposableNamedOnnxvalue` and etc.

Provide more examples with `IOBinding`, although `OrtValue` API
potentially makes `IOBinding` redundant for most of scenarios, since
`OrtValue` can be created on top of any memory.

Run all the pre-trained models now with `OrtValue` API as well.
Obsolete `OrtExternalMemory class`. Obsolete IOBinding API that takes
`FixedBufferOnnxValue`.

### Motivation and Context
Make the API efficient and uniform with C++.

This aspires to address: 
https://github.com/microsoft/onnxruntime/issues/14918
https://github.com/microsoft/onnxruntime/issues/15381

Cc: @Craigacp
2023-06-29 08:59:23 -07:00

59 lines
1.9 KiB
C#

// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
namespace Microsoft.ML.OnnxRuntime
{
/// <summary>
/// Enum conresponding to native onnxruntime error codes. Must be in sync with the native API
/// </summary>
internal enum ErrorCode
{
Ok = 0,
Fail = 1,
InvalidArgument = 2,
NoSuchFile = 3,
NoModel = 4,
EngineError = 5,
RuntimeException = 6,
InvalidProtobuf = 7,
ModelLoaded = 8,
NotImplemented = 9,
InvalidGraph = 10,
ShapeInferenceNotRegistered = 11,
RequirementNotRegistered = 12,
}
/// <summary>
/// The Exception that is thrown for errors related ton OnnxRuntime
/// </summary>
public class OnnxRuntimeException: Exception
{
private static Dictionary<ErrorCode, string> errorCodeToString = new Dictionary<ErrorCode, string>()
{
{ ErrorCode.Ok, "Ok" },
{ ErrorCode.Fail, "Fail" },
{ ErrorCode.InvalidArgument, "InvalidArgument"} ,
{ ErrorCode.NoSuchFile, "NoSuchFile" },
{ ErrorCode.NoModel, "NoModel" },
{ ErrorCode.EngineError, "EngineError" },
{ ErrorCode.RuntimeException, "RuntimeException" },
{ ErrorCode.InvalidProtobuf, "InvalidProtobuf" },
{ ErrorCode.ModelLoaded, "ModelLoaded" },
{ ErrorCode.NotImplemented, "NotImplemented" },
{ ErrorCode.InvalidGraph, "InvalidGraph" },
{ ErrorCode.ShapeInferenceNotRegistered, "ShapeInferenceNotRegistered" },
{ ErrorCode.RequirementNotRegistered, "RequirementNotRegistered" },
};
internal OnnxRuntimeException(ErrorCode errorCode, string message)
:base("[ErrorCode:" + errorCodeToString[errorCode] + "] " + message)
{
}
}
}