Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnnxRuntimeBase.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnnxRuntimeBase.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2020 CERN for the benefit of the Acts project
4 //
5 // This Source Code Form is subject to the terms of the Mozilla Public
6 // License, v. 2.0. If a copy of the MPL was not distributed with this
7 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 
9 #pragma once
10 #include <vector>
11 
12 #include <Eigen/Dense>
13 #include <onnxruntime_cxx_api.h>
14 
15 namespace Acts {
16 
17 using NetworkBatchInput =
18  Eigen::Array<float, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>;
19 
20 // General class that sets up the ONNX runtime framework for loading an ML model
21 // and using it for inference.
23  public:
25  OnnxRuntimeBase() = default;
26 
31  OnnxRuntimeBase(Ort::Env& env, const char* modelPath);
32 
34  ~OnnxRuntimeBase() = default;
35 
41  std::vector<float> runONNXInference(
42  std::vector<float>& inputTensorValues) const;
43 
49  std::vector<std::vector<float>> runONNXInference(
50  NetworkBatchInput& inputTensorValues) const;
51 
57  std::vector<std::vector<std::vector<float>>> runONNXInferenceMultiOutput(
58  NetworkBatchInput& inputTensorValues) const;
59 
60  private:
62  std::unique_ptr<Ort::Session> m_session;
63  std::vector<Ort::AllocatedStringPtr> m_inputNodeNamesAllocated;
64  std::vector<const char*> m_inputNodeNames;
65  std::vector<int64_t> m_inputNodeDims;
66  std::vector<Ort::AllocatedStringPtr> m_outputNodeNamesAllocated;
67  std::vector<const char*> m_outputNodeNames;
68  std::vector<std::vector<int64_t>> m_outputNodeDims;
69 };
70 
71 } // namespace Acts