ATLAS Offline Software
Toggle main menu visibility
Loading...
Searching...
No Matches
Control
AthenaExamples
AthExTriton
src
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).
4
#include "
ExampleAsyncMLInferenceWithTriton.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
ExampleAsyncMLInferenceWithTriton::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
return
StatusCode::SUCCESS;
60
}
61
62
StatusCode
ExampleAsyncMLInferenceWithTriton::execute
(
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 =
67
m_input_tensor_values_notFlat
.size() /
m_batchSize
.value();
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 :
76
m_input_tensor_values_notFlat
|
77
std::views::drop(batch_idx *
m_batchSize
.value()) |
78
std::views::take(
m_batchSize
.value())) {
79
std::vector<float> flatten =
80
EvaluateUtils::flattenNestedVectors
(imageData);
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
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
ExampleAsyncMLInferenceWithTriton.h
PathResolver.h
size
size_t size() const
Number of registered mappings.
AthInfer::ExampleAsyncMLInferenceWithTriton::initialize
virtual StatusCode initialize() override
Function initialising the algorithm.
Definition
ExampleAsyncMLInferenceWithTriton.cxx:19
AthInfer::ExampleAsyncMLInferenceWithTriton::m_pixelFileName
Gaudi::Property< std::string > m_pixelFileName
Name of the model file to load.
Definition
ExampleAsyncMLInferenceWithTriton.h:42
AthInfer::ExampleAsyncMLInferenceWithTriton::m_tritonTool
ToolHandle< AthInfer::IAthInferenceTool > m_tritonTool
Tool handle for the Triton client.
Definition
ExampleAsyncMLInferenceWithTriton.h:52
AthInfer::ExampleAsyncMLInferenceWithTriton::m_input_tensor_values_notFlat
std::vector< std::vector< std::vector< float > > > m_input_tensor_values_notFlat
Definition
ExampleAsyncMLInferenceWithTriton.h:55
AthInfer::ExampleAsyncMLInferenceWithTriton::execute
virtual StatusCode execute(const EventContext &ctx) const override
Function executing the algorithm for a single event.
Definition
ExampleAsyncMLInferenceWithTriton.cxx:62
AthInfer::ExampleAsyncMLInferenceWithTriton::m_batchSize
Gaudi::Property< int > m_batchSize
Following properties needed to be consdered if the .onnx model is evaluated in batch mode.
Definition
ExampleAsyncMLInferenceWithTriton.h:48
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