NVIDIA DeepStream SDK API Reference

7.0 Release
infer_iprocess.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2018-2022 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 
20 #ifndef __NVDSINFERSERVER_IPROCESS_H__
21 #define __NVDSINFERSERVER_IPROCESS_H__
22 
23 #include <stdarg.h>
24 #include <condition_variable>
25 #include <functional>
26 #include <list>
27 #include <memory>
28 #include <mutex>
29 #include <queue>
30 #include <tuple>
31 
32 #include "infer_common.h"
33 #include "infer_datatypes.h"
34 #include "infer_utils.h"
35 
36 namespace nvdsinferserver {
37 
42 public:
43  using PreprocessDone =
44  std::function<void(NvDsInferStatus, SharedBatchArray)>;
45 
46  IPreprocessor() = default;
47  virtual ~IPreprocessor() = default;
48 
50  SharedCuStream mainStream, PreprocessDone done) = 0;
51 
52 private:
53  DISABLE_CLASS_COPY(IPreprocessor);
54 };
55 
60 public:
61  using PostprocessDone =
62  std::function<void(NvDsInferStatus, SharedBatchArray)>;
63  IPostprocessor() = default;
64  virtual ~IPostprocessor() = default;
65 
67  SharedCuStream mainStream, PostprocessDone done) = 0;
68 
70  SharedCuStream mainStream, PostprocessDone done) = 0;
71 
72 private:
73  DISABLE_CLASS_COPY(IPostprocessor);
74 };
75 
80 public:
84  ~BasePreprocessor() override = default;
85 
86  /*
87  * @brief Helper functions to access member variables.
88  */
90  void setUniqueId(int id) { m_UniqueId = id; }
91  int uniqueId() const { return m_UniqueId; }
92  void setTransformIdx(int idx) { m_TransformIdx = idx; }
104  PreprocessDone done) override {
105  assert(src && (int)src->getSize() > m_TransformIdx);
106  SharedBatchBuf& from = src->buf(m_TransformIdx);
107  SharedBatchBuf to = requestOutBuffer(from);
108  if (!to) {
110  }
111  to->setBufId(src->bufId());
112  NvDsInferStatus status = transformImpl(from, to, mainStream);
113  src->buf(m_TransformIdx) = to;
114  mainStream.reset();
115  if (status == NVDSINFER_SUCCESS) {
116  done(status, std::move(src));
117  }
118  return status;
119  }
120 
127  const std::vector<int>& devIds) = 0;
128 
129 private:
136  virtual SharedBatchBuf requestOutBuffer(SharedBatchBuf& inBuf) = 0;
144  virtual NvDsInferStatus transformImpl(SharedBatchBuf& src,
145  SharedBatchBuf& dst, SharedCuStream& mainStream) = 0;
146 
147 private:
152  int m_TransformIdx = 0;
156  int m_UniqueId = 0;
157 };
158 
163 public:
168  : m_UniqueId(uid), m_ProcessType(type) {}
172  ~BasePostprocessor() override = default;
173 
174  /*
175  * @brief Helper functions to access member variables.
176  */
178  void setUniqueId(int id) { m_UniqueId = id; }
179  int uniqueId() const { return m_UniqueId; }
180  InferPostprocessType networkType() const { return m_ProcessType; }
191  SharedCuStream mainStream, PostprocessDone done) override {
192  return genericPostProcess_(std::move(inBuf), std::move(mainStream),
193  std::move(done), &BasePostprocessor::requestCudaOutBufs,
194  &BasePostprocessor::postCudaImpl);
195  }
196 
205  SharedCuStream mainStream, PostprocessDone done) override {
206  return genericPostProcess_(std::move(inBuf), std::move(mainStream),
207  std::move(done), &BasePostprocessor::requestHostOutBufs,
208  &BasePostprocessor::postHostImpl);
209  }
210 
217  const std::vector<int>& devIds) = 0;
218 
219 private:
227  virtual NvDsInferStatus postCudaImpl(SharedBatchArray& inBuf,
228  SharedBatchArray& outbuf, SharedCuStream& mainStream) = 0;
236  virtual NvDsInferStatus postHostImpl(SharedBatchArray& inBuf,
237  SharedBatchArray& outbuf, SharedCuStream& mainStream) = 0;
238 
245  virtual SharedBatchArray requestCudaOutBufs(const SharedBatchArray& inBuf) = 0;
252  virtual SharedBatchArray requestHostOutBufs(const SharedBatchArray& inBuf) = 0;
253 
263  template <typename RequestBuf, typename Impl>
264  NvDsInferStatus genericPostProcess_(SharedBatchArray inBuf,
265  SharedCuStream mainStream, PostprocessDone done, RequestBuf reqBuf,
266  Impl impl) {
267  assert(inBuf);
268  SharedBatchArray outBuf = (this->*reqBuf)(inBuf);
269  if (!outBuf) {
271  }
272  outBuf->setBufId(inBuf->bufId());
273  NvDsInferStatus status = (this->*impl)(inBuf, outBuf, mainStream);
274  inBuf.reset();
275  mainStream.reset();
276  if (status == NVDSINFER_SUCCESS) {
277  done(status, std::move(outBuf));
278  }
279  return status;
280  }
281 
282 private:
286  int m_UniqueId = 0;
291 };
292 
297 template <class BasePreprocessorT>
298 class ThreadPreprocessor : public BasePreprocessorT {
299 private:
300  using Item = std::tuple<SharedBatchArray, SharedCuStream,
302  enum { kBuf, kStream, kDone };
303 
304 public:
305  template <typename... Args>
306  ThreadPreprocessor(Args&&... args)
307  : BasePreprocessorT(std::forward<Args>(args)...),
308  m_Worker(
309  [this](Item i) -> bool {
311  std::move(std::get<kDone>(i));
312  NvDsInferStatus s = BasePreprocessorT::transform(
313  std::move(std::get<kBuf>(i)),
314  std::move(std::get<kStream>(i)), done);
315  if (s != NVDSINFER_SUCCESS) {
316  done(s, nullptr);
317  }
318  return true;
319  },
320  "Preproc")
321  {
322  }
323  ~ThreadPreprocessor() override { m_Worker.join(); }
324  void setThreadName(const std::string &name) {
325  m_Worker.setThreadName(name);
326  }
328  IPreprocessor::PreprocessDone done) final {
329  Item item = std::make_tuple(
330  std::move(src), std::move(mainStream), std::move(done));
331  if (m_Worker.queueItem(std::move(item))) {
332  return NVDSINFER_SUCCESS;
333  }
335  }
336 
337 private:
338  QueueThread<std::vector<Item>> m_Worker;
339 };
340 
345 template <class BasePostprocessorT>
346 class ThreadCudaPostprocessor : public BasePostprocessorT {
347  using ItemCuda = std::tuple<SharedBatchArray, SharedCuStream,
349  enum { kBuf, kStream, kDone };
350 
351 public:
352  template <typename... Args>
353  ThreadCudaPostprocessor(Args&&... args)
354  : BasePostprocessorT(std::forward<Args>(args)...),
355  m_CudaWorker(
356  [this](ItemCuda i) -> bool {
358  std::move(std::get<kDone>(i));
359  NvDsInferStatus s = BasePostprocessorT::postCudaProcess(
360  std::move(std::get<kBuf>(i)),
361  std::move(std::get<kStream>(i)), done);
362  if (s != NVDSINFER_SUCCESS) {
363  done(s, nullptr);
364  }
365  return true;
366  },
367  "PostCuda")
368  {}
369  ~ThreadCudaPostprocessor() { m_CudaWorker.join(); }
371  SharedCuStream mainStream, IPostprocessor::PostprocessDone done) final {
372  ItemCuda item = std::make_tuple(
373  std::move(inBuf), std::move(mainStream), std::move(done));
374  if (m_CudaWorker.queueItem(std::move(item))) {
375  return NVDSINFER_SUCCESS;
376  }
378  }
379 
380 private:
381  QueueThread<std::vector<ItemCuda>> m_CudaWorker;
382 };
383 
388 template <typename BasePostprocessorT>
389 class ThreadHostPostprocessor : public BasePostprocessorT {
390  using ItemHost = std::tuple<SharedBatchArray, SharedCuStream,
392  enum { kBuf, kStream, kDone };
393 
394 public:
395  template <typename... Args>
396  ThreadHostPostprocessor(Args&&... args)
397  : BasePostprocessorT(std::forward<Args>(args)...),
398  m_HostWorker(
399  [this](ItemHost i) -> bool {
401  std::move(std::get<kDone>(i));
402  NvDsInferStatus s = BasePostprocessorT::postHostProcess(
403  std::move(std::get<kBuf>(i)),
404  std::move(std::get<kStream>(i)), done);
405  if (s != NVDSINFER_SUCCESS) {
406  done(s, nullptr);
407  }
408  return true;
409  },
410  "PostHost")
411  {
412  }
413 
414  ~ThreadHostPostprocessor() { m_HostWorker.join(); }
415 
417  SharedCuStream mainStream, IPostprocessor::PostprocessDone done) final {
418  ItemHost item = std::make_tuple(
419  std::move(inBuf), std::move(mainStream), std::move(done));
420  if (m_HostWorker.queueItem(std::move(item))) {
421  return NVDSINFER_SUCCESS;
422  }
424  }
425 
426 private:
427  QueueThread<std::vector<ItemHost>> m_HostWorker;
428 };
429 
430 } // namespace nvdsinferserver
431 
432 #endif
nvdsinferserver
This is a header file for pre-processing cuda kernels with normalization and mean subtraction require...
Definition: infer_custom_process.h:24
nvdsinferserver::IPostprocessor::IPostprocessor
IPostprocessor()=default
nvdsinferserver::BasePostprocessor::allocateResource
virtual NvDsInferStatus allocateResource(const std::vector< int > &devIds)=0
Allocate resource like output buffer pool.
nvdsinferserver::BasePreprocessor::transform
NvDsInferStatus transform(SharedBatchArray src, SharedCuStream mainStream, PreprocessDone done) override
Perform the transformation on the input buffer from the buffer array, indexed using m_TransformIdx.
Definition: infer_iprocess.h:103
nvdsinferserver::BasePostprocessor::~BasePostprocessor
~BasePostprocessor() override=default
Destructor default.
nvdsinferserver::ThreadCudaPostprocessor::~ThreadCudaPostprocessor
~ThreadCudaPostprocessor()
Definition: infer_iprocess.h:369
nvdsinferserver::BasePostprocessor::postHostProcess
NvDsInferStatus postHostProcess(SharedBatchArray inBuf, SharedCuStream mainStream, PostprocessDone done) override
Acquire an output buffer array and call host side processing steps.
Definition: infer_iprocess.h:204
nvdsinferserver::IPostprocessor::postCudaProcess
virtual NvDsInferStatus postCudaProcess(SharedBatchArray inBuf, SharedCuStream mainStream, PostprocessDone done)=0
nvdsinferserver::SharedBatchBuf
std::shared_ptr< BaseBatchBuffer > SharedBatchBuf
Common buffer interfaces (internal).
Definition: infer_common.h:71
nvdsinferserver::ThreadPreprocessor::~ThreadPreprocessor
~ThreadPreprocessor() override
Definition: infer_iprocess.h:323
nvdsinferserver::IPostprocessor::PostprocessDone
std::function< void(NvDsInferStatus, SharedBatchArray)> PostprocessDone
Definition: infer_iprocess.h:62
nvdsinferserver::InferPostprocessType
InferPostprocessType
Inference post processing types.
Definition: infer_datatypes.h:103
nvdsinferserver::BasePostprocessor
Base post-processor class.
Definition: infer_iprocess.h:162
infer_datatypes.h
Header file for the data types used in the inference processing.
nvdsinferserver::IPreprocessor::PreprocessDone
std::function< void(NvDsInferStatus, SharedBatchArray)> PreprocessDone
Definition: infer_iprocess.h:44
nvdsinferserver::ThreadPreprocessor::transform
NvDsInferStatus transform(SharedBatchArray src, SharedCuStream mainStream, IPreprocessor::PreprocessDone done) final
Definition: infer_iprocess.h:327
nvdsinferserver::IPostprocessor
Post-processor interface class.
Definition: infer_iprocess.h:59
nvdsinferserver::BasePostprocessor::BasePostprocessor
BasePostprocessor(InferPostprocessType type, int uid)
Constructor, save process type and ID.
Definition: infer_iprocess.h:167
nvdsinferserver::IPreprocessor::~IPreprocessor
virtual ~IPreprocessor()=default
NVDSINFER_SUCCESS
@ NVDSINFER_SUCCESS
NvDsInferContext operation succeeded.
Definition: nvdsinfer.h:220
nvdsinferserver::ThreadCudaPostprocessor::postCudaProcess
NvDsInferStatus postCudaProcess(SharedBatchArray inBuf, SharedCuStream mainStream, IPostprocessor::PostprocessDone done) final
Definition: infer_iprocess.h:370
nvdsinferserver::BasePreprocessor::uniqueId
int uniqueId() const
Definition: infer_iprocess.h:91
nvdsinferserver::IPostprocessor::postHostProcess
virtual NvDsInferStatus postHostProcess(SharedBatchArray inBuf, SharedCuStream mainStream, PostprocessDone done)=0
nvdsinferserver::IPreprocessor::IPreprocessor
IPreprocessor()=default
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
nvdsinferserver::BasePreprocessor
Base preprocessor class.
Definition: infer_iprocess.h:79
nvdsinferserver::IPostprocessor::~IPostprocessor
virtual ~IPostprocessor()=default
nvdsinferserver::BasePostprocessor::postCudaProcess
NvDsInferStatus postCudaProcess(SharedBatchArray inBuf, SharedCuStream mainStream, PostprocessDone done) override
Acquire an output buffer array and call CUDA post processing steps.
Definition: infer_iprocess.h:190
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::BasePostprocessor::setUniqueId
void setUniqueId(int id)
Definition: infer_iprocess.h:178
NVDSINFER_UNKNOWN_ERROR
@ NVDSINFER_UNKNOWN_ERROR
Unknown error was encountered.
Definition: nvdsinfer.h:243
nvdsinferserver::ThreadHostPostprocessor::ThreadHostPostprocessor
ThreadHostPostprocessor(Args &&... args)
Definition: infer_iprocess.h:396
nvdsinferserver::InferPostprocessType::kOther
@ kOther
Custom post processing.
nvdsinferserver::BasePreprocessor::setTransformIdx
void setTransformIdx(int idx)
Definition: infer_iprocess.h:92
nvdsinferserver::BasePreprocessor::setUniqueId
void setUniqueId(int id)
Definition: infer_iprocess.h:90
nvdsinferserver::ThreadPreprocessor
Preprocessor thread queue class template.
Definition: infer_iprocess.h:298
nvdsinferserver::ThreadPreprocessor::ThreadPreprocessor
ThreadPreprocessor(Args &&... args)
Definition: infer_iprocess.h:306
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: infer_common.h:84
nvdsinferserver::BasePostprocessor::networkType
InferPostprocessType networkType() const
Definition: infer_iprocess.h:180
nvdsinferserver::ThreadCudaPostprocessor
A CUDA post processor thread queue template class.
Definition: infer_iprocess.h:346
nvdsinferserver::ThreadPreprocessor::setThreadName
void setThreadName(const std::string &name)
Definition: infer_iprocess.h:324
nvdsinferserver::BasePostprocessor::uniqueId
int uniqueId() const
Definition: infer_iprocess.h:179
nvdsinferserver::BasePreprocessor::~BasePreprocessor
~BasePreprocessor() override=default
Destructor default.
nvdsinferserver::ThreadCudaPostprocessor::ThreadCudaPostprocessor
ThreadCudaPostprocessor(Args &&... args)
Definition: infer_iprocess.h:353
nvdsinferserver::ThreadHostPostprocessor
A host post processor thread queue template class.
Definition: infer_iprocess.h:389
nvdsinferserver::BasePreprocessor::allocateResource
virtual NvDsInferStatus allocateResource(const std::vector< int > &devIds)=0
Allocate resource like output buffer pool.
nvdsinferserver::IPreprocessor
Preprocessor interface class.
Definition: infer_iprocess.h:41
nvdsinferserver::ThreadHostPostprocessor::~ThreadHostPostprocessor
~ThreadHostPostprocessor()
Definition: infer_iprocess.h:414
nvdsinferserver::IPreprocessor::transform
virtual NvDsInferStatus transform(SharedBatchArray src, SharedCuStream mainStream, PreprocessDone done)=0
NVDSINFER_RESOURCE_ERROR
@ NVDSINFER_RESOURCE_ERROR
Resource error was encountered.
Definition: nvdsinfer.h:235
nvdsinferserver::SharedBatchArray
std::shared_ptr< BaseBatchArray > SharedBatchArray
Definition: infer_common.h:75
nvdsinferserver::ThreadHostPostprocessor::postHostProcess
NvDsInferStatus postHostProcess(SharedBatchArray inBuf, SharedCuStream mainStream, IPostprocessor::PostprocessDone done) final
Definition: infer_iprocess.h:416
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: nvdsinfer.h:218