2018-11-20 00:48:22 +00:00
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System ;
using System.Runtime.InteropServices ;
namespace Microsoft.ML.OnnxRuntime
{
/// <summary>
/// NamedOnnxValue type, must match the native enum
/// </summary>
internal static class NativeMethods
{
private const string nativeLib = "onnxruntime.dll" ;
internal const CharSet charSet = CharSet . Ansi ;
#region Runtime / Environment API
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* OrtStatus* */ OrtInitialize (
2018-11-20 00:48:22 +00:00
LogLevel default_warning_level ,
string logId ,
2018-12-18 19:39:46 +00:00
out IntPtr /*(OrtEnv*)*/ env ) ;
// OrtReleaseEnv should not be used
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern void OrtReleaseEnv ( IntPtr /*(OrtEnv*)*/ env ) ;
2018-11-20 00:48:22 +00:00
#endregion Runtime / Environment API
#region Status API
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern ErrorCode OrtGetErrorCode ( IntPtr /*(OrtStatus*)*/ status ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* char* */ OrtGetErrorMessage ( IntPtr /* (OrtStatus*) */ status ) ;
2018-11-20 00:48:22 +00:00
// returns char*, need to convert to string by the caller.
2018-12-18 19:39:46 +00:00
// does not free the underlying OrtStatus*
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern void OrtReleaseStatus ( IntPtr /*(OrtStatus*)*/ statusPtr ) ;
2018-11-20 00:48:22 +00:00
#endregion Status API
#region InferenceSession API
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* OrtStatus* */ OrtCreateInferenceSession (
IntPtr /* (OrtEnv*) */ environment ,
2018-11-20 00:48:22 +00:00
[MarshalAs(UnmanagedType.LPWStr)] string modelPath , //the model path is consumed as a wchar* in the C-api
2018-12-14 22:54:23 +00:00
IntPtr /* (OrtSessionOptions*) */ sessopnOptions ,
2018-11-20 00:48:22 +00:00
out IntPtr /**/ session ) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern IntPtr /*(ONNStatus*)*/ OrtRunInference (
2018-12-18 19:39:46 +00:00
IntPtr /*(OrtSession*)*/ session ,
IntPtr /*(OrtSessionRunOptions*)*/ runOptions , // can be null to use the default options
2018-11-20 00:48:22 +00:00
string [ ] inputNames ,
2018-12-18 19:39:46 +00:00
IntPtr [ ] /* (OrtValue*[])*/ inputValues ,
2018-11-20 00:48:22 +00:00
ulong inputCount , /* TODO: size_t, make it portable for x86 arm */
string [ ] outputNames ,
ulong outputCount , /* TODO: size_t, make it portable for x86 and arm */
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 5 /*index of outputCount*/)] [ In , Out ]
IntPtr [ ] outputValues /* An array of output value pointers. Array must be allocated by the caller */
) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetInputCount (
IntPtr /*(OrtSession*)*/ session ,
2018-11-20 00:48:22 +00:00
out ulong /* TODO: size_t */ count ) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetOutputCount (
IntPtr /*(OrtSession*)*/ session ,
2018-11-20 00:48:22 +00:00
out ulong /*TODO: size_t port*/ count ) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetInputName (
IntPtr /*(OrtSession*)*/ session ,
2018-11-20 00:48:22 +00:00
ulong index , //TODO: port size_t
2018-12-14 22:54:23 +00:00
IntPtr /*(OrtAllocator*)*/ allocator ,
2018-11-20 00:48:22 +00:00
out IntPtr /*(char**)*/ name ) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetOutputName (
IntPtr /*(OrtSession*)*/ session ,
2018-11-20 00:48:22 +00:00
ulong index , //TODO: port size_t
2018-12-14 22:54:23 +00:00
IntPtr /*(OrtAllocator*)*/ allocator ,
2018-11-20 00:48:22 +00:00
out IntPtr /*(char**)*/ name ) ;
2018-12-14 22:54:23 +00:00
// release the typeinfo using OrtReleaseObject
2018-11-23 04:56:43 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetInputTypeInfo (
IntPtr /*(const OrtSession*)*/ session ,
2018-11-23 04:56:43 +00:00
ulong index , //TODO: port for size_t
2018-12-14 22:54:23 +00:00
out IntPtr /*(struct OrtTypeInfo**)*/ typeInfo ) ;
2018-11-23 04:56:43 +00:00
2018-12-14 22:54:23 +00:00
// release the typeinfo using OrtReleaseObject
2018-11-23 04:56:43 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtInferenceSessionGetOutputTypeInfo (
IntPtr /*(const OrtSession*)*/ session ,
2018-11-23 04:56:43 +00:00
ulong index , //TODO: port for size_t
2018-12-14 22:54:23 +00:00
out IntPtr /* (struct OrtTypeInfo**)*/ typeInfo ) ;
2018-11-23 04:56:43 +00:00
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern void OrtReleaseSession ( IntPtr /*(OrtSession*)*/ session ) ;
2018-11-20 00:48:22 +00:00
#endregion InferenceSession API
#region SessionOptions API
2018-11-23 04:56:43 +00:00
2018-12-14 22:54:23 +00:00
//Release using OrtReleaseObject
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern IntPtr /*OrtSessionOptions* */ OrtCreateSessionOptions ( ) ;
2018-11-20 00:48:22 +00:00
2018-11-23 04:56:43 +00:00
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern IntPtr /*(OrtSessionOptions*)*/ OrtCloneSessionOptions ( IntPtr /*(OrtSessionOptions*)*/ sessionOptions ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtEnableSequentialExecution ( IntPtr /*(OrtSessionOptions*)*/ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtDisableSequentialExecution ( IntPtr /*(OrtSessionOptions*)*/ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtEnableProfiling ( IntPtr /* OrtSessionOptions* */ options , string profilePathPrefix ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtDisableProfiling ( IntPtr /* OrtSessionOptions* */ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtEnableMemPattern ( IntPtr /* OrtSessionOptions* */ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtDisableMemPattern ( IntPtr /* OrtSessionOptions* */ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtEnableCpuMemArena ( IntPtr /* OrtSessionOptions* */ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtDisableCpuMemArena ( IntPtr /* OrtSessionOptions* */ options ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtSetSessionLogId ( IntPtr /* OrtSessionOptions* */ options , string logId ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtSetSessionLogVerbosityLevel ( IntPtr /* OrtSessionOptions* */ options , LogLevel sessionLogVerbosityLevel ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern int OrtSetSessionThreadPoolSize ( IntPtr /* OrtSessionOptions* */ options , int sessionThreadPoolSize ) ;
2018-11-20 00:48:22 +00:00
2018-11-23 04:56:43 +00:00
///**
// * The order of invocation indicates the preference order as well. In other words call this method
// * on your most preferred execution provider first followed by the less preferred ones.
// * Calling this API is optional in which case onnxruntime will use its internal CPU execution provider.
// */
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtSessionOptionsAppendExecutionProvider ( IntPtr /*(OrtSessionOptions*)*/ options , IntPtr /* (OrtProviderFactoryPtr*)*/ factory ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtCreateCpuExecutionProviderFactory ( int use_arena , out IntPtr /*(OrtProviderFactoryPtr*)*/ factory ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtCreateMkldnnExecutionProviderFactory ( int use_arena , out IntPtr /*(OrtProviderFactoryPtr**)*/ factory ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtCreateCUDAExecutionProviderFactory ( int device_id , out IntPtr /*(OrtProviderFactoryPtr**)*/ factory ) ;
2018-11-23 04:56:43 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtCreateNupharExecutionProviderFactory ( int device_id , string target_str , out IntPtr /*(OrtProviderFactoryPtr**)*/ factory ) ;
2018-11-23 04:56:43 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtAddCustomOp ( IntPtr /*(OrtSessionOptions*)*/ options , string custom_op_path ) ;
2018-11-20 00:48:22 +00:00
#endregion
#region Allocator / AllocatorInfo API
//TODO: consider exposing them publicly, when allocator API is exposed
public enum AllocatorType
{
DeviceAllocator = 0 ,
ArenaAllocator = 1
}
//TODO: consider exposing them publicly when allocator API is exposed
public enum MemoryType
{
CpuInput = - 2 , // Any CPU memory used by non-CPU execution provider
CpuOutput = - 1 , // CPU accessible memory outputted by non-CPU execution provider, i.e. CUDA_PINNED
Cpu = CpuOutput , // temporary CPU accessible memory allocated by non-CPU execution provider, i.e. CUDA_PINNED
Default = 0 , // the default allocator for execution provider
}
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* (OrtStatus*)*/ OrtCreateAllocatorInfo (
2018-11-20 00:48:22 +00:00
IntPtr /*(const char*) */ name ,
AllocatorType allocatorType ,
int identifier ,
MemoryType memType ,
2018-12-14 22:54:23 +00:00
out IntPtr /*(OrtAllocatorInfo*)*/ allocatorInfo // memory ownership transfered to caller
2018-11-20 00:48:22 +00:00
) ;
2018-12-18 19:39:46 +00:00
//ORT_API_STATUS(OrtCreateCpuAllocatorInfo, enum OrtAllocatorType type, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out)
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* (OrtStatus*)*/ OrtCreateCpuAllocatorInfo (
2018-11-20 00:48:22 +00:00
AllocatorType allocatorType ,
MemoryType memoryType ,
2018-12-14 22:54:23 +00:00
out IntPtr /*(OrtAllocatorInfo*)*/ allocatorInfo
2018-11-20 00:48:22 +00:00
) ;
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void ReleaseOrtAllocatorInfo ( IntPtr /*(OrtAllocatorInfo*)*/ allocatorInfo ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtCreateDefaultAllocator ( out IntPtr /*(OrtAllocator**)*/ allocator ) ;
2018-11-20 00:48:22 +00:00
/// <summary>
/// Releases/Unrefs any object, including the Allocator
/// </summary>
/// <param name="ptr"></param>
/// <returns>remaining ref count</returns>
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern uint /*remaining ref count*/ OrtReleaseObject ( IntPtr /*(void*)*/ ptr ) ;
2018-11-20 00:48:22 +00:00
/// <summary>
/// Release any object allocated by an allocator
/// </summary>
/// <param name="allocator"></param>
/// <param name="memory"></param>
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtAllocatorFree ( IntPtr allocator , IntPtr memory ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern IntPtr /*(const struct OrtAllocatorInfo*)*/ OrtAllocatorGetInfo ( IntPtr /*(const OrtAllocator*)*/ ptr ) ;
2018-11-20 00:48:22 +00:00
#endregion Allocator / AllocatorInfo API
#region Tensor / OnnxValue API
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /* OrtStatus */ OrtCreateTensorWithDataAsOrtValue (
2018-12-14 22:54:23 +00:00
IntPtr /* (const OrtAllocatorInfo*) */ allocatorInfo ,
2018-11-20 00:48:22 +00:00
IntPtr /* (void*) */ dataBufferHandle ,
ulong dataLength , //size_t, TODO: make it portable for x86, arm
ulong [ ] shape , //size_t* or size_t[], TODO: make it portable for x86, arm
ulong shapeLength , //size_t, TODO: make it portable for x86, arm
TensorElementType type ,
2018-12-18 19:39:46 +00:00
out IntPtr /* OrtValue** */ outputValue ) ;
2018-11-20 00:48:22 +00:00
/// This function doesn't work with string tensor
2018-12-18 19:39:46 +00:00
/// this is a no-copy method whose pointer is only valid until the backing OrtValue* is free'd.
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtGetTensorMutableData ( IntPtr /*(OrtValue*)*/ value , out IntPtr /* (void**)*/ dataBufferHandle ) ;
2018-11-20 00:48:22 +00:00
//[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
//public static extern IntPtr /*(OrtStatus*)*/ OrtGetTensorShapeDimCount(IntPtr /*(OrtValue*)*/ value, out ulong dimension); //size_t TODO: make it portable for x86, arm
2018-11-20 00:48:22 +00:00
//[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
//public static extern IntPtr /*(OrtStatus*)*/ OrtGetTensorShapeElementCount(IntPtr /*(OrtValue*)*/value, out ulong count);
2018-11-20 00:48:22 +00:00
2018-11-23 04:56:43 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/
OrtCastTypeInfoToTensorInfo ( IntPtr /*(struct OrtTypeInfo*)*/ typeInfo ) ;
2018-11-23 04:56:43 +00:00
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern IntPtr /*(OrtStatus*)*/ OrtGetTensorShapeAndType ( IntPtr /*(OrtValue*)*/ value , out IntPtr /*(struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern TensorElementType OrtGetTensorElementType ( IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern ulong /*TODO: port for size_t */ OrtGetNumOfDimensions ( IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern void OrtGetDimensions (
IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo ,
2018-11-20 00:48:22 +00:00
long [ ] dim_values ,
ulong dim_values_length ) ;
/ * *
* How many elements does this tensor have .
* May return a negative value
* e . g .
* [ ] - > 1
* [ 1 , 3 , 4 ] - > 12
* [ 2 , 0 , 4 ] - > 0
* [ - 1 , 3 , 4 ] - > - 1
* /
[DllImport(nativeLib, CharSet = charSet)]
2018-12-14 22:54:23 +00:00
public static extern long OrtGetTensorShapeElementCount ( IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo ) ;
2018-11-20 00:48:22 +00:00
[DllImport(nativeLib, CharSet = charSet)]
2018-12-18 19:39:46 +00:00
public static extern void OrtReleaseValue ( IntPtr /*(OrtValue*)*/ value ) ;
2018-11-20 00:48:22 +00:00
#endregion
} //class NativeMethods
} //namespace