Analysis Software
Documentation for sPHENIX simulation software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
OnnxEdgeClassifier.cpp
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file OnnxEdgeClassifier.cpp
1 // This file is part of the Acts project.
2 //
3 // Copyright (C) 2023 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 
10 
11 #include <onnxruntime_cxx_api.h>
12 #include <torch/script.h>
13 
15 
16 using namespace torch::indexing;
17 
18 namespace Acts {
19 
20 OnnxEdgeClassifier::OnnxEdgeClassifier(const Config &cfg,
21  std::unique_ptr<const Logger> logger)
22  : m_logger(std::move(logger)), m_cfg(cfg) {
23  m_env = std::make_unique<Ort::Env>(ORT_LOGGING_LEVEL_WARNING,
24  "ExaTrkX - edge classifier");
25 
26  Ort::SessionOptions session_options;
27  session_options.SetIntraOpNumThreads(1);
28  session_options.SetGraphOptimizationLevel(
29  GraphOptimizationLevel::ORT_ENABLE_EXTENDED);
30 
31  m_model = std::make_unique<Ort::Session>(*m_env, m_cfg.modelPath.c_str(),
32  session_options);
33 
34  Ort::AllocatorWithDefaultOptions allocator;
35 
37  std::string(m_model->GetInputNameAllocated(0, allocator).get());
39  std::string(m_model->GetInputNameAllocated(1, allocator).get());
41  std::string(m_model->GetOutputNameAllocated(0, allocator).get());
42 }
43 
45 
46 std::tuple<std::any, std::any, std::any> OnnxEdgeClassifier::operator()(
47  std::any inputNodes, std::any inputEdges, int) {
48  Ort::AllocatorWithDefaultOptions allocator;
49  auto memoryInfo = Ort::MemoryInfo::CreateCpu(
50  OrtAllocatorType::OrtArenaAllocator, OrtMemType::OrtMemTypeDefault);
51 
52  auto eInputTensor = std::any_cast<std::shared_ptr<Ort::Value>>(inputNodes);
53  auto edgeList = std::any_cast<std::vector<int64_t>>(inputEdges);
54  const int numEdges = edgeList.size() / 2;
55 
56  std::vector<const char *> fInputNames{m_inputNameNodes.c_str(),
57  m_inputNameEdges.c_str()};
58  std::vector<Ort::Value> fInputTensor;
59  fInputTensor.push_back(std::move(*eInputTensor));
60  std::vector<int64_t> fEdgeShape{2, numEdges};
61  fInputTensor.push_back(Ort::Value::CreateTensor<int64_t>(
62  memoryInfo, edgeList.data(), edgeList.size(), fEdgeShape.data(),
63  fEdgeShape.size()));
64 
65  // filtering outputs
66  std::vector<const char *> fOutputNames{m_outputNameScores.c_str()};
67  std::vector<float> fOutputData(numEdges);
68 
69  auto outputDims = m_model->GetOutputTypeInfo(0)
70  .GetTensorTypeAndShapeInfo()
71  .GetDimensionsCount();
72  using Shape = std::vector<int64_t>;
73  Shape fOutputShape = outputDims == 2 ? Shape{numEdges, 1} : Shape{numEdges};
74  std::vector<Ort::Value> fOutputTensor;
75  fOutputTensor.push_back(Ort::Value::CreateTensor<float>(
76  memoryInfo, fOutputData.data(), fOutputData.size(), fOutputShape.data(),
77  fOutputShape.size()));
78  runSessionWithIoBinding(*m_model, fInputNames, fInputTensor, fOutputNames,
79  fOutputTensor);
80 
81  ACTS_DEBUG("Get scores for " << numEdges << " edges.");
82  torch::Tensor edgeListCTen = torch::tensor(edgeList, {torch::kInt64});
83  edgeListCTen = edgeListCTen.reshape({2, numEdges});
84 
85  torch::Tensor fOutputCTen = torch::tensor(fOutputData, {torch::kFloat32});
86  fOutputCTen = fOutputCTen.sigmoid();
87 
88  torch::Tensor filterMask = fOutputCTen > m_cfg.cut;
89  torch::Tensor edgesAfterFCTen = edgeListCTen.index({Slice(), filterMask});
90 
91  std::vector<int64_t> edgesAfterFiltering;
92  std::copy(edgesAfterFCTen.data_ptr<int64_t>(),
93  edgesAfterFCTen.data_ptr<int64_t>() + edgesAfterFCTen.numel(),
94  std::back_inserter(edgesAfterFiltering));
95 
96  int64_t numEdgesAfterF = edgesAfterFiltering.size() / 2;
97  ACTS_DEBUG("Finished edge classification, after cut: " << numEdgesAfterF
98  << " edges.");
99 
100  return {std::make_shared<Ort::Value>(std::move(fInputTensor[0])),
101  edgesAfterFiltering, fOutputCTen};
102 }
103 
104 } // namespace Acts