20 using namespace ::Ort;
26 if (session_options) {
29 SessionOptions sess_opts;
30 sess_opts.SetIntraOpNumThreads(1);
33 AllocatorWithDefaultOptions allocator;
36 size_t num_input_nodes =
session_->GetInputCount();
41 for (
size_t i = 0;
i < num_input_nodes;
i++) {
48 auto type_info =
session_->GetInputTypeInfo(
i);
49 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
50 size_t num_dims = tensor_info.GetDimensionsCount();
55 size_t num_output_nodes =
session_->GetOutputCount();
60 for (
size_t i = 0;
i < num_output_nodes;
i++) {
67 auto type_info =
session_->GetOutputTypeInfo(
i);
68 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
69 size_t num_dims = tensor_info.GetDimensionsCount();
82 const std::vector<std::vector<int64_t>>& input_shapes,
83 const std::vector<std::string>& output_names,
84 int64_t batch_size)
const {
85 assert(input_names.size() == input_values.size());
86 assert(input_shapes.empty() || input_names.size() == input_shapes.size());
87 assert(batch_size > 0);
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()) {
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()) {
102 input_dims[0] = batch_size;
104 input_dims = input_shapes[input_pos];
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()) {
110 <<
"Input array " <<
name <<
" has a wrong size of " <<
value->size() <<
", expected " << expected_len;
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));
119 std::vector<const char*> run_output_node_names;
120 if (output_names.empty()) {
123 for (
const auto&
name : output_names) {
124 run_output_node_names.push_back(
name.c_str());
129 auto output_tensors =
session_->Run(RunOptions{
nullptr},
131 input_tensors.data(),
132 input_tensors.size(),
133 run_output_node_names.data(),
134 run_output_node_names.size());
138 for (
auto& output_tensor : output_tensors) {
139 assert(output_tensor.IsTensor());
142 auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
143 auto length = tensor_info.GetElementCount();
145 auto floatarr = output_tensor.GetTensorMutableData<
float>();
146 outputs.emplace_back(floatarr, floatarr + length);
148 assert(outputs.size() == run_output_node_names.size());
157 throw cms::Exception(
"RuntimeError") <<
"Needs to call createSession() first before getting the output names!";
164 throw cms::Exception(
"RuntimeError") <<
"Output name " << output_name <<
" is invalid!";
std::unique_ptr<::Ort::Session > session_
std::map< std::string, std::vector< int64_t > > input_node_dims_
std::map< std::string, std::vector< int64_t > > output_node_dims_
static const ::Ort::Env env_
void find(edm::Handle< EcalRecHitCollection > &hits, DetId thisDet, std::vector< EcalRecHitCollection::const_iterator > &hit, bool debug=false)
std::vector< std::vector< float > > FloatArrays
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(const std::string &model_path, const ::Ort::SessionOptions *session_options=nullptr)
std::vector< const char * > output_node_names_
const std::vector< std::string > & getOutputNames() const
char data[epos_bytes_allocation]
const std::vector< int64_t > & getOutputShape(const std::string &output_name) const
std::vector< std::string > input_node_strings_
std::vector< const char * > input_node_names_
std::vector< std::string > output_node_strings_