ATLAS Offline Software
TritonTool.cxx
Go to the documentation of this file.
1 // Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2 
4 
5 namespace tc = triton::client;
6 
8  const std::string& name,
9  const IInterface* parent)
10  : base_class(type, name, parent)
11 {
12  declareInterface<AthInfer::IAthInferenceTool>(this);
13 }
14 
16 
17  m_options = std::make_unique<tc::InferOptions>(m_modelName.value());
18  m_options->model_version_ = m_modelVersion;
19  m_options->client_timeout_ = m_clientTimeout;
20 
21  return getClient()? StatusCode::SUCCESS : StatusCode::FAILURE;
22 }
23 
24 tc::InferenceServerGrpcClient* AthInfer::TritonTool::getClient() const {
25  thread_local std::unique_ptr<tc::InferenceServerGrpcClient> threadClient;
26  if (!threadClient) {
27  std::string url = m_url.value() + ":" + std::to_string(m_port); // always use the gRPC port
28 
29  bool verbose = false;
30 
31  tc::Error err = tc::InferenceServerGrpcClient::Create(&threadClient, url, verbose, m_useSSL);
32  if (!err.IsOk()) {
33  ATH_MSG_ERROR("Failed to create Triton gRPC client for model: " + m_modelName.value() + " at url: " + url);
34  ATH_MSG_ERROR("Error message: " + err.Message());
35  return nullptr;
36  }
37 
38  ATH_MSG_INFO("Triton client created for model: "+ m_modelName.value() + " at url: "+ url);
39 
40  }
41  return threadClient.get();
42 }
43 
45 
46  // Create the tensor for the input data.
47  // Use shared_ptr to manage the memory of the InferInput objects.
48  std::vector<std::shared_ptr<tc::InferInput> > inputs_;
49  inputs_.reserve(inputData.size());
50 
51  for (auto& [inputName, inputInfo]: inputData) {
52  const std::vector<int64_t>& inputShape = inputInfo.first;
53  const auto& variant = inputInfo.second;
54 
55  const auto status = std::visit([&](const auto& dataVec) {
56  using T = std::decay_t<decltype(dataVec[0])>;
57  return prepareInput<T>(inputName, inputShape, dataVec, inputs_);
58  }, variant);
59 
60  if (status != StatusCode::SUCCESS) return status;
61  }
62 
63  // construct raw points for inference
64  std::vector<tc::InferInput*> rawInputs;
65  for (auto& input: inputs_) {
66  rawInputs.push_back(input.get());
67  }
68 
69  // perform the inference.
70  tc::InferResult* rawResultPtr = nullptr;
71  tc::Headers http_headers;
72  grpc_compression_algorithm compression_algorithm =
73  grpc_compression_algorithm::GRPC_COMPRESS_NONE;
74 
76  getClient()->Infer(
77  &rawResultPtr, *m_options, rawInputs, {}, http_headers, compression_algorithm),
78  "unable to run model "+ m_modelName.value() + " error: " + err.Message()
79  );
80 
81  std::shared_ptr<tc::InferResult> results(rawResultPtr);
82 
83  // Get the result of the inference.
84  for (auto& [outputName, outputInfo]: outputData) {
85  auto& variant = outputInfo.second;
86 
87  const auto status = std::visit([&](auto& dataVec) {
88  using T = std::decay_t<decltype(dataVec[0])>;
89  return extractOutput<T>(outputName, results, dataVec);
90  }, variant);
91 
92  if (status != StatusCode::SUCCESS) return status;
93  }
94  return StatusCode::SUCCESS;
95 }
ATH_MSG_INFO
#define ATH_MSG_INFO(x)
Definition: AthMsgStreamMacros.h:31
TrigInDetValidation_menu_test.tc
tc
Definition: TrigInDetValidation_menu_test.py:8
AthInfer::TritonTool::TritonTool
TritonTool()=delete
AthInfer::TritonTool::inference
virtual StatusCode inference(InputDataMap &inputData, OutputDataMap &outputData) const override final
Definition: TritonTool.cxx:44
physics_parameters.url
string url
Definition: physics_parameters.py:27
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
rerun_display.client
client
Definition: rerun_display.py:31
AthInfer::TritonTool::initialize
StatusCode initialize() override final
Definition: TritonTool.cxx:15
ATH_MSG_ERROR
#define ATH_MSG_ERROR(x)
Definition: AthMsgStreamMacros.h:33
dqt_zlumi_pandas.err
err
Definition: dqt_zlumi_pandas.py:183
EL::StatusCode
::StatusCode StatusCode
StatusCode definition for legacy code.
Definition: PhysicsAnalysis/D3PDTools/EventLoop/EventLoop/StatusCode.h:22
PlotPulseshapeFromCool.input
input
Definition: PlotPulseshapeFromCool.py:106
AthInfer::OutputDataMap
std::map< std::string, InferenceData > OutputDataMap
Definition: IAthInferenceTool.h:17
test_pyathena.parent
parent
Definition: test_pyathena.py:15
add-xsec-uncert-quadrature-N.results
dictionary results
Definition: add-xsec-uncert-quadrature-N.py:39
AthInfer::TritonTool::getClient
tc::InferenceServerGrpcClient * getClient() const
Definition: TritonTool.cxx:24
FAIL_IF_ERR
#define FAIL_IF_ERR(X, MSG)
Definition: TritonTool.h:21
name
std::string name
Definition: Control/AthContainers/Root/debug.cxx:240
ActsTrk::to_string
std::string to_string(const DetectorType &type)
Definition: GeometryDefs.h:34
TritonTool.h
lumiFormat.outputName
string outputName
Definition: lumiFormat.py:65
python.TriggerHandler.verbose
verbose
Definition: TriggerHandler.py:296
L1Topo::Error
Error
The different types of error that can be flagged in the L1TopoRDO.
Definition: Error.h:16
merge.status
status
Definition: merge.py:16
AthInfer::InputDataMap
std::map< std::string, InferenceData > InputDataMap
Definition: IAthInferenceTool.h:16
TSU::T
unsigned long long T
Definition: L1TopoDataTypes.h:35