Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
runSessionWithIoBinding.hpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file runSessionWithIoBinding.hpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2022 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 
11 #include <onnxruntime_cxx_api.h>
12 
13 inline void runSessionWithIoBinding(Ort::Session& sess,
14  std::vector<const char*>& inputNames,
15  std::vector<Ort::Value>& inputData,
16  std::vector<const char*>& outputNames,
17  std::vector<Ort::Value>& outputData) {
18  if (inputNames.size() < 1) {
19  throw std::runtime_error("Onnxruntime input data mapping cannot be empty");
20  }
21  if (inputNames.size() != inputData.size()) {
22  throw std::runtime_error("inputData size mismatch");
23  }
24 
25  Ort::IoBinding iobinding(sess);
26  for (size_t idx = 0; idx < inputNames.size(); ++idx) {
27  iobinding.BindInput(inputNames[idx], inputData[idx]);
28  }
29 
30  for (size_t idx = 0; idx < outputNames.size(); ++idx) {
31  iobinding.BindOutput(outputNames[idx], outputData[idx]);
32  }
33 
34  sess.Run(Ort::RunOptions{nullptr}, iobinding);
35 }