Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-11-20 09:33:17

0001 // SPDX-License-Identifier: LGPL-3.0-or-later
0002 // Copyright (C) 2022 - 2024 Wouter Deconinck, Tooba Ali, Dmitry Kalinkin
0003 
0004 #include <onnxruntime_c_api.h>
0005 #include <onnxruntime_cxx_api.h>
0006 #include <algorithm>
0007 #include <cstddef>
0008 #include <format>
0009 #include <gsl/pointers>
0010 #include <iterator>
0011 #include <sstream>
0012 #include <stdexcept>
0013 #include <tuple>
0014 
0015 #include "ONNXInference.h"
0016 
0017 namespace eicrecon {
0018 
0019 static std::string print_shape(const std::vector<std::int64_t>& v) {
0020   std::stringstream ss("");
0021   for (std::size_t i = 0; i < v.size() - 1; i++) {
0022     ss << v[i] << " x ";
0023   }
0024   ss << v[v.size() - 1];
0025   return ss.str();
0026 }
0027 
0028 static bool check_shape_consistency(const std::vector<std::int64_t>& shape1,
0029                                     const std::vector<std::int64_t>& shape2) {
0030   if (shape2.size() != shape1.size()) {
0031     return false;
0032   }
0033   for (std::size_t ix = 0; ix < shape1.size(); ix++) {
0034     if ((shape1[ix] != -1) && (shape2[ix] != -1) && (shape1[ix] != shape2[ix])) {
0035       return false;
0036     }
0037   }
0038   return true;
0039 }
0040 
0041 template <typename T>
0042 static Ort::Value iters_to_tensor(typename std::vector<T>::const_iterator data_begin,
0043                                   typename std::vector<T>::const_iterator data_end,
0044                                   std::vector<int64_t>::const_iterator shape_begin,
0045                                   std::vector<int64_t>::const_iterator shape_end) {
0046   Ort::MemoryInfo mem_info = Ort::MemoryInfo::CreateCpu(OrtAllocatorType::OrtArenaAllocator,
0047                                                         OrtMemType::OrtMemTypeDefault);
0048   // Dereferencing the begin iterator of an empty range (&*begin) is undefined
0049   // behavior, so pass a null pointer for empty data/shape ranges instead.
0050   T* data_ptr              = (data_begin == data_end) ? nullptr : const_cast<T*>(&*data_begin);
0051   const int64_t* shape_ptr = (shape_begin == shape_end) ? nullptr : &*shape_begin;
0052   auto tensor = Ort::Value::CreateTensor<T>(mem_info, data_ptr, data_end - data_begin, shape_ptr,
0053                                             shape_end - shape_begin);
0054   return tensor;
0055 }
0056 
0057 void ONNXInference::init() {
0058   // onnxruntime setup
0059   m_env = Ort::Env(ORT_LOGGING_LEVEL_WARNING, name().data());
0060   Ort::SessionOptions session_options;
0061   session_options.SetInterOpNumThreads(1);
0062   session_options.SetIntraOpNumThreads(1);
0063   try {
0064     m_session = Ort::Session(m_env, m_cfg.modelPath.c_str(), session_options);
0065     Ort::AllocatorWithDefaultOptions allocator;
0066 
0067     // print name/shape of inputs
0068     debug("Input Node Name/Shape:");
0069     for (std::size_t i = 0; i < m_session.GetInputCount(); i++) {
0070       m_input_names.emplace_back(m_session.GetInputNameAllocated(i, allocator).get());
0071       m_input_shapes.emplace_back(
0072           m_session.GetInputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
0073       debug("\t{} : {}", m_input_names.at(i), print_shape(m_input_shapes.at(i)));
0074     }
0075 
0076     // print name/shape of outputs
0077     debug("Output Node Name/Shape: {}", m_session.GetOutputCount());
0078     for (std::size_t i = 0; i < m_session.GetOutputCount(); i++) {
0079       m_output_names.emplace_back(m_session.GetOutputNameAllocated(i, allocator).get());
0080 
0081       if (m_session.GetOutputTypeInfo(i).GetONNXType() != ONNX_TYPE_TENSOR) {
0082         m_output_shapes.emplace_back();
0083         debug("\t{} : not a tensor", m_output_names.at(i));
0084       } else {
0085         m_output_shapes.emplace_back(
0086             m_session.GetOutputTypeInfo(i).GetTensorTypeAndShapeInfo().GetShape());
0087         debug("\t{} : {}", m_output_names.at(i), print_shape(m_output_shapes.at(i)));
0088       }
0089     }
0090 
0091     // convert names to char*
0092     m_input_names_char.resize(m_input_names.size(), nullptr);
0093     std::ranges::transform(m_input_names, std::begin(m_input_names_char),
0094                            [&](const std::string& str) { return str.c_str(); });
0095     m_output_names_char.resize(m_output_names.size(), nullptr);
0096     std::ranges::transform(m_output_names, std::begin(m_output_names_char),
0097                            [&](const std::string& str) { return str.c_str(); });
0098 
0099   } catch (const Ort::Exception& exception) {
0100     error("ONNX error {}", exception.what());
0101     throw;
0102   }
0103 }
0104 
0105 void ONNXInference::process(const ONNXInference::Input& input,
0106                             const ONNXInference::Output& output) const {
0107 
0108   const auto [in_tensors] = input;
0109   auto [out_tensors]      = output;
0110 
0111   // Require valid inputs
0112   if (in_tensors.size() != m_input_names.size()) {
0113     error("The ONNX model requires {} tensors, whereas {} were provided", m_input_names.size(),
0114           in_tensors.size());
0115     throw std::runtime_error(
0116         std::format("The ONNX model requires {} tensors, whereas {} were provided",
0117                     m_input_names.size(), in_tensors.size()));
0118   }
0119 
0120   // Prepare input tensor
0121   std::vector<float> input_tensor_values;
0122   std::vector<Ort::Value> input_tensors;
0123 
0124   for (std::size_t ix = 0; ix < m_input_names.size(); ix++) {
0125     edm4eic::Tensor in_tensor = in_tensors[ix]->at(0);
0126     if (in_tensor.getElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) {
0127       input_tensors.emplace_back(
0128           iters_to_tensor<float>(in_tensor.floatData_begin(), in_tensor.floatData_end(),
0129                                  in_tensor.shape_begin(), in_tensor.shape_end()));
0130     } else if (in_tensor.getElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64) {
0131       input_tensors.emplace_back(
0132           iters_to_tensor<int64_t>(in_tensor.int64Data_begin(), in_tensor.int64Data_end(),
0133                                    in_tensor.shape_begin(), in_tensor.shape_end()));
0134     }
0135 
0136     auto input_shape = input_tensors[ix].GetTensorTypeAndShapeInfo().GetShape();
0137     std::vector<std::int64_t> input_expected_shape = m_input_shapes[ix];
0138     if (!check_shape_consistency(input_shape, input_expected_shape)) {
0139       error("Input tensor shape incorrect {} != {}", print_shape(input_shape),
0140             print_shape(input_expected_shape));
0141       throw std::runtime_error(std::format("Input tensor shape incorrect {} != {}",
0142                                            print_shape(input_shape),
0143                                            print_shape(input_expected_shape)));
0144     }
0145   }
0146 
0147   // Attempt inference
0148   std::vector<Ort::Value> onnx_values;
0149   try {
0150     onnx_values = m_session.Run(Ort::RunOptions{nullptr}, m_input_names_char.data(),
0151                                 input_tensors.data(), m_input_names_char.size(),
0152                                 m_output_names_char.data(), m_output_names_char.size());
0153   } catch (const Ort::Exception& exception) {
0154     error("Error running model inference: {}", exception.what());
0155     throw;
0156   }
0157 
0158   try {
0159     for (std::size_t ix = 0; ix < onnx_values.size(); ix++) {
0160       Ort::Value& onnx_tensor = onnx_values[ix];
0161       if (!onnx_tensor.IsTensor()) {
0162         error("The output \"{}\" is not a tensor. ONNXType {} is not yet supported. Skipping...",
0163               m_output_names_char[ix], static_cast<int>(onnx_tensor.GetTypeInfo().GetONNXType()));
0164         continue;
0165       }
0166       auto onnx_tensor_type             = onnx_tensor.GetTensorTypeAndShapeInfo();
0167       edm4eic::MutableTensor out_tensor = out_tensors[ix]->create();
0168       out_tensor.setElementType(static_cast<int32_t>(onnx_tensor_type.GetElementType()));
0169       std::size_t num_values = 1;
0170       for (int64_t dim_size : onnx_tensor_type.GetShape()) {
0171         out_tensor.addToShape(dim_size);
0172         num_values *= dim_size;
0173       }
0174       if (onnx_tensor_type.GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_FLOAT) {
0175         auto* data = onnx_tensor.GetTensorMutableData<float>();
0176         for (std::size_t value_ix = 0; value_ix < num_values; value_ix++) {
0177           out_tensor.addToFloatData(data[value_ix]);
0178         }
0179       } else if (onnx_tensor_type.GetElementType() == ONNX_TENSOR_ELEMENT_DATA_TYPE_INT64) {
0180         auto* data = onnx_tensor.GetTensorMutableData<int64_t>();
0181         for (std::size_t value_ix = 0; value_ix < num_values; value_ix++) {
0182           out_tensor.addToInt64Data(data[value_ix]);
0183         }
0184       } else {
0185         error("Unsupported ONNXTensorElementDataType {}",
0186               static_cast<int>(onnx_tensor_type.GetElementType()));
0187       }
0188     }
0189   } catch (const Ort::Exception& exception) {
0190     error("Error running model inference: {}", exception.what());
0191     throw;
0192   }
0193 }
0194 
0195 } // namespace eicrecon