ATLAS Offline Software
Loading...
Searching...
No Matches
BucketInferenceToolBase.h
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4#ifndef MUONINFERENCETOOLS_BUCKETINFERENCETOOLBASE_H
5#define MUONINFERENCETOOLS_BUCKETINFERENCETOOLBASE_H
6
11
15
16#include <onnxruntime_cxx_api.h>
17#include <array>
18#include <cstddef>
19#include <cstdint>
20#include <string>
21#include <string_view>
22#include <vector>
23
24class ActsGeometryContext;
25
26namespace MuonML {
27
40class BucketInferenceToolBase : public extends<AthAlgTool, IGraphInferenceTool> {
41public:
42 using base_class::base_class;
43 ~BucketInferenceToolBase() override = default;
44
46 StatusCode buildGraph(const EventContext& ctx, GraphRawData& graphData) const;
47
49 StatusCode runInference(GraphRawData& graphData) const;
50
51protected:
52 static constexpr std::size_t kBucketFeatureCount = 6;
53 static constexpr std::size_t kNodeFeatureCount = 10;
54 static constexpr std::size_t kEdgeFeatureCount = 7;
55 static constexpr std::array<std::string_view, kNodeFeatureCount> kDefaultNodeFeatureNames = {
56 "segmentPositionX_m", "segmentPositionY_m", "segmentPositionZ_m",
57 "segmentDirectionX", "segmentDirectionY", "segmentDirectionZ",
58 "bucket_chamberIndex", "bucket_layers", "bucket_sector", "bucket_segments"};
59
60 StatusCode setupModel();
61 Ort::Session& model() const;
62
63 static std::string trimFeatureToken(std::string s);
64 static std::vector<std::string> parseFeatureNames(const std::string& raw);
65
67 StatusCode buildFeaturesOnly(const EventContext& ctx, GraphRawData& graphData) const;
68
71 StatusCode buildTransformerInputs(const EventContext& ctx, GraphRawData& graphData) const;
72
74 StatusCode runNamedInference(GraphRawData& graphData,
75 const std::vector<const char*>& inputNames,
76 const std::vector<const char*>& outputNames) const;
77
78 SG::ReadHandleKey<MuonR4::SpacePointContainer> m_readKey{this, "ReadSpacePoints", "MuonSpacePoints"};
79 ActsTrk::GeoContextReadKey_t m_geoCtxKey{this, "AlignmentKey", "ActsAlignment", "cond handle key"};
80
81 // ONNX I/O name for the GNN output tensor (model-dependent)
82 Gaudi::Property<std::string> m_outputName{this, "OutputName", "logits"};
83
84 // Sparse-graph parameters (GNN)
85 Gaudi::Property<int> m_minLayers{this, "MinLayersValid", 3};
86 Gaudi::Property<int> m_maxChamberDelta{this, "MaxChamberDelta", 13};
87 Gaudi::Property<int> m_maxSectorDelta{this, "MaxSectorDelta", 1};
88 Gaudi::Property<double> m_maxDistXY{this, "MaxDistXY", 6800.0};
89 Gaudi::Property<double> m_maxAbsDz{this, "MaxAbsDz", 15000.0};
90
91 // Debug/validation knobs
92 Gaudi::Property<unsigned int> m_debugDumpFirstNNodes{this, "DebugDumpFirstNNodes", 5};
93 Gaudi::Property<unsigned int> m_debugDumpFirstNEdges{this, "DebugDumpFirstNEdges", 12};
94 Gaudi::Property<bool> m_validateEdges{this, "ValidateEdges", true};
95 Gaudi::Property<bool> m_sanitizeNonFinitePredictions{
96 this, "SanitizeNonFinitePredictions", false,
97 "When true, replace non-finite ONNX outputs with -100 and log a warning."};
98
99 // CUDA / IoBinding state (set in setupModel via dynamic_cast)
100 bool m_isCuda{false};
102
103private:
104 ToolHandle<AthOnnx::IOnnxRuntimeSessionTool> m_onnxSessionTool{
105 this, "ModelSession", ""};
106};
107
108} // namespace MuonML
109
110#endif
111
Property holding a SG store/key/clid from which a ReadHandle is made.
StatusCode buildGraph(const EventContext &ctx, GraphRawData &graphData) const
GNN-style graph builder (features + edges). Kept for tools that want it.
StatusCode buildFeaturesOnly(const EventContext &ctx, GraphRawData &graphData) const
Build only features (N,6); attaches one tensor in graph.dataTensor[0].
static std::string trimFeatureToken(std::string s)
ActsTrk::GeoContextReadKey_t m_geoCtxKey
StatusCode buildTransformerInputs(const EventContext &ctx, GraphRawData &graphData) const
Build Transformer inputs: features [1,S,6] and pad_mask [1,S] (False = valid), as tensors 0 and 1.
static constexpr std::array< std::string_view, kNodeFeatureCount > kDefaultNodeFeatureNames
Gaudi::Property< std::string > m_outputName
static constexpr std::size_t kEdgeFeatureCount
Gaudi::Property< unsigned int > m_debugDumpFirstNEdges
Gaudi::Property< unsigned int > m_debugDumpFirstNNodes
static constexpr std::size_t kBucketFeatureCount
static std::vector< std::string > parseFeatureNames(const std::string &raw)
StatusCode runNamedInference(GraphRawData &graphData, const std::vector< const char * > &inputNames, const std::vector< const char * > &outputNames) const
Generic named inference, for tools with different I/O conventions.
Gaudi::Property< bool > m_sanitizeNonFinitePredictions
ToolHandle< AthOnnx::IOnnxRuntimeSessionTool > m_onnxSessionTool
StatusCode runInference(GraphRawData &graphData) const
Default ONNX run for GNN case: inputs {"features","edge_index"} -> outputs {"logits"}...
SG::ReadHandleKey< MuonR4::SpacePointContainer > m_readKey
~BucketInferenceToolBase() override=default
static constexpr std::size_t kNodeFeatureCount
Gaudi::Property< double > m_maxDistXY
Property holding a SG store/key/clid from which a ReadHandle is made.
SG::ReadHandleKey< GeometryContext > GeoContextReadKey_t
Abrivate the ReadHandleKey to declare the data dependency on the Geometry context.
Helper struct to ship the Graph from the space point buckets to ONNX.
Definition GraphData.h:25