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

This class is in charge of loading the correct HyPER model based on the Ttbar di-lepton topology. More...

#include <HyPERTtbarDiLeptonModel.h>

Inheritance diagram for EventReco::HyPERTtbarDiLeptonModel:
Collaboration diagram for EventReco::HyPERTtbarDiLeptonModel:

Public Member Functions

 HyPERTtbarDiLeptonModel (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, std::less<> > 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 di-lepton topology.

Definition at line 20 of file HyPERTtbarDiLeptonModel.h.

Member Typedef Documentation

◆ InputData

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

Definition at line 288 of file HyPERModel.h.

Constructor & Destructor Documentation

◆ HyPERTtbarDiLeptonModel()

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

Definition at line 22 of file HyPERTtbarDiLeptonModel.h.

25 : HyPERModel(trainedOnEven, trainedOnOdd, HyPERTopology::TtbarDiLepton) {}
HyPERModel(const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnEven, const AthOnnx::IOnnxRuntimeInferenceTool *trainedOnOdd, HyPERTopology topology)
Definition HyPERModel.h:61

Member Function Documentation

◆ clearInputs()

void EventReco::HyPERModel::clearInputs ( )
inlineinherited

Definition at line 239 of file HyPERModel.h.

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

◆ clearOutputs()

void EventReco::HyPERModel::clearOutputs ( )
inlineinherited

Definition at line 243 of file HyPERModel.h.

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

◆ contentToString()

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

Definition at line 296 of file HyPERModel.h.

296 {
297 std::string row = "[";
298 for (const T& value : values) row += std::to_string(value) + ", ";
299 return row + "]";
300 }
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 157 of file HyPERModel.h.

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

28 {
29 return std::vector<std::string>{"x_s", "edge_index",
30 "edge_attr_s", "u_s",
31 "batch", "edge_index_h",
32 "batch_hyperedge"};
33 }

◆ getModelOutputs()

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

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

Implements EventReco::HyPERModel.

Definition at line 38 of file HyPERTtbarDiLeptonModel.h.

38 {
39 return std::vector<HyPEROutputNode>{
40 {"hyperedge_prime", true, HyPEROutputDim::HyperEdges, true},
41 {"batch_hyperedge", false, HyPEROutputDim::HyperEdges, false},
42 {"edge_prime", true, HyPEROutputDim::Edges, true},
43 {"classification_score", true, HyPEROutputDim::Single, true}};
44 }

◆ getOutputNames()

std::vector< std::string > EventReco::HyPERTtbarDiLeptonModel::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 46 of file HyPERTtbarDiLeptonModel.h.

46 {
47 return std::vector<std::string>{"hyperedge_prime", "edge_prime",
48 "classification_score"};
49 }

◆ 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 223 of file HyPERModel.h.

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

◆ getTopology()

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

Definition at line 111 of file HyPERModel.h.

111{ return m_topology; }
HyPERTopology m_topology
Definition HyPERModel.h:280

◆ 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 75 of file HyPERModel.h.

75 {
76 using namespace asg::msgUserCode;
78 ANA_MSG_ERROR("HyPERModel: ONNX inference tools have not been set");
79 return StatusCode::FAILURE;
80 }
81
82 const std::vector<std::string> inputNames = getInputNames();
83 m_inputIndex.clear();
84 for (std::size_t i = 0; i < inputNames.size(); ++i) {
85 m_inputIndex[inputNames[i]] = i;
86 }
87 m_boundInputs.assign(inputNames.size(), BoundInput{});
88
90 m_outputIndex.clear();
91 for (std::size_t i = 0; i < m_outputNodes.size(); ++i) {
93 }
94 m_outputsFloat.assign(m_outputNodes.size(), std::vector<float>{});
95 m_outputsInt64.assign(m_outputNodes.size(), std::vector<int64_t>{});
96
97 // The dynamic output dimensions are read back off these two inputs.
98 if (m_inputIndex.find(s_edgeIndexName) == m_inputIndex.end() ||
100 ANA_MSG_ERROR("HyPERModel: the input names must contain '"
101 << s_edgeIndexName << "' and '" << s_hyperEdgeIndexName
102 << "'");
103 return StatusCode::FAILURE;
104 }
105
106 return StatusCode::SUCCESS;
107 }
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 248 of file HyPERModel.h.

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

◆ printOutputInfo()

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

Definition at line 266 of file HyPERModel.h.

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

◆ 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 137 of file HyPERModel.h.

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

◆ setTopology()

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

◆ shapeToString()

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

Definition at line 302 of file HyPERModel.h.

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

Member Data Documentation

◆ m_boundInputs

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

Definition at line 312 of file HyPERModel.h.

312{};

◆ m_inputIndex

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

Definition at line 311 of file HyPERModel.h.

311{};

◆ m_outputIndex

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

Definition at line 315 of file HyPERModel.h.

315{};

◆ m_outputNodes

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

Definition at line 314 of file HyPERModel.h.

314{};

◆ m_outputsFloat

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

Definition at line 316 of file HyPERModel.h.

316{};

◆ m_outputsInt64

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

Definition at line 317 of file HyPERModel.h.

317{};

◆ m_toolTrainedOnEven

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

Definition at line 308 of file HyPERModel.h.

308{nullptr};

◆ m_toolTrainedOnOdd

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

Definition at line 309 of file HyPERModel.h.

309{nullptr};

◆ m_topology

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

Definition at line 280 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 285 of file HyPERModel.h.

◆ s_hyperEdgeIndexName

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

Definition at line 286 of file HyPERModel.h.


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