ATLAS Offline Software
Loading...
Searching...
No Matches
ActsGnnModuleMapFinderTool.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
6
8
9#include "ActsPlugins/Gnn/CudaTrackBuilding.hpp"
10#include "ActsPlugins/Gnn/GnnPipeline.hpp"
11#include "ActsPlugins/Gnn/ModuleMapCuda.hpp"
12#include "ActsPlugins/Gnn/OnnxEdgeClassifier.hpp"
13#include "ActsPlugins/Gnn/TensorRTEdgeClassifier.hpp"
14#include "ActsPlugins/Gnn/TorchEdgeClassifier.hpp"
15#include "ActsPlugins/Gnn/EdgeLayerConnector.hpp"
16
20#include "ActsGnnHookTool.h"
21
22#include <algorithm>
23#include <numeric>
24
25
28
29 m_logger = makeActsAthenaLogger(this, "ActsGnnModuleMapFinderTool");
30
31 // Build ACTS GNN pipeline components
32
33 // 1. Graph constructor (ModuleMapCuda)
34 ActsPlugins::ModuleMapCuda::Config gcCfg;
35 gcCfg.rScale = kScaleR;
36 gcCfg.zScale = kScaleZ;
37 gcCfg.phiScale = kScalePhi;
38 gcCfg.moduleMapPath = m_moduleMapPath.value();
39 gcCfg.gpuBlocks = 512;
40 auto gc = std::make_shared<ActsPlugins::ModuleMapCuda>(
41 gcCfg, m_logger->cloneWithSuffix("ModuleMap"));
42
43 // 2. Edge classifier (ONNX / Torch / TensorRT)
44 // ONNX and Torch are not thread-safe: a mutex is emplaced to serialise run().
45 // TensorRT manages concurrency internally via execution contexts: no guard needed.
46 std::shared_ptr<ActsPlugins::EdgeClassificationBase> gnn;
47 if (m_gnnPath.value().find(".onnx") != std::string::npos) {
48#ifdef ACTS_GNN_ONNX_BACKEND
49 ActsPlugins::OnnxEdgeClassifier::Config gnnCfg;
50 gnnCfg.modelPath = m_gnnPath.value();
51 gnnCfg.cut = m_edgeCut.value();
52 gnn = std::make_shared<ActsPlugins::OnnxEdgeClassifier>(
53 gnnCfg, m_logger->cloneWithSuffix("GNN"));
54 m_runMutex.emplace();
55#else
56 ATH_MSG_FATAL("Not compiled with ONNX, cannot interpret *.onnx files");
57 return StatusCode::FAILURE;
58#endif
59 } else if (m_gnnPath.value().find(".pt") != std::string::npos) {
60#ifdef ACTS_GNN_TORCH_BACKEND
61 ActsPlugins::TorchEdgeClassifier::Config gnnCfg;
62 gnnCfg.modelPath = m_gnnPath.value();
63 gnnCfg.cut = m_edgeCut.value();
64 gnn = std::make_shared<ActsPlugins::TorchEdgeClassifier>(
65 gnnCfg, m_logger->cloneWithSuffix("GNN"));
66 m_runMutex.emplace();
67#else
68 ATH_MSG_FATAL("Not compiled with Torch, cannot interpret *.pt files");
69 return StatusCode::FAILURE;
70#endif
71 } else if (m_gnnPath.value().find(".engine") != std::string::npos) {
72#ifdef ACTS_GNN_WITH_TENSORRT
73 ActsPlugins::TensorRTEdgeClassifier::Config gnnCfg;
74 gnnCfg.modelPath = m_gnnPath.value();
75 gnnCfg.cut = m_edgeCut.value();
76 gnnCfg.numExecutionContexts = m_numTrtContexts.value();
77 gnn = std::make_shared<ActsPlugins::TensorRTEdgeClassifier>(
78 gnnCfg, m_logger->cloneWithSuffix("GNN"));
79#else
80 ATH_MSG_FATAL("Not compiled with TensorRT, cannot interpret *.engine files");
81 return StatusCode::FAILURE;
82#endif
83 } else {
84 ATH_MSG_FATAL("Unknown extension for GNN model: " << m_gnnPath.value());
85 return StatusCode::FAILURE;
86 }
87
88 // 3. Track builder
89 std::shared_ptr<ActsPlugins::TrackBuildingBase> tb;
90 ATH_MSG_INFO("Configure CC&JunctionRemoval as graph segmentation algorithm");
92 ActsPlugins::EdgeLayerConnector::Config tbCfg;
93 tbCfg.maxHitsPerTrack = m_elcMaxHitsPerTrack;
94 tbCfg.blockSize = 512;
95 tbCfg.weightsCut = m_edgeCut;
96 tb = std::make_shared<ActsPlugins::EdgeLayerConnector>(
97 tbCfg, m_logger->cloneWithSuffix("ELC"));
98 } else {
99 ActsPlugins::CudaTrackBuilding::Config tbCfg;
100 tbCfg.doJunctionRemoval = true;
101 tb = std::make_shared<ActsPlugins::CudaTrackBuilding>(
102 tbCfg, m_logger->cloneWithSuffix("CC&JR"));
103 }
104
105 // 4. Assemble pipeline
106 m_gnnPipeline = std::make_unique<ActsPlugins::GnnPipeline>(
107 gc, std::vector{std::move(gnn)}, tb, m_logger->cloneWithSuffix("Pipeline"));
108
109 return StatusCode::SUCCESS;
110}
111
113 const std::vector<const Trk::SpacePoint*>& spacepoints,
114 std::vector<std::vector<uint32_t>>& tracks,
115 std::unordered_map<int, std::unordered_map<int, float>>* edgeMap) const {
116
117 const std::size_t nSP = spacepoints.size();
118
119 ATH_MSG_DEBUG("Processing " << nSP << " spacepoints with " << NUM_FEATURES << " features");
120
121 // Sort spacepoint indices by module ID (required by module map graph construction)
122 std::vector<std::size_t> sortIdx(nSP);
123 std::iota(sortIdx.begin(), sortIdx.end(), 0);
124 std::ranges::sort(sortIdx, std::less{}, [&](std::size_t i) {
125 return spacepoints[i]->clusterList().first->detectorElement()->identify().get_compact();
126 });
127
128 // Build features, module IDs, and IDs directly in sorted order
129 std::vector<float> features(NUM_FEATURES * nSP);
130 std::vector<std::uint64_t> moduleIds(nSP);
131 std::vector<int> ids(nSP);
132
133 for (std::size_t k = 0; k < nSP; ++k) {
134 const std::size_t origIdx = sortIdx[k];
135 auto featureMap = m_spacepointFeatureTool->getFeatures(spacepoints[origIdx]);
136 // Use detector element identifier, not cluster identifier, to get the module ID
137 moduleIds[k] = spacepoints[origIdx]->clusterList().first->detectorElement()->identify().get_compact();
138 ids[k] = static_cast<int>(k);
139 for (std::size_t j = 0; j < NUM_FEATURES; ++j) {
140 features[k * NUM_FEATURES + j] = featureMap[FEATURE_NAMES[j]] / FEATURE_SCALES[j];
141 }
142 }
143
144 // Run GNN pipeline (mutex present for ONNX/Torch, absent for TRT)
145 auto candidates = [&] {
146 std::unique_lock<std::mutex> lock;
147 if (m_runMutex) lock = std::unique_lock<std::mutex>(*m_runMutex);
148
149 if (edgeMap != nullptr) {
150 ScoredGraphHook hook;
151 auto result = m_gnnPipeline->run(features, moduleIds, ids, ActsPlugins::Device::Cuda(0), hook);
152
153 // Retrieve edgeScores and edgeIndex from hook
154 const std::vector<float>& edgeScores = hook.getEdgeScores();
155 const std::vector<std::int64_t>& edgeIndex = hook.getEdgeIndex();
156 const std::size_t nEdges = edgeScores.size();
157
158 // Create a map to acces edge score (sorted indices back to original spacepoint indices)
159 for (std::size_t i = 0; i < nEdges; ++i) {
160 std::int64_t src = edgeIndex[i];
161 std::int64_t dst = edgeIndex[nEdges + i];
162 (*edgeMap)[sortIdx[src]][sortIdx[dst]] = edgeScores[i];
163 }
164 return result;
165 }
166
167 return m_gnnPipeline->run(features, moduleIds, ids, ActsPlugins::Device::Cuda(0));;
168 }();
169
170 ATH_MSG_DEBUG("GNN pipeline returned " << candidates.size() << " candidates");
171
172 // Filter by minimum measurements and convert indices back to original ordering
173 tracks.clear();
174 tracks.reserve(candidates.size());
175
176 for (const auto& candidate : candidates) {
177 if (candidate.size() < m_minCandidateMeasurements.value()) {
178 continue;
179 }
180
181 // Map sorted indices back to original spacepoint indices
182 std::vector<uint32_t> track;
183 track.reserve(candidate.size());
184 for (int sortedIdx : candidate) {
185 track.push_back(static_cast<uint32_t>(sortIdx[sortedIdx]));
186 }
187 tracks.push_back(std::move(track));
188 }
189
190 ATH_MSG_DEBUG("Returning " << tracks.size() << " track candidates after filtering (>= "
191 << m_minCandidateMeasurements.value() << " measurements)");
192
193 return StatusCode::SUCCESS;
194}
195
196MsgStream& InDet::ActsGnnModuleMapFinderTool::dump(MsgStream& out) const {
197 out << "\n";
198 out << "|---------------------------------------------------------------------|\n" ;
199 out << "| ActsGnnModuleMapFinderTool |\n" ;
200 out << "|---------------------------------------------------------------------|\n" ;
201 return out;
202}
203
204std::ostream& InDet::ActsGnnModuleMapFinderTool::dump(std::ostream& out) const {
205 return out;
206}
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_FATAL(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
virtual void lock()=0
Interface to allow an object to lock itself when made const in SG.
std::unique_ptr< const Acts::Logger > makeActsAthenaLogger(IMessageSvc *svc, const std::string &name, int level, std::optional< std::string > parent_name)
static constexpr std::array< float, NUM_FEATURES > FEATURE_SCALES
static constexpr std::array< const char *, NUM_FEATURES > FEATURE_NAMES
virtual MsgStream & dump(MsgStream &out) const override
std::unique_ptr< const Acts::Logger > m_logger
ToolHandle< ISpacepointFeatureTool > m_spacepointFeatureTool
static constexpr std::size_t NUM_FEATURES
std::unique_ptr< ActsPlugins::GnnPipeline > m_gnnPipeline
virtual StatusCode getTracks(const std::vector< const Trk::SpacePoint * > &spacepoints, std::vector< std::vector< uint32_t > > &tracks, std::unordered_map< int, std::unordered_map< int, float > > *edgeMap=nullptr) const override
const std::vector< float > & getEdgeScores() const
const std::vector< int64_t > & getEdgeIndex() const