More C++ API improvements and conversions (#998)

* More C++ API improvements and conversions
* Mark more constructors as explicit
* Fix CSharp function name changes
* Change more test cases to use C++ API
This commit is contained in:
Ryan Hill 2019-05-13 13:56:54 -07:00 committed by GitHub
parent c69dff7928
commit 3408494407
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 172 additions and 222 deletions

View file

@ -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];

View file

@ -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(

View file

@ -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)
{

View file

@ -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>();
float* floatarr = output_tensors.front().GetTensorMutableData<float>();
assert(abs(floatarr[0] - 0.000045) < 1e-6);
// score the model, and print scores for first 5 classes

View file

@ -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);

View file

@ -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);
/*

View file

@ -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<OrtEnv> {
};
struct CustomOpDomain : Base<OrtCustomOpDomain> {
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<OrtRunOptions> {
};
struct SessionOptions : Base<OrtSessionOptions> {
SessionOptions(nullptr_t) {}
explicit SessionOptions(nullptr_t) {}
SessionOptions();
explicit SessionOptions(OrtSessionOptions* p) : Base<OrtSessionOptions>{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<OrtSessionOptions> {
};
struct Session : Base<OrtSession> {
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 <unsigned InputCount>
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<Value> 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<OrtSession> {
};
struct TensorTypeAndShapeInfo : Base<OrtTensorTypeAndShapeInfo> {
TensorTypeAndShapeInfo(nullptr_t) {}
explicit TensorTypeAndShapeInfo(nullptr_t) {}
explicit TensorTypeAndShapeInfo(OrtTensorTypeAndShapeInfo* p) : Base<OrtTensorTypeAndShapeInfo>{p} {}
ONNXTensorElementDataType GetElementType() const;
@ -185,20 +187,28 @@ struct TensorTypeAndShapeInfo : Base<OrtTensorTypeAndShapeInfo> {
};
struct TypeInfo : Base<OrtTypeInfo> {
TypeInfo(nullptr_t) {}
explicit TypeInfo(nullptr_t) {}
explicit TypeInfo(OrtTypeInfo* p) : Base<OrtTypeInfo>{p} {}
Unowned<TensorTypeAndShapeInfo> GetTensorTypeAndShapeInfo() const;
};
struct Value : Base<OrtValue> {
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<Value>& values);
Value(nullptr_t) {}
explicit Value(nullptr_t) {}
explicit Value(OrtValue* p) : Base<OrtValue>{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 <typename T>
T* GetTensorMutableData();
@ -206,9 +216,9 @@ struct Value : Base<OrtValue> {
};
struct Allocator : Base<OrtAllocator> {
static Allocator Create_Default();
static Allocator CreateDefault();
Allocator(nullptr_t) {}
explicit Allocator(nullptr_t) {}
explicit Allocator(OrtAllocator* p) : Base<OrtAllocator>{p} {}
void* Alloc(size_t size);
@ -218,15 +228,19 @@ struct Allocator : Base<OrtAllocator> {
};
struct AllocatorInfo : Base<OrtAllocatorInfo> {
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<OrtAllocatorInfo>{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<OrtRunOptions>{
OrtCreateRunOptions()} {}
inline RunOptions::RunOptions() : Base<OrtRunOptions>{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<OrtSessionOptions>{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 <unsigned InputCount>
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<OrtValue*, InputCount> 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<Value> 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<OrtValue*> ort_input_values(input_values, input_values + input_count);
std::vector<OrtValue*> 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<Value> 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<int64_t> TensorTypeAndShapeInfo::GetShape() const {
std::vector<int64_t> output;
output.resize(GetDimensionsCount());
GetDimensions(output.data(), output.size());
return output;
std::vector<int64_t> out(GetDimensionsCount(), 0);
GetDimensions(out.data(), out.size());
return out;
}
inline Unowned<TensorTypeAndShapeInfo> TypeInfo::GetTensorTypeAndShapeInfo() const {
return Unowned<TensorTypeAndShapeInfo>{const_cast<OrtTensorTypeAndShapeInfo*>(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<Value>& values) {
OrtValue* out;
std::vector<OrtValue*> 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 <typename T>
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<int64_t> GetTensorShape(const OrtTensorTypeAndShapeInfo* info) {
size_t dims = OrtGetNumOfDimensions(info);
size_t dims = OrtGetDimensionsCount(info);
std::vector<int64_t> ret(dims);
OrtGetDimensions(info, ret.data(), ret.size());
return ret;

View file

@ -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;

View file

@ -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

View file

@ -45,9 +45,9 @@ constexpr OrtCustomOpApi g_custom_op_api = {
&OrtGetTensorShapeAndType,
&OrtGetTensorShapeElementCount,
&OrtGetNumOfDimensions,
&OrtGetDimensionsCount,
&OrtGetDimensions,
&OrtSetDims,
&OrtSetDimensions,
&OrtGetTensorMutableData,

View file

@ -928,7 +928,7 @@ ORT_API_STATUS_IMPL(OrtGetValue, const OrtValue* value, int index, OrtAllocator*
///////////////////
// OrtCreateValue
template <typename T>
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<T>;
auto vec_ptr = std::make_unique<SeqType>();
vec_ptr->reserve(num_values);
@ -946,7 +946,7 @@ static OrtStatus* OrtCreateValueImplSeqHelperMap(OrtValue** const in, int num_va
}
template <typename T>
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<T>;
auto vec_ptr = std::make_unique<SeqType>();
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);

View file

@ -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());

View file

@ -116,7 +116,7 @@ class TensorflowTestSession : public TestSession {
ORT_THROW_ON_ERROR(OrtGetTensorShapeAndType(value, &shape));
size_t buffer_length = 0;
std::vector<int64_t> 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
} // namespace onnxruntime

View file

@ -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;

View file

@ -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);

View file

@ -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<int64_t> 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<float> 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<PATH_TYPE>(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);
}

View file

@ -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<int64_t> real_dims(dims.size());
OrtGetDimensions(p, real_dims.data(), real_dims.size());
ASSERT_EQ(real_dims, dims);

View file

@ -28,109 +28,59 @@ struct RelAllocations {
TEST_F(CApiTest, CreateGetVectorOfMapsInt64Float) { // support zipmap output type seq(map(int64, float))
// Creation
std::unique_ptr<MockedOrtAllocator> default_allocator(std::make_unique<MockedOrtAllocator>());
OrtAllocatorInfo* info;
ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault, &info));
std::unique_ptr<OrtAllocatorInfo, decltype(&OrtReleaseAllocatorInfo)> rel_info(info, OrtReleaseAllocatorInfo);
RelAllocations<OrtValue> rel(&OrtReleaseValue);
RelAllocations<OrtStatus> rels(&OrtReleaseStatus);
auto default_allocator = std::make_unique<MockedOrtAllocator>();
Ort::AllocatorInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
const int N = 3;
const int NUM_KV_PAIRS = 4;
std::vector<OrtValue*> in(N);
std::vector<Ort::Value> in;
std::vector<int64_t> keys{3, 1, 2, 0};
std::vector<int64_t> dims = {4};
std::vector<float> 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<OrtValue*> 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<int64_t> keys_ret_u;
int64_t* keys_ret = keys_ret_u.get();
st = OrtGetTensorMutableData(keys_ort, reinterpret_cast<void**>(&keys_ret));
rels.add(st);
ASSERT_EQ(st, nullptr);
ASSERT_NE(keys_ret, nullptr);
int64_t* keys_ret = keys_ort.GetTensorMutableData<int64_t>();
ASSERT_EQ(std::set<int64_t>(keys_ret, keys_ret + NUM_KV_PAIRS),
std::set<int64_t>(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<float> values_ret_u;
float* values_ret = values_ret_u.get();
st = OrtGetTensorMutableData(values_ort, reinterpret_cast<void**>(&values_ret));
rels.add(st);
ASSERT_EQ(st, nullptr);
ASSERT_NE(values_ret, nullptr);
float* values_ret = values_ort.GetTensorMutableData<float>();
ASSERT_EQ(std::set<float>(values_ret, values_ret + NUM_KV_PAIRS),
std::set<float>(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<MockedOrtAllocator> default_allocator(std::make_unique<MockedOrtAllocator>());
OrtAllocatorInfo* info;
ORT_THROW_ON_ERROR(OrtCreateAllocatorInfo("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault, &info));
std::unique_ptr<OrtAllocatorInfo, decltype(&OrtReleaseAllocatorInfo)> rel_info(info, OrtReleaseAllocatorInfo);
RelAllocations<OrtValue> rel(&OrtReleaseValue);
RelAllocations<OrtStatus> rels(&OrtReleaseStatus);
auto default_allocator = std::make_unique<MockedOrtAllocator>();
Ort::AllocatorInfo info("Cpu", OrtDeviceAllocator, 0, OrtMemTypeDefault);
const int N = 3;
const int64_t NUM_KV_PAIRS = 4;
std::vector<OrtValue*> in(N);
std::vector<Ort::Value> in;
const char* keys_arr[NUM_KV_PAIRS] = {"abc", "def", "ghi", "jkl"};
std::vector<std::string> keys{keys_arr, keys_arr + NUM_KV_PAIRS};
std::vector<int64_t> dims = {NUM_KV_PAIRS};
std::vector<float> 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<OrtValue*> 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<size_t> 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<std::string> 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::string>(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<float> values_ret_u;
float* values_ret = values_ret_u.get();
st = OrtGetTensorMutableData(values_ort, reinterpret_cast<void**>(&values_ret));
rels.add(st);
ASSERT_EQ(st, nullptr);
ASSERT_NE(values_ret, nullptr);
float* values_ret = values_ort.GetTensorMutableData<float>();
ASSERT_EQ(std::set<float>(values_ret, values_ret + NUM_KV_PAIRS),
std::set<float>(std::begin(values), std::end(values)));
}