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::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 81 of file ONNXRuntime.cc.

81 {}

Member Function Documentation

◆ getOutputNames()

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

Definition at line 147 of file ONNXRuntime.cc.

147  {
148  if (session_) {
149  return output_node_strings_;
150  } else {
151  throw cms::Exception("RuntimeError") << "Needs to call createSession() first before getting the output names!";
152  }
153  }

References Exception, output_node_strings_, and session_.

◆ getOutputShape()

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

Definition at line 155 of file ONNXRuntime.cc.

155  {
156  auto iter = output_node_dims_.find(output_name);
157  if (iter == output_node_dims_.end()) {
158  throw cms::Exception("RuntimeError") << "Output name " << output_name << " is invalid!";
159  } else {
160  return iter->second;
161  }
162  }

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::string > &  output_names = {},
int64_t  batch_size = 1 
) const

Definition at line 83 of file ONNXRuntime.cc.

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

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

Member Data Documentation

◆ env_

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

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

Referenced by run().

◆ input_node_names_

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

Definition at line 56 of file ONNXRuntime.h.

Referenced by run().

◆ input_node_strings_

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

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

Referenced by getOutputShape().

◆ output_node_names_

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

Definition at line 60 of file ONNXRuntime.h.

Referenced by run().

◆ output_node_strings_

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

Definition at line 59 of file ONNXRuntime.h.

Referenced by getOutputNames().

◆ session_

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

Definition at line 53 of file ONNXRuntime.h.

Referenced by getOutputNames(), and run().

cms::Ort::ONNXRuntime::session_
std::unique_ptr<::Ort::Session > session_
Definition: ONNXRuntime.h:53
hltPfDeepFlavourJetTags_cfi.output_names
output_names
Definition: hltPfDeepFlavourJetTags_cfi.py:21
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:55
cms::Ort::ONNXRuntime::input_node_names_
std::vector< const char * > input_node_names_
Definition: ONNXRuntime.h:56
cms::Ort::ONNXRuntime::output_node_strings_
std::vector< std::string > output_node_strings_
Definition: ONNXRuntime.h:59
cms::Ort::ONNXRuntime::input_node_dims_
std::map< std::string, std::vector< int64_t > > input_node_dims_
Definition: ONNXRuntime.h:57
value
Definition: value.py:1
hltPfDeepFlavourJetTags_cfi.input_names
input_names
Definition: hltPfDeepFlavourJetTags_cfi.py:12
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:246
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:60
cms::Ort::ONNXRuntime::output_node_dims_
std::map< std::string, std::vector< int64_t > > output_node_dims_
Definition: ONNXRuntime.h:61