diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index 1be5004ac1fa6..a98a6e0a69aae 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -40,10 +40,12 @@ extern "C" { #endif #define ORT_API_CALL _stdcall #define ORT_MUST_USE_RESULT +#define ORTCHAR_T wchar_t #else #define ORT_EXPORT #define ORT_API_CALL #define ORT_MUST_USE_RESULT __attribute__((warn_unused_result)) +#define ORTCHAR_T char #endif // Any pointer marked with _In_ or _Out_, cannot be NULL. @@ -58,6 +60,46 @@ extern "C" { #define NO_EXCEPTION #endif +// Copied from TensorProto::DataType +// Currently, Ort doesn't support complex64, complex128, bfloat16 types +typedef enum ONNXTensorElementDataType { + ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED = 0, + ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT = 1, // maps to c type float + ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8 = 2, // maps to c type uint8_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8 = 3, // maps to c type int8_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16 = 4, // maps to c type uint16_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16 = 5, // maps to c type int16_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32 = 6, // maps to c type int32_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 = 7, // maps to c type int64_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING = 8, // maps to c++ type std::string + ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL = 9, // + ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16 = 10, + ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE = 11, // maps to c type double + ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32 = 12, // maps to c type uint32_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64 = 13, // maps to c type uint64_t + ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64 = 14, // complex with float32 real and imaginary components + ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128 = 15, // complex with float64 real and imaginary components + ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16 = 16, // Non-IEEE floating-point format based on IEEE754 single-precision +} ONNXTensorElementDataType; + +// Synced with onnx TypeProto oneof +typedef enum ONNXType { + ONNX_TYPE_UNKNOWN, + ONNX_TYPE_TENSOR, + ONNX_TYPE_SEQUENCE, + ONNX_TYPE_MAP, + ONNX_TYPE_OPAQUE, + ONNX_TYPE_SPARSETENSOR, +} ONNXType; + +typedef enum OrtLoggingLevel { + ORT_LOGGING_LEVEL_kVERBOSE = 0, + ORT_LOGGING_LEVEL_kINFO = 1, + ORT_LOGGING_LEVEL_kWARNING = 2, + ORT_LOGGING_LEVEL_kERROR = 3, + ORT_LOGGING_LEVEL_kFATAL = 4 +} OrtLoggingLevel; + typedef enum OrtErrorCode { ORT_OK = 0, ORT_FAIL = 1, @@ -121,126 +163,6 @@ typedef struct OrtSessionOptions OrtSessionOptions; struct OrtEnv; typedef struct OrtEnv OrtEnv; -/** - * \param msg A null-terminated string. Its content will be copied into the newly created OrtStatus - */ -ORT_API(OrtStatus*, OrtCreateStatus, OrtErrorCode code, _In_ const char* msg) -ORT_ALL_ARGS_NONNULL; - -ORT_API(OrtErrorCode, OrtGetErrorCode, _In_ const OrtStatus* status) -ORT_ALL_ARGS_NONNULL; -/** - * \param status must not be NULL - * \return The error message inside the `status`. Don't free the returned value. - */ -ORT_API(const char*, OrtGetErrorMessage, _In_ const OrtStatus* status) -ORT_ALL_ARGS_NONNULL; - -// -// Tensor Type and Shapes -// - -// Copied from TensorProto::DataType -// Currently, Ort doesn't support complex64, complex128, bfloat16 types -typedef enum ONNXTensorElementDataType { - ONNX_TENSOR_ELEMENT_DATA_TYPE_UNDEFINED = 0, - ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT = 1, // maps to c type float - ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT8 = 2, // maps to c type uint8_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_INT8 = 3, // maps to c type int8_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT16 = 4, // maps to c type uint16_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_INT16 = 5, // maps to c type int16_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_INT32 = 6, // maps to c type int32_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64 = 7, // maps to c type int64_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING = 8, // maps to c++ type std::string - ONNX_TENSOR_ELEMENT_DATA_TYPE_BOOL = 9, // - ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT16 = 10, - ONNX_TENSOR_ELEMENT_DATA_TYPE_DOUBLE = 11, // maps to c type double - ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT32 = 12, // maps to c type uint32_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_UINT64 = 13, // maps to c type uint64_t - ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX64 = 14, // complex with float32 real and imaginary components - ONNX_TENSOR_ELEMENT_DATA_TYPE_COMPLEX128 = 15, // complex with float64 real and imaginary components - ONNX_TENSOR_ELEMENT_DATA_TYPE_BFLOAT16 = 16, // Non-IEEE floating-point format based on IEEE754 single-precision -} ONNXTensorElementDataType; - -// Sync with onnx TypeProto oneof -typedef enum ONNXType { - ONNX_TYPE_UNKNOWN, - ONNX_TYPE_TENSOR, - ONNX_TYPE_SEQUENCE, - ONNX_TYPE_MAP, - ONNX_TYPE_OPAQUE, - ONNX_TYPE_SPARSETENSOR, -} ONNXType; - -/** - * Don't free the returned value - */ -ORT_API(const OrtTensorTypeAndShapeInfo*, OrtCastTypeInfoToTensorInfo, _In_ OrtTypeInfo*); - -/** - * The retured value should be released by calling OrtReleaseObject - */ -ORT_API(OrtTensorTypeAndShapeInfo*, OrtCreateTensorTypeAndShapeInfo); - -ORT_API_STATUS(OrtSetTensorElementType, _In_ OrtTensorTypeAndShapeInfo*, enum ONNXTensorElementDataType type); - -/** - * \param info Created from OrtCreateTensorTypeAndShapeInfo() function - * \param dim_values An array with length of `dim_count`. Its elements can contain negative values. - * \param dim_count length of dim_values - */ -ORT_API_STATUS(OrtSetDims, OrtTensorTypeAndShapeInfo* info, _In_ const int64_t* dim_values, size_t dim_count); - -ORT_API(enum ONNXTensorElementDataType, OrtGetTensorElementType, _In_ const OrtTensorTypeAndShapeInfo*); -ORT_API(size_t, OrtGetNumOfDimensions, _In_ const OrtTensorTypeAndShapeInfo* info); -ORT_API(void, OrtGetDimensions, _In_ const OrtTensorTypeAndShapeInfo* info, _Out_ int64_t* dim_values, size_t 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 - * return a negative value if unknown. (That this shape contains a symbolic variable which - * represents an unknown dimension.) - */ -ORT_API(int64_t, OrtGetTensorShapeElementCount, _In_ const OrtTensorTypeAndShapeInfo* info); - -/** - * \param out Should be freed by OrtReleaseObject after use - */ -ORT_API_STATUS(OrtGetTensorShapeAndType, _In_ const OrtValue* value, _Out_ OrtTensorTypeAndShapeInfo** out); - -/** - * Get the type information of an OrtValue - * \param value - * \param out The returned value should be freed by OrtReleaseObject after use - */ -ORT_API_STATUS(OrtGetTypeInfo, _In_ const OrtValue* value, OrtTypeInfo** out); - -ORT_API(enum ONNXType, OrtGetValueType, _In_ const OrtValue* value); - -// -// OrtRunOptions -// - -/** - * \return A pointer of the newly created object. The pointer should be freed by OrtReleaseObject after use - */ -ORT_API(OrtRunOptions*, OrtCreateRunOptions); - -ORT_API_STATUS(OrtRunOptionsSetRunLogVerbosityLevel, _In_ OrtRunOptions*, unsigned int); -ORT_API_STATUS(OrtRunOptionsSetRunTag, _In_ OrtRunOptions*, _In_ const char* run_tag); - -ORT_API(unsigned int, OrtRunOptionsGetRunLogVerbosityLevel, _In_ OrtRunOptions*); -ORT_API(const char*, OrtRunOptionsGetRunTag, _In_ OrtRunOptions*); - -// set a flag so that any running OrtRunInference* calls that are using this instance of ORtRunOptions -// will exit as soon as possible if the flag is true. -ORT_API(void, OrtRunOptionsSetTerminate, _In_ OrtRunOptions*, _In_ bool value); - /** * Every type inherented from OrtObject should be deleted by OrtReleaseObject(...). */ @@ -252,22 +174,15 @@ typedef struct OrtObject { } OrtObject; -/** - * This function is a wrapper to "(*(OrtObject**)ptr)->AddRef(ptr)" - * WARNING: There is NO type checking in this function. - * Before calling this function, caller should make sure current ref count > 0 - * \return the new reference count - */ -ORT_API(uint32_t, OrtAddRefToObject, _In_ void* ptr); +//inherented from OrtObject +typedef struct OrtAllocatorInterface { + struct OrtObject parent; + void*(ORT_API_CALL* Alloc)(void* this_, size_t size); + void(ORT_API_CALL* Free)(void* this_, void* p); + const struct OrtAllocatorInfo*(ORT_API_CALL* Info)(const void* this_); +} OrtAllocatorInterface; -/** - * - * A wrapper to "(*(OrtObject**)ptr)->Release(ptr)" - * WARNING: There is NO type checking in this function. - * \param ptr Can be NULL. If it's NULL, this function will return zero. - * \return the new reference count. - */ -ORT_API(uint32_t, OrtReleaseObject, _Inout_opt_ void* ptr); +typedef OrtAllocatorInterface* OrtAllocator; //Inherented from OrtObject typedef struct OrtProviderFactoryInterface { @@ -275,6 +190,40 @@ typedef struct OrtProviderFactoryInterface { OrtStatus*(ORT_API_CALL* CreateProvider)(void* this_, OrtProvider** out); } OrtProviderFactoryInterface; +typedef void(ORT_API_CALL* OrtLoggingFunction)( + void* param, OrtLoggingLevel severity, const char* category, const char* logid, const char* code_location, + const char* message); + +/** + * OrtEnv is process-wide. For each process, only one OrtEnv can be created. + * \param out Should be freed by `OrtReleaseObject` after use + */ +ORT_API_STATUS(OrtInitialize, OrtLoggingLevel default_warning_level, _In_ const char* logid, _Out_ OrtEnv** out) +ORT_ALL_ARGS_NONNULL; + +/** + * OrtEnv is process-wise. For each process, only one OrtEnv can be created. Don't do it multiple times + * \param out Should be freed by `OrtReleaseObject` after use + */ +ORT_API_STATUS(OrtInitializeWithCustomLogger, OrtLoggingFunction logging_function, + _In_opt_ void* logger_param, OrtLoggingLevel default_warning_level, + _In_ const char* logid, + _Out_ OrtEnv** out); + +// TODO: document the path separator convention? '/' vs '\' +// TODO: should specify the access characteristics of model_path. Is this read only during the +// execution of OrtCreateInferenceSession, or does the OrtSession retain a handle to the file/directory +// and continue to access throughout the OrtSession lifetime? +// What sort of access is needed to model_path : read or read/write? +// TODO: allow loading from an in-memory byte-array +ORT_API_STATUS(OrtCreateInferenceSession, _In_ OrtEnv* env, _In_ const ORTCHAR_T* model_path, + _In_ const OrtSessionOptions* options, _Out_ OrtSession** out); + +ORT_API_STATUS(OrtRunInference, _Inout_ OrtSession* sess, + _In_ OrtRunOptions* run_options, + _In_ const char* const* input_names, _In_ const OrtValue* const* input, size_t input_len, + _In_ const char* const* output_names, size_t output_names_len, _Out_ OrtValue** output); + /** * \return A pointer of the newly created object. The pointer should be freed by OrtReleaseObject after use */ @@ -285,11 +234,11 @@ ORT_API(OrtSessionOptions*, OrtCloneSessionOptions, OrtSessionOptions*); ORT_API(void, OrtEnableSequentialExecution, _In_ OrtSessionOptions* options); ORT_API(void, OrtDisableSequentialExecution, _In_ OrtSessionOptions* options); -// enable profiling for this session. +// Enable profiling for this session. ORT_API(void, OrtEnableProfiling, _In_ OrtSessionOptions* options, _In_ const char* profile_file_prefix); ORT_API(void, OrtDisableProfiling, _In_ OrtSessionOptions* options); -// enable the memory pattern optimization. +// Enable the memory pattern optimization. // The idea is if the input shapes are the same, we could trace the internal memory allocation // and generate a memory pattern for future request. So next time we could just do one allocation // with a big chunk for all the internal memory allocation. @@ -320,94 +269,38 @@ ORT_API(void, OrtSessionOptionsAppendExecutionProvider, _In_ OrtSessionOptions* ORT_API(void, OrtAddCustomOp, _In_ OrtSessionOptions* options, const char* custom_op_path); -typedef enum OrtAllocatorType { - OrtDeviceAllocator = 0, - OrtArenaAllocator = 1 -} OrtAllocatorType; - -/** - memory types for allocator, exec provider specific types should be extended in each provider -*/ -typedef enum OrtMemType { - OrtMemTypeCPUInput = -2, // Any CPU memory used by non-CPU execution provider - OrtMemTypeCPUOutput = -1, // CPU accessible memory outputted by non-CPU execution provider, i.e. CUDA_PINNED - OrtMemTypeCPU = OrtMemTypeCPUOutput, // temporary CPU accessible memory allocated by non-CPU execution provider, i.e. CUDA_PINNED - OrtMemTypeDefault = 0, // the default allocator for execution provider -} OrtMemType; - -ORT_API_STATUS(OrtCreateAllocatorInfo, _In_ const char* name1, enum OrtAllocatorType type, int id1, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out); +ORT_API_STATUS(OrtInferenceSessionGetInputCount, _In_ const OrtSession* sess, _Out_ size_t* out); +ORT_API_STATUS(OrtInferenceSessionGetOutputCount, _In_ const OrtSession* sess, _Out_ size_t* out); /** - * Test if two allocation info are equal - * \return 0, equal. zero, not equal + * \param out should be freed by OrtReleaseObject after use */ -ORT_API(int, OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2) -ORT_ALL_ARGS_NONNULL; +ORT_API_STATUS(OrtInferenceSessionGetInputTypeInfo, _In_ const OrtSession* sess, size_t index, _Out_ OrtTypeInfo** out); + /** - * Do not free the returned value + * \param out should be freed by OrtReleaseObject after use */ -ORT_API(const char*, OrtAllocatorInfoGetName, _In_ OrtAllocatorInfo* ptr); -ORT_API(int, OrtAllocatorInfoGetId, _In_ OrtAllocatorInfo* ptr); -ORT_API(OrtMemType, OrtAllocatorInfoGetMemType, _In_ OrtAllocatorInfo* ptr); -ORT_API(OrtAllocatorType, OrtAllocatorInfoGetType, _In_ OrtAllocatorInfo* ptr); - -//inherented from OrtObject -typedef struct OrtAllocatorInterface { - struct OrtObject parent; - void*(ORT_API_CALL* Alloc)(void* this_, size_t size); - void(ORT_API_CALL* Free)(void* this_, void* p); - const struct OrtAllocatorInfo*(ORT_API_CALL* Info)(const void* this_); -} OrtAllocatorInterface; - -typedef OrtAllocatorInterface* OrtAllocator; - -ORT_API(void*, OrtAllocatorAlloc, _Inout_ OrtAllocator* ptr, size_t size); -ORT_API(void, OrtAllocatorFree, _Inout_ OrtAllocator* ptr, void* p); -ORT_API(const OrtAllocatorInfo*, OrtAllocatorGetInfo, _In_ const OrtAllocator* ptr); +ORT_API_STATUS(OrtInferenceSessionGetOutputTypeInfo, _In_ const OrtSession* sess, size_t index, _Out_ OrtTypeInfo** out); -typedef enum OrtLoggingLevel { - ORT_LOGGING_LEVEL_kVERBOSE = 0, - ORT_LOGGING_LEVEL_kINFO = 1, - ORT_LOGGING_LEVEL_kWARNING = 2, - ORT_LOGGING_LEVEL_kERROR = 3, - ORT_LOGGING_LEVEL_kFATAL = 4 -} OrtLoggingLevel; +ORT_API_STATUS(OrtInferenceSessionGetInputName, _In_ const OrtSession* sess, size_t index, + _Inout_ OrtAllocator* allocator, _Out_ char** value); +ORT_API_STATUS(OrtInferenceSessionGetOutputName, _In_ const OrtSession* sess, size_t index, + _Inout_ OrtAllocator* allocator, _Out_ char** value); -typedef void(ORT_API_CALL* OrtLoggingFunction)( - void* param, OrtLoggingLevel severity, const char* category, const char* logid, const char* code_location, - const char* message); /** - * OrtEnv is process-wise. For each process, only one OrtEnv can be created. Don't do it multiple times - * \param out Should be freed by `OrtReleaseObject` after use + * \return A pointer to the newly created object. The pointer should be freed by OrtReleaseObject after use */ -ORT_API_STATUS(OrtInitialize, OrtLoggingLevel default_warning_level, _In_ const char* logid, _Out_ OrtEnv** out) -ORT_ALL_ARGS_NONNULL; +ORT_API(OrtRunOptions*, OrtCreateRunOptions); -/** - * OrtEnv is process-wise. For each process, only one OrtEnv can be created. Don't do it multiple times - * \param out Should be freed by `OrtReleaseObject` after use - */ -ORT_API_STATUS(OrtInitializeWithCustomLogger, OrtLoggingFunction logging_function, - _In_opt_ void* logger_param, OrtLoggingLevel default_warning_level, - _In_ const char* logid, - _Out_ OrtEnv** out); +ORT_API_STATUS(OrtRunOptionsSetRunLogVerbosityLevel, _In_ OrtRunOptions*, unsigned int); +ORT_API_STATUS(OrtRunOptionsSetRunTag, _In_ OrtRunOptions*, _In_ const char* run_tag); -// TODO: document the path separator convention? '/' vs '\' -// TODO: should specify the access characteristics of model_path. Is this read only during the -// execution of OrtCreateInferenceSession, or does the OrtSession retain a handle to the file/directory -// and continue to access throughout the OrtSession lifetime? -// What sort of access is needed to model_path : read or read/write? -// TODO: allow loading from an in-memory byte-array -#ifdef _WIN32 -ORT_API_STATUS(OrtCreateInferenceSession, _In_ OrtEnv* env, _In_ const wchar_t* model_path, - _In_ const OrtSessionOptions* options, _Out_ OrtSession** out); -#else -ORT_API_STATUS(OrtCreateInferenceSession, _In_ OrtEnv* env, _In_ const char* model_path, - _In_ const OrtSessionOptions* options, _Out_ OrtSession** out); -#endif +ORT_API(unsigned int, OrtRunOptionsGetRunLogVerbosityLevel, _In_ OrtRunOptions*); +ORT_API(const char*, OrtRunOptionsGetRunTag, _In_ OrtRunOptions*); -// Call OrtReleaseObject to release the returned value -ORT_API_STATUS(OrtCreateDefaultAllocator, _Out_ OrtAllocator** out); +// set a flag so that any running OrtRunInference* calls that are using this instance of ORtRunOptions +// will exit as soon as possible if the flag is true. +ORT_API(void, OrtRunOptionsSetTerminate, _In_ OrtRunOptions*, _In_ bool value); /** * Create a tensor from an allocator. OrtReleaseValue will also release the buffer inside the output value @@ -428,61 +321,159 @@ ORT_API_STATUS(OrtCreateTensorWithDataAsOrtValue, _In_ const OrtAllocatorInfo* i _In_ void* p_data, size_t p_data_len, _In_ const size_t* shape, size_t shape_len, ONNXTensorElementDataType type, _Out_ OrtValue** out); -/// This function doesn't work with string tensor -/// this is a no-copy method whose pointer is only valid until the backing OrtValue is free'd. +// This function doesn't work with string tensor +// this is a no-copy method whose pointer is only valid until the backing OrtValue is free'd. ORT_API_STATUS(OrtGetTensorMutableData, _Inout_ OrtValue* value, _Out_ void** out); /** * Test if an OrtValue is a tensor - * \return zero, false. non-zero true + * \return zero if false. non-zero if true */ ORT_API(int, OrtIsTensor, _In_ const OrtValue* value); /** - * \param value A tensor created from OrtCreateTensor*** function. + * \param value A tensor created from OrtCreateTensor... function. * \param s each A string array. Each string in this array must be null terminated. * \param s_len length of s */ ORT_API_STATUS(OrtFillStringTensor, _In_ OrtValue* value, _In_ const char* const* s, size_t s_len); /** - * \param value A tensor created from OrtCreateTensor*** function. + * \param value A tensor created from OrtCreateTensor... function. * \param len total data length, not including the trailing '\0' chars. */ ORT_API_STATUS(OrtGetStringTensorDataLength, _In_ const OrtValue* value, _Out_ size_t* len); /** * \param s string contents. Each string is NOT null-terminated. - * \param value A tensor created from OrtCreateTensor*** function. + * \param value A tensor created from OrtCreateTensor... function. * \param s_len total data length, get it from OrtGetStringTensorDataLength */ ORT_API_STATUS(OrtGetStringTensorContent, _In_ const OrtValue* value, _Out_ void* s, size_t s_len, _Out_ size_t* offsets, size_t offsets_len); -ORT_API_STATUS(OrtRunInference, _Inout_ OrtSession* sess, - _In_ OrtRunOptions* run_options, - _In_ const char* const* input_names, _In_ const OrtValue* const* input, size_t input_len, - _In_ const char* const* output_names, size_t output_names_len, _Out_ OrtValue** output); +ORT_API_STATUS(OrtTensorProtoToOrtValue, _Inout_ OrtAllocator* allocator, + _In_ const void* input, int input_len, _Out_ OrtValue** out); -ORT_API_STATUS(OrtInferenceSessionGetInputCount, _In_ const OrtSession* sess, _Out_ size_t* out); -ORT_API_STATUS(OrtInferenceSessionGetOutputCount, _In_ const OrtSession* sess, _Out_ size_t* out); +/** + * Don't free the returned value + */ +ORT_API(const OrtTensorTypeAndShapeInfo*, OrtCastTypeInfoToTensorInfo, _In_ OrtTypeInfo*); /** - * \param out should be freed by OrtReleaseObject after use + * The retured value should be released by calling OrtReleaseObject */ -ORT_API_STATUS(OrtInferenceSessionGetInputTypeInfo, _In_ const OrtSession* sess, size_t index, _Out_ OrtTypeInfo** out); +ORT_API(OrtTensorTypeAndShapeInfo*, OrtCreateTensorTypeAndShapeInfo); + +ORT_API_STATUS(OrtSetTensorElementType, _In_ OrtTensorTypeAndShapeInfo*, enum ONNXTensorElementDataType type); /** - * \param out should be freed by OrtReleaseObject after use + * \param info Created from OrtCreateTensorTypeAndShapeInfo() function + * \param dim_values An array with length of `dim_count`. Its elements can contain negative values. + * \param dim_count length of dim_values */ -ORT_API_STATUS(OrtInferenceSessionGetOutputTypeInfo, _In_ const OrtSession* sess, size_t index, _Out_ OrtTypeInfo** out); +ORT_API_STATUS(OrtSetDims, OrtTensorTypeAndShapeInfo* info, _In_ const int64_t* dim_values, size_t dim_count); -ORT_API_STATUS(OrtInferenceSessionGetInputName, _In_ const OrtSession* sess, size_t index, - _Inout_ OrtAllocator* allocator, _Out_ char** value); -ORT_API_STATUS(OrtInferenceSessionGetOutputName, _In_ const OrtSession* sess, size_t index, - _Inout_ OrtAllocator* allocator, _Out_ char** value); +ORT_API(enum ONNXTensorElementDataType, OrtGetTensorElementType, _In_ const OrtTensorTypeAndShapeInfo*); +ORT_API(size_t, OrtGetNumOfDimensions, _In_ const OrtTensorTypeAndShapeInfo* info); +ORT_API(void, OrtGetDimensions, _In_ const OrtTensorTypeAndShapeInfo* info, _Out_ int64_t* dim_values, size_t dim_values_length); -ORT_API_STATUS(OrtTensorProtoToOrtValue, _Inout_ OrtAllocator* allocator, - _In_ const void* input, int input_len, _Out_ OrtValue** out); +/** + * 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 + * return a negative value if unknown. (That this shape contains a symbolic variable which + * represents an unknown dimension.) + */ +ORT_API(int64_t, OrtGetTensorShapeElementCount, _In_ const OrtTensorTypeAndShapeInfo* info); + +/** + * \param out Should be freed by OrtReleaseObject after use + */ +ORT_API_STATUS(OrtGetTensorShapeAndType, _In_ const OrtValue* value, _Out_ OrtTensorTypeAndShapeInfo** out); + +/** + * Get the type information of an OrtValue + * \param value + * \param out The returned value should be freed by OrtReleaseObject after use + */ +ORT_API_STATUS(OrtGetTypeInfo, _In_ const OrtValue* value, OrtTypeInfo** out); + +ORT_API(enum ONNXType, OrtGetValueType, _In_ const OrtValue* value); + +/** + * This function is a wrapper to "(*(OrtObject**)ptr)->AddRef(ptr)" + * WARNING: There is NO type checking in this function. + * Before calling this function, caller should make sure current ref count > 0 + * \return the new reference count + */ +ORT_API(uint32_t, OrtAddRefToObject, _In_ void* ptr); + +/** + * + * A wrapper to "(*(OrtObject**)ptr)->Release(ptr)" + * WARNING: There is NO type checking in this function. + * \param ptr Can be NULL. If it's NULL, this function will return zero. + * \return the new reference count. + */ +ORT_API(uint32_t, OrtReleaseObject, _Inout_opt_ void* ptr); + +typedef enum OrtAllocatorType { + OrtDeviceAllocator = 0, + OrtArenaAllocator = 1 +} OrtAllocatorType; + +/** + memory types for allocator, exec provider specific types should be extended in each provider +*/ +typedef enum OrtMemType { + OrtMemTypeCPUInput = -2, // Any CPU memory used by non-CPU execution provider + OrtMemTypeCPUOutput = -1, // CPU accessible memory outputted by non-CPU execution provider, i.e. CUDA_PINNED + OrtMemTypeCPU = OrtMemTypeCPUOutput, // temporary CPU accessible memory allocated by non-CPU execution provider, i.e. CUDA_PINNED + OrtMemTypeDefault = 0, // the default allocator for execution provider +} OrtMemType; + +ORT_API_STATUS(OrtCreateAllocatorInfo, _In_ const char* name1, enum OrtAllocatorType type, int id1, enum OrtMemType mem_type1, _Out_ OrtAllocatorInfo** out); + +/** + * Test if two allocation info are equal + * \return 0, equal. zero, not equal + */ +ORT_API(int, OrtCompareAllocatorInfo, _In_ const OrtAllocatorInfo* info1, _In_ const OrtAllocatorInfo* info2) +ORT_ALL_ARGS_NONNULL; + +/** + * Do not free the returned value + */ +ORT_API(const char*, OrtAllocatorInfoGetName, _In_ OrtAllocatorInfo* ptr); +ORT_API(int, OrtAllocatorInfoGetId, _In_ OrtAllocatorInfo* ptr); +ORT_API(OrtMemType, OrtAllocatorInfoGetMemType, _In_ OrtAllocatorInfo* ptr); +ORT_API(OrtAllocatorType, OrtAllocatorInfoGetType, _In_ OrtAllocatorInfo* ptr); + +ORT_API(void*, OrtAllocatorAlloc, _Inout_ OrtAllocator* ptr, size_t size); +ORT_API(void, OrtAllocatorFree, _Inout_ OrtAllocator* ptr, void* p); +ORT_API(const OrtAllocatorInfo*, OrtAllocatorGetInfo, _In_ const OrtAllocator* ptr); + +// Call OrtReleaseObject to release the returned value +ORT_API_STATUS(OrtCreateDefaultAllocator, _Out_ OrtAllocator** out); + +/** + * \param msg A null-terminated string. Its content will be copied into the newly created OrtStatus + */ +ORT_API(OrtStatus*, OrtCreateStatus, OrtErrorCode code, _In_ const char* msg) +ORT_ALL_ARGS_NONNULL; + +ORT_API(OrtErrorCode, OrtGetErrorCode, _In_ const OrtStatus* status) +ORT_ALL_ARGS_NONNULL; +/** + * \param status must not be NULL + * \return The error message inside the `status`. Don't free the returned value. + */ +ORT_API(const char*, OrtGetErrorMessage, _In_ const OrtStatus* status) +ORT_ALL_ARGS_NONNULL; /** * Deprecated. Please use OrtReleaseObject