NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.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_FUNC_UTILS_H__
19 #define __NVDSINFER_FUNC_UTILS_H__
20 
21 #include <dlfcn.h>
22 #include <stdarg.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <cassert>
26 #include <condition_variable>
27 #include <mutex>
28 #include <sstream>
29 #include <string>
30 #include <unordered_set>
31 
32 #include <NvInfer.h>
33 #include <NvInferRuntime.h>
34 #include <nvdsinfer.h>
35 #include <nvdsinfer_context.h>
36 #include <nvdsinfer_logger.h>
37 
38 /* This file provides APIs/macros for some frequently used functionality. */
39 
40 #define DISABLE_CLASS_COPY(NoCopyClass) \
41  NoCopyClass(const NoCopyClass&) = delete; \
42  void operator=(const NoCopyClass&) = delete
43 
44 #define SIMPLE_MOVE_COPY(Cls) \
45  Cls& operator=(Cls&& o) { \
46  move_copy(std::move(o)); \
47  return *this; \
48  } \
49  Cls(Cls&& o) { move_copy(std::move(o)); }
50 
51 #define CHECK_NVINFER_ERROR(err, action, fmt, ...) \
52  do { \
53  NvDsInferStatus ifStatus = (err); \
54  if (ifStatus != NVDSINFER_SUCCESS) { \
55  auto errStr = NvDsInferStatus2Str(ifStatus); \
56  dsInferError(fmt ", nvinfer error:%s", ##__VA_ARGS__, errStr); \
57  action; \
58  } \
59  } while (0)
60 
61 #define RETURN_NVINFER_ERROR(err, fmt, ...) \
62  CHECK_NVINFER_ERROR(err, return ifStatus, fmt, ##__VA_ARGS__)
63 
64 #define CHECK_CUDA_ERR_W_ACTION(err, action, fmt, ...) \
65  do { \
66  cudaError_t errnum = (err); \
67  if (errnum != cudaSuccess) { \
68  dsInferError(fmt ", cuda err_no:%d, err_str:%s", ##__VA_ARGS__, \
69  (int)errnum, cudaGetErrorName(errnum)); \
70  action; \
71  } \
72  } while (0)
73 
74 #define CHECK_CUDA_ERR_NO_ACTION(err, fmt, ...) \
75  CHECK_CUDA_ERR_W_ACTION(err, , fmt, ##__VA_ARGS__)
76 
77 #define RETURN_CUDA_ERR(err, fmt, ...) \
78  CHECK_CUDA_ERR_W_ACTION( \
79  err, return NVDSINFER_CUDA_ERROR, fmt, ##__VA_ARGS__)
80 
81 #define READ_SYMBOL(lib, func_name) \
82  lib->symbol<decltype(&func_name)>(#func_name)
83 
84 namespace nvdsinfer {
85 
86 inline const char* safeStr(const char* str)
87 {
88  return !str ? "" : str;
89 }
90 
91 inline const char* safeStr(const std::string& str)
92 {
93  return str.c_str();
94 }
95 
96 inline bool string_empty(const char* str)
97 {
98  return !str || strlen(str) == 0;
99 }
100 
101 inline bool file_accessible(const char* path)
102 {
103  assert(path);
104  return (access(path, F_OK) != -1);
105 }
106 
107 inline bool file_accessible(const std::string& path)
108 {
109  return (!path.empty()) && file_accessible(path.c_str());
110 }
111 
112 std::string dims2Str(const nvinfer1::Dims& d);
113 std::string dims2Str(const NvDsInferDims& d);
114 std::string batchDims2Str(const NvDsInferBatchDims& d);
115 
116 std::string dataType2Str(const nvinfer1::DataType type);
117 std::string dataType2Str(const NvDsInferDataType type);
118 std::string networkMode2Str(const NvDsInferNetworkMode type);
119 
120 /* Custom unique_ptr subclass with deleter functions for TensorRT objects. */
121 template <class T>
122 class UniquePtrWDestroy : public std::unique_ptr<T, void (*)(T*)>
123 {
124 public:
125  UniquePtrWDestroy(T* t = nullptr)
126  : std::unique_ptr<T, void (*)(T*)>(t, [](T* t) {
127  if (t)
128  t->destroy();
129  }) {}
130 };
131 
132 template <class T>
133 class SharedPtrWDestroy : public std::shared_ptr<T>
134 {
135 public:
136  SharedPtrWDestroy(T* t = nullptr)
137  : std::shared_ptr<T>(t, [](T* t) {
138  if (t)
139  t->destroy();
140  }) {}
141 };
142 
143 class DlLibHandle
144 {
145 public:
146  DlLibHandle(const std::string& path, int mode = RTLD_LAZY);
147  ~DlLibHandle();
148 
149  bool isValid() const { return m_LibHandle; }
150  const std::string& getPath() const { return m_LibPath; }
151 
152  template <typename FuncPtr>
153  FuncPtr symbol(const char* func)
154  {
155  assert(!string_empty(func));
156  if (!m_LibHandle)
157  return nullptr;
158  return (FuncPtr)dlsym(m_LibHandle, func);
159  }
160 
161  template <typename FuncPtr>
162  FuncPtr symbol(const std::string& func)
163  {
164  return symbol<FuncPtr>(func.c_str());
165  }
166 
167 private:
168  void* m_LibHandle{nullptr};
169  const std::string m_LibPath;
170 };
171 
172 
173 template <typename Container>
174 class GuardQueue
175 {
176 public:
177  typedef typename Container::value_type T;
178  void push(const T& data)
179  {
180  std::unique_lock<std::mutex> lock(m_Mutex);
181  m_Queue.push_back(data);
182  m_Cond.notify_one();
183  }
184  T pop()
185  {
186  std::unique_lock<std::mutex> lock(m_Mutex);
187  m_Cond.wait(lock, [this]() { return !m_Queue.empty(); });
188  assert(!m_Queue.empty());
189  T ret = std::move(*m_Queue.begin());
190  m_Queue.erase(m_Queue.begin());
191  return ret;
192  }
193  bool isEmpty()
194  {
195  std::unique_lock<std::mutex> lock(m_Mutex);
196  return m_Queue.empty();
197  }
198  void clear()
199  {
200  std::unique_lock<std::mutex> lock(m_Mutex);
201  m_Queue.clear();
202  }
203 
204 private:
205  std::mutex m_Mutex;
206  std::condition_variable m_Cond;
207  Container m_Queue;
208 };
209 
213 inline uint32_t
215 {
216  switch (t)
217  {
218  case INT64:
219  return 8;
220  case INT32:
221  case FLOAT:
222  return 4;
223  case HALF:
224  return 2;
225  case INT8:
226  case UINT8:
227  return 1;
228  default:
229  dsInferError(
230  "Failed to get element size on Unknown datatype:%d", (int)t);
231  return 0;
232  }
233 }
234 
235 /* Convert between TRT's nvinfer1::Dims representation and DeepStream's
236  * NvDsInferDimsCHW/NvDsInferDims representation. */
237 nvinfer1::Dims ds2TrtDims(const NvDsInferDimsCHW& dims);
238 nvinfer1::Dims ds2TrtDims(const NvDsInferDims& dims);
239 NvDsInferDims trt2DsDims(const nvinfer1::Dims& dims);
240 
241 /* Add batch size to provided dims to get full dims as nvinfer1::Dims. */
242 nvinfer1::Dims CombineDimsBatch(const NvDsInferDims& dims, int batch);
243 /* Split full dims provided in the form of nvinfer1::Dims into batch size and
244  * layer dims. */
245 void SplitFullDims(
246  const nvinfer1::Dims& fullDims, NvDsInferDims& dims, int& batch);
247 
248 /* Convert from TRT's nvinfer1::Dims representation to DeepStream's
249  * NvDsInferBatchDims representation. */
250 inline void
251 convertFullDims(const nvinfer1::Dims& fullDims, NvDsInferBatchDims& batchDims)
252 {
253  SplitFullDims(fullDims, batchDims.dims, batchDims.batchSize);
254 }
255 
256 void normalizeDims(NvDsInferDims& dims);
257 
258 bool hasWildcard(const nvinfer1::Dims& dims);
259 bool hasWildcard(const NvDsInferDims& dims);
260 
261 /* Equality / inequality operators implementation for nvinfer1::Dims */
262 bool operator<=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
263 bool operator>(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
264 bool operator==(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
265 bool operator!=(const nvinfer1::Dims& a, const nvinfer1::Dims& b);
266 
267 /* Equality / inequality operators implementation for NvDsInferDims */
268 bool operator<=(const NvDsInferDims& a, const NvDsInferDims& b);
269 bool operator>(const NvDsInferDims& a, const NvDsInferDims& b);
270 bool operator==(const NvDsInferDims& a, const NvDsInferDims& b);
271 bool operator!=(const NvDsInferDims& a, const NvDsInferDims& b);
272 
273 
274 bool isValidOutputFormat(const std::string& fmt);
275 bool isValidOutputDataType(const std::string& dataType);
276 nvinfer1::DataType str2DataType(const std::string& dataType);
277 uint32_t str2TensorFormat(const std::string& fmt);
278 
279 struct BuildParams;
280 bool validateIOTensorNames(const BuildParams& params,
281  const nvinfer1::INetworkDefinition& network);
282 bool isValidDeviceType(const std::string& fmt);
283 bool isValidPrecisionType(const std::string& dataType);
284 nvinfer1::DataType str2PrecisionType(const std::string& dataType);
285 nvinfer1::DeviceType str2DeviceType(const std::string& deviceType);
286 
287 } // namespace nvdsinfer
288 
289 #endif
nvdsinfer::SharedPtrWDestroy::SharedPtrWDestroy
SharedPtrWDestroy(T *t=nullptr)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:136
nvdsinfer::DlLibHandle::getPath
const std::string & getPath() const
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:150
UINT8
@ UINT8
Specifies UINT8 format.
Definition: sources/includes/nvdsinfer.h:90
nvdsinfer::isValidPrecisionType
bool isValidPrecisionType(const std::string &dataType)
nvdsinfer::safeStr
const char * safeStr(const char *str)
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:86
nvdsinfer::ds2TrtDims
nvinfer1::Dims ds2TrtDims(const NvDsInferDimsCHW &dims)
nvdsinfer::isValidOutputFormat
bool isValidOutputFormat(const std::string &fmt)
nvdsinfer::GuardQueue::pop
T pop()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:184
nvdsinfer::GuardQueue::push
void push(const T &data)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:178
nvdsinfer::dims2Str
std::string dims2Str(const nvinfer1::Dims &d)
ds3d::DataType
DataType
Definition: sources/includes/ds3d/common/idatatype.h:82
nvdsinfer::normalizeDims
void normalizeDims(NvDsInferDims &dims)
nvdsinfer::DlLibHandle::DlLibHandle
DlLibHandle(const std::string &path, int mode=RTLD_LAZY)
nvdsinfer::CombineDimsBatch
nvinfer1::Dims CombineDimsBatch(const NvDsInferDims &dims, int batch)
nvdsinfer::operator>
bool operator>(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::str2PrecisionType
nvinfer1::DataType str2PrecisionType(const std::string &dataType)
NvDsInferDims
Holds the dimensions of a layer.
Definition: sources/includes/nvdsinfer.h:51
nvdsinfer::networkMode2Str
std::string networkMode2Str(const NvDsInferNetworkMode type)
nvdsinfer::str2DataType
nvinfer1::DataType str2DataType(const std::string &dataType)
NvDsInferDataType
NvDsInferDataType
Specifies the data type of a layer.
Definition: sources/includes/nvdsinfer.h:77
NvDsInferNetworkMode
NvDsInferNetworkMode
Defines internal data formats used by the inference engine.
Definition: sources/includes/nvdsinfer_context.h:127
nvdsinfer
Definition: sources/includes/nvdsinfer_logger.h:59
nvdsinfer::GuardQueue::T
Container::value_type T
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:177
nvdsinfer::operator!=
bool operator!=(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::str2TensorFormat
uint32_t str2TensorFormat(const std::string &fmt)
FLOAT
@ FLOAT
Specifies FP32 format.
Definition: sources/includes/nvdsinfer.h:80
nvdsinfer::hasWildcard
bool hasWildcard(const nvinfer1::Dims &dims)
nvdsinfer::GuardQueue::clear
void clear()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:198
HALF
@ HALF
Specifies FP16 format.
Definition: sources/includes/nvdsinfer.h:82
INT32
@ INT32
Specifies INT32 format.
Definition: sources/includes/nvdsinfer.h:86
INT8
@ INT8
Specifies INT8 format.
Definition: sources/includes/nvdsinfer.h:84
nvdsinfer::convertFullDims
void convertFullDims(const nvinfer1::Dims &fullDims, NvDsInferBatchDims &batchDims)
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:251
nvdsinfer::UniquePtrWDestroy::UniquePtrWDestroy
UniquePtrWDestroy(T *t=nullptr)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:125
NvDsInferDimsCHW
Holds the dimensions of a three-dimensional layer.
Definition: sources/includes/nvdsinfer.h:64
nvdsinfer::operator==
bool operator==(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::operator<=
bool operator<=(const nvinfer1::Dims &a, const nvinfer1::Dims &b)
nvdsinfer::dataType2Str
std::string dataType2Str(const nvinfer1::DataType type)
nvdsinfer::DlLibHandle::symbol
FuncPtr symbol(const std::string &func)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:162
nvdsinfer::file_accessible
bool file_accessible(const char *path)
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:101
nvdsinfer::DlLibHandle::~DlLibHandle
~DlLibHandle()
nvdsinfer::DlLibHandle::symbol
FuncPtr symbol(const char *func)
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:153
INT64
@ INT64
Specifies INT64 format.
Definition: sources/includes/nvdsinfer.h:88
nvdsinfer::str2DeviceType
nvinfer1::DeviceType str2DeviceType(const std::string &deviceType)
nvdsinfer::string_empty
bool string_empty(const char *str)
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:96
nvdsinfer::trt2DsDims
NvDsInferDims trt2DsDims(const nvinfer1::Dims &dims)
nvdsinfer::SplitFullDims
void SplitFullDims(const nvinfer1::Dims &fullDims, NvDsInferDims &dims, int &batch)
nvdsinfer::GuardQueue::isEmpty
bool isEmpty()
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:193
nvdsinfer::getElementSize
uint32_t getElementSize(NvDsInferDataType t)
Get the size of the element from the data type.
Definition: sources/libs/nvdsinfer/nvdsinfer_func_utils.h:214
nvdsinfer::batchDims2Str
std::string batchDims2Str(const NvDsInferBatchDims &d)
nvdsinfer::validateIOTensorNames
bool validateIOTensorNames(const BuildParams &params, const nvinfer1::INetworkDefinition &network)
INFER_EXPORT_API::fullDims
InferDims fullDims(int batchSize, const InferDims &in)
Extend the dimensions to include batch size.
nvdsinfer::isValidDeviceType
bool isValidDeviceType(const std::string &fmt)
nvdsinfer::DlLibHandle::isValid
bool isValid() const
Definition: 9.1/sources/libs/nvdsinfer/nvdsinfer_func_utils.h:149
dsInferError
#define dsInferError(fmt,...)
Definition: sources/includes/nvdsinfer_logger.h:35
nvdsinfer::isValidOutputDataType
bool isValidOutputDataType(const std::string &dataType)