diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs b/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs index fd5133534b..8e58d78ced 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/InferenceSession.cs @@ -315,7 +315,7 @@ namespace Microsoft.ML.OnnxRuntime Type dotnetType = null; int width = 0; TensorElementTypeConverter.GetTypeAndWidth(type, out dotnetType, out width); - ulong numDimensions = NativeMethods.OrtGetNumOfDimensions(tensorInfo); + ulong numDimensions = NativeMethods.OrtGetDimensionsCount(tensorInfo); long[] dimensions = new long[(int)numDimensions]; NativeMethods.OrtGetDimensions(tensorInfo, dimensions, numDimensions); int[] intDimensions = new int[(int)numDimensions]; diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs index 68da5be406..504c110e01 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeMethods.cs @@ -302,7 +302,7 @@ namespace Microsoft.ML.OnnxRuntime public static extern TensorElementType OrtGetTensorElementType(IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo); [DllImport(nativeLib, CharSet = charSet)] - public static extern ulong /*TODO: port for size_t */OrtGetNumOfDimensions(IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo); + public static extern ulong /*TODO: port for size_t */OrtGetDimensionsCount(IntPtr /*(const struct OrtTensorTypeAndShapeInfo*)*/ typeAndShapeInfo); [DllImport(nativeLib, CharSet = charSet)] public static extern void OrtGetDimensions( diff --git a/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs b/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs index 10dc1b4701..e737a40273 100644 --- a/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs +++ b/csharp/src/Microsoft.ML.OnnxRuntime/NativeOnnxTensorMemory.cs @@ -41,7 +41,7 @@ namespace Microsoft.ML.OnnxRuntime _elementWidth = width; - ulong dimension = NativeMethods.OrtGetNumOfDimensions(typeAndShape); + ulong dimension = NativeMethods.OrtGetDimensionsCount(typeAndShape); long count = NativeMethods.OrtGetTensorShapeElementCount(typeAndShape); // count can be negative. if (count < 0) { diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/CXX_Api_Sample.cpp b/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/CXX_Api_Sample.cpp index 09897e6f0b..39b9cccb19 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/CXX_Api_Sample.cpp +++ b/csharp/test/Microsoft.ML.OnnxRuntime.EndToEndTests.Capi/CXX_Api_Sample.cpp @@ -37,7 +37,7 @@ int main(int argc, char* argv[]) { //************************************************************************* // print model input layer (node names, types, shape etc.) - Ort::Allocator allocator = Ort::Allocator::Create_Default(); + Ort::Allocator allocator = Ort::Allocator::CreateDefault(); // print number of model input nodes size_t num_input_nodes = session.GetInputCount(); @@ -97,16 +97,16 @@ int main(int argc, char* argv[]) { input_tensor_values[i] = (float)i / (input_tensor_size + 1); // create input tensor object from data values - Ort::AllocatorInfo allocator_info = Ort::AllocatorInfo::Create_Cpu(OrtArenaAllocator, OrtMemTypeDefault); - Ort::Value input_tensors[1] = {Ort::Value::CreateTensor(allocator_info, input_tensor_values.data(), input_tensor_size * sizeof(float), input_node_dims.data(), 4, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT)}; - assert(input_tensors[0].IsTensor()); + Ort::AllocatorInfo allocator_info = Ort::AllocatorInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault); + Ort::Value input_tensor = Ort::Value::CreateTensor(allocator_info, input_tensor_values.data(), input_tensor_size * sizeof(float), input_node_dims.data(), 4, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); + assert(input_tensor.IsTensor()); // score model & input tensor, get back output tensor - Ort::Value output_tensor = session.Run(nullptr, input_node_names.data(), input_tensors, output_node_names.data(), 1); - assert(output_tensor.IsTensor()); + auto output_tensors = session.Run(Ort::RunOptions{nullptr}, input_node_names.data(), &input_tensor, 1, output_node_names.data(), 1); + assert(output_tensors.size() == 1 && output_tensors.front().IsTensor()); // Get pointer to output tensor float values - float* floatarr = output_tensor.GetTensorMutableData(); + float* floatarr = output_tensors.front().GetTensorMutableData(); assert(abs(floatarr[0] - 0.000045) < 1e-6); // score the model, and print scores for first 5 classes diff --git a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs index c57a72deac..008c868547 100644 --- a/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs +++ b/csharp/test/Microsoft.ML.OnnxRuntime.Tests/InferenceTest.cs @@ -687,7 +687,7 @@ namespace Microsoft.ML.OnnxRuntime.Tests "OrtSessionOptionsAppendExecutionProvider_CPU","OrtCreateAllocatorInfo","OrtCreateCpuAllocatorInfo", "OrtCreateDefaultAllocator","OrtAllocatorFree","OrtAllocatorGetInfo", "OrtCreateTensorWithDataAsOrtValue","OrtGetTensorMutableData", "OrtReleaseAllocatorInfo", - "OrtCastTypeInfoToTensorInfo","OrtGetTensorShapeAndType","OrtGetTensorElementType","OrtGetNumOfDimensions", + "OrtCastTypeInfoToTensorInfo","OrtGetTensorShapeAndType","OrtGetTensorElementType","OrtGetDimensionsCount", "OrtGetDimensions","OrtGetTensorShapeElementCount","OrtReleaseValue"}; var hModule = LoadLibrary(module); diff --git a/include/onnxruntime/core/session/onnxruntime_c_api.h b/include/onnxruntime/core/session/onnxruntime_c_api.h index edc46ee61d..df85109480 100644 --- a/include/onnxruntime/core/session/onnxruntime_c_api.h +++ b/include/onnxruntime/core/session/onnxruntime_c_api.h @@ -387,10 +387,10 @@ ORT_API_STATUS(OrtSetTensorElementType, _In_ OrtTensorTypeAndShapeInfo*, enum ON * \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_STATUS(OrtSetDimensions, 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(size_t, OrtGetDimensionsCount, _In_ const OrtTensorTypeAndShapeInfo* info); ORT_API(void, OrtGetDimensions, _In_ const OrtTensorTypeAndShapeInfo* info, _Out_ int64_t* dim_values, size_t dim_values_length); /** @@ -525,7 +525,7 @@ ORT_API_STATUS(OrtGetValueCount, const OrtValue* value, size_t* out); * sequence. 'in' should be an arrary of N OrtValues. * \value_type should be either map or sequence. */ -ORT_API_STATUS(OrtCreateValue, OrtValue** const in, int num_values, enum ONNXType value_type, +ORT_API_STATUS(OrtCreateValue, OrtValue** const in, size_t num_values, enum ONNXType value_type, OrtValue** out); /* diff --git a/include/onnxruntime/core/session/onnxruntime_cxx_api.h b/include/onnxruntime/core/session/onnxruntime_cxx_api.h index dcb84ba024..454850753b 100644 --- a/include/onnxruntime/core/session/onnxruntime_cxx_api.h +++ b/include/onnxruntime/core/session/onnxruntime_cxx_api.h @@ -106,6 +106,9 @@ struct Unowned : T { ~Unowned() { this->p_ = nullptr; } }; +struct Allocator; +struct AllocatorInfo; +struct Env; struct TypeInfo; struct Value; @@ -115,8 +118,8 @@ struct Env : Base { }; struct CustomOpDomain : Base { - CustomOpDomain(nullptr_t) {} - CustomOpDomain(const char* domain); + explicit CustomOpDomain(nullptr_t) {} + explicit CustomOpDomain(const char* domain); void Add(OrtCustomOp* op); }; @@ -135,11 +138,11 @@ struct RunOptions : Base { }; struct SessionOptions : Base { - SessionOptions(nullptr_t) {} + explicit SessionOptions(nullptr_t) {} SessionOptions(); explicit SessionOptions(OrtSessionOptions* p) : Base{p} {} - SessionOptions clone() const; + SessionOptions Clone() const; SessionOptions& SetThreadPoolSize(int session_thread_pool_size); SessionOptions& SetGraphOptimizationLevel(uint32_t graph_optimization_level); @@ -156,12 +159,11 @@ struct SessionOptions : Base { }; struct Session : Base { - Session(nullptr_t) {} - Session(OrtEnv* env, const ORTCHAR_T* model_path, const OrtSessionOptions* options); + explicit Session(nullptr_t) {} + Session(Env& env, const ORTCHAR_T* model_path, const SessionOptions& options); - template - Value Run(OrtRunOptions* run_options, const char* const* input_names, Value (&input)[InputCount], - const char* const* output_names, size_t output_names_len); + std::vector Run(RunOptions& run_options, const char* const* input_names, Value* input_values, size_t input_count, + const char* const* output_names, size_t output_names_count); size_t GetInputCount() const; size_t GetOutputCount() const; @@ -174,7 +176,7 @@ struct Session : Base { }; struct TensorTypeAndShapeInfo : Base { - TensorTypeAndShapeInfo(nullptr_t) {} + explicit TensorTypeAndShapeInfo(nullptr_t) {} explicit TensorTypeAndShapeInfo(OrtTensorTypeAndShapeInfo* p) : Base{p} {} ONNXTensorElementDataType GetElementType() const; @@ -185,20 +187,28 @@ struct TensorTypeAndShapeInfo : Base { }; struct TypeInfo : Base { - TypeInfo(nullptr_t) {} + explicit TypeInfo(nullptr_t) {} explicit TypeInfo(OrtTypeInfo* p) : Base{p} {} Unowned GetTensorTypeAndShapeInfo() const; }; struct Value : Base { - static Value CreateTensor(const OrtAllocatorInfo* info, void* p_data, size_t p_data_len, const int64_t* shape, size_t shape_len, + static Value CreateTensor(const AllocatorInfo& info, void* p_data, size_t p_data_len, const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type); + static Value CreateMap(Value& keys, Value& values); + static Value CreateSequence(std::vector& values); - Value(nullptr_t) {} + explicit Value(nullptr_t) {} explicit Value(OrtValue* p) : Base{p} {} bool IsTensor() const; + size_t GetCount() const; // If a non tensor, returns 2 for map and N for sequence, where N is the number of elements + Value GetValue(int index, OrtAllocator* allocator) const; + + size_t GetStringTensorDataLength() const; + void GetStringTensorContent(void* buffer, size_t buffer_length, size_t* offsets, size_t offsets_count) const; + template T* GetTensorMutableData(); @@ -206,9 +216,9 @@ struct Value : Base { }; struct Allocator : Base { - static Allocator Create_Default(); + static Allocator CreateDefault(); - Allocator(nullptr_t) {} + explicit Allocator(nullptr_t) {} explicit Allocator(OrtAllocator* p) : Base{p} {} void* Alloc(size_t size); @@ -218,15 +228,19 @@ struct Allocator : Base { }; struct AllocatorInfo : Base { - static AllocatorInfo Create_Cpu(OrtAllocatorType type, OrtMemType mem_type1); + static AllocatorInfo CreateCpu(OrtAllocatorType type, OrtMemType mem_type1); + + explicit AllocatorInfo(nullptr_t) {} + AllocatorInfo(const char* name, OrtAllocatorType type, int id, OrtMemType mem_type); - AllocatorInfo(nullptr_t) {} explicit AllocatorInfo(OrtAllocatorInfo* p) : Base{p} {} }; + } // namespace Ort namespace Ort { -inline Allocator Allocator::Create_Default() { + +inline Allocator Allocator::CreateDefault() { OrtAllocator* p; ORT_THROW_ON_ERROR(OrtCreateDefaultAllocator(&p)); return Allocator(p); @@ -244,12 +258,16 @@ inline const OrtAllocatorInfo* Allocator::GetInfo() const { return OrtAllocatorGetInfo(p_); } -inline AllocatorInfo AllocatorInfo::Create_Cpu(OrtAllocatorType type, OrtMemType mem_type) { +inline AllocatorInfo AllocatorInfo::CreateCpu(OrtAllocatorType type, OrtMemType mem_type) { OrtAllocatorInfo* p; ORT_THROW_ON_ERROR(OrtCreateCpuAllocatorInfo(type, mem_type, &p)); return AllocatorInfo(p); } +inline AllocatorInfo::AllocatorInfo(const char* name, OrtAllocatorType type, int id, OrtMemType mem_type) { + ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo(name, type, id, mem_type, &p_)); +} + inline Env::Env(OrtLoggingLevel default_warning_level, _In_ const char* logid) { ORT_THROW_ON_ERROR(OrtCreateEnv(default_warning_level, logid, &p_)); } @@ -262,8 +280,7 @@ inline void CustomOpDomain::Add(OrtCustomOp* op) { ORT_THROW_ON_ERROR(OrtCustomOpDomain_Add(p_, op)); } -inline RunOptions::RunOptions() : Base{ - OrtCreateRunOptions()} {} +inline RunOptions::RunOptions() : Base{OrtCreateRunOptions()} {} inline RunOptions& RunOptions::SetRunLogVerbosityLevel(unsigned int level) { ORT_THROW_ON_ERROR(OrtRunOptionsSetRunLogVerbosityLevel(p_, level)); @@ -291,7 +308,7 @@ inline RunOptions& RunOptions::SetTerminate(bool flag) { inline SessionOptions::SessionOptions() : Base{OrtCreateSessionOptions()} { } -inline SessionOptions SessionOptions::clone() const { +inline SessionOptions SessionOptions::Clone() const { return SessionOptions{OrtCloneSessionOptions(p_)}; } @@ -336,19 +353,17 @@ inline SessionOptions& SessionOptions::Add(OrtCustomOpDomain* custom_op_domain) return *this; } -inline Session::Session(OrtEnv* env, const ORTCHAR_T* model_path, const OrtSessionOptions* options) { +inline Session::Session(Env& env, const ORTCHAR_T* model_path, const SessionOptions& options) { ORT_THROW_ON_ERROR(OrtCreateSession(env, model_path, options, &p_)); } -template -inline Value Session::Run(OrtRunOptions* run_options, const char* const* input_names, Value (&inputs)[InputCount], - const char* const* output_names, size_t output_names_len) { - std::array internal_inputs; - std::copy_n(inputs, InputCount, internal_inputs.data()); - - OrtValue* out{}; - ORT_THROW_ON_ERROR(OrtRun(p_, run_options, input_names, internal_inputs.data(), InputCount, output_names, output_names_len, &out)); - return Value{out}; +inline std::vector Session::Run(RunOptions& run_options, const char* const* input_names, Value* input_values, size_t input_count, + const char* const* output_names, size_t output_names_count) { + std::vector ort_input_values(input_values, input_values + input_count); + std::vector ort_out(output_names_count); + ORT_THROW_ON_ERROR(OrtRun(p_, run_options, input_names, ort_input_values.data(), ort_input_values.size(), output_names, output_names_count, ort_out.data())); + std::vector out(ort_out.begin(), ort_out.end()); + return out; } inline size_t Session::GetInputCount() const { @@ -392,7 +407,7 @@ inline ONNXTensorElementDataType TensorTypeAndShapeInfo::GetElementType() const } inline size_t TensorTypeAndShapeInfo::GetDimensionsCount() const { - return OrtGetNumOfDimensions(p_); + return OrtGetDimensionsCount(p_); } inline void TensorTypeAndShapeInfo::GetDimensions(int64_t* values, size_t values_count) const { @@ -400,38 +415,73 @@ inline void TensorTypeAndShapeInfo::GetDimensions(int64_t* values, size_t values } inline std::vector TensorTypeAndShapeInfo::GetShape() const { - std::vector output; - output.resize(GetDimensionsCount()); - GetDimensions(output.data(), output.size()); - return output; + std::vector out(GetDimensionsCount(), 0); + GetDimensions(out.data(), out.size()); + return out; } inline Unowned TypeInfo::GetTensorTypeAndShapeInfo() const { return Unowned{const_cast(OrtCastTypeInfoToTensorInfo(p_))}; } -inline Value Value::CreateTensor(const OrtAllocatorInfo* info, void* p_data, size_t p_data_len, const int64_t* shape, size_t shape_len, +inline Value Value::CreateTensor(const AllocatorInfo& info, void* p_data, size_t p_data_len, const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type) { OrtValue* out; ORT_THROW_ON_ERROR(OrtCreateTensorWithDataAsOrtValue(info, p_data, p_data_len, shape, shape_len, type, &out)); - return Value(out); + return Value{out}; } +inline Value Value::CreateMap(Value& keys, Value& values) { + OrtValue* out; + OrtValue* inputs[2] = {keys, values}; + ORT_THROW_ON_ERROR(OrtCreateValue(inputs, 2, ONNX_TYPE_MAP, &out)); + return Value{out}; +} + +inline Value Value::CreateSequence(std::vector& values) { + OrtValue* out; + std::vector values_ort{values.data(), values.data() + values.size()}; + ORT_THROW_ON_ERROR(OrtCreateValue(values_ort.data(), values_ort.size(), ONNX_TYPE_SEQUENCE, &out)); + return Value{out}; +} // namespace Ort + inline bool Value::IsTensor() const { return OrtIsTensor(p_) != 0; } +inline size_t Value::GetCount() const { + size_t out; + ORT_THROW_ON_ERROR(OrtGetValueCount(p_, &out)); + return out; +} + +inline Value Value::GetValue(int index, OrtAllocator* allocator) const { + OrtValue* out; + ORT_THROW_ON_ERROR(OrtGetValue(p_, index, allocator, &out)); + return Value{out}; +} + +inline size_t Value::GetStringTensorDataLength() const { + size_t out; + ORT_THROW_ON_ERROR(OrtGetStringTensorDataLength(p_, &out)); + return out; +} + +inline void Value::GetStringTensorContent(void* buffer, size_t buffer_length, size_t* offsets, size_t offsets_count) const { + ORT_THROW_ON_ERROR(OrtGetStringTensorContent(p_, buffer, buffer_length, offsets, offsets_count)); +} + template T* Value::GetTensorMutableData() { - T* output; - ORT_THROW_ON_ERROR(OrtGetTensorMutableData(p_, (void**)&output)); - return output; + T* out; + ORT_THROW_ON_ERROR(OrtGetTensorMutableData(p_, (void**)&out)); + return out; } inline TensorTypeAndShapeInfo Value::GetTensorTypeAndShapeInfo() const { - OrtTensorTypeAndShapeInfo* output; - ORT_THROW_ON_ERROR(OrtGetTensorShapeAndType(p_, &output)); - return TensorTypeAndShapeInfo{output}; + OrtTensorTypeAndShapeInfo* out; + ORT_THROW_ON_ERROR(OrtGetTensorShapeAndType(p_, &out)); + return TensorTypeAndShapeInfo{out}; } } // namespace Ort @@ -500,7 +550,7 @@ inline OrtValue* OrtCreateTensorWithDataAsOrtValue(_In_ const OrtAllocatorInfo* } inline std::vector GetTensorShape(const OrtTensorTypeAndShapeInfo* info) { - size_t dims = OrtGetNumOfDimensions(info); + size_t dims = OrtGetDimensionsCount(info); std::vector ret(dims); OrtGetDimensions(info, ret.data(), ret.size()); return ret; diff --git a/onnxruntime/core/framework/tensor_type_and_shape.cc b/onnxruntime/core/framework/tensor_type_and_shape.cc index ea17763bac..d3fddd4878 100644 --- a/onnxruntime/core/framework/tensor_type_and_shape.cc +++ b/onnxruntime/core/framework/tensor_type_and_shape.cc @@ -38,7 +38,7 @@ ORT_API_STATUS_IMPL(OrtSetTensorElementType, _In_ OrtTensorTypeAndShapeInfo* thi API_IMPL_END } -ORT_API_STATUS_IMPL(OrtSetDims, OrtTensorTypeAndShapeInfo* this_ptr, _In_ const int64_t* dim_values, size_t dim_count) { +ORT_API_STATUS_IMPL(OrtSetDimensions, OrtTensorTypeAndShapeInfo* this_ptr, _In_ const int64_t* dim_values, size_t dim_count) { API_IMPL_BEGIN this_ptr->shape = onnxruntime::TensorShape(dim_values, dim_count); return nullptr; @@ -49,7 +49,7 @@ ORT_API(enum ONNXTensorElementDataType, OrtGetTensorElementType, _In_ const stru return info->type; } -ORT_API(size_t, OrtGetNumOfDimensions, _In_ const struct OrtTensorTypeAndShapeInfo* info) { +ORT_API(size_t, OrtGetDimensionsCount, _In_ const struct OrtTensorTypeAndShapeInfo* info) { return info->shape.NumDimensions(); } @@ -113,7 +113,7 @@ OrtStatus* GetTensorShapeAndType(const onnxruntime::TensorShape* shape, return status; } if (shape != nullptr) { - status = OrtSetDims(ret, shape->GetDims().data(), shape->GetDims().size()); + status = OrtSetDimensions(ret, shape->GetDims().data(), shape->GetDims().size()); if (status != nullptr) { OrtReleaseTensorTypeAndShapeInfo(ret); return status; diff --git a/onnxruntime/core/providers/cpu/symbols.txt b/onnxruntime/core/providers/cpu/symbols.txt index 54cce323f3..97bca394be 100644 --- a/onnxruntime/core/providers/cpu/symbols.txt +++ b/onnxruntime/core/providers/cpu/symbols.txt @@ -34,9 +34,9 @@ OrtEnableProfiling OrtEnableSequentialExecution OrtFillStringTensor OrtGetDimensions +OrtGetDimensionsCount OrtGetErrorCode OrtGetErrorMessage -OrtGetNumOfDimensions OrtGetStringTensorContent OrtGetStringTensorDataLength OrtGetTensorElementType @@ -75,7 +75,7 @@ OrtSessionGetOutputCount OrtSessionGetOutputName OrtSessionGetOutputTypeInfo OrtSessionOptionsAppendExecutionProvider_CPU -OrtSetDims +OrtSetDimensions OrtSetSessionLogId OrtSetSessionLogVerbosityLevel OrtSetSessionGraphOptimizationLevel diff --git a/onnxruntime/core/session/custom_ops.cc b/onnxruntime/core/session/custom_ops.cc index 63d737652c..d2c021ca9e 100644 --- a/onnxruntime/core/session/custom_ops.cc +++ b/onnxruntime/core/session/custom_ops.cc @@ -45,9 +45,9 @@ constexpr OrtCustomOpApi g_custom_op_api = { &OrtGetTensorShapeAndType, &OrtGetTensorShapeElementCount, - &OrtGetNumOfDimensions, + &OrtGetDimensionsCount, &OrtGetDimensions, - &OrtSetDims, + &OrtSetDimensions, &OrtGetTensorMutableData, diff --git a/onnxruntime/core/session/onnxruntime_c_api.cc b/onnxruntime/core/session/onnxruntime_c_api.cc index 2b226d4274..b624359b63 100644 --- a/onnxruntime/core/session/onnxruntime_c_api.cc +++ b/onnxruntime/core/session/onnxruntime_c_api.cc @@ -928,7 +928,7 @@ ORT_API_STATUS_IMPL(OrtGetValue, const OrtValue* value, int index, OrtAllocator* /////////////////// // OrtCreateValue template -static OrtStatus* OrtCreateValueImplSeqHelperMap(OrtValue** const in, int num_values, OrtValue** out) { +static OrtStatus* OrtCreateValueImplSeqHelperMap(OrtValue** const in, size_t num_values, OrtValue** out) { using SeqType = std::vector; auto vec_ptr = std::make_unique(); vec_ptr->reserve(num_values); @@ -946,7 +946,7 @@ static OrtStatus* OrtCreateValueImplSeqHelperMap(OrtValue** const in, int num_va } template -static OrtStatus* OrtCreateValueImplSeqHelper(OrtValue** const in, int num_values, OrtValue** out) { +static OrtStatus* OrtCreateValueImplSeqHelper(OrtValue** const in, size_t num_values, OrtValue** out) { using SeqType = std::vector; auto vec_ptr = std::make_unique(); vec_ptr->reserve(num_values); @@ -967,7 +967,7 @@ static OrtStatus* OrtCreateValueImplSeqHelper(OrtValue** const in, int num_value return nullptr; } -static OrtStatus* OrtCreateValueImplSeq(OrtValue** const in, int num_values, OrtValue** out) { +static OrtStatus* OrtCreateValueImplSeq(OrtValue** const in, size_t num_values, OrtValue** out) { // We only support limited sequence types. For the sake of simplicity the type of the first // OrtValue* in OrtValue** will determine the type of the vector used to create the output OrtValue // this type should be either a tensor of limited types or map of limited types @@ -1057,7 +1057,7 @@ static OrtStatus* OrtCreateValueImplMapHelper(const Tensor& key_tensor, const Te } } -static OrtStatus* OrtCreateValueImplMap(OrtValue** const in, int num_values, OrtValue** out) { +static OrtStatus* OrtCreateValueImplMap(OrtValue** const in, size_t num_values, OrtValue** out) { if (num_values != NUM_MAP_INDICES) { return OrtCreateStatus(ORT_FAIL, "For map type num_values MUST be 2"); } @@ -1090,7 +1090,7 @@ static OrtStatus* OrtCreateValueImplMap(OrtValue** const in, int num_values, Ort return OrtCreateStatus(ORT_FAIL, "Key type is not supported yet."); } -static OrtStatus* OrtCreateValueImpl(OrtValue** const in, int num_values, enum ONNXType value_type, +static OrtStatus* OrtCreateValueImpl(OrtValue** const in, size_t num_values, enum ONNXType value_type, OrtValue** out) { if (num_values <= 0) { return OrtCreateStatus(ORT_FAIL, "Number of values should be at least 1."); @@ -1104,7 +1104,7 @@ static OrtStatus* OrtCreateValueImpl(OrtValue** const in, int num_values, enum O return OrtCreateStatus(ORT_FAIL, "Input is not of type sequence or map."); } -ORT_API_STATUS_IMPL(OrtCreateValue, OrtValue** const in, int num_values, enum ONNXType value_type, +ORT_API_STATUS_IMPL(OrtCreateValue, OrtValue** const in, size_t num_values, enum ONNXType value_type, OrtValue** out) { API_IMPL_BEGIN return OrtCreateValueImpl(in, num_values, value_type, out); diff --git a/onnxruntime/test/onnx/runner.cc b/onnxruntime/test/onnx/runner.cc index 60801f3703..084b19edea 100644 --- a/onnxruntime/test/onnx/runner.cc +++ b/onnxruntime/test/onnx/runner.cc @@ -465,7 +465,7 @@ void RunSingleTestCase(ITestCase* info, Ort::Env& env, const Ort::SessionOptions try { DataRunner* r = nullptr; std::string node_name = info->GetNodeName(); - auto sf2 = sf.clone(); + auto sf2 = sf.Clone(); sf2.SetLogId(info->GetTestCaseName().c_str()); Ort::Session session_object{env, info->GetModelUrl(), sf2}; LOGF_DEFAULT(INFO, "testing %s\n", info->GetTestCaseName().c_str()); diff --git a/onnxruntime/test/perftest/tf_test_session.h b/onnxruntime/test/perftest/tf_test_session.h index 603568353a..73b8508f7d 100644 --- a/onnxruntime/test/perftest/tf_test_session.h +++ b/onnxruntime/test/perftest/tf_test_session.h @@ -116,7 +116,7 @@ class TensorflowTestSession : public TestSession { ORT_THROW_ON_ERROR(OrtGetTensorShapeAndType(value, &shape)); size_t buffer_length = 0; std::vector dims; - size_t dim_count = OrtGetNumOfDimensions(shape); + size_t dim_count = OrtGetDimensionsCount(shape); dims.resize(dim_count); OrtGetDimensions(shape, dims.data(), dim_count); int64_t ele_count = OrtGetTensorShapeElementCount(shape); @@ -224,4 +224,4 @@ class TensorflowTestSession : public TestSession { }; } // namespace perftest -} // namespace onnxruntime \ No newline at end of file +} // namespace onnxruntime diff --git a/onnxruntime/test/shared_lib/fns_candy_style_transfer.c b/onnxruntime/test/shared_lib/fns_candy_style_transfer.c index a058268162..da5ffd16a8 100644 --- a/onnxruntime/test/shared_lib/fns_candy_style_transfer.c +++ b/onnxruntime/test/shared_lib/fns_candy_style_transfer.c @@ -97,7 +97,7 @@ static int read_png_file(const char* input_file, size_t* height, size_t* width, static int write_tensor_to_png_file(OrtValue* tensor, const char* output_file) { struct OrtTensorTypeAndShapeInfo* shape_info; ORT_ABORT_ON_ERROR(OrtGetTensorShapeAndType(tensor, &shape_info)); - size_t dim_count = OrtGetNumOfDimensions(shape_info); + size_t dim_count = OrtGetDimensionsCount(shape_info); if (dim_count != 4) { printf("output tensor must have 4 dimensions"); return -1; diff --git a/onnxruntime/test/shared_lib/test_allocator.cc b/onnxruntime/test/shared_lib/test_allocator.cc index 4c7ceb006c..28dd873ef9 100644 --- a/onnxruntime/test/shared_lib/test_allocator.cc +++ b/onnxruntime/test/shared_lib/test_allocator.cc @@ -18,7 +18,7 @@ TEST_F(CApiTest, allocation_info) { } TEST_F(CApiTest, DefaultAllocator) { - Ort::Allocator default_allocator = Ort::Allocator::Create_Default(); + Ort::Allocator default_allocator = Ort::Allocator::CreateDefault(); char* p = (char*)default_allocator.Alloc(100); ASSERT_NE(p, nullptr); memset(p, 0, 100); diff --git a/onnxruntime/test/shared_lib/test_inference.cc b/onnxruntime/test/shared_lib/test_inference.cc index abc9e006f1..9f70f47170 100644 --- a/onnxruntime/test/shared_lib/test_inference.cc +++ b/onnxruntime/test/shared_lib/test_inference.cc @@ -47,7 +47,7 @@ void RunSession(OrtAllocator* env, OrtSession* session_object, ORT_THROW_ON_ERROR(OrtGetTensorShapeAndType(output_tensor, &shape_info_ptr)); shape_info.reset(shape_info_ptr); } - size_t rtensor_dims = OrtGetNumOfDimensions(shape_info.get()); + size_t rtensor_dims = OrtGetDimensionsCount(shape_info.get()); std::vector shape_array(rtensor_dims); OrtGetDimensions(shape_info.get(), shape_array.data(), shape_array.size()); ASSERT_EQ(shape_array, dims_y); @@ -247,11 +247,10 @@ TEST_F(CApiTest, custom_op_handler) { std::vector expected_values_y = {2.0f, 4.0f, 6.0f, 8.0f, 10.0f, 12.0f}; MyCustomOp custom_op; - OrtCustomOpDomain* custom_op_domain = OrtCreateCustomOpDomain(""); - ORT_THROW_ON_ERROR(OrtCustomOpDomain_Add(custom_op_domain, &custom_op)); + Ort::CustomOpDomain custom_op_domain(""); + custom_op_domain.Add(&custom_op); TestInference(env, CUSTOM_OP_MODEL_URI, inputs, "Y", expected_dims_y, expected_values_y, 0, custom_op_domain); - OrtReleaseCustomOpDomain(custom_op_domain); } #ifdef ORT_RUN_EXTERNAL_ONNX_TESTS @@ -308,7 +307,7 @@ TEST_F(CApiTest, create_tensor_with_data) { ORT_THROW_ON_ERROR(OrtGetTypeInfo(tensor.get(), &type_info)); const struct OrtTensorTypeAndShapeInfo* tensor_info = OrtCastTypeInfoToTensorInfo(type_info); ASSERT_NE(tensor_info, nullptr); - ASSERT_EQ(1, OrtGetNumOfDimensions(tensor_info)); + ASSERT_EQ(1, OrtGetDimensionsCount(tensor_info)); OrtReleaseTypeInfo(type_info); } diff --git a/onnxruntime/test/shared_lib/test_io_types.cc b/onnxruntime/test/shared_lib/test_io_types.cc index f67325f34f..37651cd17c 100644 --- a/onnxruntime/test/shared_lib/test_io_types.cc +++ b/onnxruntime/test/shared_lib/test_io_types.cc @@ -33,7 +33,7 @@ static void TestModelInfo(const OrtSession* inference_session, bool is_input, co enum ONNXTensorElementDataType ele_type = OrtGetTensorElementType(p); ASSERT_EQ(ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT, ele_type); - ASSERT_EQ(dims.size(), OrtGetNumOfDimensions(p)); + ASSERT_EQ(dims.size(), OrtGetDimensionsCount(p)); std::vector real_dims(dims.size()); OrtGetDimensions(p, real_dims.data(), real_dims.size()); ASSERT_EQ(real_dims, dims); diff --git a/onnxruntime/test/shared_lib/test_nontensor_types.cc b/onnxruntime/test/shared_lib/test_nontensor_types.cc index ca3cdc5cee..fd9e8c0357 100644 --- a/onnxruntime/test/shared_lib/test_nontensor_types.cc +++ b/onnxruntime/test/shared_lib/test_nontensor_types.cc @@ -28,109 +28,59 @@ struct RelAllocations { TEST_F(CApiTest, CreateGetVectorOfMapsInt64Float) { // support zipmap output type seq(map(int64, float)) // Creation - std::unique_ptr default_allocator(std::make_unique()); - OrtAllocatorInfo* info; - ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault, &info)); - std::unique_ptr rel_info(info, OrtReleaseAllocatorInfo); - - RelAllocations rel(&OrtReleaseValue); - RelAllocations rels(&OrtReleaseStatus); + auto default_allocator = std::make_unique(); + Ort::AllocatorInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault); const int N = 3; const int NUM_KV_PAIRS = 4; - std::vector in(N); + std::vector in; std::vector keys{3, 1, 2, 0}; std::vector dims = {4}; std::vector values{3.0f, 1.0f, 2.f, 0.f}; for (int i = 0; i < N; ++i) { // create key tensor - OrtValue* keys_tensor = OrtCreateTensorWithDataAsOrtValue(info, keys.data(), keys.size() * sizeof(int64_t), - dims, ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64); - ASSERT_NE(keys_tensor, nullptr); - rel.add(keys_tensor); - + Ort::Value keys_tensor = Ort::Value::CreateTensor(info, keys.data(), keys.size() * sizeof(int64_t), + dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64); // create value tensor - OrtValue* values_tensor = OrtCreateTensorWithDataAsOrtValue(info, values.data(), values.size() * sizeof(float), - dims, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); - ASSERT_NE(values_tensor, nullptr); - rel.add(values_tensor); - + Ort::Value values_tensor = Ort::Value::CreateTensor(info, values.data(), values.size() * sizeof(float), + dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); // create map ort value - std::vector map_in{keys_tensor, values_tensor}; - OrtValue* map_ort = nullptr; - OrtStatus* stx = OrtCreateValue(map_in.data(), 2, ONNX_TYPE_MAP, &map_ort); - rels.add(stx); - rel.add(map_ort); - ASSERT_EQ(stx, nullptr); - ASSERT_NE(map_ort, nullptr); - - in[i] = map_ort; + in.emplace_back(Ort::Value::CreateMap(keys_tensor, values_tensor)); } // repeat above 3 steps N times and store the result in an OrtValue array // create sequence ort value - OrtValue* seq_ort = nullptr; - OrtStatus* sty = OrtCreateValue(in.data(), N, ONNX_TYPE_SEQUENCE, &seq_ort); - rels.add(sty); - rel.add(seq_ort); - ASSERT_EQ(sty, nullptr); - ASSERT_NE(seq_ort, nullptr); + Ort::Value seq_ort = Ort::Value::CreateSequence(in); // Get count - size_t num_values = 0; - OrtStatus* st2 = OrtGetValueCount(seq_ort, &num_values); - rels.add(st2); - ASSERT_EQ(st2, nullptr); + size_t num_values = seq_ort.GetCount(); ASSERT_EQ(num_values, N); // test negative case - OrtValue* tmp = nullptr; - OrtStatus* st_temp = OrtGetValue(seq_ort, 999, default_allocator.get(), &tmp); - rels.add(st_temp); - rel.add(tmp); - ASSERT_NE(st_temp, nullptr); + bool failed = false; + try { + auto temp = seq_ort.GetValue(999, default_allocator.get()); + } catch (const Ort::Exception& e) { + failed = e.GetOrtErrorCode() == ORT_RUNTIME_EXCEPTION; + } + ASSERT_EQ(failed, true); // Fetch for (int idx = 0; idx < N; ++idx) { - OrtValue* map_out = nullptr; - OrtStatus* st = OrtGetValue(seq_ort, idx, default_allocator.get(), &map_out); - rel.add(map_out); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(map_out, nullptr); + Ort::Value map_out = seq_ort.GetValue(idx, default_allocator.get()); // fetch the map // first fetch the keys - OrtValue* keys_ort = nullptr; - st = OrtGetValue(map_out, 0, default_allocator.get(), &keys_ort); - rel.add(keys_ort); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(keys_ort, nullptr); + Ort::Value keys_ort = map_out.GetValue(0, default_allocator.get()); - std::unique_ptr keys_ret_u; - int64_t* keys_ret = keys_ret_u.get(); - st = OrtGetTensorMutableData(keys_ort, reinterpret_cast(&keys_ret)); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(keys_ret, nullptr); + int64_t* keys_ret = keys_ort.GetTensorMutableData(); ASSERT_EQ(std::set(keys_ret, keys_ret + NUM_KV_PAIRS), std::set(std::begin(keys), std::end(keys))); // second fetch the values - OrtValue* values_ort = nullptr; - st = OrtGetValue(map_out, 1, default_allocator.get(), &values_ort); - rel.add(values_ort); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(values_ort, nullptr); + Ort::Value values_ort = map_out.GetValue(1, default_allocator.get()); - std::unique_ptr values_ret_u; - float* values_ret = values_ret_u.get(); - st = OrtGetTensorMutableData(values_ort, reinterpret_cast(&values_ret)); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(values_ret, nullptr); + float* values_ret = values_ort.GetTensorMutableData(); ASSERT_EQ(std::set(values_ret, values_ret + NUM_KV_PAIRS), std::set(std::begin(values), std::end(values))); } @@ -138,89 +88,50 @@ TEST_F(CApiTest, CreateGetVectorOfMapsInt64Float) { // support zipmap output ty TEST_F(CApiTest, CreateGetVectorOfMapsStringFloat) { // support zipmap output type seq(map(string, float)) // Creation - std::unique_ptr default_allocator(std::make_unique()); - OrtAllocatorInfo* info; - ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault, &info)); - std::unique_ptr rel_info(info, OrtReleaseAllocatorInfo); - - RelAllocations rel(&OrtReleaseValue); - RelAllocations rels(&OrtReleaseStatus); + auto default_allocator = std::make_unique(); + Ort::AllocatorInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault); const int N = 3; const int64_t NUM_KV_PAIRS = 4; - std::vector in(N); + std::vector in; const char* keys_arr[NUM_KV_PAIRS] = {"abc", "def", "ghi", "jkl"}; std::vector keys{keys_arr, keys_arr + NUM_KV_PAIRS}; std::vector dims = {NUM_KV_PAIRS}; std::vector values{3.0f, 1.0f, 2.f, 0.f}; for (int i = 0; i < N; ++i) { // create key tensor - OrtValue* keys_tensor = OrtCreateTensorWithDataAsOrtValue(info, keys.data(), keys.size() * sizeof(std::string), - dims, ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING); - ASSERT_NE(keys_tensor, nullptr); - rel.add(keys_tensor); - + Ort::Value keys_tensor = Ort::Value::CreateTensor(info, keys.data(), keys.size() * sizeof(std::string), + dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_STRING); // create value tensor - OrtValue* values_tensor = OrtCreateTensorWithDataAsOrtValue(info, values.data(), values.size() * sizeof(float), - dims, ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); - ASSERT_NE(values_tensor, nullptr); - rel.add(values_tensor); + Ort::Value values_tensor = Ort::Value::CreateTensor(info, values.data(), values.size() * sizeof(float), + dims.data(), dims.size(), ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT); // create map ort value - std::vector map_in{keys_tensor, values_tensor}; - OrtValue* map_ort = nullptr; - OrtStatus* stx = OrtCreateValue(map_in.data(), 2, ONNX_TYPE_MAP, &map_ort); - rels.add(stx); - rel.add(map_ort); - ASSERT_EQ(stx, nullptr); - ASSERT_NE(map_ort, nullptr); - - in[i] = map_ort; + in.emplace_back(Ort::Value::CreateMap(keys_tensor, values_tensor)); } // repeat above 3 steps N times and store the result in an OrtValue array // create sequence ort value - OrtValue* seq_ort = nullptr; - OrtStatus* sty = OrtCreateValue(in.data(), N, ONNX_TYPE_SEQUENCE, &seq_ort); - rels.add(sty); - rel.add(seq_ort); - ASSERT_EQ(sty, nullptr); - ASSERT_NE(seq_ort, nullptr); + Ort::Value seq_ort = Ort::Value::CreateSequence(in); // Get count - size_t num_values; - OrtStatus* st2 = OrtGetValueCount(seq_ort, &num_values); - rels.add(st2); - ASSERT_EQ(st2, nullptr); + size_t num_values = seq_ort.GetCount(); ASSERT_EQ(num_values, N); // Fetch for (int idx = 0; idx < N; ++idx) { - OrtValue* map_out = nullptr; - OrtStatus* st = OrtGetValue(seq_ort, idx, default_allocator.get(), &map_out); - rel.add(map_out); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(map_out, nullptr); + Ort::Value map_out = seq_ort.GetValue(idx, default_allocator.get()); // fetch the map // first fetch the keys - OrtValue* keys_ort = nullptr; - st = OrtGetValue(map_out, 0, default_allocator.get(), &keys_ort); - rel.add(keys_ort); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(keys_ort, nullptr); + Ort::Value keys_ort = map_out.GetValue(0, default_allocator.get()); - size_t data_len; - st = OrtGetStringTensorDataLength(keys_ort, &data_len); - rels.add(st); - ASSERT_EQ(st, nullptr); + size_t data_len = keys_ort.GetStringTensorDataLength(); std::string result(data_len, '\0'); std::vector offsets(NUM_KV_PAIRS); - st = OrtGetStringTensorContent(keys_ort, (void*)result.data(), data_len, offsets.data(), offsets.size()); - rels.add(st); + keys_ort.GetStringTensorContent((void*)result.data(), data_len, offsets.data(), offsets.size()); + const char* s = result.data(); std::set keys_ret; for (size_t i = 0; i < offsets.size(); ++i) { @@ -232,19 +143,9 @@ TEST_F(CApiTest, CreateGetVectorOfMapsStringFloat) { // support zipmap output t ASSERT_EQ(keys_ret, std::set(std::begin(keys), std::end(keys))); // second fetch the values - OrtValue* values_ort = nullptr; - st = OrtGetValue(map_out, 1, default_allocator.get(), &values_ort); - rel.add(values_ort); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(values_ort, nullptr); + Ort::Value values_ort = map_out.GetValue(1, default_allocator.get()); - std::unique_ptr values_ret_u; - float* values_ret = values_ret_u.get(); - st = OrtGetTensorMutableData(values_ort, reinterpret_cast(&values_ret)); - rels.add(st); - ASSERT_EQ(st, nullptr); - ASSERT_NE(values_ret, nullptr); + float* values_ret = values_ort.GetTensorMutableData(); ASSERT_EQ(std::set(values_ret, values_ret + NUM_KV_PAIRS), std::set(std::begin(values), std::end(values))); }