21 using namespace ::Ort;
27 if (session_options) {
28 session_ = std::make_unique<Session>(
env_, model_path.c_str(), *session_options);
30 SessionOptions sess_opts;
31 sess_opts.SetIntraOpNumThreads(1);
32 session_ = std::make_unique<Session>(
env_, model_path.c_str(), sess_opts);
34 AllocatorWithDefaultOptions allocator;
37 size_t num_input_nodes =
session_->GetInputCount();
42 for (
size_t i = 0;
i < num_input_nodes;
i++) {
49 auto type_info =
session_->GetInputTypeInfo(
i);
50 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
51 size_t num_dims = tensor_info.GetDimensionsCount();
56 size_t num_output_nodes =
session_->GetOutputCount();
61 for (
size_t i = 0;
i < num_output_nodes;
i++) {
68 auto type_info =
session_->GetOutputTypeInfo(
i);
69 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
70 size_t num_dims = tensor_info.GetDimensionsCount();
83 const std::vector<std::vector<int64_t>>& input_shapes,
84 const std::vector<std::string>& output_names,
85 int64_t batch_size)
const {
86 assert(input_names.size() == input_values.size());
87 assert(input_shapes.empty() || input_names.size() == input_shapes.size());
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()) {
98 auto input_pos = iter - input_names.begin();
99 auto value = input_values.begin() + input_pos;
100 std::vector<int64_t> input_dims;
101 if (input_shapes.empty()) {
103 input_dims[0] = batch_size;
105 input_dims = input_shapes[input_pos];
108 auto expected_len = std::accumulate(input_dims.begin(), input_dims.end(), 1, std::multiplies<int64_t>());
109 if (expected_len != (int64_t)
value->size()) {
111 <<
"Input array " <<
name <<
" has a wrong size of " <<
value->size() <<
", expected " << expected_len;
114 Value::CreateTensor<float>(memory_info,
value->data(),
value->size(), input_dims.data(), input_dims.size());
115 assert(input_tensor.IsTensor());
116 input_tensors.emplace_back(
std::move(input_tensor));
120 std::vector<const char*> run_output_node_names;
121 if (output_names.empty()) {
124 for (
const auto&
name : output_names) {
125 run_output_node_names.push_back(
name.c_str());
130 auto output_tensors =
session_->Run(RunOptions{
nullptr},
132 input_tensors.data(),
133 input_tensors.size(),
134 run_output_node_names.data(),
135 run_output_node_names.size());
139 for (
auto& output_tensor : output_tensors) {
140 assert(output_tensor.IsTensor());
143 auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
144 auto length = tensor_info.GetElementCount();
146 auto floatarr = output_tensor.GetTensorMutableData<
float>();
147 outputs.emplace_back(floatarr, floatarr + length);
149 assert(outputs.size() == run_output_node_names.size());
158 throw cms::Exception(
"RuntimeError") <<
"Needs to call createSession() first before getting the output names!";
165 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_