CMS 3D CMS Logo

List of all members | Public Member Functions | Static Public Member Functions | Private Attributes | Static Private Attributes
cms::Ort::ONNXRuntime Class Reference

#include <ONNXRuntime.h>

Public Member Functions

const std::vector< std::string > & getOutputNames () const
 
const std::vector< int64_t > & getOutputShape (const std::string &output_name) const
 
 ONNXRuntime (const std::string &model_path, const ::Ort::SessionOptions *session_options=nullptr)
 
 ONNXRuntime (const ONNXRuntime &)=delete
 
ONNXRuntimeoperator= (const ONNXRuntime &)=delete
 
FloatArrays run (const std::vector< std::string > &input_names, FloatArrays &input_values, const std::vector< std::vector< int64_t >> &input_shapes={}, const std::vector< std::string > &output_names={}, int64_t batch_size=1) const
 
 ~ONNXRuntime ()
 

Static Public Member Functions

::Ort::SessionOptions defaultSessionOptions (Backend backend=Backend::cpu)
 

Private Attributes

std::map< std::string, std::vector< int64_t > > input_node_dims_
 
std::vector< const char * > input_node_names_
 
std::vector< std::string > input_node_strings_
 
std::map< std::string, std::vector< int64_t > > output_node_dims_
 
std::vector< const char * > output_node_names_
 
std::vector< std::string > output_node_strings_
 
std::unique_ptr<::Ort::Session > session_
 

Static Private Attributes

static const ::Ort::Env env_
 

Detailed Description

Definition at line 30 of file ONNXRuntime.h.

Constructor & Destructor Documentation

◆ ONNXRuntime() [1/2]

cms::Ort::ONNXRuntime::ONNXRuntime ( const std::string &  model_path,
const ::Ort::SessionOptions *  session_options = nullptr 
)

◆ ONNXRuntime() [2/2]

cms::Ort::ONNXRuntime::ONNXRuntime ( const ONNXRuntime )
delete

◆ ~ONNXRuntime()

cms::Ort::ONNXRuntime::~ONNXRuntime ( )

Definition at line 74 of file ONNXRuntime.cc.

74 {}

Member Function Documentation

◆ defaultSessionOptions()

SessionOptions cms::Ort::ONNXRuntime::defaultSessionOptions ( Backend  backend = Backend::cpu)
static

Definition at line 76 of file ONNXRuntime.cc.

References jetsAK4_CHS_cff::backend, cms::Ort::cuda, and AlcaSiPixelAliHarvester0T_cff::options.

Referenced by BaseMVACache::BaseMVACache(), and OnlineDQMDigiAD::OnlineDQMDigiAD().

76  {
77  SessionOptions sess_opts;
78  sess_opts.SetIntraOpNumThreads(1);
79  if (backend == Backend::cuda) {
80  // https://www.onnxruntime.ai/docs/reference/execution-providers/CUDA-ExecutionProvider.html
81  OrtCUDAProviderOptions options;
82  sess_opts.AppendExecutionProvider_CUDA(options);
83  }
84  return sess_opts;
85  }

◆ getOutputNames()

const std::vector< std::string > & cms::Ort::ONNXRuntime::getOutputNames ( ) const

Definition at line 164 of file ONNXRuntime.cc.

References Exception, output_node_strings_, and session_.

164  {
165  if (session_) {
166  return output_node_strings_;
167  } else {
168  throw cms::Exception("RuntimeError") << "Needs to call createSession() first before getting the output names!";
169  }
170  }
std::unique_ptr<::Ort::Session > session_
Definition: ONNXRuntime.h:62
std::vector< std::string > output_node_strings_
Definition: ONNXRuntime.h:68

◆ getOutputShape()

const std::vector< int64_t > & cms::Ort::ONNXRuntime::getOutputShape ( const std::string &  output_name) const

Definition at line 172 of file ONNXRuntime.cc.

References Exception, and output_node_dims_.

172  {
173  auto iter = output_node_dims_.find(output_name);
174  if (iter == output_node_dims_.end()) {
175  throw cms::Exception("RuntimeError") << "Output name " << output_name << " is invalid!";
176  } else {
177  return iter->second;
178  }
179  }
std::map< std::string, std::vector< int64_t > > output_node_dims_
Definition: ONNXRuntime.h:70

◆ operator=()

ONNXRuntime& cms::Ort::ONNXRuntime::operator= ( const ONNXRuntime )
delete

◆ run()

FloatArrays cms::Ort::ONNXRuntime::run ( const std::vector< std::string > &  input_names,
FloatArrays input_values,
const std::vector< std::vector< int64_t >> &  input_shapes = {},
const std::vector< std::string > &  output_names = {},
int64_t  batch_size = 1 
) const

Definition at line 87 of file ONNXRuntime.cc.

References cms::cuda::assert(), Exception, spr::find(), HLT_2023v12_cff::input_names, input_node_dims_, input_node_names_, input_node_strings_, eostools::move(), Skims_PA_cff::name, HLT_2023v12_cff::output_names, output_node_names_, PatBasicFWLiteJetAnalyzer_Selector_cfg::outputs, and session_.

