ATLAS Offline Software
Loading...
Searching...
No Matches
ExampleAsyncMLInferenceWithTriton.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
2
3// Local include(s).
5
6#include "EvaluateUtils.h"
7
8// Framework include(s).
9#include <arpa/inet.h>
10
12
13// Standard include(s)
14#include <ranges>
15#include <utility> //std::pair
16
17namespace AthInfer {
18
20 if (m_batchSize.value() < 1) {
21 ATH_MSG_ERROR("Requested an invalid batch size: " << m_batchSize.value());
22 return StatusCode::FAILURE;
23 }
24
25 // Fetch tools
26 ATH_CHECK(m_tritonTool.retrieve());
27
28 // read input file, and the target file for comparison.
29 std::string pixelFilePath =
31 ATH_MSG_INFO("Using pixel file: " << pixelFilePath);
32
33 try {
37 "Total no. of samples: " << m_input_tensor_values_notFlat.size());
38 } catch (const std::exception& e) {
39 ATH_MSG_ERROR(e.what());
40 return StatusCode::FAILURE;
41 }
42
43 if (std::size_t(m_batchSize.value()) > m_input_tensor_values_notFlat.size()) {
44 ATH_MSG_ERROR("The batch size requested ("
45 << m_batchSize.value()
46 << ") is greater than the number of available "
47 "samples ("
48 << m_input_tensor_values_notFlat.size() << ")");
49 return StatusCode::FAILURE;
50 }
51
52 if (m_input_tensor_values_notFlat.size() % m_batchSize.value() != 0) {
53 ATH_MSG_ERROR("The number of samples ("
55 << ") is not a multiple of the requested batch size ("
56 << m_batchSize.value() << ")");
57 return StatusCode::FAILURE;
58 }
59 return StatusCode::SUCCESS;
60}
61
63 [[maybe_unused]] const EventContext& ctx) const {
64 // We know we have at least one image, otherwise we would have errored out
65 // earlier
66 const std::size_t n_batches =
68 const auto n_rows = std::int64_t(m_input_tensor_values_notFlat[0].size());
69 const auto n_cols = std::int64_t(m_input_tensor_values_notFlat[0][0].size());
70
71 for (std::size_t batch_idx = 0; batch_idx < n_batches; ++batch_idx) {
72 // prepare inputs
73 std::vector<float> inputDataVector;
74 inputDataVector.reserve(m_batchSize.value() * n_rows * n_cols);
75 for (const std::vector<std::vector<float>>& imageData :
77 std::views::drop(batch_idx * m_batchSize.value()) |
78 std::views::take(m_batchSize.value())) {
79 std::vector<float> flatten =
81 inputDataVector.insert(inputDataVector.end(), flatten.begin(),
82 flatten.end());
83 }
84
85 std::vector<int64_t> inputShape = {m_batchSize.value(), n_rows, n_cols};
86
87 AthInfer::InputDataMap inputData;
88 inputData["flatten_input:0"] =
89 std::make_pair(inputShape, std::move(inputDataVector));
90
91 const std::int64_t n_scores = 10;
92 AthInfer::OutputDataMap outputData;
93 outputData["dense_1/Softmax:0"] = std::make_pair(
94 std::vector<int64_t>{m_batchSize, n_scores}, std::vector<float>{});
95
96 ATH_CHECK(m_tritonTool->inference(inputData, outputData));
97
98 auto const& outputScores =
99 std::get<std::vector<float>>(outputData["dense_1/Softmax:0"].second);
100
101 if (outputScores.size() != std::size_t(n_scores * m_batchSize.value())) {
102 ATH_MSG_ERROR("Got back " << outputScores.size()
103 << " scores when it should have been "
104 << n_scores << " * " << m_batchSize.value()
105 << " = " << n_scores * m_batchSize.value());
106 return StatusCode::FAILURE;
107 }
108
109 for (int img_idx = 0; img_idx < m_batchSize.value(); img_idx++) {
110 std::span scores(outputScores.begin() + img_idx * n_scores,
111 outputScores.begin() + (img_idx + 1) * n_scores);
112 ATH_MSG_DEBUG("Scores for img " << img_idx << " of batch " << batch_idx
113 << ": "
114 << EvaluateUtils::spanToString(scores));
115 const auto max_elem = std::ranges::max_element(scores);
116 ATH_MSG_DEBUG("Class: " << max_elem - scores.begin()
117 << " has the highest score: " << *max_elem
118 << " in img " << img_idx << " of batch "
119 << batch_idx);
120 }
121 }
122 return StatusCode::SUCCESS;
123}
124} // namespace AthInfer
#define ATH_CHECK
Evaluate an expression and check for errors.
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
#define ATH_MSG_DEBUG(x)
size_t size() const
Number of registered mappings.
virtual StatusCode initialize() override
Function initialising the algorithm.
Gaudi::Property< std::string > m_pixelFileName
Name of the model file to load.
ToolHandle< AthInfer::IAthInferenceTool > m_tritonTool
Tool handle for the Triton client.
std::vector< std::vector< std::vector< float > > > m_input_tensor_values_notFlat
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm for a single event.
Gaudi::Property< int > m_batchSize
Following properties needed to be consdered if the .onnx model is evaluated in batch mode.
static std::string find_calib_file(const std::string &logical_file_name)
std::map< std::string, InferenceData > OutputDataMap
std::map< std::string, InferenceData > InputDataMap
std::vector< std::vector< std::vector< float > > > read_mnist_pixel_notFlat(const std::string &full_path)
std::vector< float > flattenNestedVectors(const std::vector< std::vector< float > > &nestedVector)