ONNX Runtime
Loading...
Searching...
No Matches
onnxruntime_cxx_api.h
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4// Summary: The Ort C++ API is a header only wrapper around the Ort C API.
5//
6// The C++ API simplifies usage by returning values directly instead of error codes, throwing exceptions on errors
7// and automatically releasing resources in the destructors. The primary purpose of C++ API is exception safety so
8// all the resources follow RAII and do not leak memory.
9//
10// Each of the C++ wrapper classes holds only a pointer to the C internal object. Treat them like smart pointers.
11// To create an empty object, pass 'nullptr' to the constructor (for example, Env e{nullptr};). However, you can't use them
12// until you assign an instance that actually holds an underlying object.
13//
14// For Ort objects only move assignment between objects is allowed, there are no copy constructors.
15// Some objects have explicit 'Clone' methods for this purpose.
16//
17// ConstXXXX types are copyable since they do not own the underlying C object, so you can pass them to functions as arguments
18// by value or by reference. ConstXXXX types are restricted to const only interfaces.
19//
20// UnownedXXXX are similar to ConstXXXX but also allow non-const interfaces.
21//
22// The lifetime of the corresponding owning object must eclipse the lifetimes of the ConstXXXX/UnownedXXXX types. They exists so you do not
23// have to fallback to C types and the API with the usual pitfalls. In general, do not use C API from your C++ code.
24
25#pragma once
26#include "onnxruntime_c_api.h"
27#include <cstddef>
28#include <cstdio>
29#include <array>
30#include <memory>
31#include <stdexcept>
32#include <string>
33#include <vector>
34#include <unordered_map>
35#include <utility>
36#include <type_traits>
37
38#ifdef ORT_NO_EXCEPTIONS
39#include <iostream>
40#endif
41
45namespace Ort {
46
51struct Exception : std::exception {
52 Exception(std::string&& string, OrtErrorCode code) : message_{std::move(string)}, code_{code} {}
53
54 OrtErrorCode GetOrtErrorCode() const { return code_; }
55 const char* what() const noexcept override { return message_.c_str(); }
56
57 private:
58 std::string message_;
59 OrtErrorCode code_;
60};
61
62#ifdef ORT_NO_EXCEPTIONS
63// The #ifndef is for the very special case where the user of this library wants to define their own way of handling errors.
64// NOTE: This header expects control flow to not continue after calling ORT_CXX_API_THROW
65#ifndef ORT_CXX_API_THROW
66#define ORT_CXX_API_THROW(string, code) \
67 do { \
68 std::cerr << Ort::Exception(string, code) \
69 .what() \
70 << std::endl; \
71 abort(); \
72 } while (false)
73#endif
74#else
75#define ORT_CXX_API_THROW(string, code) \
76 throw Ort::Exception(string, code)
77#endif
78
79// This is used internally by the C++ API. This class holds the global variable that points to the OrtApi,
80// it's in a template so that we can define a global variable in a header and make
81// it transparent to the users of the API.
82template <typename T>
83struct Global {
84 static const OrtApi* api_;
85};
86
87// If macro ORT_API_MANUAL_INIT is defined, no static initialization will be performed. Instead, user must call InitApi() before using it.
88template <typename T>
89#ifdef ORT_API_MANUAL_INIT
90const OrtApi* Global<T>::api_{};
91inline void InitApi() noexcept { Global<void>::api_ = OrtGetApiBase()->GetApi(ORT_API_VERSION); }
92
93// Used by custom operator libraries that are not linked to onnxruntime. Sets the global API object, which is
94// required by C++ APIs.
95//
96// Example mycustomop.cc:
97//
98// #define ORT_API_MANUAL_INIT
99// #include <onnxruntime_cxx_api.h>
100// #undef ORT_API_MANUAL_INIT
101//
102// OrtStatus* ORT_API_CALL RegisterCustomOps(OrtSessionOptions* options, const OrtApiBase* api_base) {
103// Ort::InitApi(api_base->GetApi(ORT_API_VERSION));
104// // ...
105// }
106//
107inline void InitApi(const OrtApi* api) noexcept { Global<void>::api_ = api; }
108#else
109#if defined(_MSC_VER) && !defined(__clang__)
110#pragma warning(push)
111// "Global initializer calls a non-constexpr function." Therefore you can't use ORT APIs in the other global initializers.
112// Please define ORT_API_MANUAL_INIT if it conerns you.
113#pragma warning(disable : 26426)
114#endif
116#if defined(_MSC_VER) && !defined(__clang__)
117#pragma warning(pop)
118#endif
119#endif
120
122inline const OrtApi& GetApi() noexcept { return *Global<void>::api_; }
123
128std::string GetVersionString();
129
135std::string GetBuildInfoString();
136
142std::vector<std::string> GetAvailableProviders();
143
183struct Float16_t {
184 uint16_t value;
185 constexpr Float16_t() noexcept : value(0) {}
186 constexpr Float16_t(uint16_t v) noexcept : value(v) {}
187 constexpr operator uint16_t() const noexcept { return value; }
188 constexpr bool operator==(const Float16_t& rhs) const noexcept { return value == rhs.value; };
189 constexpr bool operator!=(const Float16_t& rhs) const noexcept { return value != rhs.value; };
190};
191
192static_assert(sizeof(Float16_t) == sizeof(uint16_t), "Sizes must match");
193
203 uint16_t value;
204 constexpr BFloat16_t() noexcept : value(0) {}
205 constexpr BFloat16_t(uint16_t v) noexcept : value(v) {}
206 constexpr operator uint16_t() const noexcept { return value; }
207 constexpr bool operator==(const BFloat16_t& rhs) const noexcept { return value == rhs.value; };
208 constexpr bool operator!=(const BFloat16_t& rhs) const noexcept { return value != rhs.value; };
209};
210
211static_assert(sizeof(BFloat16_t) == sizeof(uint16_t), "Sizes must match");
212
219 uint8_t value;
220 constexpr Float8E4M3FN_t() noexcept : value(0) {}
221 constexpr Float8E4M3FN_t(uint8_t v) noexcept : value(v) {}
222 constexpr operator uint8_t() const noexcept { return value; }
223 // nan values are treated like any other value for operator ==, !=
224 constexpr bool operator==(const Float8E4M3FN_t& rhs) const noexcept { return value == rhs.value; };
225 constexpr bool operator!=(const Float8E4M3FN_t& rhs) const noexcept { return value != rhs.value; };
226};
227
228static_assert(sizeof(Float8E4M3FN_t) == sizeof(uint8_t), "Sizes must match");
229
236 uint8_t value;
237 constexpr Float8E4M3FNUZ_t() noexcept : value(0) {}
238 constexpr Float8E4M3FNUZ_t(uint8_t v) noexcept : value(v) {}
239 constexpr operator uint8_t() const noexcept { return value; }
240 // nan values are treated like any other value for operator ==, !=
241 constexpr bool operator==(const Float8E4M3FNUZ_t& rhs) const noexcept { return value == rhs.value; };
242 constexpr bool operator!=(const Float8E4M3FNUZ_t& rhs) const noexcept { return value != rhs.value; };
243};
244
245static_assert(sizeof(Float8E4M3FNUZ_t) == sizeof(uint8_t), "Sizes must match");
246
253 uint8_t value;
254 constexpr Float8E5M2_t() noexcept : value(0) {}
255 constexpr Float8E5M2_t(uint8_t v) noexcept : value(v) {}
256 constexpr operator uint8_t() const noexcept { return value; }
257 // nan values are treated like any other value for operator ==, !=
258 constexpr bool operator==(const Float8E5M2_t& rhs) const noexcept { return value == rhs.value; };
259 constexpr bool operator!=(const Float8E5M2_t& rhs) const noexcept { return value != rhs.value; };
260};
261
262static_assert(sizeof(Float8E5M2_t) == sizeof(uint8_t), "Sizes must match");
263
270 uint8_t value;
271 constexpr Float8E5M2FNUZ_t() noexcept : value(0) {}
272 constexpr Float8E5M2FNUZ_t(uint8_t v) noexcept : value(v) {}
273 constexpr operator uint8_t() const noexcept { return value; }
274 // nan values are treated like any other value for operator ==, !=
275 constexpr bool operator==(const Float8E5M2FNUZ_t& rhs) const noexcept { return value == rhs.value; };
276 constexpr bool operator!=(const Float8E5M2FNUZ_t& rhs) const noexcept { return value != rhs.value; };
277};
278
279static_assert(sizeof(Float8E5M2FNUZ_t) == sizeof(uint8_t), "Sizes must match");
280
281namespace detail {
282// This is used internally by the C++ API. This macro is to make it easy to generate overloaded methods for all of the various OrtRelease* functions for every Ort* type
283// This can't be done in the C API since C doesn't have function overloading.
284#define ORT_DEFINE_RELEASE(NAME) \
285 inline void OrtRelease(Ort##NAME* ptr) { GetApi().Release##NAME(ptr); }
286
287ORT_DEFINE_RELEASE(Allocator);
288ORT_DEFINE_RELEASE(MemoryInfo);
289ORT_DEFINE_RELEASE(CustomOpDomain);
290ORT_DEFINE_RELEASE(ThreadingOptions);
291ORT_DEFINE_RELEASE(Env);
292ORT_DEFINE_RELEASE(RunOptions);
293ORT_DEFINE_RELEASE(Session);
294ORT_DEFINE_RELEASE(SessionOptions);
295ORT_DEFINE_RELEASE(TensorTypeAndShapeInfo);
296ORT_DEFINE_RELEASE(SequenceTypeInfo);
297ORT_DEFINE_RELEASE(MapTypeInfo);
298ORT_DEFINE_RELEASE(TypeInfo);
299ORT_DEFINE_RELEASE(Value);
300ORT_DEFINE_RELEASE(ModelMetadata);
301ORT_DEFINE_RELEASE(IoBinding);
302ORT_DEFINE_RELEASE(ArenaCfg);
303ORT_DEFINE_RELEASE(Status);
304ORT_DEFINE_RELEASE(OpAttr);
305ORT_DEFINE_RELEASE(Op);
306ORT_DEFINE_RELEASE(KernelInfo);
307
308#undef ORT_DEFINE_RELEASE
309
313template <typename T>
314struct Unowned {
315 using Type = T;
316};
317
337template <typename T>
338struct Base {
339 using contained_type = T;
340
341 constexpr Base() = default;
342 constexpr explicit Base(contained_type* p) noexcept : p_{p} {}
344
345 Base(const Base&) = delete;
346 Base& operator=(const Base&) = delete;
347
348 Base(Base&& v) noexcept : p_{v.p_} { v.p_ = nullptr; }
349 Base& operator=(Base&& v) noexcept {
350 OrtRelease(p_);
351 p_ = v.release();
352 return *this;
353 }
354
355 constexpr operator contained_type*() const noexcept { return p_; }
356
360 T* p = p_;
361 p_ = nullptr;
362 return p;
363 }
364
365 protected:
367};
368
369// Undefined. For const types use Base<Unowned<const T>>
370template <typename T>
371struct Base<const T>;
372
380template <typename T>
381struct Base<Unowned<T>> {
383
384 constexpr Base() = default;
385 constexpr explicit Base(contained_type* p) noexcept : p_{p} {}
386
387 ~Base() = default;
388
389 Base(const Base&) = default;
390 Base& operator=(const Base&) = default;
391
392 Base(Base&& v) noexcept : p_{v.p_} { v.p_ = nullptr; }
393 Base& operator=(Base&& v) noexcept {
394 p_ = nullptr;
395 std::swap(p_, v.p_);
396 return *this;
397 }
398
399 constexpr operator contained_type*() const noexcept { return p_; }
400
401 protected:
403};
404
405// Light functor to release memory with OrtAllocator
408 explicit AllocatedFree(OrtAllocator* allocator)
409 : allocator_(allocator) {}
410 void operator()(void* ptr) const {
411 if (ptr) allocator_->Free(allocator_, ptr);
412 }
413};
414
415} // namespace detail
416
417struct AllocatorWithDefaultOptions;
418struct Env;
419struct TypeInfo;
420struct Value;
421struct ModelMetadata;
422
427using AllocatedStringPtr = std::unique_ptr<char, detail::AllocatedFree>;
428
433struct Status : detail::Base<OrtStatus> {
434 explicit Status(std::nullptr_t) noexcept {}
435 explicit Status(OrtStatus* status) noexcept;
436 explicit Status(const Exception&) noexcept;
437 explicit Status(const std::exception&) noexcept;
438 Status(const char* message, OrtErrorCode code) noexcept;
439 std::string GetErrorMessage() const;
441 bool IsOK() const noexcept;
442};
443
451
454
457
460
463
466
468 ThreadingOptions& SetGlobalCustomThreadCreationOptions(void* ort_custom_thread_creation_options);
469
472};
473
479struct Env : detail::Base<OrtEnv> {
480 explicit Env(std::nullptr_t) {}
481
483 Env(OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING, _In_ const char* logid = "");
484
486 Env(OrtLoggingLevel logging_level, const char* logid, OrtLoggingFunction logging_function, void* logger_param);
487
489 Env(const OrtThreadingOptions* tp_options, OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING, _In_ const char* logid = "");
490
492 Env(const OrtThreadingOptions* tp_options, OrtLoggingFunction logging_function, void* logger_param,
493 OrtLoggingLevel logging_level = ORT_LOGGING_LEVEL_WARNING, _In_ const char* logid = "");
494
496 explicit Env(OrtEnv* p) : Base<OrtEnv>{p} {}
497
500
502
503 Env& CreateAndRegisterAllocator(const OrtMemoryInfo* mem_info, const OrtArenaCfg* arena_cfg);
504
505 Env& CreateAndRegisterAllocatorV2(const std::string& provider_type, const OrtMemoryInfo* mem_info, const std::unordered_map<std::string, std::string>& options, const OrtArenaCfg* arena_cfg);
506};
507
511struct CustomOpDomain : detail::Base<OrtCustomOpDomain> {
512 explicit CustomOpDomain(std::nullptr_t) {}
513
515 explicit CustomOpDomain(const char* domain);
516
517 // This does not take ownership of the op, simply registers it.
518 void Add(const OrtCustomOp* op);
519};
520
524struct RunOptions : detail::Base<OrtRunOptions> {
525 explicit RunOptions(std::nullptr_t) {}
527
530
533
534 RunOptions& SetRunTag(const char* run_tag);
535 const char* GetRunTag() const;
536
537 RunOptions& AddConfigEntry(const char* config_key, const char* config_value);
538
545
551};
552
553namespace detail {
554// Utility function that returns a SessionOption config entry key for a specific custom operator.
555// Ex: custom_op.[custom_op_name].[config]
556std::string MakeCustomOpConfigEntryKey(const char* custom_op_name, const char* config);
557} // namespace detail
558
569 CustomOpConfigs() = default;
570 ~CustomOpConfigs() = default;
575
584 CustomOpConfigs& AddConfig(const char* custom_op_name, const char* config_key, const char* config_value);
585
594 const std::unordered_map<std::string, std::string>& GetFlattenedConfigs() const;
595
596 private:
597 std::unordered_map<std::string, std::string> flat_configs_;
598};
599
605struct SessionOptions;
606
607namespace detail {
608// we separate const-only methods because passing const ptr to non-const methods
609// is only discovered when inline methods are compiled which is counter-intuitive
610template <typename T>
612 using B = Base<T>;
613 using B::B;
614
616
617 std::string GetConfigEntry(const char* config_key) const;
618 bool HasConfigEntry(const char* config_key) const;
619 std::string GetConfigEntryOrDefault(const char* config_key, const std::string& def);
620};
621
622template <typename T>
625 using B::B;
626
627 SessionOptionsImpl& SetIntraOpNumThreads(int intra_op_num_threads);
628 SessionOptionsImpl& SetInterOpNumThreads(int inter_op_num_threads);
630
633
634 SessionOptionsImpl& SetOptimizedModelFilePath(const ORTCHAR_T* optimized_model_file);
635
636 SessionOptionsImpl& EnableProfiling(const ORTCHAR_T* profile_file_prefix);
638
640
643
645
646 SessionOptionsImpl& SetLogId(const char* logid);
648
650
652
653 SessionOptionsImpl& AddConfigEntry(const char* config_key, const char* config_value);
654
655 SessionOptionsImpl& AddInitializer(const char* name, const OrtValue* ort_val);
656 SessionOptionsImpl& AddExternalInitializers(const std::vector<std::string>& names, const std::vector<Value>& ort_values);
657
670 SessionOptionsImpl& AppendExecutionProvider(const std::string& provider_name,
671 const std::unordered_map<std::string, std::string>& provider_options = {});
672
674 SessionOptionsImpl& SetCustomThreadCreationOptions(void* ort_custom_thread_creation_options);
676
680 SessionOptionsImpl& RegisterCustomOpsLibrary(const ORTCHAR_T* library_name, const CustomOpConfigs& custom_op_configs = {});
681
683};
684} // namespace detail
685
688
692struct SessionOptions : detail::SessionOptionsImpl<OrtSessionOptions> {
693 explicit SessionOptions(std::nullptr_t) {}
695 explicit SessionOptions(OrtSessionOptions* p) : SessionOptionsImpl<OrtSessionOptions>{p} {}
698};
699
703struct ModelMetadata : detail::Base<OrtModelMetadata> {
704 explicit ModelMetadata(std::nullptr_t) {}
706
714
722
730
738
746
753 std::vector<AllocatedStringPtr> GetCustomMetadataMapKeysAllocated(OrtAllocator* allocator) const;
754
765
766 int64_t GetVersion() const;
767};
768
769struct IoBinding;
770
771namespace detail {
772
773// we separate const-only methods because passing const ptr to non-const methods
774// is only discovered when inline methods are compiled which is counter-intuitive
775template <typename T>
777 using B = Base<T>;
778 using B::B;
779
780 size_t GetInputCount() const;
781 size_t GetOutputCount() const;
783
792
801
810
811 uint64_t GetProfilingStartTimeNs() const;
813
814 TypeInfo GetInputTypeInfo(size_t index) const;
815 TypeInfo GetOutputTypeInfo(size_t index) const;
817};
818
819template <typename T>
822 using B::B;
823
841 std::vector<Value> Run(const RunOptions& run_options, const char* const* input_names, const Value* input_values, size_t input_count,
842 const char* const* output_names, size_t output_count);
843
847 void Run(const RunOptions& run_options, const char* const* input_names, const Value* input_values, size_t input_count,
848 const char* const* output_names, Value* output_values, size_t output_count);
849
850 void Run(const RunOptions& run_options, const IoBinding&);
851
859};
860
861} // namespace detail
862
865
869struct Session : detail::SessionImpl<OrtSession> {
870 explicit Session(std::nullptr_t) {}
871 Session(const Env& env, const ORTCHAR_T* model_path, const SessionOptions& options);
872 Session(const Env& env, const ORTCHAR_T* model_path, const SessionOptions& options,
873 OrtPrepackedWeightsContainer* prepacked_weights_container);
874 Session(const Env& env, const void* model_data, size_t model_data_length, const SessionOptions& options);
875 Session(const Env& env, const void* model_data, size_t model_data_length, const SessionOptions& options,
876 OrtPrepackedWeightsContainer* prepacked_weights_container);
877
878 ConstSession GetConst() const { return ConstSession{this->p_}; }
879 UnownedSession GetUnowned() const { return UnownedSession{this->p_}; }
880};
881
882namespace detail {
883template <typename T>
884struct MemoryInfoImpl : Base<T> {
885 using B = Base<T>;
886 using B::B;
887
888 std::string GetAllocatorName() const;
890 int GetDeviceId() const;
893
894 template <typename U>
895 bool operator==(const MemoryInfoImpl<U>& o) const;
896};
897} // namespace detail
898
899// Const object holder that does not own the underlying object
901
905struct MemoryInfo : detail::MemoryInfoImpl<OrtMemoryInfo> {
907 explicit MemoryInfo(std::nullptr_t) {}
908 explicit MemoryInfo(OrtMemoryInfo* p) : MemoryInfoImpl<OrtMemoryInfo>{p} {}
909 MemoryInfo(const char* name, OrtAllocatorType type, int id, OrtMemType mem_type);
910 ConstMemoryInfo GetConst() const { return ConstMemoryInfo{this->p_}; }
911};
912
913namespace detail {
914template <typename T>
916 using B = Base<T>;
917 using B::B;
918
920 size_t GetElementCount() const;
921
922 size_t GetDimensionsCount() const;
923
928 [[deprecated("use GetShape()")]] void GetDimensions(int64_t* values, size_t values_count) const;
929
930 void GetSymbolicDimensions(const char** values, size_t values_count) const;
931
932 std::vector<int64_t> GetShape() const;
933};
934
935} // namespace detail
936
938
943 explicit TensorTypeAndShapeInfo(std::nullptr_t) {}
944 explicit TensorTypeAndShapeInfo(OrtTensorTypeAndShapeInfo* p) : TensorTypeAndShapeInfoImpl{p} {}
946};
947
948namespace detail {
949template <typename T>
951 using B = Base<T>;
952 using B::B;
954};
955
956} // namespace detail
957
959
963struct SequenceTypeInfo : detail::SequenceTypeInfoImpl<OrtSequenceTypeInfo> {
964 explicit SequenceTypeInfo(std::nullptr_t) {}
965 explicit SequenceTypeInfo(OrtSequenceTypeInfo* p) : SequenceTypeInfoImpl<OrtSequenceTypeInfo>{p} {}
967};
968
969namespace detail {
970template <typename T>
972 using B = Base<T>;
973 using B::B;
975};
976
977} // namespace detail
978
979// This is always owned by the TypeInfo and can only be obtained from it.
981
982namespace detail {
983template <typename T>
985 using B = Base<T>;
986 using B::B;
989};
990
991} // namespace detail
992
994
998struct MapTypeInfo : detail::MapTypeInfoImpl<OrtMapTypeInfo> {
999 explicit MapTypeInfo(std::nullptr_t) {}
1000 explicit MapTypeInfo(OrtMapTypeInfo* p) : MapTypeInfoImpl<OrtMapTypeInfo>{p} {}
1001 ConstMapTypeInfo GetConst() const { return ConstMapTypeInfo{this->p_}; }
1002};
1003
1004namespace detail {
1005template <typename T>
1007 using B = Base<T>;
1008 using B::B;
1009
1014
1016};
1017} // namespace detail
1018
1024
1029struct TypeInfo : detail::TypeInfoImpl<OrtTypeInfo> {
1030 explicit TypeInfo(std::nullptr_t) {}
1031 explicit TypeInfo(OrtTypeInfo* p) : TypeInfoImpl<OrtTypeInfo>{p} {}
1032
1033 ConstTypeInfo GetConst() const { return ConstTypeInfo{this->p_}; }
1034};
1035
1036namespace detail {
1037// This structure is used to feed sparse tensor values
1038// information for use with FillSparseTensor<Format>() API
1039// if the data type for the sparse tensor values is numeric
1040// use data.p_data, otherwise, use data.str pointer to feed
1041// values. data.str is an array of const char* that are zero terminated.
1042// number of strings in the array must match shape size.
1043// For fully sparse tensors use shape {0} and set p_data/str
1044// to nullptr.
1046 const int64_t* values_shape;
1048 union {
1049 const void* p_data;
1050 const char** str;
1051 } data;
1052};
1053
1054// Provides a way to pass shape in a single
1055// argument
1056struct Shape {
1057 const int64_t* shape;
1059};
1060
1061template <typename T>
1063 using B = Base<T>;
1064 using B::B;
1065
1069 template <typename R>
1070 void GetOpaqueData(const char* domain, const char* type_name, R&) const;
1071
1072 bool IsTensor() const;
1073 bool HasValue() const;
1074
1075 size_t GetCount() const; // If a non tensor, returns 2 for map and N for sequence, where N is the number of elements
1076 Value GetValue(int index, OrtAllocator* allocator) const;
1077
1085
1100 void GetStringTensorContent(void* buffer, size_t buffer_length, size_t* offsets, size_t offsets_count) const;
1101
1108 template <typename R>
1109 const R* GetTensorData() const;
1110
1115 const void* GetTensorRawData() const;
1116
1124
1132
1138
1147 void GetStringTensorElement(size_t buffer_length, size_t element_index, void* buffer) const;
1148
1155 std::string GetStringTensorElement(size_t element_index) const;
1156
1163 size_t GetStringTensorElementLength(size_t element_index) const;
1164
1165#if !defined(DISABLE_SPARSE_TENSORS)
1173
1180
1189
1199 template <typename R>
1200 const R* GetSparseTensorIndicesData(OrtSparseIndicesFormat indices_format, size_t& num_indices) const;
1201
1206 bool IsSparseTensor() const;
1207
1216 template <typename R>
1217 const R* GetSparseTensorValues() const;
1218
1219#endif
1220};
1221
1222template <typename T>
1225 using B::B;
1226
1232 template <typename R>
1234
1240
1242 // Obtain a reference to an element of data at the location specified
1248 template <typename R>
1249 R& At(const std::vector<int64_t>& location);
1250
1256 void FillStringTensor(const char* const* s, size_t s_len);
1257
1263 void FillStringTensorElement(const char* s, size_t index);
1264
1277 char* GetResizedStringTensorElementBuffer(size_t index, size_t buffer_length);
1278
1279#if !defined(DISABLE_SPARSE_TENSORS)
1288 void UseCooIndices(int64_t* indices_data, size_t indices_num);
1289
1300 void UseCsrIndices(int64_t* inner_data, size_t inner_num, int64_t* outer_data, size_t outer_num);
1301
1310 void UseBlockSparseIndices(const Shape& indices_shape, int32_t* indices_data);
1311
1321 void FillSparseTensorCoo(const OrtMemoryInfo* data_mem_info, const OrtSparseValuesParam& values_param,
1322 const int64_t* indices_data, size_t indices_num);
1323
1335 void FillSparseTensorCsr(const OrtMemoryInfo* data_mem_info,
1336 const OrtSparseValuesParam& values,
1337 const int64_t* inner_indices_data, size_t inner_indices_num,
1338 const int64_t* outer_indices_data, size_t outer_indices_num);
1339
1350 const OrtSparseValuesParam& values,
1351 const Shape& indices_shape,
1352 const int32_t* indices_data);
1353
1354#endif
1355};
1356
1357} // namespace detail
1358
1361
1365struct Value : detail::ValueImpl<OrtValue> {
1369
1370 explicit Value(std::nullptr_t) {}
1371 explicit Value(OrtValue* p) : Base{p} {}
1372 Value(Value&&) = default;
1373 Value& operator=(Value&&) = default;
1374
1375 ConstValue GetConst() const { return ConstValue{this->p_}; }
1376 UnownedValue GetUnowned() const { return UnownedValue{this->p_}; }
1377
1386 template <typename T>
1387 static Value CreateTensor(const OrtMemoryInfo* info, T* p_data, size_t p_data_element_count, const int64_t* shape, size_t shape_len);
1388
1398 static Value CreateTensor(const OrtMemoryInfo* info, void* p_data, size_t p_data_byte_count, const int64_t* shape, size_t shape_len,
1400
1412 template <typename T>
1413 static Value CreateTensor(OrtAllocator* allocator, const int64_t* shape, size_t shape_len);
1414
1426 static Value CreateTensor(OrtAllocator* allocator, const int64_t* shape, size_t shape_len, ONNXTensorElementDataType type);
1427
1436 static Value CreateMap(const Value& keys, const Value& values);
1437
1445 static Value CreateSequence(const std::vector<Value>& values);
1446
1455 template <typename T>
1456 static Value CreateOpaque(const char* domain, const char* type_name, const T& value);
1457
1458#if !defined(DISABLE_SPARSE_TENSORS)
1469 template <typename T>
1470 static Value CreateSparseTensor(const OrtMemoryInfo* info, T* p_data, const Shape& dense_shape,
1471 const Shape& values_shape);
1472
1489 static Value CreateSparseTensor(const OrtMemoryInfo* info, void* p_data, const Shape& dense_shape,
1490 const Shape& values_shape, ONNXTensorElementDataType type);
1491
1501 template <typename T>
1502 static Value CreateSparseTensor(OrtAllocator* allocator, const Shape& dense_shape);
1503
1515 static Value CreateSparseTensor(OrtAllocator* allocator, const Shape& dense_shape, ONNXTensorElementDataType type);
1516
1517#endif // !defined(DISABLE_SPARSE_TENSORS)
1518};
1519
1527 MemoryAllocation(OrtAllocator* allocator, void* p, size_t size);
1532 MemoryAllocation& operator=(MemoryAllocation&&) noexcept;
1533
1534 void* get() { return p_; }
1535 size_t size() const { return size_; }
1536
1537 private:
1538 OrtAllocator* allocator_;
1539 void* p_;
1540 size_t size_;
1541};
1542
1543namespace detail {
1544template <typename T>
1545struct AllocatorImpl : Base<T> {
1546 using B = Base<T>;
1547 using B::B;
1548
1549 void* Alloc(size_t size);
1551 void Free(void* p);
1553};
1554
1555} // namespace detail
1556
1560struct AllocatorWithDefaultOptions : detail::AllocatorImpl<detail::Unowned<OrtAllocator>> {
1561 explicit AllocatorWithDefaultOptions(std::nullptr_t) {}
1563};
1564
1568struct Allocator : detail::AllocatorImpl<OrtAllocator> {
1569 explicit Allocator(std::nullptr_t) {}
1570 Allocator(const Session& session, const OrtMemoryInfo*);
1571};
1572
1574
1575namespace detail {
1576namespace binding_utils {
1577// Bring these out of template
1578std::vector<std::string> GetOutputNamesHelper(const OrtIoBinding* binding, OrtAllocator*);
1579std::vector<Value> GetOutputValuesHelper(const OrtIoBinding* binding, OrtAllocator*);
1580} // namespace binding_utils
1581
1582template <typename T>
1584 using B = Base<T>;
1585 using B::B;
1586
1587 std::vector<std::string> GetOutputNames() const;
1588 std::vector<std::string> GetOutputNames(OrtAllocator*) const;
1589 std::vector<Value> GetOutputValues() const;
1590 std::vector<Value> GetOutputValues(OrtAllocator*) const;
1591};
1592
1593template <typename T>
1596 using B::B;
1597
1598 void BindInput(const char* name, const Value&);
1599 void BindOutput(const char* name, const Value&);
1600 void BindOutput(const char* name, const OrtMemoryInfo*);
1605};
1606
1607} // namespace detail
1608
1611
1615struct IoBinding : detail::IoBindingImpl<OrtIoBinding> {
1616 explicit IoBinding(std::nullptr_t) {}
1617 explicit IoBinding(Session& session);
1618 ConstIoBinding GetConst() const { return ConstIoBinding{this->p_}; }
1619 UnownedIoBinding GetUnowned() const { return UnownedIoBinding{this->p_}; }
1620};
1621
1626struct ArenaCfg : detail::Base<OrtArenaCfg> {
1627 explicit ArenaCfg(std::nullptr_t) {}
1636 ArenaCfg(size_t max_mem, int arena_extend_strategy, int initial_chunk_size_bytes, int max_dead_bytes_per_chunk);
1637};
1638
1639//
1640// Custom OPs (only needed to implement custom OPs)
1641//
1642
1646struct OpAttr : detail::Base<OrtOpAttr> {
1647 OpAttr(const char* name, const void* data, int len, OrtOpAttrType type);
1648};
1649
1658#define ORT_CXX_LOG(logger, message_severity, message) \
1659 do { \
1660 if (message_severity >= logger.GetLoggingSeverityLevel()) { \
1661 Ort::ThrowOnError(logger.LogMessage(message_severity, ORT_FILE, __LINE__, \
1662 static_cast<const char*>(__FUNCTION__), message)); \
1663 } \
1664 } while (false)
1665
1674#define ORT_CXX_LOG_NOEXCEPT(logger, message_severity, message) \
1675 do { \
1676 if (message_severity >= logger.GetLoggingSeverityLevel()) { \
1677 static_cast<void>(logger.LogMessage(message_severity, ORT_FILE, __LINE__, \
1678 static_cast<const char*>(__FUNCTION__), message)); \
1679 } \
1680 } while (false)
1681
1693#define ORT_CXX_LOGF(logger, message_severity, /*format,*/...) \
1694 do { \
1695 if (message_severity >= logger.GetLoggingSeverityLevel()) { \
1696 Ort::ThrowOnError(logger.LogFormattedMessage(message_severity, ORT_FILE, __LINE__, \
1697 static_cast<const char*>(__FUNCTION__), __VA_ARGS__)); \
1698 } \
1699 } while (false)
1700
1712#define ORT_CXX_LOGF_NOEXCEPT(logger, message_severity, /*format,*/...) \
1713 do { \
1714 if (message_severity >= logger.GetLoggingSeverityLevel()) { \
1715 static_cast<void>(logger.LogFormattedMessage(message_severity, ORT_FILE, __LINE__, \
1716 static_cast<const char*>(__FUNCTION__), __VA_ARGS__)); \
1717 } \
1718 } while (false)
1719
1730struct Logger {
1734 Logger() = default;
1735
1739 explicit Logger(std::nullptr_t) {}
1740
1747 explicit Logger(const OrtLogger* logger);
1748
1749 ~Logger() = default;
1750
1751 Logger(const Logger&) = default;
1752 Logger& operator=(const Logger&) = default;
1753
1754 Logger(Logger&& v) noexcept = default;
1755 Logger& operator=(Logger&& v) noexcept = default;
1756
1763
1776 Status LogMessage(OrtLoggingLevel log_severity_level, const ORTCHAR_T* file_path, int line_number,
1777 const char* func_name, const char* message) const noexcept;
1778
1793 template <typename... Args>
1794 Status LogFormattedMessage(OrtLoggingLevel log_severity_level, const ORTCHAR_T* file_path, int line_number,
1795 const char* func_name, const char* format, Args&&... args) const noexcept;
1796
1797 private:
1798 const OrtLogger* logger_{};
1799 OrtLoggingLevel cached_severity_level_{};
1800};
1801
1810 size_t GetInputCount() const;
1811 size_t GetOutputCount() const;
1812 ConstValue GetInput(size_t index) const;
1813 UnownedValue GetOutput(size_t index, const int64_t* dim_values, size_t dim_count) const;
1814 UnownedValue GetOutput(size_t index, const std::vector<int64_t>& dims) const;
1815 void* GetGPUComputeStream() const;
1817 OrtAllocator* GetAllocator(const OrtMemoryInfo& memory_info) const;
1818
1819 private:
1820 OrtKernelContext* ctx_;
1821};
1822
1823struct KernelInfo;
1824
1825namespace detail {
1826namespace attr_utils {
1827void GetAttr(const OrtKernelInfo* p, const char* name, float&);
1828void GetAttr(const OrtKernelInfo* p, const char* name, int64_t&);
1829void GetAttr(const OrtKernelInfo* p, const char* name, std::string&);
1830void GetAttrs(const OrtKernelInfo* p, const char* name, std::vector<float>&);
1831void GetAttrs(const OrtKernelInfo* p, const char* name, std::vector<int64_t>&);
1832} // namespace attr_utils
1833
1834template <typename T>
1836 using B = Base<T>;
1837 using B::B;
1838
1840
1841 template <typename R> // R is only implemented for float, int64_t, and string
1842 R GetAttribute(const char* name) const {
1843 R val;
1844 attr_utils::GetAttr(this->p_, name, val);
1845 return val;
1846 }
1847
1848 template <typename R> // R is only implemented for std::vector<float>, std::vector<int64_t>
1849 std::vector<R> GetAttributes(const char* name) const {
1850 std::vector<R> result;
1851 attr_utils::GetAttrs(this->p_, name, result);
1852 return result;
1853 }
1854
1855 Value GetTensorAttribute(const char* name, OrtAllocator* allocator) const;
1856
1857 size_t GetInputCount() const;
1858 size_t GetOutputCount() const;
1859
1860 std::string GetInputName(size_t index) const;
1861 std::string GetOutputName(size_t index) const;
1862
1863 TypeInfo GetInputTypeInfo(size_t index) const;
1864 TypeInfo GetOutputTypeInfo(size_t index) const;
1865
1866 ConstValue GetTensorConstantInput(size_t index, int* is_constant) const;
1867
1868 std::string GetNodeName() const;
1870};
1871
1872} // namespace detail
1873
1875
1882struct KernelInfo : detail::KernelInfoImpl<OrtKernelInfo> {
1883 explicit KernelInfo(std::nullptr_t) {}
1884 explicit KernelInfo(OrtKernelInfo* info);
1885 ConstKernelInfo GetConst() const { return ConstKernelInfo{this->p_}; }
1886};
1887
1891struct Op : detail::Base<OrtOp> {
1892 explicit Op(std::nullptr_t) {}
1893
1894 explicit Op(OrtOp*);
1895
1896 static Op Create(const OrtKernelInfo* info, const char* op_name, const char* domain,
1897 int version, const char** type_constraint_names,
1898 const ONNXTensorElementDataType* type_constraint_values,
1899 size_t type_constraint_count,
1900 const OpAttr* attr_values,
1901 size_t attr_count,
1902 size_t input_count, size_t output_count);
1903
1904 void Invoke(const OrtKernelContext* context,
1905 const Value* input_values,
1906 size_t input_count,
1907 Value* output_values,
1908 size_t output_count);
1909
1910 // For easier refactoring
1911 void Invoke(const OrtKernelContext* context,
1912 const OrtValue* const* input_values,
1913 size_t input_count,
1914 OrtValue* const* output_values,
1915 size_t output_count);
1916};
1917
1918template <typename TOp, typename TKernel, bool WithStatus = false>
1922 OrtCustomOp::GetName = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetName(); };
1923
1924 OrtCustomOp::GetExecutionProviderType = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetExecutionProviderType(); };
1925
1926 OrtCustomOp::GetInputTypeCount = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetInputTypeCount(); };
1927 OrtCustomOp::GetInputType = [](const OrtCustomOp* this_, size_t index) { return static_cast<const TOp*>(this_)->GetInputType(index); };
1928 OrtCustomOp::GetInputMemoryType = [](const OrtCustomOp* this_, size_t index) { return static_cast<const TOp*>(this_)->GetInputMemoryType(index); };
1929
1930 OrtCustomOp::GetOutputTypeCount = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetOutputTypeCount(); };
1931 OrtCustomOp::GetOutputType = [](const OrtCustomOp* this_, size_t index) { return static_cast<const TOp*>(this_)->GetOutputType(index); };
1932
1933#if defined(_MSC_VER) && !defined(__clang__)
1934#pragma warning(push)
1935#pragma warning(disable : 26409)
1936#endif
1937 OrtCustomOp::KernelDestroy = [](void* op_kernel) { delete static_cast<TKernel*>(op_kernel); };
1938#if defined(_MSC_VER) && !defined(__clang__)
1939#pragma warning(pop)
1940#endif
1941 OrtCustomOp::GetInputCharacteristic = [](const OrtCustomOp* this_, size_t index) { return static_cast<const TOp*>(this_)->GetInputCharacteristic(index); };
1942 OrtCustomOp::GetOutputCharacteristic = [](const OrtCustomOp* this_, size_t index) { return static_cast<const TOp*>(this_)->GetOutputCharacteristic(index); };
1943
1944 OrtCustomOp::GetVariadicInputMinArity = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetVariadicInputMinArity(); };
1945 OrtCustomOp::GetVariadicInputHomogeneity = [](const OrtCustomOp* this_) { return static_cast<int>(static_cast<const TOp*>(this_)->GetVariadicInputHomogeneity()); };
1946 OrtCustomOp::GetVariadicOutputMinArity = [](const OrtCustomOp* this_) { return static_cast<const TOp*>(this_)->GetVariadicOutputMinArity(); };
1947 OrtCustomOp::GetVariadicOutputHomogeneity = [](const OrtCustomOp* this_) { return static_cast<int>(static_cast<const TOp*>(this_)->GetVariadicOutputHomogeneity()); };
1948 if constexpr (WithStatus) {
1949 OrtCustomOp::CreateKernelV2 = [](const OrtCustomOp* this_, const OrtApi* api, const OrtKernelInfo* info, void** op_kernel) -> OrtStatusPtr {
1950 return static_cast<const TOp*>(this_)->CreateKernelV2(*api, info, op_kernel);
1951 };
1952 OrtCustomOp::KernelComputeV2 = [](void* op_kernel, OrtKernelContext* context) -> OrtStatusPtr {
1953 return static_cast<TKernel*>(op_kernel)->ComputeV2(context);
1954 };
1955 } else {
1958
1959 OrtCustomOp::CreateKernel = [](const OrtCustomOp* this_, const OrtApi* api, const OrtKernelInfo* info) { return static_cast<const TOp*>(this_)->CreateKernel(*api, info); };
1960 OrtCustomOp::KernelCompute = [](void* op_kernel, OrtKernelContext* context) {
1961 static_cast<TKernel*>(op_kernel)->Compute(context);
1962 };
1963 }
1964 }
1965
1966 // Default implementation of GetExecutionProviderType that returns nullptr to default to the CPU provider
1967 const char* GetExecutionProviderType() const { return nullptr; }
1968
1969 // Default implementations of GetInputCharacteristic() and GetOutputCharacteristic() below
1970 // (inputs and outputs are required by default)
1972 return OrtCustomOpInputOutputCharacteristic::INPUT_OUTPUT_REQUIRED;
1973 }
1974
1976 return OrtCustomOpInputOutputCharacteristic::INPUT_OUTPUT_REQUIRED;
1977 }
1978
1979 // Default implemention of GetInputMemoryType() that returns OrtMemTypeDefault
1980 OrtMemType GetInputMemoryType(size_t /*index*/) const {
1981 return OrtMemTypeDefault;
1982 }
1983
1984 // Default implementation of GetVariadicInputMinArity() returns 1 to specify that a variadic input
1985 // should expect at least 1 argument.
1987 return 1;
1988 }
1989
1990 // Default implementation of GetVariadicInputHomegeneity() returns true to specify that all arguments
1991 // to a variadic input should be of the same type.
1993 return true;
1994 }
1995
1996 // Default implementation of GetVariadicOutputMinArity() returns 1 to specify that a variadic output
1997 // should produce at least 1 output value.
1999 return 1;
2000 }
2001
2002 // Default implementation of GetVariadicOutputHomegeneity() returns true to specify that all output values
2003 // produced by a variadic output should be of the same type.
2005 return true;
2006 }
2007
2008 // Declare list of session config entries used by this Custom Op.
2009 // Implement this function in order to get configs from CustomOpBase::GetSessionConfigs().
2010 // This default implementation returns an empty vector of config entries.
2011 std::vector<std::string> GetSessionConfigKeys() const {
2012 return std::vector<std::string>{};
2013 }
2014
2015 protected:
2016 // Helper function that returns a map of session config entries specified by CustomOpBase::GetSessionConfigKeys.
2017 void GetSessionConfigs(std::unordered_map<std::string, std::string>& out, ConstSessionOptions options) const;
2018};
2019
2020} // namespace Ort
2021
2022#include "onnxruntime_cxx_inline.h"
struct OrtMemoryInfo OrtMemoryInfo
Definition: onnxruntime_c_api.h:278
struct OrtKernelInfo OrtKernelInfo
Definition: onnxruntime_c_api.h:357
OrtLoggingLevel
Logging severity levels.
Definition: onnxruntime_c_api.h:233
OrtMemoryInfoDeviceType
This mimics OrtDevice type constants so they can be returned in the API.
Definition: onnxruntime_c_api.h:381
void(* OrtLoggingFunction)(void *param, OrtLoggingLevel severity, const char *category, const char *logid, const char *code_location, const char *message)
Definition: onnxruntime_c_api.h:322
void(* OrtCustomJoinThreadFn)(OrtCustomThreadHandle ort_custom_thread_handle)
Custom thread join function.
Definition: onnxruntime_c_api.h:695
OrtCustomOpInputOutputCharacteristic
Definition: onnxruntime_c_api.h:4336
struct OrtTensorRTProviderOptionsV2 OrtTensorRTProviderOptionsV2
Definition: onnxruntime_c_api.h:295
struct OrtThreadingOptions OrtThreadingOptions
Definition: onnxruntime_c_api.h:292
struct OrtSequenceTypeInfo OrtSequenceTypeInfo
Definition: onnxruntime_c_api.h:286
struct OrtDnnlProviderOptions OrtDnnlProviderOptions
Definition: onnxruntime_c_api.h:298
OrtSparseIndicesFormat
Definition: onnxruntime_c_api.h:222
struct OrtPrepackedWeightsContainer OrtPrepackedWeightsContainer
Definition: onnxruntime_c_api.h:294
struct OrtCustomOpDomain OrtCustomOpDomain
Definition: onnxruntime_c_api.h:289
struct OrtIoBinding OrtIoBinding
Definition: onnxruntime_c_api.h:279
OrtAllocatorType
Definition: onnxruntime_c_api.h:363
struct OrtOp OrtOp
Definition: onnxruntime_c_api.h:299
struct OrtModelMetadata OrtModelMetadata
Definition: onnxruntime_c_api.h:290
struct OrtTypeInfo OrtTypeInfo
Definition: onnxruntime_c_api.h:283
struct OrtTensorTypeAndShapeInfo OrtTensorTypeAndShapeInfo
Definition: onnxruntime_c_api.h:284
struct OrtCUDAProviderOptionsV2 OrtCUDAProviderOptionsV2
Definition: onnxruntime_c_api.h:296
struct OrtKernelContext OrtKernelContext
Definition: onnxruntime_c_api.h:359
struct OrtCANNProviderOptions OrtCANNProviderOptions
Definition: onnxruntime_c_api.h:297
struct OrtSessionOptions OrtSessionOptions
Definition: onnxruntime_c_api.h:288
struct OrtValue OrtValue
Definition: onnxruntime_c_api.h:281
GraphOptimizationLevel
Graph optimization level.
Definition: onnxruntime_c_api.h:331
OrtStatus * OrtStatusPtr
Definition: onnxruntime_c_api.h:306
OrtMemType
Memory types for allocated memory, execution provider specific types should be extended in each provi...
Definition: onnxruntime_c_api.h:372
OrtSparseFormat
Definition: onnxruntime_c_api.h:214
ONNXType
Definition: onnxruntime_c_api.h:202
struct OrtEnv OrtEnv
Definition: onnxruntime_c_api.h:276
OrtErrorCode
Definition: onnxruntime_c_api.h:241
struct OrtStatus OrtStatus
Definition: onnxruntime_c_api.h:277
#define ORT_API_VERSION
The API version defined in this header.
Definition: onnxruntime_c_api.h:40
struct OrtLogger OrtLogger
Definition: onnxruntime_c_api.h:301
struct OrtMapTypeInfo OrtMapTypeInfo
Definition: onnxruntime_c_api.h:285
struct OrtArenaCfg OrtArenaCfg
Definition: onnxruntime_c_api.h:293
ExecutionMode
Definition: onnxruntime_c_api.h:338
OrtOpAttrType
Definition: onnxruntime_c_api.h:256
OrtCustomThreadHandle(* OrtCustomCreateThreadFn)(void *ort_custom_thread_creation_options, OrtThreadWorkerFn ort_thread_worker_fn, void *ort_worker_fn_param)
Ort custom thread creation function.
Definition: onnxruntime_c_api.h:688
ONNXTensorElementDataType
Definition: onnxruntime_c_api.h:176
const OrtApiBase * OrtGetApiBase(void)
The Onnxruntime library's entry point to access the C API.
@ ORT_LOGGING_LEVEL_WARNING
Warning messages.
Definition: onnxruntime_c_api.h:236
@ OrtMemTypeDefault
The default allocator for execution provider.
Definition: onnxruntime_c_api.h:376
void GetAttr(const OrtKernelInfo *p, const char *name, float &)
void GetAttrs(const OrtKernelInfo *p, const char *name, std::vector< float > &)
std::vector< Value > GetOutputValuesHelper(const OrtIoBinding *binding, OrtAllocator *)
std::vector< std::string > GetOutputNamesHelper(const OrtIoBinding *binding, OrtAllocator *)
void OrtRelease(OrtAllocator *ptr)
Definition: onnxruntime_cxx_api.h:287
std::string MakeCustomOpConfigEntryKey(const char *custom_op_name, const char *config)
All C++ Onnxruntime APIs are defined inside this namespace.
Definition: onnxruntime_cxx_api.h:45
std::unique_ptr< char, detail::AllocatedFree > AllocatedStringPtr
unique_ptr typedef used to own strings allocated by OrtAllocators and release them at the end of the ...
Definition: onnxruntime_cxx_api.h:427
const OrtApi & GetApi() noexcept
This returns a reference to the OrtApi interface in use.
Definition: onnxruntime_cxx_api.h:122
std::string GetBuildInfoString()
This function returns the onnxruntime build information: including git branch, git commit id,...
std::string GetVersionString()
This function returns the onnxruntime version string.
std::vector< std::string > GetAvailableProviders()
This is a C++ wrapper for OrtApi::GetAvailableProviders() and returns a vector of strings representin...
Wrapper around OrtAllocator.
Definition: onnxruntime_cxx_api.h:1568
Allocator(const Session &session, const OrtMemoryInfo *)
Allocator(std::nullptr_t)
Convenience to create a class member and then replace with an instance.
Definition: onnxruntime_cxx_api.h:1569
Wrapper around OrtAllocator default instance that is owned by Onnxruntime.
Definition: onnxruntime_cxx_api.h:1560
AllocatorWithDefaultOptions(std::nullptr_t)
Convenience to create a class member and then replace with an instance.
Definition: onnxruntime_cxx_api.h:1561
it is a structure that represents the configuration of an arena based allocator
Definition: onnxruntime_cxx_api.h:1626
ArenaCfg(std::nullptr_t)
Create an empty ArenaCfg object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:1627
ArenaCfg(size_t max_mem, int arena_extend_strategy, int initial_chunk_size_bytes, int max_dead_bytes_per_chunk)
bfloat16 (Brain Floating Point) data type
Definition: onnxruntime_cxx_api.h:202
uint16_t value
Definition: onnxruntime_cxx_api.h:203
constexpr bool operator!=(const BFloat16_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:208
constexpr BFloat16_t(uint16_t v) noexcept
Definition: onnxruntime_cxx_api.h:205
constexpr bool operator==(const BFloat16_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:207
constexpr BFloat16_t() noexcept
Definition: onnxruntime_cxx_api.h:204
Definition: onnxruntime_cxx_api.h:1919
OrtCustomOpInputOutputCharacteristic GetOutputCharacteristic(size_t) const
Definition: onnxruntime_cxx_api.h:1975
OrtCustomOpInputOutputCharacteristic GetInputCharacteristic(size_t) const
Definition: onnxruntime_cxx_api.h:1971
OrtMemType GetInputMemoryType(size_t) const
Definition: onnxruntime_cxx_api.h:1980
std::vector< std::string > GetSessionConfigKeys() const
Definition: onnxruntime_cxx_api.h:2011
bool GetVariadicInputHomogeneity() const
Definition: onnxruntime_cxx_api.h:1992
int GetVariadicInputMinArity() const
Definition: onnxruntime_cxx_api.h:1986
CustomOpBase()
Definition: onnxruntime_cxx_api.h:1920
bool GetVariadicOutputHomogeneity() const
Definition: onnxruntime_cxx_api.h:2004
int GetVariadicOutputMinArity() const
Definition: onnxruntime_cxx_api.h:1998
const char * GetExecutionProviderType() const
Definition: onnxruntime_cxx_api.h:1967
void GetSessionConfigs(std::unordered_map< std::string, std::string > &out, ConstSessionOptions options) const
Class that represents session configuration entries for one or more custom operators.
Definition: onnxruntime_cxx_api.h:568
~CustomOpConfigs()=default
CustomOpConfigs & AddConfig(const char *custom_op_name, const char *config_key, const char *config_value)
Adds a session configuration entry/value for a specific custom operator.
CustomOpConfigs & operator=(CustomOpConfigs &&o)=default
CustomOpConfigs(CustomOpConfigs &&o)=default
CustomOpConfigs()=default
const std::unordered_map< std::string, std::string > & GetFlattenedConfigs() const
Returns a flattened map of custom operator configuration entries and their values.
CustomOpConfigs(const CustomOpConfigs &)=default
CustomOpConfigs & operator=(const CustomOpConfigs &)=default
Custom Op Domain.
Definition: onnxruntime_cxx_api.h:511
CustomOpDomain(std::nullptr_t)
Create an empty CustomOpDomain object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:512
CustomOpDomain(const char *domain)
Wraps OrtApi::CreateCustomOpDomain.
void Add(const OrtCustomOp *op)
Wraps CustomOpDomain_Add.
The Env (Environment)
Definition: onnxruntime_cxx_api.h:479
Env & EnableTelemetryEvents()
Wraps OrtApi::EnableTelemetryEvents.
Env(OrtEnv *p)
C Interop Helper.
Definition: onnxruntime_cxx_api.h:496
Env & CreateAndRegisterAllocatorV2(const std::string &provider_type, const OrtMemoryInfo *mem_info, const std::unordered_map< std::string, std::string > &options, const OrtArenaCfg *arena_cfg)
Wraps OrtApi::CreateAndRegisterAllocatorV2.
Env(std::nullptr_t)
Create an empty Env object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:480
Env(OrtLoggingLevel logging_level=ORT_LOGGING_LEVEL_WARNING, const char *logid="")
Wraps OrtApi::CreateEnv.
Env(const OrtThreadingOptions *tp_options, OrtLoggingLevel logging_level=ORT_LOGGING_LEVEL_WARNING, const char *logid="")
Wraps OrtApi::CreateEnvWithGlobalThreadPools.
Env(const OrtThreadingOptions *tp_options, OrtLoggingFunction logging_function, void *logger_param, OrtLoggingLevel logging_level=ORT_LOGGING_LEVEL_WARNING, const char *logid="")
Wraps OrtApi::CreateEnvWithCustomLoggerAndGlobalThreadPools.
Env(OrtLoggingLevel logging_level, const char *logid, OrtLoggingFunction logging_function, void *logger_param)
Wraps OrtApi::CreateEnvWithCustomLogger.
Env & CreateAndRegisterAllocator(const OrtMemoryInfo *mem_info, const OrtArenaCfg *arena_cfg)
Wraps OrtApi::CreateAndRegisterAllocator.
Env & UpdateEnvWithCustomLogLevel(OrtLoggingLevel log_severity_level)
Wraps OrtApi::UpdateEnvWithCustomLogLevel.
Env & DisableTelemetryEvents()
Wraps OrtApi::DisableTelemetryEvents.
All C++ methods that can fail will throw an exception of this type.
Definition: onnxruntime_cxx_api.h:51
const char * what() const noexcept override
Definition: onnxruntime_cxx_api.h:55
OrtErrorCode GetOrtErrorCode() const
Definition: onnxruntime_cxx_api.h:54
Exception(std::string &&string, OrtErrorCode code)
Definition: onnxruntime_cxx_api.h:52
IEEE 754 half-precision floating point data type.
Definition: onnxruntime_cxx_api.h:183
constexpr bool operator!=(const Float16_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:189
constexpr Float16_t(uint16_t v) noexcept
Definition: onnxruntime_cxx_api.h:186
uint16_t value
Definition: onnxruntime_cxx_api.h:184
constexpr bool operator==(const Float16_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:188
constexpr Float16_t() noexcept
Definition: onnxruntime_cxx_api.h:185
float8e4m3fn (Float8 Floating Point) data type
Definition: onnxruntime_cxx_api.h:218
uint8_t value
Definition: onnxruntime_cxx_api.h:219
constexpr Float8E4M3FN_t(uint8_t v) noexcept
Definition: onnxruntime_cxx_api.h:221
constexpr bool operator==(const Float8E4M3FN_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:224
constexpr Float8E4M3FN_t() noexcept
Definition: onnxruntime_cxx_api.h:220
constexpr bool operator!=(const Float8E4M3FN_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:225
float8e4m3fnuz (Float8 Floating Point) data type
Definition: onnxruntime_cxx_api.h:235
constexpr bool operator==(const Float8E4M3FNUZ_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:241
uint8_t value
Definition: onnxruntime_cxx_api.h:236
constexpr Float8E4M3FNUZ_t() noexcept
Definition: onnxruntime_cxx_api.h:237
constexpr bool operator!=(const Float8E4M3FNUZ_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:242
constexpr Float8E4M3FNUZ_t(uint8_t v) noexcept
Definition: onnxruntime_cxx_api.h:238
float8e5m2 (Float8 Floating Point) data type
Definition: onnxruntime_cxx_api.h:252
constexpr Float8E5M2_t(uint8_t v) noexcept
Definition: onnxruntime_cxx_api.h:255
uint8_t value
Definition: onnxruntime_cxx_api.h:253
constexpr bool operator!=(const Float8E5M2_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:259
constexpr Float8E5M2_t() noexcept
Definition: onnxruntime_cxx_api.h:254
constexpr bool operator==(const Float8E5M2_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:258
float8e5m2fnuz (Float8 Floating Point) data type
Definition: onnxruntime_cxx_api.h:269
constexpr Float8E5M2FNUZ_t() noexcept
Definition: onnxruntime_cxx_api.h:271
constexpr Float8E5M2FNUZ_t(uint8_t v) noexcept
Definition: onnxruntime_cxx_api.h:272
constexpr bool operator!=(const Float8E5M2FNUZ_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:276
constexpr bool operator==(const Float8E5M2FNUZ_t &rhs) const noexcept
Definition: onnxruntime_cxx_api.h:275
uint8_t value
Definition: onnxruntime_cxx_api.h:270
Definition: onnxruntime_cxx_api.h:83
static const OrtApi * api_
Definition: onnxruntime_cxx_api.h:84
Wrapper around OrtIoBinding.
Definition: onnxruntime_cxx_api.h:1615
UnownedIoBinding GetUnowned() const
Definition: onnxruntime_cxx_api.h:1619
ConstIoBinding GetConst() const
Definition: onnxruntime_cxx_api.h:1618
IoBinding(Session &session)
IoBinding(std::nullptr_t)
Create an empty object for convenience. Sometimes, we want to initialize members later.
Definition: onnxruntime_cxx_api.h:1616
This class wraps a raw pointer OrtKernelContext* that is being passed to the custom kernel Compute() ...
Definition: onnxruntime_cxx_api.h:1808
KernelContext(OrtKernelContext *context)
Logger GetLogger() const
ConstValue GetInput(size_t index) const
OrtAllocator * GetAllocator(const OrtMemoryInfo &memory_info) const
void * GetGPUComputeStream() const
size_t GetInputCount() const
size_t GetOutputCount() const
UnownedValue GetOutput(size_t index, const std::vector< int64_t > &dims) const
UnownedValue GetOutput(size_t index, const int64_t *dim_values, size_t dim_count) const
This struct owns the OrtKernInfo* pointer when a copy is made. For convenient wrapping of OrtKernelIn...
Definition: onnxruntime_cxx_api.h:1882
KernelInfo(OrtKernelInfo *info)
Take ownership of the instance.
ConstKernelInfo GetConst() const
Definition: onnxruntime_cxx_api.h:1885
KernelInfo(std::nullptr_t)
Create an empty instance to initialize later.
Definition: onnxruntime_cxx_api.h:1883
This class represents an ONNX Runtime logger that can be used to log information with an associated s...
Definition: onnxruntime_cxx_api.h:1730
Logger(Logger &&v) noexcept=default
Logger & operator=(Logger &&v) noexcept=default
Logger & operator=(const Logger &)=default
~Logger()=default
Logger(const Logger &)=default
Logger()=default
Logger(std::nullptr_t)
Definition: onnxruntime_cxx_api.h:1739
Logger(const OrtLogger *logger)
OrtLoggingLevel GetLoggingSeverityLevel() const noexcept
Wrapper around OrtMapTypeInfo.
Definition: onnxruntime_cxx_api.h:998
ConstMapTypeInfo GetConst() const
Definition: onnxruntime_cxx_api.h:1001
MapTypeInfo(OrtMapTypeInfo *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:1000
MapTypeInfo(std::nullptr_t)
Create an empty MapTypeInfo object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:999
Represents native memory allocation coming from one of the OrtAllocators registered with OnnxRuntime....
Definition: onnxruntime_cxx_api.h:1526
MemoryAllocation(MemoryAllocation &&) noexcept
MemoryAllocation & operator=(const MemoryAllocation &)=delete
MemoryAllocation(const MemoryAllocation &)=delete
MemoryAllocation(OrtAllocator *allocator, void *p, size_t size)
size_t size() const
Definition: onnxruntime_cxx_api.h:1535
Wrapper around OrtMemoryInfo.
Definition: onnxruntime_cxx_api.h:905
MemoryInfo(const char *name, OrtAllocatorType type, int id, OrtMemType mem_type)
MemoryInfo(std::nullptr_t)
No instance is created.
Definition: onnxruntime_cxx_api.h:907
MemoryInfo(OrtMemoryInfo *p)
Take ownership of a pointer created by C Api.
Definition: onnxruntime_cxx_api.h:908
static MemoryInfo CreateCpu(OrtAllocatorType type, OrtMemType mem_type1)
ConstMemoryInfo GetConst() const
Definition: onnxruntime_cxx_api.h:910
Wrapper around OrtModelMetadata.
Definition: onnxruntime_cxx_api.h:703
AllocatedStringPtr GetDescriptionAllocated(OrtAllocator *allocator) const
Returns a copy of the description.
std::vector< AllocatedStringPtr > GetCustomMetadataMapKeysAllocated(OrtAllocator *allocator) const
Returns a vector of copies of the custom metadata keys.
ModelMetadata(std::nullptr_t)
Create an empty ModelMetadata object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:704
AllocatedStringPtr GetGraphDescriptionAllocated(OrtAllocator *allocator) const
Returns a copy of the graph description.
AllocatedStringPtr GetProducerNameAllocated(OrtAllocator *allocator) const
Returns a copy of the producer name.
AllocatedStringPtr GetGraphNameAllocated(OrtAllocator *allocator) const
Returns a copy of the graph name.
AllocatedStringPtr LookupCustomMetadataMapAllocated(const char *key, OrtAllocator *allocator) const
Looks up a value by a key in the Custom Metadata map.
ModelMetadata(OrtModelMetadata *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:705
AllocatedStringPtr GetDomainAllocated(OrtAllocator *allocator) const
Returns a copy of the domain name.
int64_t GetVersion() const
Wraps OrtApi::ModelMetadataGetVersion.
This struct provides life time management for custom op attribute.
Definition: onnxruntime_cxx_api.h:1646
OpAttr(const char *name, const void *data, int len, OrtOpAttrType type)
Create and own custom defined operation.
Definition: onnxruntime_cxx_api.h:1891
Op(OrtOp *)
Take ownership of the OrtOp.
static Op Create(const OrtKernelInfo *info, const char *op_name, const char *domain, int version, const char **type_constraint_names, const ONNXTensorElementDataType *type_constraint_values, size_t type_constraint_count, const OpAttr *attr_values, size_t attr_count, size_t input_count, size_t output_count)
Op(std::nullptr_t)
Create an empty Operator object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:1892
void Invoke(const OrtKernelContext *context, const OrtValue *const *input_values, size_t input_count, OrtValue *const *output_values, size_t output_count)
void Invoke(const OrtKernelContext *context, const Value *input_values, size_t input_count, Value *output_values, size_t output_count)
RunOptions.
Definition: onnxruntime_cxx_api.h:524
int GetRunLogSeverityLevel() const
Wraps OrtApi::RunOptionsGetRunLogSeverityLevel.
RunOptions & SetTerminate()
Terminates all currently executing Session::Run calls that were made using this RunOptions instance.
RunOptions & SetRunTag(const char *run_tag)
wraps OrtApi::RunOptionsSetRunTag
RunOptions & UnsetTerminate()
Clears the terminate flag so this RunOptions instance can be used in a new Session::Run call without ...
int GetRunLogVerbosityLevel() const
Wraps OrtApi::RunOptionsGetRunLogVerbosityLevel.
RunOptions(std::nullptr_t)
Create an empty RunOptions object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:525
RunOptions & SetRunLogVerbosityLevel(int)
Wraps OrtApi::RunOptionsSetRunLogVerbosityLevel.
RunOptions & SetRunLogSeverityLevel(int)
Wraps OrtApi::RunOptionsSetRunLogSeverityLevel.
RunOptions & AddConfigEntry(const char *config_key, const char *config_value)
Wraps OrtApi::AddRunConfigEntry.
const char * GetRunTag() const
Wraps OrtApi::RunOptionsGetRunTag.
RunOptions()
Wraps OrtApi::CreateRunOptions.
Wrapper around OrtSequenceTypeInfo.
Definition: onnxruntime_cxx_api.h:963
SequenceTypeInfo(std::nullptr_t)
Create an empty SequenceTypeInfo object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:964
ConstSequenceTypeInfo GetConst() const
Definition: onnxruntime_cxx_api.h:966
SequenceTypeInfo(OrtSequenceTypeInfo *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:965
Wrapper around OrtSession.
Definition: onnxruntime_cxx_api.h:869
Session(std::nullptr_t)
Create an empty Session object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:870
UnownedSession GetUnowned() const
Definition: onnxruntime_cxx_api.h:879
Session(const Env &env, const char *model_path, const SessionOptions &options, OrtPrepackedWeightsContainer *prepacked_weights_container)
Wraps OrtApi::CreateSessionWithPrepackedWeightsContainer.
Session(const Env &env, const void *model_data, size_t model_data_length, const SessionOptions &options, OrtPrepackedWeightsContainer *prepacked_weights_container)
Wraps OrtApi::CreateSessionFromArrayWithPrepackedWeightsContainer.
Session(const Env &env, const char *model_path, const SessionOptions &options)
Wraps OrtApi::CreateSession.
ConstSession GetConst() const
Definition: onnxruntime_cxx_api.h:878
Session(const Env &env, const void *model_data, size_t model_data_length, const SessionOptions &options)
Wraps OrtApi::CreateSessionFromArray.
Wrapper around OrtSessionOptions.
Definition: onnxruntime_cxx_api.h:692
SessionOptions(std::nullptr_t)
Create an empty SessionOptions object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:693
UnownedSessionOptions GetUnowned() const
Definition: onnxruntime_cxx_api.h:696
SessionOptions()
Wraps OrtApi::CreateSessionOptions.
ConstSessionOptions GetConst() const
Definition: onnxruntime_cxx_api.h:697
SessionOptions(OrtSessionOptions *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:695
The Status that holds ownership of OrtStatus received from C API Use it to safely destroy OrtStatus* ...
Definition: onnxruntime_cxx_api.h:433
OrtErrorCode GetErrorCode() const
Status(const char *message, OrtErrorCode code) noexcept
Creates status instance out of null-terminated string message.
bool IsOK() const noexcept
Returns true if instance represents an OK (non-error) status.
Status(OrtStatus *status) noexcept
Takes ownership of OrtStatus instance returned from the C API.
std::string GetErrorMessage() const
Status(const Exception &) noexcept
Creates status instance out of exception.
Status(const std::exception &) noexcept
Creates status instance out of exception.
Status(std::nullptr_t) noexcept
Create an empty object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:434
Wrapper around OrtTensorTypeAndShapeInfo.
Definition: onnxruntime_cxx_api.h:942
TensorTypeAndShapeInfo(std::nullptr_t)
Create an empty TensorTypeAndShapeInfo object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:943
ConstTensorTypeAndShapeInfo GetConst() const
Definition: onnxruntime_cxx_api.h:945
TensorTypeAndShapeInfo(OrtTensorTypeAndShapeInfo *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:944
The ThreadingOptions.
Definition: onnxruntime_cxx_api.h:448
ThreadingOptions & SetGlobalCustomThreadCreationOptions(void *ort_custom_thread_creation_options)
Wraps OrtApi::SetGlobalCustomThreadCreationOptions.
ThreadingOptions()
Wraps OrtApi::CreateThreadingOptions.
ThreadingOptions & SetGlobalInterOpNumThreads(int inter_op_num_threads)
Wraps OrtApi::SetGlobalInterOpNumThreads.
ThreadingOptions & SetGlobalCustomCreateThreadFn(OrtCustomCreateThreadFn ort_custom_create_thread_fn)
Wraps OrtApi::SetGlobalCustomCreateThreadFn.
ThreadingOptions & SetGlobalCustomJoinThreadFn(OrtCustomJoinThreadFn ort_custom_join_thread_fn)
Wraps OrtApi::SetGlobalCustomJoinThreadFn.
ThreadingOptions & SetGlobalSpinControl(int allow_spinning)
Wraps OrtApi::SetGlobalSpinControl.
ThreadingOptions & SetGlobalDenormalAsZero()
Wraps OrtApi::SetGlobalDenormalAsZero.
ThreadingOptions & SetGlobalIntraOpNumThreads(int intra_op_num_threads)
Wraps OrtApi::SetGlobalIntraOpNumThreads.
Type information that may contain either TensorTypeAndShapeInfo or the information about contained se...
Definition: onnxruntime_cxx_api.h:1029
TypeInfo(std::nullptr_t)
Create an empty TypeInfo object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:1030
ConstTypeInfo GetConst() const
Definition: onnxruntime_cxx_api.h:1033
TypeInfo(OrtTypeInfo *p)
C API Interop.
Definition: onnxruntime_cxx_api.h:1031
Wrapper around OrtValue.
Definition: onnxruntime_cxx_api.h:1365
static Value CreateSparseTensor(const OrtMemoryInfo *info, void *p_data, const Shape &dense_shape, const Shape &values_shape, ONNXTensorElementDataType type)
Creates an OrtValue instance containing SparseTensor. This constructs a sparse tensor that makes use ...
static Value CreateSparseTensor(const OrtMemoryInfo *info, T *p_data, const Shape &dense_shape, const Shape &values_shape)
This is a simple forwarding method to the other overload that helps deducing data type enum value fro...
Value & operator=(Value &&)=default
static Value CreateSparseTensor(OrtAllocator *allocator, const Shape &dense_shape, ONNXTensorElementDataType type)
Creates an instance of OrtValue containing sparse tensor. The created instance has no data....
Value(Value &&)=default
Value(std::nullptr_t)
Create an empty Value object, must be assigned a valid one to be used.
Definition: onnxruntime_cxx_api.h:1370
static Value CreateTensor(const OrtMemoryInfo *info, T *p_data, size_t p_data_element_count, const int64_t *shape, size_t shape_len)
Creates a tensor with a user supplied buffer. Wraps OrtApi::CreateTensorWithDataAsOrtValue.
Value(OrtValue *p)
Used for interop with the C API.
Definition: onnxruntime_cxx_api.h:1371
static Value CreateSparseTensor(OrtAllocator *allocator, const Shape &dense_shape)
This is a simple forwarding method to the below CreateSparseTensor. This helps to specify data type e...
static Value CreateTensor(OrtAllocator *allocator, const int64_t *shape, size_t shape_len, ONNXTensorElementDataType type)
Creates an OrtValue with a tensor using the supplied OrtAllocator. Wraps OrtApi::CreateTensorAsOrtVal...
UnownedValue GetUnowned() const
Definition: onnxruntime_cxx_api.h:1376
static Value CreateSequence(const std::vector< Value > &values)
Creates an OrtValue with a Sequence Onnx type representation. The API would ref-count the supplied Or...
static Value CreateMap(const Value &keys, const Value &values)
Creates an OrtValue with a Map Onnx type representation. The API would ref-count the supplied OrtValu...
static Value CreateTensor(const OrtMemoryInfo *info, void *p_data, size_t p_data_byte_count, const int64_t *shape, size_t shape_len, ONNXTensorElementDataType type)
Creates a tensor with a user supplied buffer. Wraps OrtApi::CreateTensorWithDataAsOrtValue.
static Value CreateTensor(OrtAllocator *allocator, const int64_t *shape, size_t shape_len)
Creates an OrtValue with a tensor using a supplied OrtAllocator. Wraps OrtApi::CreateTensorAsOrtValue...
static Value CreateOpaque(const char *domain, const char *type_name, const T &value)
Creates an OrtValue wrapping an Opaque type. This is used for experimental support of non-tensor type...
ConstValue GetConst() const
Definition: onnxruntime_cxx_api.h:1375
Definition: onnxruntime_cxx_api.h:406
AllocatedFree(OrtAllocator *allocator)
Definition: onnxruntime_cxx_api.h:408
OrtAllocator * allocator_
Definition: onnxruntime_cxx_api.h:407
void operator()(void *ptr) const
Definition: onnxruntime_cxx_api.h:410
Definition: onnxruntime_cxx_api.h:1545
ConstMemoryInfo GetInfo() const
void * Alloc(size_t size)
MemoryAllocation GetAllocation(size_t size)
Base & operator=(Base &&v) noexcept
Definition: onnxruntime_cxx_api.h:393
typename Unowned< T >::Type contained_type
Definition: onnxruntime_cxx_api.h:382
Base(Base &&v) noexcept
Definition: onnxruntime_cxx_api.h:392
Base(const Base &)=default
constexpr Base(contained_type *p) noexcept
Definition: onnxruntime_cxx_api.h:385
Base & operator=(const Base &)=default
Used internally by the C++ API. C++ wrapper types inherit from this. This is a zero cost abstraction ...
Definition: onnxruntime_cxx_api.h:338
Base(Base &&v) noexcept
Definition: onnxruntime_cxx_api.h:348
constexpr Base()=default
contained_type * release()
Relinquishes ownership of the contained C object pointer The underlying object is not destroyed.
Definition: onnxruntime_cxx_api.h:359
Base(const Base &)=delete
constexpr Base(contained_type *p) noexcept
Definition: onnxruntime_cxx_api.h:342
Base & operator=(const Base &)=delete
Base & operator=(Base &&v) noexcept
Definition: onnxruntime_cxx_api.h:349
contained_type * p_
Definition: onnxruntime_cxx_api.h:366
~Base()
Definition: onnxruntime_cxx_api.h:343
T contained_type
Definition: onnxruntime_cxx_api.h:339
Definition: onnxruntime_cxx_api.h:1583
std::vector< Value > GetOutputValues(OrtAllocator *) const
std::vector< std::string > GetOutputNames(OrtAllocator *) const
std::vector< Value > GetOutputValues() const
std::vector< std::string > GetOutputNames() const
Definition: onnxruntime_cxx_api.h:776
TypeInfo GetInputTypeInfo(size_t index) const
Wraps OrtApi::SessionGetInputTypeInfo.
size_t GetOutputCount() const
Returns the number of model outputs.
uint64_t GetProfilingStartTimeNs() const
Wraps OrtApi::SessionGetProfilingStartTimeNs.
ModelMetadata GetModelMetadata() const
Wraps OrtApi::SessionGetModelMetadata.
size_t GetInputCount() const
Returns the number of model inputs.
TypeInfo GetOutputTypeInfo(size_t index) const
Wraps OrtApi::SessionGetOutputTypeInfo.
AllocatedStringPtr GetOverridableInitializerNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of the overridable initializer name at then specified index.
AllocatedStringPtr GetOutputNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of output name at then specified index.
size_t GetOverridableInitializerCount() const
Returns the number of inputs that have defaults that can be overridden.
AllocatedStringPtr GetInputNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of input name at the specified index.
TypeInfo GetOverridableInitializerTypeInfo(size_t index) const
Wraps OrtApi::SessionGetOverridableInitializerTypeInfo.
Definition: onnxruntime_cxx_api.h:611
std::string GetConfigEntry(const char *config_key) const
Wraps OrtApi::GetSessionConfigEntry.
std::string GetConfigEntryOrDefault(const char *config_key, const std::string &def)
SessionOptions Clone() const
Creates and returns a copy of this SessionOptions object. Wraps OrtApi::CloneSessionOptions.
bool HasConfigEntry(const char *config_key) const
Wraps OrtApi::HasSessionConfigEntry.
Definition: onnxruntime_cxx_api.h:1062
void GetStringTensorContent(void *buffer, size_t buffer_length, size_t *offsets, size_t offsets_count) const
The API copies all of the UTF-8 encoded string data contained within a tensor or a sparse tensor into...
void GetStringTensorElement(size_t buffer_length, size_t element_index, void *buffer) const
The API copies UTF-8 encoded bytes for the requested string element contained within a tensor or a sp...
TensorTypeAndShapeInfo GetSparseTensorIndicesTypeShapeInfo(OrtSparseIndicesFormat format) const
The API returns type and shape information for the specified indices. Each supported indices have the...
const void * GetTensorRawData() const
Returns a non-typed pointer to a tensor contained data.
std::string GetStringTensorElement(size_t element_index) const
Returns string tensor UTF-8 encoded string element. Use of this API is recommended over GetStringTens...
size_t GetStringTensorElementLength(size_t element_index) const
The API returns a byte length of UTF-8 encoded string element contained in either a tensor or a spare...
size_t GetStringTensorDataLength() const
This API returns a full length of string data contained within either a tensor or a sparse Tensor....
bool IsSparseTensor() const
Returns true if the OrtValue contains a sparse tensor.
TypeInfo GetTypeInfo() const
The API returns type information for data contained in a tensor. For sparse tensors it returns type i...
const R * GetSparseTensorIndicesData(OrtSparseIndicesFormat indices_format, size_t &num_indices) const
The API retrieves a pointer to the internal indices buffer. The API merely performs a convenience dat...
bool IsTensor() const
Returns true if Value is a tensor, false for other types like map/sequence/etc.
ConstMemoryInfo GetTensorMemoryInfo() const
This API returns information about the memory allocation used to hold data.
const R * GetSparseTensorValues() const
The API returns a pointer to an internal buffer of the sparse tensor containing non-zero values....
TensorTypeAndShapeInfo GetTensorTypeAndShapeInfo() const
The API returns type information for data contained in a tensor. For sparse tensors it returns type i...
Value GetValue(int index, OrtAllocator *allocator) const
size_t GetCount() const
< Return true if OrtValue contains data and returns false if the OrtValue is a None
void GetOpaqueData(const char *domain, const char *type_name, R &) const
Obtains a pointer to a user defined data for experimental purposes.
TensorTypeAndShapeInfo GetSparseTensorValuesTypeAndShapeInfo() const
The API returns type and shape information for stored non-zero values of the sparse tensor....
const R * GetTensorData() const
Returns a const typed pointer to the tensor contained data. No type checking is performed,...
OrtSparseFormat GetSparseFormat() const
The API returns the sparse data format this OrtValue holds in a sparse tensor. If the sparse tensor w...
Definition: onnxruntime_cxx_api.h:1594
void BindOutput(const char *name, const Value &)
void BindInput(const char *name, const Value &)
void BindOutput(const char *name, const OrtMemoryInfo *)
Definition: onnxruntime_cxx_api.h:1835
Value GetTensorAttribute(const char *name, OrtAllocator *allocator) const
TypeInfo GetInputTypeInfo(size_t index) const
std::vector< R > GetAttributes(const char *name) const
Definition: onnxruntime_cxx_api.h:1849
R GetAttribute(const char *name) const
Definition: onnxruntime_cxx_api.h:1842
TypeInfo GetOutputTypeInfo(size_t index) const
KernelInfo Copy() const
std::string GetNodeName() const
std::string GetInputName(size_t index) const
size_t GetOutputCount() const
size_t GetInputCount() const
ConstValue GetTensorConstantInput(size_t index, int *is_constant) const
std::string GetOutputName(size_t index) const
Definition: onnxruntime_cxx_api.h:984
ONNXTensorElementDataType GetMapKeyType() const
Wraps OrtApi::GetMapKeyType.
TypeInfo GetMapValueType() const
Wraps OrtApi::GetMapValueType.
Definition: onnxruntime_cxx_api.h:884
std::string GetAllocatorName() const
OrtMemType GetMemoryType() const
OrtMemoryInfoDeviceType GetDeviceType() const
OrtAllocatorType GetAllocatorType() const
bool operator==(const MemoryInfoImpl< U > &o) const
Definition: onnxruntime_cxx_api.h:971
TypeInfo GetOptionalElementType() const
Wraps OrtApi::CastOptionalTypeToContainedTypeInfo.
Definition: onnxruntime_cxx_api.h:1045
const char ** str
Definition: onnxruntime_cxx_api.h:1050
const int64_t * values_shape
Definition: onnxruntime_cxx_api.h:1046
size_t values_shape_len
Definition: onnxruntime_cxx_api.h:1047
const void * p_data
Definition: onnxruntime_cxx_api.h:1049
Definition: onnxruntime_cxx_api.h:950
TypeInfo GetSequenceElementType() const
Wraps OrtApi::GetSequenceElementType.
Definition: onnxruntime_cxx_api.h:820
AllocatedStringPtr EndProfilingAllocated(OrtAllocator *allocator)
End profiling and return a copy of the profiling file name.
void Run(const RunOptions &run_options, const IoBinding &)
Wraps OrtApi::RunWithBinding.
std::vector< Value > Run(const RunOptions &run_options, const char *const *input_names, const Value *input_values, size_t input_count, const char *const *output_names, size_t output_count)
Run the model returning results in an Ort allocated vector.
void Run(const RunOptions &run_options, const char *const *input_names, const Value *input_values, size_t input_count, const char *const *output_names, Value *output_values, size_t output_count)
Run the model returning results in user provided outputs Same as Run(const RunOptions&,...
Definition: onnxruntime_cxx_api.h:623
SessionOptionsImpl & DisableMemPattern()
Wraps OrtApi::DisableMemPattern.
SessionOptionsImpl & SetCustomJoinThreadFn(OrtCustomJoinThreadFn ort_custom_join_thread_fn)
Wraps OrtApi::SessionOptionsSetCustomJoinThreadFn.
SessionOptionsImpl & SetLogSeverityLevel(int level)
Wraps OrtApi::SetSessionLogSeverityLevel.
SessionOptionsImpl & AppendExecutionProvider(const std::string &provider_name, const std::unordered_map< std::string, std::string > &provider_options={})
Wraps OrtApi::SessionOptionsAppendExecutionProvider. Currently supports QNN, SNPE and XNNPACK.
SessionOptionsImpl & EnableOrtCustomOps()
Wraps OrtApi::EnableOrtCustomOps.
SessionOptionsImpl & SetCustomCreateThreadFn(OrtCustomCreateThreadFn ort_custom_create_thread_fn)
Wraps OrtApi::SessionOptionsSetCustomCreateThreadFn.
SessionOptionsImpl & AppendExecutionProvider_CANN(const OrtCANNProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_Dnnl.
SessionOptionsImpl & SetGraphOptimizationLevel(GraphOptimizationLevel graph_optimization_level)
Wraps OrtApi::SetSessionGraphOptimizationLevel.
SessionOptionsImpl & AppendExecutionProvider_MIGraphX(const OrtMIGraphXProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_CANN.
SessionOptionsImpl & DisableCpuMemArena()
Wraps OrtApi::DisableCpuMemArena.
SessionOptionsImpl & Add(OrtCustomOpDomain *custom_op_domain)
Wraps OrtApi::AddCustomOpDomain.
SessionOptionsImpl & AddConfigEntry(const char *config_key, const char *config_value)
Wraps OrtApi::AddSessionConfigEntry.
SessionOptionsImpl & EnableMemPattern()
Wraps OrtApi::EnableMemPattern.
SessionOptionsImpl & AppendExecutionProvider_Dnnl(const OrtDnnlProviderOptions &provider_options)
SessionOptionsImpl & SetCustomThreadCreationOptions(void *ort_custom_thread_creation_options)
Wraps OrtApi::SessionOptionsSetCustomThreadCreationOptions.
SessionOptionsImpl & AddExternalInitializers(const std::vector< std::string > &names, const std::vector< Value > &ort_values)
Wraps OrtApi::AddExternalInitializers.
SessionOptionsImpl & SetLogId(const char *logid)
Wraps OrtApi::SetSessionLogId.
SessionOptionsImpl & AppendExecutionProvider_CUDA_V2(const OrtCUDAProviderOptionsV2 &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_CUDA_V2.
SessionOptionsImpl & SetExecutionMode(ExecutionMode execution_mode)
Wraps OrtApi::SetSessionExecutionMode.
SessionOptionsImpl & DisablePerSessionThreads()
Wraps OrtApi::DisablePerSessionThreads.
SessionOptionsImpl & RegisterCustomOpsLibrary(const char *library_name, const CustomOpConfigs &custom_op_configs={})
SessionOptionsImpl & AppendExecutionProvider_TensorRT_V2(const OrtTensorRTProviderOptionsV2 &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_TensorRT.
SessionOptionsImpl & RegisterCustomOpsUsingFunction(const char *function_name)
Wraps OrtApi::RegisterCustomOpsUsingFunction.
SessionOptionsImpl & DisableProfiling()
Wraps OrtApi::DisableProfiling.
SessionOptionsImpl & SetIntraOpNumThreads(int intra_op_num_threads)
Wraps OrtApi::SetIntraOpNumThreads.
SessionOptionsImpl & AppendExecutionProvider_ROCM(const OrtROCMProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_ROCM.
SessionOptionsImpl & AppendExecutionProvider_OpenVINO(const OrtOpenVINOProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_OpenVINO.
SessionOptionsImpl & EnableCpuMemArena()
Wraps OrtApi::EnableCpuMemArena.
SessionOptionsImpl & AddInitializer(const char *name, const OrtValue *ort_val)
Wraps OrtApi::AddInitializer.
SessionOptionsImpl & SetInterOpNumThreads(int inter_op_num_threads)
Wraps OrtApi::SetInterOpNumThreads.
SessionOptionsImpl & EnableProfiling(const char *profile_file_prefix)
Wraps OrtApi::EnableProfiling.
SessionOptionsImpl & SetOptimizedModelFilePath(const char *optimized_model_file)
Wraps OrtApi::SetOptimizedModelFilePath.
SessionOptionsImpl & AppendExecutionProvider_TensorRT(const OrtTensorRTProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_TensorRT.
SessionOptionsImpl & AppendExecutionProvider_CUDA(const OrtCUDAProviderOptions &provider_options)
Wraps OrtApi::SessionOptionsAppendExecutionProvider_CUDA.
Definition: onnxruntime_cxx_api.h:1056
const int64_t * shape
Definition: onnxruntime_cxx_api.h:1057
size_t shape_len
Definition: onnxruntime_cxx_api.h:1058
Definition: onnxruntime_cxx_api.h:915
size_t GetElementCount() const
Wraps OrtApi::GetTensorShapeElementCount.
void GetDimensions(int64_t *values, size_t values_count) const
Wraps OrtApi::GetDimensions.
std::vector< int64_t > GetShape() const
Uses GetDimensionsCount & GetDimensions to return a std::vector of the shape.
void GetSymbolicDimensions(const char **values, size_t values_count) const
Wraps OrtApi::GetSymbolicDimensions.
size_t GetDimensionsCount() const
Wraps OrtApi::GetDimensionsCount.
ONNXTensorElementDataType GetElementType() const
Wraps OrtApi::GetTensorElementType.
Definition: onnxruntime_cxx_api.h:1006
ONNXType GetONNXType() const
ConstSequenceTypeInfo GetSequenceTypeInfo() const
Wraps OrtApi::CastTypeInfoToSequenceTypeInfo.
ConstMapTypeInfo GetMapTypeInfo() const
Wraps OrtApi::CastTypeInfoToMapTypeInfo.
ConstOptionalTypeInfo GetOptionalTypeInfo() const
wraps OrtApi::CastTypeInfoToOptionalTypeInfo
ConstTensorTypeAndShapeInfo GetTensorTypeAndShapeInfo() const
Wraps OrtApi::CastTypeInfoToTensorInfo.
This is a tagging template type. Use it with Base<T> to indicate that the C++ interface object has no...
Definition: onnxruntime_cxx_api.h:314
T Type
Definition: onnxruntime_cxx_api.h:315
Definition: onnxruntime_cxx_api.h:1223
void FillStringTensorElement(const char *s, size_t index)
Set a single string in a string tensor.
R * GetTensorMutableData()
Returns a non-const typed pointer to an OrtValue/Tensor contained buffer No type checking is performe...
R & At(const std::vector< int64_t > &location)
void UseBlockSparseIndices(const Shape &indices_shape, int32_t *indices_data)
Supplies BlockSparse format specific indices and marks the contained sparse tensor as being a BlockSp...
void FillSparseTensorBlockSparse(const OrtMemoryInfo *data_mem_info, const OrtSparseValuesParam &values, const Shape &indices_shape, const int32_t *indices_data)
The API will allocate memory using the allocator instance supplied to the CreateSparseTensor() API an...
void * GetTensorMutableRawData()
Returns a non-typed non-const pointer to a tensor contained data.
void UseCooIndices(int64_t *indices_data, size_t indices_num)
Supplies COO format specific indices and marks the contained sparse tensor as being a COO format tens...
void FillSparseTensorCoo(const OrtMemoryInfo *data_mem_info, const OrtSparseValuesParam &values_param, const int64_t *indices_data, size_t indices_num)
The API will allocate memory using the allocator instance supplied to the CreateSparseTensor() API an...
void FillStringTensor(const char *const *s, size_t s_len)
Set all strings at once in a string tensor.
void UseCsrIndices(int64_t *inner_data, size_t inner_num, int64_t *outer_data, size_t outer_num)
Supplies CSR format specific indices and marks the contained sparse tensor as being a CSR format tens...
void FillSparseTensorCsr(const OrtMemoryInfo *data_mem_info, const OrtSparseValuesParam &values, const int64_t *inner_indices_data, size_t inner_indices_num, const int64_t *outer_indices_data, size_t outer_indices_num)
The API will allocate memory using the allocator instance supplied to the CreateSparseTensor() API an...
char * GetResizedStringTensorElementBuffer(size_t index, size_t buffer_length)
Allocate if necessary and obtain a pointer to a UTF-8 encoded string element buffer indexed by the fl...
Memory allocation interface.
Definition: onnxruntime_c_api.h:315
void(* Free)(struct OrtAllocator *this_, void *p)
Free a block of memory previously allocated with OrtAllocator::Alloc.
Definition: onnxruntime_c_api.h:318
const OrtApi *(* GetApi)(uint32_t version)
Get a pointer to the requested version of the OrtApi.
Definition: onnxruntime_c_api.h:655
The C API.
Definition: onnxruntime_c_api.h:706
CUDA Provider Options.
Definition: onnxruntime_c_api.h:399
Definition: onnxruntime_c_api.h:4346
int(* GetVariadicInputHomogeneity)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4392
OrtCustomOpInputOutputCharacteristic(* GetOutputCharacteristic)(const struct OrtCustomOp *op, size_t index)
Definition: onnxruntime_c_api.h:4376
size_t(* GetInputTypeCount)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4364
int(* GetVariadicOutputMinArity)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4396
const char *(* GetName)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4357
size_t(* GetOutputTypeCount)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4366
void(* KernelDestroy)(void *op_kernel)
Definition: onnxruntime_c_api.h:4372
int(* GetVariadicOutputHomogeneity)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4401
OrtMemType(* GetInputMemoryType)(const struct OrtCustomOp *op, size_t index)
Definition: onnxruntime_c_api.h:4383
void *(* CreateKernel)(const struct OrtCustomOp *op, const OrtApi *api, const OrtKernelInfo *info)
Definition: onnxruntime_c_api.h:4353
uint32_t version
Definition: onnxruntime_c_api.h:4347
ONNXTensorElementDataType(* GetInputType)(const struct OrtCustomOp *op, size_t index)
Definition: onnxruntime_c_api.h:4363
OrtCustomOpInputOutputCharacteristic(* GetInputCharacteristic)(const struct OrtCustomOp *op, size_t index)
Definition: onnxruntime_c_api.h:4375
const char *(* GetExecutionProviderType)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4360
ONNXTensorElementDataType(* GetOutputType)(const struct OrtCustomOp *op, size_t index)
Definition: onnxruntime_c_api.h:4365
int(* GetVariadicInputMinArity)(const struct OrtCustomOp *op)
Definition: onnxruntime_c_api.h:4387
OrtStatusPtr(* CreateKernelV2)(const struct OrtCustomOp *op, const OrtApi *api, const OrtKernelInfo *info, void **kernel)
Definition: onnxruntime_c_api.h:4404
OrtStatusPtr(* KernelComputeV2)(void *op_kernel, OrtKernelContext *context)
Definition: onnxruntime_c_api.h:4409
void(* KernelCompute)(void *op_kernel, OrtKernelContext *context)
Definition: onnxruntime_c_api.h:4371
MIGraphX Provider Options.
Definition: onnxruntime_c_api.h:600
OpenVINO Provider Options.
Definition: onnxruntime_c_api.h:610
ROCM Provider Options.
Definition: onnxruntime_c_api.h:486
TensorRT Provider Options.
Definition: onnxruntime_c_api.h:572