ATLAS Offline Software
Loading...
Searching...
No Matches
TritonTool.cxx
Go to the documentation of this file.
1// Copyright (C) 2002-2025 CERN for the benefit of the ATLAS collaboration
2
4
5namespace 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
24tc::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
44StatusCode AthInfer::TritonTool::inference(InputDataMap& inputData, OutputDataMap& outputData) const {
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}
#define ATH_MSG_ERROR(x)
#define ATH_MSG_INFO(x)
static Double_t tc
bool extractOutput(const std::string &name, const std::shared_ptr< tc::InferResult > &result, std::vector< T > &outputVec)
bool prepareInput(const std::string &name, const std::vector< int64_t > &shape, const std::vector< T > &data, std::vector< std::shared_ptr< tc::InferInput > > &inputs)
#define FAIL_IF_ERR(X, MSG)
Definition TritonTool.h:21
StatusCode initialize() override final
virtual StatusCode inference(InputDataMap &inputData, OutputDataMap &outputData) const override final
StringProperty m_modelVersion
Definition TritonTool.h:49
FloatProperty m_clientTimeout
Definition TritonTool.h:50
tc::InferenceServerGrpcClient * getClient() const
StringProperty m_modelName
Definition TritonTool.h:47
StringProperty m_url
Definition TritonTool.h:51
IntegerProperty m_port
Definition TritonTool.h:48
BooleanProperty m_useSSL
Definition TritonTool.h:52
std::unique_ptr< tc::InferOptions > m_options
Definition TritonTool.h:56
bool verbose
Definition hcg.cxx:73
std::map< std::string, InferenceData > OutputDataMap
std::map< std::string, InferenceData > InputDataMap