91  {
92  assert(input_names.size() == input_values.size());
93  assert(input_shapes.empty() || input_names.size() == input_shapes.size());
94  assert(batch_size > 0);
95 
96  // create input tensor objects from data values
97  std::vector<Value> input_tensors;
98  auto memory_info = MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
99  for (const auto& name : input_node_strings_) {
100  auto iter = std::find(input_names.begin(), input_names.end(), name);
101  if (iter == input_names.end()) {
102  throw cms::Exception("RuntimeError") << "Input " << name << " is not provided!";
103  }
104  auto input_pos = iter - input_names.begin();
105  auto value = input_values.begin() + input_pos;
106  std::vector<int64_t> input_dims;
107  if (input_shapes.empty()) {
108  input_dims = input_node_dims_.at(name);
109  input_dims[0] = batch_size;
110  } else {
111  input_dims = input_shapes[input_pos];
112  // rely on the given input_shapes to set the batch size
113  if (input_dims[0] != batch_size) {
114  throw cms::Exception("RuntimeError") << "The first element of `input_shapes` (" << input_dims[0]
115  << ") does not match the given `batch_size` (" << batch_size << ")";
116  }
117  }
118  auto expected_len = std::accumulate(input_dims.begin(), input_dims.end(), 1, std::multiplies<int64_t>());
119  if (expected_len != (int64_t)value->size()) {
120  throw cms::Exception("RuntimeError")
121  << "Input array " << name << " has a wrong size of " << value->size() << ", expected " << expected_len;
122  }
123  auto input_tensor =
124  Value::CreateTensor<float>(memory_info, value->data(), value->size(), input_dims.data(), input_dims.size());
125  assert(input_tensor.IsTensor());
126  input_tensors.emplace_back(std::move(input_tensor));
127  }
128 
129  // set output node names; will get all outputs if `output_names` is not provided
130  std::vector<const char*> run_output_node_names;
131  if (output_names.empty()) {
132  run_output_node_names = output_node_names_;
133  } else {
134  for (const auto& name : output_names) {
135  run_output_node_names.push_back(name.c_str());
136  }
137  }
138 
139  // run
140  auto output_tensors = session_->Run(RunOptions{nullptr},
141  input_node_names_.data(),
142  input_tensors.data(),
143  input_tensors.size(),
144  run_output_node_names.data(),
145  run_output_node_names.size());
146 
147  // convert output to floats
149  for (auto& output_tensor : output_tensors) {
150  assert(output_tensor.IsTensor());
151 
152  // get output shape
153  auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
154  auto length = tensor_info.GetElementCount();
155 
156  auto floatarr = output_tensor.GetTensorMutableData<float>();
157  outputs.emplace_back(floatarr, floatarr + length);
158  }
159  assert(outputs.size() == run_output_node_names.size());
160 
161  return outputs;
162  }
std::unique_ptr<::Ort::Session > session_
Definition: ONNXRuntime.h:62
std::map< std::string, std::vector< int64_t > > input_node_dims_
Definition: ONNXRuntime.h:66
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
assert(be >=bs)
Definition: value.py:1
std::vector< const char * > output_node_names_
Definition: ONNXRuntime.h:69
std::vector< std::string > input_node_strings_
Definition: ONNXRuntime.h:64
std::vector< const char * > input_node_names_
Definition: ONNXRuntime.h:65
def move(src, dest)
Definition: eostools.py:511

Member Data Documentation

◆ env_

const Env cms::Ort::ONNXRuntime::env_
staticprivate

Definition at line 61 of file ONNXRuntime.h.

◆ input_node_dims_

std::map<std::string, std::vector<int64_t> > cms::Ort::ONNXRuntime::input_node_dims_
private

Definition at line 66 of file ONNXRuntime.h.

Referenced by run().

◆ input_node_names_

std::vector<const char*> cms::Ort::ONNXRuntime::input_node_names_
private

Definition at line 65 of file ONNXRuntime.h.

Referenced by run().

◆ input_node_strings_

std::vector<std::string> cms::Ort::ONNXRuntime::input_node_strings_
private

Definition at line 64 of file ONNXRuntime.h.

Referenced by run().

◆ output_node_dims_

std::map<std::string, std::vector<int64_t> > cms::Ort::ONNXRuntime::output_node_dims_
private

Definition at line 70 of file ONNXRuntime.h.

Referenced by getOutputShape().

◆ output_node_names_

std::vector<const char*> cms::Ort::ONNXRuntime::output_node_names_
private

Definition at line 69 of file ONNXRuntime.h.

Referenced by run().

◆ output_node_strings_

std::vector<std::string> cms::Ort::ONNXRuntime::output_node_strings_
private

Definition at line 68 of file ONNXRuntime.h.

Referenced by getOutputNames().

◆ session_

std::unique_ptr<::Ort::Session> cms::Ort::ONNXRuntime::session_
private

Definition at line 62 of file ONNXRuntime.h.

Referenced by getOutputNames(), and run().