ATLAS Offline Software
Loading...
Searching...
No Matches
EventReco::HyPERTtbarAllHadronicModel Class Reference

This class is in charge of loading the correct HyPER model based on the Ttbar all hadronic topology. More...

#include <HyPERTtbarAllHadronicModel.h>

Inheritance diagram for EventReco::HyPERTtbarAllHadronicModel:
Collaboration diagram for EventReco::HyPERTtbarAllHadronicModel:

Public Member Functions

 HyPERTtbarAllHadronicModel (const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnEven, const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnOdd)
std::vector< std::string > getInputNames () const override
 Input node names in the order the ONNX graph declares them.
std::vector< HyPEROutputNode > getModelOutputs () const override
 Every output node of the ONNX graph, in declaration order.
std::vector< std::string > getOutputNames () const override
 Model-specific output names consumed by the parser.
StatusCode initialize ()
 Resolve the node name -> node index maps.
void setTopology (HyPERTopology topology)=delete
HyPERTopology getTopology () const
template<typename T>
void setInputs (const std::string &node, std::vector< T > &values, const std::vector< int64_t > &shape)
 Bind an input by node name.
StatusCode evaluate (unsigned sessionIndex)
 Run the model of the requested fold over the bound inputs.
template<typename T>
T * getOutputs (const std::string &node)
 Pointer to the buffer of the named output.
void clearInputs ()
void clearOutputs ()
void printInputInfo (bool printContent=false) const
void printOutputInfo (bool printContent=false) const

Protected Attributes

HyPERTopology m_topology {HyPERTopology::NotSelected}

Private Types

using InputData = std::variant<std::vector<float>*, std::vector<int64_t>*>

Static Private Member Functions

template<typename T>
static std::string contentToString (const std::vector< T > &values)
static std::string shapeToString (const std::vector< int64_t > &shape)

Private Attributes

const AthOnnx::IOnnxRuntimeInferenceTool * m_toolTrainedOnEven {nullptr}
const AthOnnx::IOnnxRuntimeInferenceTool * m_toolTrainedOnOdd {nullptr}
std::map< std::string, std::size_t > m_inputIndex {}
std::vector< BoundInput > m_boundInputs {}
std::vector< HyPEROutputNode > m_outputNodes {}
std::map< std::string, std::size_t > m_outputIndex {}
std::vector< std::vector< float > > m_outputsFloat {}
std::vector< std::vector< int64_t > > m_outputsInt64 {}

Static Private Attributes

static constexpr const char * s_edgeIndexName = "edge_index"
 Input nodes whose shapes carry the graph's dynamic dimensions.
static constexpr const char * s_hyperEdgeIndexName = "edge_index_h"

Detailed Description

This class is in charge of loading the correct HyPER model based on the Ttbar all hadronic topology.

Definition at line 20 of file HyPERTtbarAllHadronicModel.h.

Member Typedef Documentation

◆ InputData

using EventReco::HyPERModel::InputData = std::variant<std::vector<float>*, std::vector<int64_t>*>
privateinherited

Definition at line 289 of file HyPERModel.h.

Constructor & Destructor Documentation

◆ HyPERTtbarAllHadronicModel()

EventReco::HyPERTtbarAllHadronicModel::HyPERTtbarAllHadronicModel ( const AthOnnx::IOnnxRuntimeInferenceTool * trainedOnEven,
const AthOnnx::IOnnxRuntimeInferenceTool * trainedOnOdd )
inline

Definition at line 22 of file HyPERTtbarAllHadronicModel.h.

