NVIDIA DeepStream SDK API Reference

7.0 Release
infer_batch_buffer.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2020-2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
3  * SPDX-License-Identifier: LicenseRef-NvidiaProprietary
4  *
5  * NVIDIA CORPORATION, its affiliates and licensors retain all intellectual
6  * property and proprietary rights in and to this material, related
7  * documentation and any modifications thereto. Any use, reproduction,
8  * disclosure or distribution of this material and related documentation
9  * without an express license agreement from NVIDIA CORPORATION or
10  * its affiliates is strictly prohibited.
11  */
12 
19 #ifndef __NVDSINFERSERVER_BATCH_BUFFER_H__
20 #define __NVDSINFERSERVER_BATCH_BUFFER_H__
21 
22 #include <algorithm>
23 #include <set>
24 #include <unordered_map>
25 #include "nvbufsurftransform.h"
26 #include "infer_common.h"
27 #include "infer_datatypes.h"
28 
29 namespace nvdsinferserver {
30 
35 protected:
36  explicit BaseBatchBuffer(uint32_t batchSize) : m_BatchSize(batchSize) {}
37 
38 public:
39  ~BaseBatchBuffer() override = default;
40  const InferBufferDescription& getBufDesc() const final { return m_Desc; }
41  uint32_t getBatchSize() const final { return m_BatchSize; }
42  uint64_t getTotalBytes() const override
43  {
44  assert(m_Desc.dataType != InferDataType::kNone);
45  assert(m_Desc.elementSize >= 0);
46  uint32_t b = getBatchSize();
47  return m_Desc.elementSize * m_Desc.dims.numElements * (b ? b : 1);
48  }
49  void setBufDesc(const InferBufferDescription& desc) { m_Desc = desc; }
50  InferBufferDescription& mutableBufDesc() { return m_Desc; }
51  virtual void setBatchSize(uint32_t size) { m_BatchSize = size; }
52  const SharedCuEvent& cuEvent() const { return m_CuEvent; }
53  void setCuEvent(SharedCuEvent e) { m_CuEvent = std::move(e); }
54  void setSyncObj(NvBufSurfTransformSyncObj_t SyncObj) { assert(m_SyncObj==nullptr); m_SyncObj = SyncObj; }
55  NvBufSurfTransformSyncObj_t& getSyncObj() { return m_SyncObj; }
57  {
58  if (m_SyncObj)
59  {
60  NvBufSurfTransformSyncObjWait (m_SyncObj, -1);
62  m_SyncObj = nullptr;
63  }
64  }
65 
66  // attached buffers
68  {
69  m_Attaches.emplace_back(std::move(buf));
70  assert(!hasAttachLoop());
71  }
72  void detach() { m_Attaches.clear(); }
73  bool hasAttachedBufs() const { return !m_Attaches.empty();}
74  const std::vector<SharedBatchBuf>& attachedBufs() const
75  {
76  return m_Attaches;
77  }
78  std::vector<SharedBatchBuf>& mutableAttachedBufs() { return m_Attaches; }
79  bool hasAttachLoop() const
80  {
81  std::set<BaseBatchBuffer*> allAttached;
82  for (auto const& buf : m_Attaches) {
83  assert(buf);
84  if (allAttached.count(buf.get())) {
85  return true;
86  }
87  allAttached.emplace(buf.get());
88  }
89  return false;
90  }
91 
92  void setBufId(uint64_t id) { m_BufId = id; }
93  uint64_t bufId() const { return m_BufId; }
94 
100  size_t getBufOffset(uint32_t batchIdx) const override
101  {
102  assert(getBufPtr(0) && getBufPtr(batchIdx));
103  if (getBufPtr(0) == nullptr || getBufPtr(batchIdx) == nullptr) {
104  return (size_t) -1;
105  } else {
106  return (uint8_t*)getBufPtr(batchIdx) - (uint8_t*)getBufPtr(0);
107  }
108  }
109 
110 private:
111  InferBufferDescription m_Desc{
113  uint32_t m_BatchSize = 0;
114  SharedCuEvent m_CuEvent;
115  std::vector<SharedBatchBuf> m_Attaches;
116  uint64_t m_BufId = UINT64_C(0); // for debug and track
119  NvBufSurfTransformSyncObj_t m_SyncObj = nullptr;
120 };
121 
125 class BaseBatchArray : public IBatchArray {
126 public:
127  ~BaseBatchArray() override = default;
128  uint32_t getSize() const final { return m_Bufs.size(); }
129  const IBatchBuffer* getBuffer(uint32_t arrayIdx) const final
130  {
131  assert(arrayIdx < (uint32_t)m_Bufs.size());
132  assert(m_Bufs.at(arrayIdx).get());
133  return m_Bufs.at(arrayIdx).get();
134  }
135  SharedIBatchBuffer getSafeBuf(uint32_t arrayIdx) const final
136  {
137  assert(arrayIdx < (uint32_t)m_Bufs.size());
138  assert(m_Bufs.at(arrayIdx).get());
139  return buf(arrayIdx);
140  }
142  {
143  SharedBatchBuf castBuf =
144  std::dynamic_pointer_cast<BaseBatchBuffer>(buf);
145  assert(castBuf);
146  addBuf(castBuf);
147  }
148  const IOptions* getOptions() const final { return m_Options.get(); }
149  SharedIOptions getSafeOptions() const { return m_Options; }
150  void setIOptions(SharedIOptions o) final { setOptions(o); }
151 
152  const std::vector<SharedBatchBuf>& bufs() const { return m_Bufs; }
153  std::vector<SharedBatchBuf>& mutableBufs() { return m_Bufs; }
154  void addBuf(SharedBatchBuf buf) { assert(buf); m_Bufs.emplace_back(std::move(buf)); }
155  const SharedBatchBuf& buf(uint32_t idx) const
156  {
157  assert(idx < (uint32_t)m_Bufs.size());
158  return m_Bufs.at(idx);
159  }
160  SharedBatchBuf& buf(uint32_t idx)
161  {
162  assert(idx < (uint32_t)m_Bufs.size());
163  return m_Bufs.at(idx);
164  }
165  const SharedCuEvent& cuEvent() const { return m_CuEvent; }
166  void setCuEvent(SharedCuEvent e) { m_CuEvent = std::move(e); }
167  int findFirstGpuId() const {
168  int gpuId = -1;
169  auto iter = std::find_if(m_Bufs.cbegin(), m_Bufs.cend(),
170  [&gpuId](const SharedBatchBuf& buf) {
171  assert(buf);
172  const InferBufferDescription& desc = buf->getBufDesc();
173  if (desc.memType == InferMemType::kGpuCuda) {
174  gpuId = desc.devId;
175  return true;
176  }
177  return false;
178  });
179  (void) iter;
180 
181  assert((iter != m_Bufs.cend() && gpuId >= 0) ||
182  (iter == m_Bufs.cend() && gpuId == -1));
183  return gpuId;
184  }
185 
186  void setBufId(uint64_t id) { m_BufId = id; }
187  uint64_t bufId() const { return m_BufId; }
188 
189  // set options
190  void setOptions(SharedIOptions o) { m_Options = std::move(o); }
191 
192 private:
193  std::vector<SharedBatchBuf> m_Bufs;
194  SharedCuEvent m_CuEvent;
195  uint64_t m_BufId = UINT64_C(0); // for debug and track
196 
197  SharedOptions m_Options;
198 };
199 
200 extern void normalizeDims(InferDims& dims);
201 
206 public:
208  void* bufBase, size_t offset, size_t bufBytes,
209  const InferBufferDescription& desc, uint32_t batchSize)
210  : BaseBatchBuffer(batchSize), m_BufBase(bufBase),
211  m_BufBytes(bufBytes), m_BufOffset(offset)
212  {
213  setBufDesc(desc);
214  normalizeDims(mutableBufDesc().dims);
215  }
216  void* getBufPtr(uint32_t batchIdx) const override {
217  assert(batchIdx == 0 || batchIdx < getBatchSize());
218  const InferBufferDescription& desc = getBufDesc();
219  assert(batchIdx <= 0 || desc.dataType != InferDataType::kString);
220  batchIdx = std::max(batchIdx, 0U);
221  return (void*)((uint8_t*)m_BufBase +
222  desc.elementSize * desc.dims.numElements * batchIdx);
223  }
224  uint64_t getTotalBytes() const final { return m_BufBytes; }
225  void* basePtr() { return m_BufBase; }
226 
232  size_t getBufOffset(uint32_t batchIdx) const override {
233  assert(batchIdx == 0 || batchIdx < getBatchSize());
234  const InferBufferDescription& desc = getBufDesc();
235  assert(batchIdx <= 0 || desc.dataType != InferDataType::kString);
236  batchIdx = std::max(batchIdx, 0U);
237  return m_BufOffset + desc.elementSize * desc.dims.numElements * batchIdx;
238  }
239 
240 private:
241  mutable void* m_BufBase = nullptr;
242  size_t m_BufBytes = 0;
246  size_t m_BufOffset = 0;
247 };
248 
250 public:
251  template <typename BufPtr>
252  WrapCBatchBuffer(BufPtr buf) : m_Impl(std::move(buf)) {
253  assert(m_Impl);
254  }
255 
256  ~WrapCBatchBuffer() final = default;
257  const InferBufferDescription& getBufDesc() const final {
258  return m_Impl->getBufDesc();
259  }
260  void* getBufPtr(uint32_t batchIdx) const final {
261  return m_Impl->getBufPtr(batchIdx);
262  }
263  uint32_t getBatchSize() const final { return m_Impl->getBatchSize(); }
264  uint64_t getTotalBytes() const final { return m_Impl->getTotalBytes(); }
265 
266 private:
267  SharedBatchBuf m_Impl;
268 };
269 
270 } // namespace nvdsinferserver
271 
272 #endif
nvdsinferserver::BaseBatchArray::buf
const SharedBatchBuf & buf(uint32_t idx) const
Definition: infer_batch_buffer.h:155
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: infer_custom_process.h:24
nvdsinferserver::BaseBatchBuffer::getBatchSize
uint32_t getBatchSize() const final
Definition: infer_batch_buffer.h:41
nvdsinferserver::BaseBatchArray::getSize
uint32_t getSize() const final
Definition: infer_batch_buffer.h:128
nvdsinferserver::BaseBatchArray::~BaseBatchArray
~BaseBatchArray() override=default
nvdsinferserver::BaseBatchBuffer::setBufId
void setBufId(uint64_t id)
Definition: infer_batch_buffer.h:92
nvdsinferserver::SharedBatchBuf
std::shared_ptr< BaseBatchBuffer > SharedBatchBuf
Common buffer interfaces (internal).
Definition: infer_common.h:71
nvdsinferserver::BaseBatchArray::getOptions
const IOptions * getOptions() const final
Definition: infer_batch_buffer.h:148
NvBufSurfTransformSyncObj_t
struct NvBufSurfTransformSyncObj * NvBufSurfTransformSyncObj_t
Holds the information about synchronization objects for asynchronous transform/composite APIs.
Definition: nvbufsurftransform.h:287
nvdsinferserver::SharedIBatchBuffer
std::shared_ptr< IBatchBuffer > SharedIBatchBuffer
Definition: infer_datatypes.h:204
nvdsinferserver::RefBatchBuffer::RefBatchBuffer
RefBatchBuffer(void *bufBase, size_t offset, size_t bufBytes, const InferBufferDescription &desc, uint32_t batchSize)
Definition: infer_batch_buffer.h:207
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::BaseBatchArray::setBufId
void setBufId(uint64_t id)
Definition: infer_batch_buffer.h:186
NvBufSurfTransformSyncObjDestroy
NvBufSurfTransform_Error NvBufSurfTransformSyncObjDestroy(NvBufSurfTransformSyncObj_t *sync_obj)
Destroy the synchronization object.
nvdsinferserver::RefBatchBuffer
A batch buffer with allocated memory.
Definition: infer_batch_buffer.h:205
nvdsinferserver::BaseBatchBuffer::cuEvent
const SharedCuEvent & cuEvent() const
Definition: infer_batch_buffer.h:52
nvdsinferserver::BaseBatchBuffer::getBufOffset
size_t getBufOffset(uint32_t batchIdx) const override
Get the offset from start of the memory allocation to the buffer pointer.
Definition: infer_batch_buffer.h:100
nvdsinferserver::BaseBatchBuffer::getSyncObj
NvBufSurfTransformSyncObj_t & getSyncObj()
Definition: infer_batch_buffer.h:55
nvdsinferserver::normalizeDims
void normalizeDims(InferDims &dims)
nvdsinferserver::BaseBatchArray::setIOptions
void setIOptions(SharedIOptions o) final
Definition: infer_batch_buffer.h:150
nvdsinferserver::RefBatchBuffer::basePtr
void * basePtr()
Definition: infer_batch_buffer.h:225
nvdsinferserver::BaseBatchArray::getBuffer
const IBatchBuffer * getBuffer(uint32_t arrayIdx) const final
Definition: infer_batch_buffer.h:129
nvdsinferserver::BaseBatchBuffer::attachedBufs
const std::vector< SharedBatchBuf > & attachedBufs() const
Definition: infer_batch_buffer.h:74
nvdsinferserver::RefBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const final
Definition: infer_batch_buffer.h:224
nvdsinferserver::BaseBatchBuffer::detach
void detach()
Definition: infer_batch_buffer.h:72
nvdsinferserver::IBatchBuffer
Interface class for a batch buffer.
Definition: infer_datatypes.h:211
nvdsinferserver::BaseBatchBuffer::waitForSyncObj
void waitForSyncObj()
Definition: infer_batch_buffer.h:56
nvdsinferserver::BaseBatchArray::setCuEvent
void setCuEvent(SharedCuEvent e)
Definition: infer_batch_buffer.h:166
nvdsinferserver::BaseBatchBuffer::setBatchSize
virtual void setBatchSize(uint32_t size)
Definition: infer_batch_buffer.h:51
nvdsinferserver::BaseBatchArray::cuEvent
const SharedCuEvent & cuEvent() const
Definition: infer_batch_buffer.h:165
nvdsinferserver::BaseBatchArray::addBuf
void addBuf(SharedBatchBuf buf)
Definition: infer_batch_buffer.h:154
nvdsinferserver::BaseBatchBuffer::bufId
uint64_t bufId() const
Definition: infer_batch_buffer.h:93
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::BaseBatchArray::mutableBufs
std::vector< SharedBatchBuf > & mutableBufs()
Definition: infer_batch_buffer.h:153
nvdsinferserver::BaseBatchArray::getSafeBuf
SharedIBatchBuffer getSafeBuf(uint32_t arrayIdx) const final
Definition: infer_batch_buffer.h:135
nvdsinferserver::BaseBatchArray::setOptions
void setOptions(SharedIOptions o)
Definition: infer_batch_buffer.h:190
nvdsinferserver::BaseBatchBuffer::getBufDesc
const InferBufferDescription & getBufDesc() const final
Definition: infer_batch_buffer.h:40
nvdsinferserver::WrapCBatchBuffer
Definition: infer_batch_buffer.h:249
nvdsinferserver::WrapCBatchBuffer::getBatchSize
uint32_t getBatchSize() const final
Definition: infer_batch_buffer.h:263
nvdsinferserver::BaseBatchArray::bufId
uint64_t bufId() const
Definition: infer_batch_buffer.h:187
nvdsinferserver::SharedOptions
std::shared_ptr< IOptions > SharedOptions
Definition: infer_common.h:73
nvdsinferserver::BaseBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const override
Definition: infer_batch_buffer.h:42
nvdsinferserver::WrapCBatchBuffer::WrapCBatchBuffer
WrapCBatchBuffer(BufPtr buf)
Definition: infer_batch_buffer.h:252
nvdsinferserver::WrapCBatchBuffer::getBufPtr
void * getBufPtr(uint32_t batchIdx) const final
Definition: infer_batch_buffer.h:260
NvBufSurfTransformSyncObjWait
NvBufSurfTransform_Error NvBufSurfTransformSyncObjWait(NvBufSurfTransformSyncObj_t sync_obj, uint32_t time_out)
Wait on the synchronization object.
nvdsinferserver::WrapCBatchBuffer::getTotalBytes
uint64_t getTotalBytes() const final
Definition: infer_batch_buffer.h:264
nvdsinferserver::BaseBatchBuffer::setSyncObj
void setSyncObj(NvBufSurfTransformSyncObj_t SyncObj)
Definition: infer_batch_buffer.h:54
nvdsinferserver::BaseBatchBuffer::mutableAttachedBufs
std::vector< SharedBatchBuf > & mutableAttachedBufs()
Definition: infer_batch_buffer.h:78
nvdsinferserver::InferBufferDescription::dataType
InferDataType dataType
Datatype associated with the buffer.
Definition: infer_datatypes.h:180
nvdsinferserver::BaseBatchBuffer::BaseBatchBuffer
BaseBatchBuffer(uint32_t batchSize)
Definition: infer_batch_buffer.h:36
nvdsinferserver::BaseBatchBuffer::hasAttachedBufs
bool hasAttachedBufs() const
Definition: infer_batch_buffer.h:73
INFER_EXPORT_API
Definition: infer_utils.h:33
nvdsinferserver::RefBatchBuffer::getBufPtr
void * getBufPtr(uint32_t batchIdx) const override
Definition: infer_batch_buffer.h:216
nvdsinferserver::IOptions
Definition: infer_ioptions.h:54
nvdsinferserver::BaseBatchArray::appendIBatchBuf
void appendIBatchBuf(SharedIBatchBuffer buf) final
Definition: infer_batch_buffer.h:141
nvdsinferserver::BaseBatchBuffer::setBufDesc
void setBufDesc(const InferBufferDescription &desc)
Definition: infer_batch_buffer.h:49
nvdsinferserver::SharedIOptions
std::shared_ptr< IOptions > SharedIOptions
Definition: infer_datatypes.h:206
nvdsinferserver::BaseBatchArray
The base class for array of batch buffers.
Definition: infer_batch_buffer.h:125
nvdsinferserver::InferDataType::kNone
@ kNone
nvdsinferserver::BaseBatchBuffer::setCuEvent
void setCuEvent(SharedCuEvent e)
Definition: infer_batch_buffer.h:53
nvdsinferserver::InferDims::numElements
unsigned int numElements
Number of elements in the layer including all dimensions.
Definition: infer_datatypes.h:153
nvdsinferserver::BaseBatchArray::buf
SharedBatchBuf & buf(uint32_t idx)
Definition: infer_batch_buffer.h:160
nvdsinferserver::InferBufferDescription::dims
InferDims dims
Dimensions of the tensor.
Definition: infer_datatypes.h:184
nvdsinferserver::BaseBatchArray::getSafeOptions
SharedIOptions getSafeOptions() const
Definition: infer_batch_buffer.h:149
nvdsinferserver::SharedCuEvent
std::shared_ptr< CudaEvent > SharedCuEvent
Definition: infer_common.h:86
nvdsinferserver::BaseBatchBuffer
The base class for batch buffers.
Definition: infer_batch_buffer.h:34
nvdsinferserver::InferBufferDescription::elementSize
uint32_t elementSize
Per element bytes, except kString (with elementSize is 0)
Definition: infer_datatypes.h:188
nvdsinferserver::BaseBatchBuffer::attach
void attach(SharedBatchBuf buf)
Definition: infer_batch_buffer.h:67
nvdsinferserver::BaseBatchArray::findFirstGpuId
int findFirstGpuId() const
Definition: infer_batch_buffer.h:167
nvdsinferserver::IBatchArray
Interface class for an array of batch buffers.
Definition: infer_datatypes.h:228
nvbufsurftransform.h
nvdsinferserver::BaseBatchBuffer::mutableBufDesc
InferBufferDescription & mutableBufDesc()
Definition: infer_batch_buffer.h:50
nvdsinferserver::BaseBatchBuffer::hasAttachLoop
bool hasAttachLoop() const
Definition: infer_batch_buffer.h:79
nvdsinferserver::InferMemType::kNone
@ kNone
nvdsinferserver::RefBatchBuffer::getBufOffset
size_t getBufOffset(uint32_t batchIdx) const override
Get the offset from start of the memory allocation to the buffer pointer.
Definition: infer_batch_buffer.h:232
nvdsinferserver::BaseBatchArray::bufs
const std::vector< SharedBatchBuf > & bufs() const
Definition: infer_batch_buffer.h:152
nvdsinferserver::InferBufferDescription
Holds the information about a inference buffer.
Definition: infer_datatypes.h:168