ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaExamples
AthExTriton
src
ExampleMLInferenceWithTriton.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).
4
#include "
ExampleMLInferenceWithTriton.h
"
5
6
#include "
EvaluateUtils.h
"
7
8
// Framework include(s).
9
#include <arpa/inet.h>
10
11
#include "
PathResolver/PathResolver.h
"
12
13
// Standard include(s)
14
#include <ranges>
15
#include <utility>
//std::pair
16
17
namespace
AthInfer
{
18
19
StatusCode
ExampleMLInferenceWithTriton::initialize
() {
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 =
30
PathResolver::find_calib_file
(
m_pixelFileName
.value());
31
ATH_MSG_INFO
(
"Using pixel file: "
<< pixelFilePath);
32
33
try
{
34
m_input_tensor_values_notFlat
=
35
EvaluateUtils::read_mnist_pixel_notFlat
(pixelFilePath);
36
ATH_MSG_INFO
(
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 ("
54
<<
m_input_tensor_values_notFlat
.size()
55
<<
") is not a multiple of the requested batch size ("
56
<<
m_batchSize
.value() <<
")"
);
57
return
StatusCode::FAILURE;
58
}
59
ATH_MSG_INFO
(
"Running "
<<
m_input_tensor_values_notFlat
.size() /
60
m_batchSize
.value()
61
<<
" batches of "
<<
m_batchSize
.value());
62
return
StatusCode::SUCCESS;
63
}
64
65
StatusCode
ExampleMLInferenceWithTriton::execute
(
66
[[maybe_unused]]
const
EventContext& ctx)
const
{
67
// We know we have at least one image, otherwise we would have errored out
68
// earlier
69
const
std::size_t n_batches =
70
m_input_tensor_values_notFlat
.size() /
m_batchSize
.value();
71
const
auto
n_rows = std::int64_t(
m_input_tensor_values_notFlat
[0].
size
());
72
const
auto
n_cols = std::int64_t(
m_input_tensor_values_notFlat
[0][0].
size
());
73
74
for
(std::size_t batch_idx = 0; batch_idx < n_batches; ++batch_idx) {
75
// prepare inputs
76
std::vector<float> inputDataVector;
77
inputDataVector.reserve(
m_batchSize
.value() * n_rows * n_cols);
78
for
(
const
std::vector<std::vector<float>>& imageData :
79
m_input_tensor_values_notFlat
|
80
std::views::drop(batch_idx *
m_batchSize
.value()) |
81
std::views::take(
m_batchSize
.value())) {
82
std::vector<float> flatten =
83
EvaluateUtils::flattenNestedVectors
(imageData);
84
inputDataVector.insert(inputDataVector.end(), flatten.begin(),
85
flatten.end());
86
}
87
88
std::vector<int64_t> inputShape = {
m_batchSize
.value(), n_rows, n_cols};
89
90
AthInfer::InputDataMap
inputData;
91
inputData[
"flatten_input:0"
] =
92
std::make_pair(inputShape, std::move(inputDataVector));
93
94
const
std::int64_t n_scores = 10;
95
AthInfer::OutputDataMap
outputData;
96
outputData[
"dense_1/Softmax:0"
] = std::make_pair(
97
std::vector<int64_t>{
m_batchSize
, n_scores}, std::vector<float>{});
98
99
ATH_CHECK
(
m_tritonTool
->inference(inputData, outputData));
100
101
auto
const
& outputScores =
102
std::get<std::vector<float>>(outputData[
"dense_1/Softmax:0"
].second);
103
104
if
(outputScores.size() != std::size_t(n_scores *
m_batchSize
.value())) {
105
ATH_MSG_ERROR
(
"Got back "
<< outputScores.size()
106
<<
" scores when it should have been "
107
<< n_scores <<
" * "
<<
m_batchSize
.value()
108
<<
" = "
<< n_scores *
m_batchSize
.value());
109
return
StatusCode::FAILURE;
110
}
111
112
for
(
int
img_idx = 0; img_idx <
m_batchSize
.value(); img_idx++) {
113
std::span scores(outputScores.begin() + img_idx * n_scores,
114
outputScores.begin() + (img_idx + 1) * n_scores);
115
ATH_MSG_DEBUG
(
"Scores for img "
<< img_idx <<
" of batch "
<< batch_idx
116
<<
": "
117
<<
EvaluateUtils::spanToString
(scores));
118
const
auto
max_elem = std::ranges::max_element(scores);
119
ATH_MSG_DEBUG
(
"Class: "
<< max_elem - scores.begin()
120
<<
" has the highest score: "
<< *max_elem
121
<<
" in img "
<< img_idx <<
" of batch "
122
<< batch_idx);
123
}
124
}
125
return
StatusCode::SUCCESS;
126
}
127
}
// namespace AthInfer
ATH_CHECK
#define ATH_CHECK
Evaluate an expression and check for errors.
Definition
AthCheckMacros.h:40
EvaluateUtils.h
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition
AthMsgStreamMacros.h:33
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition
AthMsgStreamMacros.h:31
ATH_MSG_DEBUG
#define ATH_MSG_DEBUG(x)
Definition
AthMsgStreamMacros.h:29
ExampleMLInferenceWithTriton.h
PathResolver.h
size
size_t size() const
Number of registered mappings.
AthInfer::ExampleMLInferenceWithTriton::execute
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm for a single event.
Definition
ExampleMLInferenceWithTriton.cxx:65
AthInfer::ExampleMLInferenceWithTriton::initialize
virtual StatusCode initialize() override
Function initialising the algorithm.
Definition
ExampleMLInferenceWithTriton.cxx:19
AthInfer::ExampleMLInferenceWithTriton::m_batchSize
Gaudi::Property< int > m_batchSize
Following properties needed to be consdered if the .onnx model is evaluated in batch mode.
Definition
ExampleMLInferenceWithTriton.h:48
AthInfer::ExampleMLInferenceWithTriton::m_input_tensor_values_notFlat
std::vector< std::vector< std::vector< float > > > m_input_tensor_values_notFlat
Definition
ExampleMLInferenceWithTriton.h:55
AthInfer::ExampleMLInferenceWithTriton::m_tritonTool
ToolHandle< AthInfer::IAthInferenceTool > m_tritonTool
Tool handle for the Triton client.
Definition
ExampleMLInferenceWithTriton.h:52
AthInfer::ExampleMLInferenceWithTriton::m_pixelFileName
Gaudi::Property< std::string > m_pixelFileName
Name of the model file to load.
Definition
ExampleMLInferenceWithTriton.h:42
PathResolver::find_calib_file
static std::string find_calib_file(const std::string &logical_file_name)
Definition
PathResolver.cxx:272
AthInfer
Definition
ExampleAsyncMLInferenceWithTriton.cxx:17
AthInfer::OutputDataMap
std::map< std::string, InferenceData > OutputDataMap
Definition
IAthInferenceTool.h:18
AthInfer::InputDataMap
std::map< std::string, InferenceData > InputDataMap
Definition
IAthInferenceTool.h:17
EvaluateUtils::read_mnist_pixel_notFlat
std::vector< std::vector< std::vector< float > > > read_mnist_pixel_notFlat(const std::string &full_path)
Definition
AthExOnnxRuntime/src/EvaluateUtils.cxx:14
EvaluateUtils::spanToString
auto spanToString
Definition
AthExOnnxRuntime/src/EvaluateUtils.h:14
EvaluateUtils::flattenNestedVectors
std::vector< float > flattenNestedVectors(const std::vector< std::vector< float > > &nestedVector)
Definition
AthExTriton/src/EvaluateUtils.cxx:77
Generated on
for ATLAS Offline Software by
1.17.0