25 : HyPERModel(trainedOnEven, trainedOnOdd,
HyPERModel(const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnEven, const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnOdd, HyPERTopology topology)
Definition HyPERModel.h:62

Member Function Documentation

◆ clearInputs()

void EventReco::HyPERModel::clearInputs ( )
inlineinherited

Definition at line 240 of file HyPERModel.h.

240 {
241 for (BoundInput& bound : m_boundInputs) bound = BoundInput{};
242 }
std::vector< BoundInput > m_boundInputs
Definition HyPERModel.h:313

◆ clearOutputs()

void EventReco::HyPERModel::clearOutputs ( )
inlineinherited

Definition at line 244 of file HyPERModel.h.

244 {
245 for (std::vector<float>& out : m_outputsFloat) out.clear();
246 for (std::vector<int64_t>& out : m_outputsInt64) out.clear();
247 }
std::vector< std::vector< float > > m_outputsFloat
Definition HyPERModel.h:317
std::vector< std::vector< int64_t > > m_outputsInt64
Definition HyPERModel.h:318

◆ contentToString()

template<typename T>
std::string EventReco::HyPERModel::contentToString ( const std::vector< T > & values)
inlinestaticprivateinherited

Definition at line 297 of file HyPERModel.h.

297 {
298 std::string row = "[";
299 for (const T& value : values) row += std::to_string(value) + ", ";
300 return row + "]";
301 }
row
Appending html table to final .html summary file.

◆ evaluate()

StatusCode EventReco::HyPERModel::evaluate ( unsigned sessionIndex)
inlineinherited

Run the model of the requested fold over the bound inputs.

Parameters
sessionIndex0 for the model trained on even events, 1 for odd.

Definition at line 158 of file HyPERModel.h.

158 {
159 using namespace asg::msgUserCode;
160
161 const AthOnnx::IOnnxRuntimeInferenceTool* tool =
162 (sessionIndex == 0) ? m_toolTrainedOnEven : m_toolTrainedOnOdd;
163
164 // Inputs, in the order the ONNX graph declares them.
165 std::vector<Ort::Value> inputTensors;
166 inputTensors.reserve(m_boundInputs.size());
167 for (std::size_t i = 0; i < m_boundInputs.size(); ++i) {
168 BoundInput& bound = m_boundInputs[i];
169 if (!bound.set) {
170 ANA_MSG_ERROR("HyPERModel: input node " << i << " was never set");
171 return StatusCode::FAILURE;
172 }
173 if (std::holds_alternative<std::vector<float>*>(bound.data)) {
174 inputTensors.push_back(AthOnnxUtils::createTensor(
175 *std::get<std::vector<float>*>(bound.data), bound.shape));
176 } else {
177 inputTensors.push_back(AthOnnxUtils::createTensor(
178 *std::get<std::vector<int64_t>*>(bound.data), bound.shape));
179 }
180 }
181
182 // The graph's dynamic dimensions, taken from the shapes of the index
183 // tensors: edge_index is {2, nEdges} and edge_index_h is {order, nHyper}.
184 const int64_t nEdges =
186 const int64_t nHyperEdges =
188
189 // Outputs, also in declaration order. ONNX Runtime writes straight into
190 // the buffers owned here, which getOutputs() then hands to the parser.
191 std::vector<Ort::Value> outputTensors;
192 outputTensors.reserve(m_outputNodes.size());
193 for (std::size_t i = 0; i < m_outputNodes.size(); ++i) {
194 const HyPEROutputNode& node = m_outputNodes[i];
195 int64_t leading = 1;
196 switch (node.dim) {
197 case HyPEROutputDim::HyperEdges: leading = nHyperEdges; break;
198 case HyPEROutputDim::Edges: leading = nEdges; break;
199 case HyPEROutputDim::Single: leading = 1; break;
200 }
201 std::vector<int64_t> shape{leading};
202 if (node.trailingOne) shape.push_back(1);
203
204 const int64_t size = AthOnnxUtils::getTensorSize(shape);
205 if (node.isFloat) {
206 m_outputsFloat[i].assign(static_cast<std::size_t>(size), 0.f);
207 outputTensors.push_back(
209 } else {
210 m_outputsInt64[i].assign(static_cast<std::size_t>(size), 0);
211 outputTensors.push_back(
213 }
214 }
215
216 return tool->inference(inputTensors, outputTensors);
217 }
#define ANA_MSG_ERROR(xmsg,...)
Macro printing error messages.
size_t size() const
Number of registered mappings.
const AthOnnx::IOnnxRuntimeInferenceTool * m_toolTrainedOnEven
Definition HyPERModel.h:309
static constexpr const char * s_edgeIndexName
Input nodes whose shapes carry the graph's dynamic dimensions.
Definition HyPERModel.h:286
static constexpr const char * s_hyperEdgeIndexName
Definition HyPERModel.h:287
std::vector< HyPEROutputNode > m_outputNodes
Definition HyPERModel.h:315
std::map< std::string, std::size_t > m_inputIndex
Definition HyPERModel.h:312
const AthOnnx::IOnnxRuntimeInferenceTool * m_toolTrainedOnOdd
Definition HyPERModel.h:310
int64_t getTensorSize(const std::vector< int64_t > &dataShape)
Ort::Value createTensor(std::vector< T > &data, const std::vector< int64_t > &dataShape)
Definition OnnxUtils.h:92

◆ getInputNames()

std::vector< std::string > EventReco::HyPERTtbarAllHadronicModel::getInputNames ( ) const
inlineoverridevirtual

Input node names in the order the ONNX graph declares them.

The order matters: ONNX Runtime is handed the tensors positionally.

Implements EventReco::HyPERModel.

Definition at line 30 of file HyPERTtbarAllHadronicModel.h.

30 {
31 return std::vector<std::string>{"x_s", "edge_index",
32 "edge_attr_s", "u_s",
33 "batch", "edge_index_h",
34 "edge_index_h_batch"};
35 }

◆ getModelOutputs()

std::vector< HyPEROutputNode > EventReco::HyPERTtbarAllHadronicModel::getModelOutputs ( ) const
inlineoverridevirtual

Every output node of the ONNX graph, in declaration order.

Implements EventReco::HyPERModel.

Definition at line 41 of file HyPERTtbarAllHadronicModel.h.

41 {
42 return std::vector<HyPEROutputNode>{
43 {"sigmoid_1", true, HyPEROutputDim::HyperEdges, true},
44 {"edge_index_h_batch", false, HyPEROutputDim::HyperEdges, false},
45 {"sigmoid", true, HyPEROutputDim::Edges, true}};
46 }

◆ getOutputNames()

std::vector< std::string > EventReco::HyPERTtbarAllHadronicModel::getOutputNames ( ) const
inlineoverridevirtual

Model-specific output names consumed by the parser.

Output names vary according to the number of message-passing layers etc. This must be defined for each and every topology.

Returns
std::vector<std::string> The output names of the model

Implements EventReco::HyPERModel.

Definition at line 48 of file HyPERTtbarAllHadronicModel.h.

48 {
49 return std::vector<std::string>{"sigmoid_1", "sigmoid"};
50 }

◆ getOutputs()

template<typename T>
T * EventReco::HyPERModel::getOutputs ( const std::string & node)
inlineinherited

Pointer to the buffer of the named output.

Valid until the next clearOutputs() or evaluate().

Definition at line 224 of file HyPERModel.h.

224 {
225 using namespace asg::msgUserCode;
226 static_assert(std::is_same_v<T, float> || std::is_same_v<T, int64_t>,
227 "HyPER ONNX outputs must be float or int64_t");
228 const auto it = m_outputIndex.find(node);
229 if (it == m_outputIndex.end()) {
230 ANA_MSG_ERROR("HyPERModel: unknown output node '" << node << "'");
231 return nullptr;
232 }
233 if constexpr (std::is_same_v<T, float>) {
234 return m_outputsFloat[it->second].data();
235 } else {
236 return m_outputsInt64[it->second].data();
237 }
238 }
std::map< std::string, std::size_t > m_outputIndex
Definition HyPERModel.h:316

◆ getTopology()

HyPERTopology EventReco::HyPERModel::getTopology ( ) const
inlineinherited

Definition at line 112 of file HyPERModel.h.

112{ return m_topology; }
HyPERTopology m_topology
Definition HyPERModel.h:281

◆ initialize()

StatusCode EventReco::HyPERModel::initialize ( )
inlineinherited

Resolve the node name -> node index maps.

Must be called once before the first evaluate(). This cannot live in the constructor because it relies on the topology-specific virtual methods below.

Definition at line 76 of file HyPERModel.h.

76 {
77 using namespace asg::msgUserCode;
79 ANA_MSG_ERROR("HyPERModel: ONNX inference tools have not been set");
80 return StatusCode::FAILURE;
81 }
82
83 const std::vector<std::string> inputNames = getInputNames();
84 m_inputIndex.clear();
85 for (std::size_t i = 0; i < inputNames.size(); ++i) {
86 m_inputIndex[inputNames[i]] = i;
87 }
88 m_boundInputs.assign(inputNames.size(), BoundInput{});
89
91 m_outputIndex.clear();
92 for (std::size_t i = 0; i < m_outputNodes.size(); ++i) {
94 }
95 m_outputsFloat.assign(m_outputNodes.size(), std::vector<float>{});
96 m_outputsInt64.assign(m_outputNodes.size(), std::vector<int64_t>{});
97
98 // The dynamic output dimensions are read back off these two inputs.
99 if (m_inputIndex.find(s_edgeIndexName) == m_inputIndex.end() ||
101 ANA_MSG_ERROR("HyPERModel: the input names must contain '"
102 << s_edgeIndexName << "' and '" << s_hyperEdgeIndexName
103 << "'");
104 return StatusCode::FAILURE;
105 }
106
107 return StatusCode::SUCCESS;
108 }
virtual std::vector< HyPEROutputNode > getModelOutputs() const =0
Every output node of the ONNX graph, in declaration order.
virtual std::vector< std::string > getInputNames() const =0
Input node names in the order the ONNX graph declares them.

◆ printInputInfo()

void EventReco::HyPERModel::printInputInfo ( bool printContent = false) const
inlineinherited

Definition at line 249 of file HyPERModel.h.

249 {
250 using namespace asg::msgUserCode;
251 const std::vector<std::string> names = getInputNames();
252 for (std::size_t i = 0; i < m_boundInputs.size(); ++i) {
253 const BoundInput& bound = m_boundInputs[i];
254 ANA_MSG_INFO(" input " << i << " '" << names[i]
255 << "' shape=" << shapeToString(bound.shape));
256 if (!printContent || !bound.set) continue;
257 if (std::holds_alternative<std::vector<float>*>(bound.data)) {
259 *std::get<std::vector<float>*>(bound.data)));
260 } else {
262 *std::get<std::vector<int64_t>*>(bound.data)));
263 }
264 }
265 }
#define ANA_MSG_INFO(xmsg,...)
Macro printing info messages.
static std::string contentToString(const std::vector< T > &values)
Definition HyPERModel.h:297
static std::string shapeToString(const std::vector< int64_t > &shape)
Definition HyPERModel.h:303

