NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/nvdsinferserver/infer_trtis_backend.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
26 #ifndef __NVDSINFER_TRTIS_BACKEND_H__
27 #define __NVDSINFER_TRTIS_BACKEND_H__
28 
29 #include "infer_base_backend.h"
30 #include "infer_common.h"
31 #include "infer_post_datatypes.h"
32 #include "infer_utils.h"
33 
34 namespace nvdsinferserver {
35 
36 class TrtServerAllocator;
37 class TrtISServer;
38 class TrtServerRequest;
39 class TrtServerResponse;
40 
44 class TrtISBackend : public BaseBackend {
45 public:
53  const std::string& name, int64_t version, TrtServerPtr ptr = nullptr);
54 
58  ~TrtISBackend() override;
59 
64  m_ClassifyParams.emplace_back(c); }
65 
70  void setOutputPoolSize(int size) { m_PerPoolSize = size; }
71  int outputPoolSize() const { return m_PerPoolSize; }
72  void setOutputMemType(InferMemType memType) { m_OutputMemType = memType; }
73  InferMemType outputMemType() const { return m_OutputMemType; }
74  void setOutputDevId(int64_t devId) { m_OutputDevId = devId; }
75  int64_t outputDevId() const { return m_OutputDevId; }
76  std::vector<TritonClassParams> getClassifyParams() {
77  return m_ClassifyParams;}
78  const std::string& model() const { return m_Model; }
79  int64_t version() const { return m_ModelVersion; }
87  NvDsInferStatus initialize() override;
88 
94  NvDsInferStatus specifyInputDims(const InputShapes& shapes) override;
95 
106  InputsConsumed bufConsumed, InferenceDone inferenceDone) override;
107 
115  void setTensorMaxBytes(const std::string& name, size_t maxBytes)
116  {
117  size_t& bytes = m_TensorMaxBytes[name];
118  bytes = std::max<size_t>(maxBytes, bytes);
119  bytes = INFER_ROUND_UP(bytes, INFER_MEM_ALIGNMENT);
120  }
121 
122 protected:
123  // interface for derived class
124 
129  virtual void requestTritonOutputNames(std::set<std::string>& outNames);
130 
136 
142 
149 
154  {
155  m_ResponseAllocator = std::move(allocator);
156  }
157 
165 
169  TrtServerPtr& server() { return m_Server; }
170 
174  using AsyncDone = std::function<void(NvDsInferStatus, SharedBatchArray)>;
185  virtual NvDsInferStatus Run(
186  SharedBatchArray inputs, InputsConsumed bufConsumed,
187  AsyncDone asyncDone);
188 
194 
205  const std::string& tensor, size_t bytes, InferMemType memType, int64_t devId);
206 
212  void releaseResponseBuf(const std::string& tensor, SharedSysMem mem);
213 
221 
225  enum { kName, kGpuId, kMemType };
229  using PoolKey = std::tuple<std::string, int64_t, InferMemType>;
234  using PoolValue = SharedBufPool<UniqSysMem>;
235 
240 
247  PoolValue createResponsePool(PoolKey& key, size_t bytes);
248 
260  std::shared_ptr<TrtServerRequest> request,
261  std::unique_ptr<TrtServerResponse> uniqResponse,
262  InputsConsumed inputsConsumed, AsyncDone asyncDone);
263 
267  struct ReorderItem {
281 
286  std::promise<void> promise;
287  std::future<void> future;
294  };
295  /*
296  * @brief Pointer to the reorder thread task.
297  */
298  using ReorderItemPtr = std::shared_ptr<ReorderItem>;
306 
313  bool debatchingOutput(SharedBatchArray& outputs, SharedBatchArray& inputs);
314 
315 private:
319  std::string m_Model;
323  int64_t m_ModelVersion = -1;
327  TrtServerPtr m_Server;
332  bool m_NeedUnload = false;
336  std::vector<TritonClassParams> m_ClassifyParams;
340  ShrTritonAllocator m_ResponseAllocator;
344  InferMemType m_OutputMemType = InferMemType::kNone;
348  int64_t m_OutputDevId = -1;
352  int m_PerPoolSize = 2;
356  std::map<PoolKey, PoolValue> m_ResponsePool;
360  using SharedMutex = std::shared_timed_mutex;
364  SharedMutex m_ResponseMutex;
368  std::unordered_map<std::string, size_t> m_TensorMaxBytes;
369 
373  using ReorderThread = QueueThread<std::vector<ReorderItemPtr>>;
377  std::unique_ptr<ReorderThread> m_ReorderThread;
378 };
379 
380 } // namespace nvdsinferserver
381 
382 #endif
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: sources/gst-plugins/gst-nvinferserver/gstnvinferserver_impl.h:69
nvdsinferserver::TrtISBackend::PoolValue
SharedBufPool< UniqSysMem > PoolValue
The buffer pool for the specified tensor, GPU and memory type combination.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:234
nvdsinferserver::TrtISBackend::findResponsePool
PoolValue findResponsePool(PoolKey &key)
Find the buffer pool for the given key.
INFER_MEM_ALIGNMENT
#define INFER_MEM_ALIGNMENT
Definition: sources/includes/nvdsinferserver/infer_defines.h:130
nvdsinferserver::UniqTritonAllocator
std::unique_ptr< TrtServerAllocator > UniqTritonAllocator
Definition: sources/libs/nvdsinferserver/infer_common.h:128
nvdsinferserver::TrtISBackend::kGpuId
@ kGpuId
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:225
nvdsinferserver::TrtISBackend::initialize
NvDsInferStatus initialize() override
Check that the server and model is ready, get the information of layers, setup reorder thread and out...
nvdsinferserver::TrtISBackend::model
const std::string & model() const
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:78
nvdsinferserver::TrtISBackend::kMemType
@ kMemType
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:225
TritonClassParams
Definition: sources/includes/nvdsinferserver/infer_post_datatypes.h:96
nvdsinferserver::TrtISBackend::ensureModelReady
virtual NvDsInferStatus ensureModelReady()
Check that the model is ready, load the model if it is not.
nvdsinferserver::IBackend::InputShapes
std::vector< InputShapeTuple > InputShapes
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:89
nvdsinferserver::IBackend::InputsConsumed
std::function< void(SharedBatchArray)> InputsConsumed
Function wrapper called after the input buffer is consumed.
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:75
nvdsinferserver::TrtISBackend::ReorderItem::outputs
SharedBatchArray outputs
Array of output batch buffers.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:280
nvdsinferserver::TrtISBackend::specifyInputDims
NvDsInferStatus specifyInputDims(const InputShapes &shapes) override
Specify the input layers for the backend.
nvdsinferserver::TrtISBackend
Triton backend processing class.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:44
nvdsinferserver::SharedBatchArray
std::shared_ptr< BaseBatchArray > SharedBatchArray
Definition: sources/libs/nvdsinferserver/infer_common.h:80
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: sources/includes/nvdsinfer.h:233
nvdsinferserver::TrtISBackend::setAllocator
void setAllocator(UniqTritonAllocator allocator)
Set the output tensor allocator.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:153
nvdsinferserver::TrtISBackend::outputMemType
InferMemType outputMemType() const
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:73
nvdsinferserver::TrtISBackend::createResponsePool
PoolValue createResponsePool(PoolKey &key, size_t bytes)
Create a new buffer pool for the key.
nvdsinferserver::TrtISBackend::AsyncDone
std::function< void(NvDsInferStatus, SharedBatchArray)> AsyncDone
Asynchronous inference done function: AsyncDone(Status, outputs).
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:174
nvdsinferserver::TrtISBackend::debatchingOutput
bool debatchingOutput(SharedBatchArray &outputs, SharedBatchArray &inputs)
Separate the batch dimension from the output buffer descriptors.
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::TrtISBackend::ReorderItem
Reorder thread task.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:267
nvdsinferserver::IBackend::InferenceDone
std::function< void(NvDsInferStatus, SharedBatchArray)> InferenceDone
Function wrapper for post inference processing.
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:71
nvdsinferserver::TrtISBackend::ReorderItem::status
NvDsInferStatus status
Status of processing.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:271
nvdsinferserver::TrtISBackend::outputDevId
int64_t outputDevId() const
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:75
nvdsinferserver::TrtISBackend::version
int64_t version() const
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:79
nvdsinferserver::TrtISBackend::ensureInputs
NvDsInferStatus ensureInputs(SharedBatchArray &inputs)
Ensure that the array of input buffers are expected by the model and reshape the input buffers if req...
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: sources/libs/nvdsinferserver/infer_common.h:89
nvdsinferserver::ShrTritonAllocator
std::shared_ptr< TrtServerAllocator > ShrTritonAllocator
Definition: sources/libs/nvdsinferserver/infer_common.h:129
nvdsinferserver::TrtISBackend::enqueue
NvDsInferStatus enqueue(SharedBatchArray inputs, SharedCuStream stream, InputsConsumed bufConsumed, InferenceDone inferenceDone) override
Enqueue an input for inference request by calling Run() and adding corresponding task to the reorder ...
nvdsinferserver::TrtISBackend::setTensorMaxBytes
void setTensorMaxBytes(const std::string &name, size_t maxBytes)
Set the maximum size for the tensor, the maximum of the existing size and new input size is used.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:115
nvdsinferserver::TrtISBackend::setOutputPoolSize
void setOutputPoolSize(int size)
Helper function to access the member variables.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:70
nvdsinferserver::TrtISBackend::ReorderItem::inferenceDone
InferenceDone inferenceDone
Inference done callback function.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:293
nvdsinferserver::TrtISBackend::ensureServerReady
virtual NvDsInferStatus ensureServerReady()
Check that the Triton inference server is live.
nvdsinferserver::TrtISBackend::requestTritonOutputNames
virtual void requestTritonOutputNames(std::set< std::string > &outNames)
Get the list of output tensor names.
nvdsinferserver::TrtISBackend::Run
virtual NvDsInferStatus Run(SharedBatchArray inputs, InputsConsumed bufConsumed, AsyncDone asyncDone)
Create an inference request and trigger asynchronous inference.
nvdsinferserver::SharedSysMem
std::shared_ptr< SysMem > SharedSysMem
Definition: sources/libs/nvdsinferserver/infer_common.h:93
nvdsinferserver::TrtServerPtr
std::shared_ptr< TrtISServer > TrtServerPtr
Definition: sources/libs/nvdsinferserver/infer_common.h:126
INFER_ROUND_UP
#define INFER_ROUND_UP(value, align)
Definition: sources/includes/nvdsinferserver/infer_defines.h:127
nvdsinferserver::TrtISBackend::allocateResponseBuf
SharedSysMem allocateResponseBuf(const std::string &tensor, size_t bytes, InferMemType memType, int64_t devId)
Acquire a buffer from the output buffer pool associated with the device ID and memory type.
nvdsinferserver::TrtISBackend::getClassifyParams
std::vector< TritonClassParams > getClassifyParams()
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:76
nvdsinferserver::InferMemType
InferMemType
The memory types of inference buffers.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:61
nvdsinferserver::TrtISBackend::PoolKey
std::tuple< std::string, int64_t, InferMemType > PoolKey
Tuple holding tensor name, GPU ID, memory type.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:229
nvdsinferserver::TrtISBackend::setOutputMemType
void setOutputMemType(InferMemType memType)
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:72
nvdsinferserver::TrtISBackend::releaseResponseBuf
void releaseResponseBuf(const std::string &tensor, SharedSysMem mem)
Release the output tensor buffer.
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
nvdsinferserver::TrtISBackend::setOutputDevId
void setOutputDevId(int64_t devId)
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:74
nvdsinferserver::TrtISBackend::inferenceDoneReorderLoop
bool inferenceDoneReorderLoop(ReorderItemPtr item)
Add input buffers to the output buffer list if required.
nvdsinferserver::TrtISBackend::~TrtISBackend
~TrtISBackend() override
Destructor.
nvdsinferserver::TrtISBackend::outputPoolSize
int outputPoolSize() const
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:71
nvdsinferserver::BaseBackend
Base class of inference backend processing.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:45
infer_base_backend.h
Header file for inference processing backend base class.
nvdsinferserver::TrtISBackend::serverInferCompleted
void serverInferCompleted(std::shared_ptr< TrtServerRequest > request, std::unique_ptr< TrtServerResponse > uniqResponse, InputsConsumed inputsConsumed, AsyncDone asyncDone)
Call the inputs consumed function and parse the inference response to form the array of output batch ...
nvdsinferserver::TrtISBackend::fixateDims
NvDsInferStatus fixateDims(const SharedBatchArray &bufs)
Extend the dimensions to include batch size for the buffers in input array.
nvdsinferserver::TrtISBackend::setupReorderThread
NvDsInferStatus setupReorderThread()
Create a loop thread that calls inferenceDoneReorderLoop on the queued items.
nvdsinferserver::TrtISBackend::ReorderItem::inputs
SharedBatchArray inputs
Array of input batch buffers.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:276
nvdsinferserver::TrtISBackend::server
TrtServerPtr & server()
Get the Triton server handle.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:169
nvdsinferserver::TrtISBackend::ReorderItem::promise
std::promise< void > promise
Synchronization objects.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:286
nvdsinferserver::TrtISBackend::ReorderItem::future
std::future< void > future
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:287
nvdsinferserver::TrtISBackend::TrtISBackend
TrtISBackend(const std::string &name, int64_t version, TrtServerPtr ptr=nullptr)
Constructor.
nvdsinferserver::TrtISBackend::setupLayersInfo
virtual NvDsInferStatus setupLayersInfo()
Get the model configuration from the server and populate layer information.
nvdsinferserver::InferMemType::kNone
@ kNone
nvdsinferserver::TrtISBackend::ReorderItemPtr
std::shared_ptr< ReorderItem > ReorderItemPtr
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:298
nvdsinferserver::TrtISBackend::addClassifyParams
void addClassifyParams(const TritonClassParams &c)
Add Triton Classification parameters to the list.
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:63
nvdsinferserver::TrtISBackend::kName
@ kName
Definition: sources/libs/nvdsinferserver/infer_trtis_backend.h:225
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231