NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2019-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 
18 #ifndef __NVDSINFER_BACKEND_H__
19 #define __NVDSINFER_BACKEND_H__
20 
21 #include <stdarg.h>
22 #include <condition_variable>
23 #include <memory>
24 #include <mutex>
25 #include <queue>
26 
27 #include <cuda_runtime_api.h>
28 
29 #include <NvInfer.h>
30 #include <NvInferRuntime.h>
31 
32 #include "nvdsinfer_func_utils.h"
33 
34 /* This file provides backend inference interface for abstracting implementation
35  * details in various cases like inferencing on implicit batch dims/full dims
36  * network, inferencing on DLA etc. This file also provides helper classes for
37  * managing the lifecycle of CUDA resources like streams, buffers, events. */
38 
39 namespace nvdsinfer {
40 
44 class CudaStream
45 {
46 public:
47  explicit CudaStream(uint flag = cudaStreamDefault, int priority = 0);
48  ~CudaStream();
49  operator cudaStream_t() { return m_Stream; }
50  cudaStream_t& ptr() { return m_Stream; }
52 
53 private:
54  void move_copy(CudaStream&& o)
55  {
56  m_Stream = o.m_Stream;
57  o.m_Stream = nullptr;
58  }
59  DISABLE_CLASS_COPY(CudaStream);
60 
61  cudaStream_t m_Stream = nullptr;
62 };
63 
67 class CudaEvent
68 {
69 public:
70  explicit CudaEvent(uint flag = cudaEventDefault);
71  ~CudaEvent();
72  operator cudaEvent_t() { return m_Event; }
73  cudaEvent_t& ptr() { return m_Event; }
75 
76 private:
77  void move_copy(CudaEvent&& o)
78  {
79  m_Event = o.m_Event;
80  o.m_Event = nullptr;
81  }
82  DISABLE_CLASS_COPY(CudaEvent);
83 
84  cudaEvent_t m_Event = nullptr;
85 };
86 
90 class CudaBuffer
91 {
92 public:
93  virtual ~CudaBuffer() = default;
94  size_t bytes() const { return m_Size; }
95 
96  template <typename T>
97  T* ptr()
98  {
99  return (T*)m_Buf;
100  }
101 
102  void* ptr() { return m_Buf; }
104 
105 protected:
106  explicit CudaBuffer(size_t s) : m_Size(s) {}
108  {
109  m_Buf = o.m_Buf;
110  o.m_Buf = nullptr;
111  m_Size = o.m_Size;
112  o.m_Size = 0;
113  }
115  void* m_Buf = nullptr;
116  size_t m_Size = 0;
117 };
118 
122 class CudaDeviceBuffer : public CudaBuffer
123 {
124 public:
125  explicit CudaDeviceBuffer(size_t size);
127 };
128 
132 class CudaHostBuffer : public CudaBuffer
133 {
134 public:
135  explicit CudaHostBuffer(size_t size);
136  ~CudaHostBuffer();
137 };
138 
142 class InferBatchBuffer
143 {
144 public:
145  InferBatchBuffer() = default;
146  virtual ~InferBatchBuffer() = default;
147 
148  /* Get device buffer pointers for bound layers associated with this batch. */
149  virtual std::vector<void*>& getDeviceBuffers() = 0;
150  /* Get the data type of the buffer(layer) for a bound layer having index
151  * `bindingIndex`. */
152  virtual NvDsInferDataType getDataType(int bindingIndex = 0) const = 0;
153  /* Get the batch dimensions for the buffer allocated for a bound layer having
154  * index `bindingIndex. */
155  virtual NvDsInferBatchDims getBatchDims(int bindingIndex = 0) const = 0;
156 
157 private:
158  DISABLE_CLASS_COPY(InferBatchBuffer);
159 };
160 
172 class BackendContext
173 {
174 public:
175  BackendContext() = default;
176  virtual ~BackendContext() = default;
177 
178  /* Initialize the backend context. */
179  virtual NvDsInferStatus initialize() = 0;
180  /* Get the number of bound layers for the engine. */
181  virtual int getNumBoundLayers() = 0;
182 
183  /* Get information for a bound layer with index `bindingIdx`. */
184  virtual const NvDsInferBatchDimsLayerInfo& getLayerInfo(int bindingIdx) = 0;
185  /* Get binding index for a bound layer with name `bindingName`. */
186  virtual bool isOutputLayer(const std::string& bindingName) = 0;
187 
188  /* Returns if the bound layer at index `bindingIdx` can support the
189  * provided batch dimensions. */
190  virtual bool canSupportBatchDims(
191  int bindingIdx, const NvDsInferBatchDims& batchDims) = 0;
192 
193  /* Get the min/max/optimal batch dimensions for a bound layer. */
194  virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx) = 0;
195  virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx) = 0;
196  virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx) = 0;
197 
198  /* Enqueue a batched buffer for inference. */
200  const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
201  CudaEvent* consumeEvent) = 0;
202 
203 private:
204  DISABLE_CLASS_COPY(BackendContext);
205 };
206 
207 class TrtEngine;
208 
213 class TrtBackendContext : public BackendContext
214 {
215 public:
217 
218 protected:
219  TrtBackendContext(std::unique_ptr<nvinfer1::IExecutionContext>&& ctx,
220  std::shared_ptr<TrtEngine> engine);
221 
222  bool isOutputLayer(const std::string& bindingName) override;
223  int getNumBoundLayers() override;
224 
225  const NvDsInferBatchDimsLayerInfo& getLayerInfo(int bindingIdx) override
226  {
227  assert(bindingIdx < (int)m_AllLayers.size());
228  return m_AllLayers[bindingIdx];
229  }
230 
231  bool canSupportBatchDims(
232  int bindingIdx, const NvDsInferBatchDims& batchDims) override;
233 
234  virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx) override
235  {
236  assert(bindingIdx < (int)m_AllLayers.size());
237  return m_AllLayers[bindingIdx].profileDims[kSELECTOR_MAX];
238  }
239  virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx) override
240  {
241  assert(bindingIdx < (int)m_AllLayers.size());
242  return m_AllLayers[bindingIdx].profileDims[kSELECTOR_MIN];
243  }
244  virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx) override
245  {
246  assert(bindingIdx < (int)m_AllLayers.size());
247  return m_AllLayers[bindingIdx].profileDims[kSELECTOR_OPT];
248  }
249 
250 protected:
251  std::unique_ptr<nvinfer1::IExecutionContext> m_Context;
252  std::shared_ptr<TrtEngine> m_CudaEngine;
253  std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayers;
254 
255  int m_GpuId = -1;
256 
257  static std::mutex sDLAExecutionMutex;
258 };
259 
263 class FullDimTrtBackendContext : public TrtBackendContext
264 {
265 public:
267  std::unique_ptr<nvinfer1::IExecutionContext>&& ctx,
268  std::shared_ptr<TrtEngine> engine, int profile = 0);
269 
270 private:
271  NvDsInferStatus initialize() override;
272 
273  NvDsInferStatus enqueueBuffer(
274  const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
275  CudaEvent* consumeEvent) override;
276 
277 protected:
278  // Only idx 0 profile supported.
279  const int m_ProfileIndex = 0;
280 };
281 
285 class DlaFullDimTrtBackendContext : public FullDimTrtBackendContext
286 {
287 public:
288  DlaFullDimTrtBackendContext(std::unique_ptr<nvinfer1::IExecutionContext>&& ctx,
289  std::shared_ptr<TrtEngine> engine, int profile = 0);
290 
292  const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
293  CudaEvent* consumeEvent) override;
294 
295 private:
296  static std::mutex sExecutionMutex;
297 };
298 
308 std::unique_ptr<TrtBackendContext> createBackendContext(
309  const std::shared_ptr<TrtEngine>& engine);
310 
311 } // end of namespace nvdsinfer
312 
313 #endif
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: sources/includes/nvbufsurftransform.h:35
nvdsinfer::TrtBackendContext::isOutputLayer
bool isOutputLayer(const std::string &bindingName) override
nvdsinfer::TrtBackendContext::TrtBackendContext
TrtBackendContext(std::unique_ptr< nvinfer1::IExecutionContext > &&ctx, std::shared_ptr< TrtEngine > engine)
nvdsinfer::FullDimTrtBackendContext::m_ProfileIndex
const int m_ProfileIndex
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:279
nvdsinfer::BackendContext::enqueueBuffer
virtual NvDsInferStatus enqueueBuffer(const std::shared_ptr< InferBatchBuffer > &buffer, CudaStream &stream, CudaEvent *consumeEvent)=0
nvdsinfer::createBackendContext
std::unique_ptr< TrtBackendContext > createBackendContext(const std::shared_ptr< TrtEngine > &engine)
Create an instance of a BackendContext.
nvdsinfer::CudaHostBuffer::~CudaHostBuffer
~CudaHostBuffer()
nvdsinfer::CudaBuffer
Helper base class for managing Cuda allocated buffers.
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:90
nvdsinfer::CudaDeviceBuffer::CudaDeviceBuffer
CudaDeviceBuffer(size_t size)
nvdsinfer::InferBatchBuffer::getBatchDims
virtual NvDsInferBatchDims getBatchDims(int bindingIndex=0) const =0
nvdsinfer::BackendContext::getMaxBatchDims
virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx)=0
nvdsinfer::TrtBackendContext::m_AllLayers
std::vector< NvDsInferBatchDimsLayerInfo > m_AllLayers
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:253
nvdsinfer::InferBatchBuffer::InferBatchBuffer
InferBatchBuffer()=default
nvdsinfer::TrtBackendContext::m_GpuId
int m_GpuId
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:255
nvdsinfer::TrtBackendContext::getLayerInfo
const NvDsInferBatchDimsLayerInfo & getLayerInfo(int bindingIdx) override
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:225
nvdsinfer::BackendContext::getOptBatchDims
virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx)=0
nvdsinfer::CudaStream
Helper class for managing Cuda Streams.
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:44
nvdsinfer::BackendContext::~BackendContext
virtual ~BackendContext()=default
nvdsinfer::DlaFullDimTrtBackendContext::DlaFullDimTrtBackendContext
DlaFullDimTrtBackendContext(std::unique_ptr< nvinfer1::IExecutionContext > &&ctx, std::shared_ptr< TrtEngine > engine, int profile=0)
nvdsinfer::TrtBackendContext::canSupportBatchDims
bool canSupportBatchDims(int bindingIdx, const NvDsInferBatchDims &batchDims) override
nvdsinfer::CudaStream::ptr
cudaStream_t & ptr()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:50
nvdsinfer::CudaEvent::~CudaEvent
~CudaEvent()
nvdsinfer::CudaBuffer::CudaBuffer
CudaBuffer(size_t s)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:106
nvdsinfer::TrtBackendContext::sDLAExecutionMutex
static std::mutex sDLAExecutionMutex
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:257
nvdsinfer::TrtBackendContext::m_Context
std::unique_ptr< nvinfer1::IExecutionContext > m_Context
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:251
nvdsinfer::CudaDeviceBuffer::~CudaDeviceBuffer
~CudaDeviceBuffer()
nvdsinfer::BackendContext::getMinBatchDims
virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx)=0
NvDsInferDataType
NvDsInferDataType
Specifies the data type of a layer.
Definition: sources/includes/nvdsinfer.h:77
nvdsinfer::InferBatchBuffer::getDeviceBuffers
virtual std::vector< void * > & getDeviceBuffers()=0
nvdsinfer::CudaBuffer::m_Buf
void * m_Buf
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:115
nvdsinfer
Definition: sources/includes/nvdsinfer_logger.h:59
nvdsinfer::BackendContext::initialize
virtual NvDsInferStatus initialize()=0
CudaStream
Helper class for managing Cuda Streams.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:102
nvdsinfer::BackendContext::getLayerInfo
virtual const NvDsInferBatchDimsLayerInfo & getLayerInfo(int bindingIdx)=0
nvdsinfer::TrtBackendContext::getNumBoundLayers
int getNumBoundLayers() override
nvdsinfer::CudaBuffer::ptr
T * ptr()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:97
nvdsinfer::InferBatchBuffer::getDataType
virtual NvDsInferDataType getDataType(int bindingIndex=0) const =0
nvdsinfer::CudaBuffer::move_copy
void move_copy(CudaBuffer &&o)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:107
nvdsinfer::TrtBackendContext::m_CudaEngine
std::shared_ptr< TrtEngine > m_CudaEngine
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:252
CudaBuffer
Helper base class for managing Cuda allocated buffers.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:128
nvdsinfer::CudaEvent::CudaEvent
CudaEvent(uint flag=cudaEventDefault)
nvdsinfer::CudaBuffer::DISABLE_CLASS_COPY
DISABLE_CLASS_COPY(CudaBuffer)
nvdsinfer::BackendContext::getNumBoundLayers
virtual int getNumBoundLayers()=0
nvdsinfer::BackendContext::BackendContext
BackendContext()=default
nvdsinfer::FullDimTrtBackendContext::FullDimTrtBackendContext
FullDimTrtBackendContext(std::unique_ptr< nvinfer1::IExecutionContext > &&ctx, std::shared_ptr< TrtEngine > engine, int profile=0)
nvdsinfer::CudaBuffer::m_Size
size_t m_Size
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:116
nvdsinfer::TrtBackendContext::getOptBatchDims
virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx) override
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:244
nvdsinfer::CudaBuffer::bytes
size_t bytes() const
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:94
nvdsinfer::DlaFullDimTrtBackendContext::enqueueBuffer
NvDsInferStatus enqueueBuffer(const std::shared_ptr< InferBatchBuffer > &buffer, CudaStream &stream, CudaEvent *consumeEvent) override
nvdsinfer::CudaEvent::ptr
cudaEvent_t & ptr()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:73
nvdsinfer_func_utils.h
nvdsinfer::CudaHostBuffer::CudaHostBuffer
CudaHostBuffer(size_t size)
nvdsinfer::BackendContext::canSupportBatchDims
virtual bool canSupportBatchDims(int bindingIdx, const NvDsInferBatchDims &batchDims)=0
nvdsinfer::CudaStream::CudaStream
CudaStream(uint flag=cudaStreamDefault, int priority=0)
CudaDeviceBuffer
CUDA device buffers.
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:166
SIMPLE_MOVE_COPY
#define SIMPLE_MOVE_COPY(Cls)
helper move function
Definition: sources/gst-plugins/gst-nvdspreprocess/nvdspreprocess_lib/nvdspreprocess_impl.h:46
nvdsinfer::TrtBackendContext::getMaxBatchDims
virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx) override
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:234
nvdsinfer::CudaBuffer::~CudaBuffer
virtual ~CudaBuffer()=default
nvdsinfer::CudaBuffer::ptr
void * ptr()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:102
nvdsinfer::TrtBackendContext::getMinBatchDims
virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx) override
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_backend.h:239
nvdsinfer::InferBatchBuffer::~InferBatchBuffer
virtual ~InferBatchBuffer()=default
nvdsinfer::BackendContext::isOutputLayer
virtual bool isOutputLayer(const std::string &bindingName)=0
nvdsinfer::CudaEvent
Helper class for managing Cuda events.
Definition: sources/libs/nvdsinfer/nvdsinfer_backend.h:67
nvdsinfer::TrtBackendContext::~TrtBackendContext
~TrtBackendContext()
nvdsinfer::CudaStream::~CudaStream
~CudaStream()
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231