◆ printOutputInfo()

void EventReco::HyPERModel::printOutputInfo ( bool printContent = false) const
inlineinherited

Definition at line 267 of file HyPERModel.h.

267 {
268 using namespace asg::msgUserCode;
269 for (std::size_t i = 0; i < m_outputNodes.size(); ++i) {
270 ANA_MSG_INFO(" output " << i << " '" << m_outputNodes[i].name << "'");
271 if (!printContent) continue;
272 if (m_outputNodes[i].isFloat) {
274 } else {
276 }
277 }
278 }

◆ setInputs()

template<typename T>
void EventReco::HyPERModel::setInputs ( const std::string & node,
std::vector< T > & values,
const std::vector< int64_t > & shape )
inlineinherited

Bind an input by node name.

The tensor aliases values, so the caller must keep it alive and unmodified until after evaluate().

Definition at line 138 of file HyPERModel.h.

139 {
140 using namespace asg::msgUserCode;
141 static_assert(std::is_same_v<T, float> || std::is_same_v<T, int64_t>,
142 "HyPER ONNX inputs must be float or int64_t");
143 const auto it = m_inputIndex.find(node);
144 if (it == m_inputIndex.end()) {
145 ANA_MSG_ERROR("HyPERModel: unknown input node '" << node << "'");
146 return;
147 }
148 BoundInput& bound = m_boundInputs[it->second];
149 bound.data = &values;
150 bound.shape = shape;
151 bound.set = true;
152 }

