CMS 3D CMS Logo

List of all members | 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 ONNXRuntime &)=delete
 
 ONNXRuntime (const std::string &model_path, const ::Ort::SessionOptions *session_options=nullptr)
 
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 ()
 

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 25 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 78 of file ONNXRuntime.cc.

78 {}

Member Function Documentation

◆ getOutputNames()

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

Definition at line 153 of file ONNXRuntime.cc.

153  {
154  if (session_) {
155  return output_node_strings_;
156  } else {
157  throw cms::Exception("RuntimeError") << "Needs to call createSession() first before getting the output names!";
158  }
159  }

References Exception, output_node_strings_, and session_.

Referenced by TrackQuality::setTrackQuality().

◆ getOutputShape()

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

Definition at line 161 of file ONNXRuntime.cc.

161  {
162  auto iter = output_node_dims_.find(output_name);
163  if (iter == output_node_dims_.end()) {
164  throw cms::Exception("RuntimeError") << "Output name " << output_name << " is invalid!";
165  } else {
166  return iter->second;
167  }
168  }

References Exception, and output_node_dims_.

◆ 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 80 of file ONNXRuntime.cc.

84  {
85  assert(input_names.size() == input_values.size());
86  assert(input_shapes.empty() || input_names.size() == input_shapes.size());
87  assert(batch_size > 0);
88 
89  // create input tensor objects from data values
90  std::vector<Value> input_tensors;
91  auto memory_info = MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
92  for (const auto& name : input_node_strings_) {
93  auto iter = std::find(input_names.begin(), input_names.end(), name);
94  if (iter == input_names.end()) {
95  throw cms::Exception("RuntimeError") << "Input " << name << " is not provided!";
96  }
97  auto input_pos = iter - input_names.begin();
98  auto value = input_values.begin() + input_pos;
99  std::vector<int64_t> input_dims;
100  if (input_shapes.empty()) {
101  input_dims = input_node_dims_.at(name);
102  input_dims[0] = batch_size;
103  } else {
104  input_dims = input_shapes[input_pos];
105  // rely on the given input_shapes to set the batch size
106  }
107  auto expected_len = std::accumulate(input_dims.begin(), input_dims.end(), 1, std::multiplies<int64_t>());
108  if (expected_len != (int64_t)value->size()) {
109  throw cms::Exception("RuntimeError")
110  << "Input array " << name << " has a wrong size of " << value->size() << ", expected " << expected_len;
111  }
112  auto input_tensor =
113  Value::CreateTensor<float>(memory_info, value->data(), value->size(), input_dims.data(), input_dims.size());
114  assert(input_tensor.IsTensor());
115  input_tensors.emplace_back(std::move(input_tensor));
116  }
117 
118  // set output node names; will get all outputs if `output_names` is not provided
119  std::vector<const char*> run_output_node_names;
120  if (output_names.empty()) {
121  run_output_node_names = output_node_names_;
122  } else {
123  for (const auto& name : output_names) {
124  run_output_node_names.push_back(name.c_str());
125  }
126  }
127 
128  // run
129  auto output_tensors = session_->Run(RunOptions{nullptr},
130  input_node_names_.data(),
131  input_tensors.data(),
132  input_tensors.size(),
133  run_output_node_names.data(),
134  run_output_node_names.size());
135 
136  // convert output to floats
138  for (auto& output_tensor : output_tensors) {
139  assert(output_tensor.IsTensor());
140 
141  // get output shape
142  auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
143  auto length = tensor_info.GetElementCount();
144 
145  auto floatarr = output_tensor.GetTensorMutableData<float>();
146  outputs.emplace_back(floatarr, floatarr + length);
147  }
148  assert(outputs.size() == run_output_node_names.size());
149 
150  return outputs;
151  }

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

Member Data Documentation

◆ env_

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

Definition at line 54 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 59 of file ONNXRuntime.h.

Referenced by run().

◆ input_node_names_

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

Definition at line 58 of file ONNXRuntime.h.

Referenced by run().

◆ input_node_strings_

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

Definition at line 57 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 63 of file ONNXRuntime.h.

Referenced by getOutputShape().

◆ output_node_names_

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

Definition at line 62 of file ONNXRuntime.h.

Referenced by run().

◆ output_node_strings_

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

Definition at line 61 of file ONNXRuntime.h.

Referenced by getOutputNames().

◆ session_

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

Definition at line 55 of file ONNXRuntime.h.

Referenced by getOutputNames(), and run().

cms::Ort::ONNXRuntime::session_
std::unique_ptr<::Ort::Session > session_
Definition: ONNXRuntime.h:55
PatBasicFWLiteJetAnalyzer_Selector_cfg.outputs
outputs
Definition: PatBasicFWLiteJetAnalyzer_Selector_cfg.py:48
cms::cuda::assert
assert(be >=bs)
spr::find
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
Definition: FindCaloHit.cc:19
cms::Ort::ONNXRuntime::input_node_strings_
std::vector< std::string > input_node_strings_
Definition: ONNXRuntime.h:57
cms::Ort::ONNXRuntime::input_node_names_
std::vector< const char * > input_node_names_
Definition: ONNXRuntime.h:58
cms::Ort::ONNXRuntime::output_node_strings_
std::vector< std::string > output_node_strings_
Definition: ONNXRuntime.h:61
cms::Ort::ONNXRuntime::input_node_dims_
std::map< std::string, std::vector< int64_t > > input_node_dims_
Definition: ONNXRuntime.h:59
pfDeepBoostedJetPreprocessParams_cfi.input_names
input_names
Definition: pfDeepBoostedJetPreprocessParams_cfi.py:4
value
Definition: value.py:1
cms::Ort::FloatArrays
std::vector< std::vector< float > > FloatArrays
Definition: ONNXRuntime.h:23
eostools.move
def move(src, dest)
Definition: eostools.py:511
Exception
Definition: hltDiff.cc:245
Skims_PA_cff.name
name
Definition: Skims_PA_cff.py:17
cms::Ort::ONNXRuntime::output_node_names_
std::vector< const char * > output_node_names_
Definition: ONNXRuntime.h:62
cms::Ort::ONNXRuntime::output_node_dims_
std::map< std::string, std::vector< int64_t > > output_node_dims_
Definition: ONNXRuntime.h:63