21 using namespace ::Ort;
27 if (session_options) {
35 size_t num_input_nodes =
session_->GetInputCount();
40 for (
size_t i = 0;
i < num_input_nodes;
i++) {
47 auto type_info =
session_->GetInputTypeInfo(
i);
48 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
49 size_t num_dims = tensor_info.GetDimensionsCount();
54 size_t num_output_nodes =
session_->GetOutputCount();
59 for (
size_t i = 0;
i < num_output_nodes;
i++) {
66 auto type_info =
session_->GetOutputTypeInfo(
i);
67 auto tensor_info = type_info.GetTensorTypeAndShapeInfo();
68 size_t num_dims = tensor_info.GetDimensionsCount();
80 SessionOptions sess_opts;
81 sess_opts.SetIntraOpNumThreads(1);
85 sess_opts.AppendExecutionProvider_CUDA(
options);
92 const std::vector<std::vector<int64_t>>& input_shapes,
94 int64_t batch_size)
const {
100 std::vector<Value> input_tensors;
101 auto memory_info = MemoryInfo::CreateCpu(OrtArenaAllocator, OrtMemTypeDefault);
108 auto value = input_values.begin() + input_pos;
109 std::vector<int64_t> input_dims;
110 if (input_shapes.empty()) {
112 input_dims[0] = batch_size;
114 input_dims = input_shapes[input_pos];
116 if (input_dims[0] != batch_size) {
117 throw cms::Exception(
"RuntimeError") <<
"The first element of `input_shapes` (" << input_dims[0]
118 <<
") does not match the given `batch_size` (" << batch_size <<
")";
121 auto expected_len = std::accumulate(input_dims.begin(), input_dims.end(), 1, std::multiplies<int64_t>());
122 if (expected_len != (int64_t)
value->size()) {
124 <<
"Input array " <<
name <<
" has a wrong size of " <<
value->size() <<
", expected " << expected_len;
127 Value::CreateTensor<float>(memory_info,
value->data(),
value->size(), input_dims.data(), input_dims.size());
128 assert(input_tensor.IsTensor());
129 input_tensors.emplace_back(
std::move(input_tensor));
133 std::vector<const char*> run_output_node_names;
138 run_output_node_names.push_back(
name.c_str());
143 auto output_tensors =
session_->Run(RunOptions{
nullptr},
145 input_tensors.data(),
146 input_tensors.size(),
147 run_output_node_names.data(),
148 run_output_node_names.size());
152 for (
auto& output_tensor : output_tensors) {
153 assert(output_tensor.IsTensor());
156 auto tensor_info = output_tensor.GetTensorTypeAndShapeInfo();
157 auto length = tensor_info.GetElementCount();
159 auto floatarr = output_tensor.GetTensorMutableData<
float>();
160 outputs.emplace_back(floatarr, floatarr + length);
171 throw cms::Exception(
"RuntimeError") <<
"Needs to call createSession() first before getting the output names!";
178 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_
::Ort::SessionOptions defaultSessionOptions(Backend backend=Backend::cpu)
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
const std::vector< std::string > & getOutputNames() const
ONNXRuntime(const std::string &model_path, const ::Ort::SessionOptions *session_options=nullptr)
const std::vector< int64_t > & getOutputShape(const std::string &output_name) const
std::vector< const char * > output_node_names_
char data[epos_bytes_allocation]
std::vector< std::string > input_node_strings_
std::vector< const char * > input_node_names_
std::vector< std::string > output_node_strings_
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