◆ setTopology()

void EventReco::HyPERModel::setTopology ( HyPERTopology topology)
deleteinherited

◆ shapeToString()

std::string EventReco::HyPERModel::shapeToString ( const std::vector< int64_t > & shape)
inlinestaticprivateinherited

Definition at line 303 of file HyPERModel.h.

303 {
304 std::string row = "(";
305 for (const int64_t dim : shape) row += std::to_string(dim) + ", ";
306 return row + ")";
307 }

Member Data Documentation

◆ m_boundInputs

std::vector<BoundInput> EventReco::HyPERModel::m_boundInputs {}
privateinherited

Definition at line 313 of file HyPERModel.h.

313{};

◆ m_inputIndex

std::map<std::string, std::size_t> EventReco::HyPERModel::m_inputIndex {}
privateinherited

Definition at line 312 of file HyPERModel.h.

312{};

◆ m_outputIndex

std::map<std::string, std::size_t> EventReco::HyPERModel::m_outputIndex {}
privateinherited

Definition at line 316 of file HyPERModel.h.

316{};

◆ m_outputNodes

std::vector<HyPEROutputNode> EventReco::HyPERModel::m_outputNodes {}
privateinherited

Definition at line 315 of file HyPERModel.h.

315{};

◆ m_outputsFloat

std::vector<std::vector<float> > EventReco::HyPERModel::m_outputsFloat {}
privateinherited

Definition at line 317 of file HyPERModel.h.

317{};

◆ m_outputsInt64

std::vector<std::vector<int64_t> > EventReco::HyPERModel::m_outputsInt64 {}
privateinherited

Definition at line 318 of file HyPERModel.h.

318{};

◆ m_toolTrainedOnEven

const AthOnnx::IOnnxRuntimeInferenceTool* EventReco::HyPERModel::m_toolTrainedOnEven {nullptr}
privateinherited

Definition at line 309 of file HyPERModel.h.

309{nullptr};

◆ m_toolTrainedOnOdd

const AthOnnx::IOnnxRuntimeInferenceTool* EventReco::HyPERModel::m_toolTrainedOnOdd {nullptr}
privateinherited

Definition at line 310 of file HyPERModel.h.

310{nullptr};

◆ m_topology

HyPERTopology EventReco::HyPERModel::m_topology {HyPERTopology::NotSelected}
protectedinherited

Definition at line 281 of file HyPERModel.h.

◆ s_edgeIndexName

const char* EventReco::HyPERModel::s_edgeIndexName = "edge_index"
staticconstexprprivateinherited

Input nodes whose shapes carry the graph's dynamic dimensions.

These are named identically in every HyPER topology.

Definition at line 286 of file HyPERModel.h.

◆ s_hyperEdgeIndexName

const char* EventReco::HyPERModel::s_hyperEdgeIndexName = "edge_index_h"
staticconstexprprivateinherited

Definition at line 287 of file HyPERModel.h.


The documentation for this class was generated from the following file: