HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
experimental_onnxruntime_cxx_inline.h
Go to the documentation of this file.
1 // Copyright (c) Microsoft Corporation. All rights reserved.
2 // Licensed under the MIT License.
3 
4 // Do not include this file directly. Please include "experimental_onnxruntime_cxx_api.h" instead.
5 //
6 // These are the inline implementations of the C++ header APIs. They are in this separate file as to not clutter
7 // the main C++ file with implementation details.
8 namespace Ort {
9 namespace Experimental {
10 
11 inline std::vector<Ort::Value> Session::Run(const std::vector<std::string>& input_names, const std::vector<Ort::Value>& input_values,
12  const std::vector<std::string>& output_names, const RunOptions& run_options) {
13  size_t output_names_count = GetOutputNames().size();
14  std::vector<Ort::Value> output_values;
15  for (size_t i = 0; i < output_names_count; i++) output_values.emplace_back(nullptr);
16  Run(input_names, input_values, output_names, output_values, run_options);
17  return output_values;
18 }
19 
20 inline void Session::Run(const std::vector<std::string>& input_names, const std::vector<Ort::Value>& input_values,
21  const std::vector<std::string>& output_names, std::vector<Ort::Value>& output_values, const RunOptions& run_options) {
22  size_t input_names_count = input_names.size();
23  size_t output_names_count = output_names.size();
24  std::vector<const char*> input_names_(input_names_count, nullptr);
25  size_t i = 0;
26  for (auto it = input_names.begin(); it != input_names.end(); it++) input_names_[i++] = (*it).c_str();
27  std::vector<const char*> output_names_(output_names_count, nullptr);
28  i = 0;
29  for (auto it = output_names.begin(); it != output_names.end(); it++) output_names_[i++] = (*it).c_str();
30  Ort::Session::Run(run_options, input_names_.data(), input_values.data(), input_names_count, output_names_.data(), output_values.data(), output_names_count);
31 }
32 
33 inline std::vector<std::string> Session::GetInputNames() const {
35  size_t node_count = GetInputCount();
36  std::vector<std::string> out(node_count);
37  for (size_t i = 0; i < node_count; i++) {
38  auto tmp = GetInputNameAllocated(i, allocator);
39  out[i] = tmp.get();
40  }
41  return out;
42 }
43 
44 inline std::vector<std::string> Session::GetOutputNames() const {
46  size_t node_count = GetOutputCount();
47  std::vector<std::string> out(node_count);
48  for (size_t i = 0; i < node_count; i++) {
49  auto tmp = GetOutputNameAllocated(i, allocator);
50  out[i] = tmp.get();
51  }
52  return out;
53 }
54 
55 inline std::vector<std::string> Session::GetOverridableInitializerNames() const {
57  size_t init_count = GetOverridableInitializerCount();
58  std::vector<std::string> out(init_count);
59  for (size_t i = 0; i < init_count; i++) {
60  auto tmp = GetOverridableInitializerNameAllocated(i, allocator);
61  out[i] = tmp.get();
62  }
63  return out;
64 }
65 
66 inline std::vector<std::vector<int64_t>> Session::GetInputShapes() const {
67  size_t node_count = GetInputCount();
68  std::vector<std::vector<int64_t>> out(node_count);
69  for (size_t i = 0; i < node_count; i++) out[i] = GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape();
70  return out;
71 }
72 
73 inline std::vector<std::vector<int64_t>> Session::GetOutputShapes() const {
74  size_t node_count = GetOutputCount();
75  std::vector<std::vector<int64_t>> out(node_count);
76  for (size_t i = 0; i < node_count; i++) out[i] = GetOutputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape();
77  return out;
78 }
79 
80 inline std::vector<std::vector<int64_t>> Session::GetOverridableInitializerShapes() const {
81  size_t init_count = GetOverridableInitializerCount();
82  std::vector<std::vector<int64_t>> out(init_count);
83  for (size_t i = 0; i < init_count; i++) out[i] = GetOverridableInitializerTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape();
84  return out;
85 }
86 
87 template <typename T>
88 inline Ort::Value Value::CreateTensor(T* p_data, size_t p_data_element_count, const std::vector<int64_t>& shape) {
89  return CreateTensor(p_data, p_data_element_count * sizeof(T), shape, TypeToTensorType<T>::type);
90 }
91 
92 inline Ort::Value Value::CreateTensor(void* p_data, size_t p_data_byte_count, const std::vector<int64_t>& shape, ONNXTensorElementDataType type) {
93  Ort::MemoryInfo info = Ort::MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
94  return Ort::Value::CreateTensor(info, p_data, p_data_byte_count, shape.data(), shape.size(), type);
95 }
96 
97 template <typename T>
98 inline Ort::Value Value::CreateTensor(const std::vector<int64_t>& shape) {
100 }
101 
102 inline Ort::Value Value::CreateTensor(const std::vector<int64_t>& shape, ONNXTensorElementDataType type) {
104  return Ort::Value::CreateTensor(allocator, shape.data(), shape.size(), type);
105 }
106 
107 } // namespace Experimental
108 } // namespace Ort
AllocatedStringPtr GetOverridableInitializerNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of the overridable initializer name at then specified index.
size_t GetInputCount() const
Returns the number of model inputs.
std::vector< std::vector< int64_t > > GetInputShapes() const
TypeInfo GetInputTypeInfo(size_t index) const
Wraps OrtApi::SessionGetInputTypeInfo.
static MemoryInfo CreateCpu(OrtAllocatorType type, OrtMemType mem_type1)
AllocatedStringPtr GetOutputNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of output name at then specified index.
std::vector< std::vector< int64_t > > GetOverridableInitializerShapes() const
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.
Wrapper around OrtValue.
static Ort::Value CreateTensor(T *p_data, size_t p_data_element_count, const std::vector< int64_t > &shape)
std::vector< std::string > GetInputNames() const
std::vector< Ort::Value > Run(const std::vector< std::string > &input_names, const std::vector< Ort::Value > &input_values, const std::vector< std::string > &output_names, const RunOptions &run_options=RunOptions())
std::vector< int64_t > GetShape() const
Uses GetDimensionsCount & GetDimensions to return a std::vector of the shape.
Wrapper around OrtMemoryInfo.
std::vector< std::string > GetOutputNames() const
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.
std::vector< std::vector< int64_t > > GetOutputShapes() const
std::vector< std::string > GetOverridableInitializerNames() const
TypeInfo GetOutputTypeInfo(size_t index) const
Wraps OrtApi::SessionGetOutputTypeInfo.
ConstTensorTypeAndShapeInfo GetTensorTypeAndShapeInfo() const
Wraps OrtApi::CastTypeInfoToTensorInfo.
TypeInfo GetOverridableInitializerTypeInfo(size_t index) const
Wraps OrtApi::SessionGetOverridableInitializerTypeInfo.
size_t GetOverridableInitializerCount() const
Returns the number of inputs that have defaults that can be overridden.
size_t GetOutputCount() const
Returns the number of model outputs.
AllocatedStringPtr GetInputNameAllocated(size_t index, OrtAllocator *allocator) const
Returns a copy of input name at the specified index.
type
Definition: core.h:1059
Wrapper around ::OrtAllocator default instance that is owned by Onnxruntime.