NVIDIA DeepStream SDK API Reference

9.1 Release
9.1/sources/libs/nvdsinferserver/infer_base_context.h
Go to the documentation of this file.
1 /*
2  * SPDX-FileCopyrightText: Copyright (c) 2018-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 
28 #ifndef __INFER_BASE_CONTEXT_H__
29 #define __INFER_BASE_CONTEXT_H__
30 
31 #include <stdarg.h>
32 #include <condition_variable>
33 #include <functional>
34 #include <list>
35 #include <memory>
36 #include <mutex>
37 #include <queue>
38 
39 #include "infer_base_backend.h"
40 #include "infer_common.h"
41 #include "infer_icontext.h"
42 #include "nvdsinferserver_config.pb.h"
43 
44 namespace ic = nvdsinferserver::config;
45 
46 namespace nvdsinferserver {
47 
48 class DlLibHandle;
49 
50 using InferCompleted = std::function<void(NvDsInferStatus, SharedBatchArray)>;
51 
57 class InferBaseContext : public IInferContext {
58 public:
60  ~InferBaseContext() override;
61 
63  const std::string& prototxt, InferLoggingFunc logFunc) final;
64  NvDsInferStatus run(SharedIBatchArray input, InferOutputCb outputCb) final;
65  NvDsInferStatus deinit() override;
66 
67 private:
68  virtual NvDsInferStatus createNNBackend(const ic::BackendParams& params,
69  int maxBatchSize, UniqBackend& backend) = 0;
70  virtual NvDsInferStatus fixateInferenceInfo(
71  const ic::InferenceConfig& config, BaseBackend& backend) = 0;
72  virtual NvDsInferStatus createPreprocessor(
73  const ic::PreProcessParams& params,
74  std::vector<UniqPreprocessor>& processors) = 0;
75  virtual NvDsInferStatus createPostprocessor(
76  const ic::PostProcessParams& params, UniqPostprocessor& processor) = 0;
77  virtual NvDsInferStatus allocateResource(
78  const ic::InferenceConfig& config) = 0;
79 
80  virtual NvDsInferStatus preInference(
81  SharedBatchArray& inputs, const ic::InferenceConfig& config)
82  {
83  return NVDSINFER_SUCCESS;
84  }
85 
86  virtual NvDsInferStatus extraOutputTensorCheck(
87  SharedBatchArray& outputs, SharedOptions inOptions)
88  {
89  return NVDSINFER_SUCCESS;
90  }
91  virtual void notifyError(NvDsInferStatus status) = 0;
92 
93  void rawDataInferDone(
94  NvDsInferStatus status, SharedBatchArray outputs, SharedOptions inOptions,
95  InferCompleted done);
96 
97 protected:
98  virtual void backendConsumedInputs(SharedBatchArray inputs) {
99  inputs.reset();
100  }
101  virtual SharedCuStream& mainStream() = 0;
102 
103  const ic::InferenceConfig& config() const { return m_Config; }
104  int maxBatchSize() const { return m_MaxBatchSize; }
105  int uniqueId() const { return m_UniqueID; }
106  BaseBackend* backend() { return m_Backend.get(); }
107  const SharedDllHandle& customLib() const { return m_CustomLib; }
108  bool needCopyInputToHost() const;
109  void print(NvDsInferLogLevel l, const char* msg);
110  bool needPreprocess() const;
111 
112 private:
113  NvDsInferStatus buidNextPreprocMap();
114  NvDsInferStatus forEachPreprocess(
115  IPreprocessor* cur, SharedBatchArray input, InferCompleted done);
116  NvDsInferStatus doInference(SharedBatchArray inputs, InferCompleted done);
117  NvDsInferStatus doPostCudaProcess(
118  SharedBatchArray inputs, InferCompleted done);
119  NvDsInferStatus doPostHostProcess(
120  SharedBatchArray inputs, InferCompleted done);
121 
122 private:
123  InferLoggingFunc m_LoggingFunc;
124  uint32_t m_UniqueID = 0;
128  uint32_t m_MaxBatchSize = 1;
129  bool m_Initialized = false;
130 
136  std::vector<UniqPreprocessor> m_Preprocessors;
137  std::unordered_map<IPreprocessor*, IPreprocessor*> m_NextPreprocMap;
138  UniqBackend m_Backend;
139  UniqPostprocessor m_Postprocessor;
140  SharedDllHandle m_CustomLib;
143  ic::InferenceConfig m_Config;
144 };
145 
146 } // namespace nvdsinferserver
147 
148 #define _MAX_LOG_LENGTH 4096
149 #define printMsg(level, tag_str, fmt, ...) \
150  do { \
151  char* baseName = strrchr((char*)__FILE__, '/'); \
152  baseName = (baseName) ? (baseName + 1) : (char*)__FILE__; \
153  std::vector<char> logMsgBuffer(_MAX_LOG_LENGTH, 0); \
154  snprintf(logMsgBuffer.data(), _MAX_LOG_LENGTH - 1, \
155  tag_str " %s() <%s:%d> [UID = %d]: " fmt, \
156  __func__, baseName, __LINE__, uniqueId(), ##__VA_ARGS__); \
157  this->print(level, logMsgBuffer.data()); \
158  } while (0)
159 
160 #define printError(fmt, ...) \
161  do { \
162  printMsg(NVDSINFER_LOG_ERROR, "Error in", fmt, ##__VA_ARGS__); \
163  } while (0)
164 
165 #define printWarning(fmt, ...) \
166  do { \
167  printMsg(NVDSINFER_LOG_WARNING, "Warning from", fmt, ##__VA_ARGS__); \
168  } while (0)
169 
170 #define printInfo(fmt, ...) \
171  do { \
172  printMsg(NVDSINFER_LOG_INFO, "Info from", fmt, ##__VA_ARGS__); \
173  } while (0)
174 
175 #define printDebug(fmt, ...) \
176  do { \
177  printMsg(NVDSINFER_LOG_DEBUG, "DEBUG", fmt, ##__VA_ARGS__); \
178  } while (0)
179 
180 #define CTX_RETURN_NVINFER_ERROR(err, fmt, ...) \
181  CHECK_NVINFER_ERROR_PRINT( \
182  err, return ifStatus, printError, fmt, ##__VA_ARGS__)
183 
184 #define CTX_RETURN_CUDA_ERR(err, fmt, ...) \
185  CHECK_CUDA_ERR_W_ACTION( \
186  err, return NVDSINFER_CUDA_ERROR, printError, fmt, ##__VA_ARGS__)
187 
188 #endif /* __INFER_BASE_CONTEXT_H__ */
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::InferBaseContext::initialize
NvDsInferStatus initialize(const std::string &prototxt, InferLoggingFunc logFunc) final
infer_common.h
Header file of the common declarations for the nvinferserver library.
nvdsinferserver::SharedDllHandle
std::shared_ptr< DlLibHandle > SharedDllHandle
Definition: sources/libs/nvdsinferserver/infer_common.h:116
nvdsinferserver::InferBaseContext::maxBatchSize
int maxBatchSize() const
Definition: sources/libs/nvdsinferserver/infer_base_context.h:104
nvdsinferserver::InferBaseContext::customLib
const SharedDllHandle & customLib() const
Definition: 9.1/sources/libs/nvdsinferserver/infer_base_context.h:107
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
NvDsInferLogLevel
NvDsInferLogLevel
Enum for the log levels of NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:262
nvdsinferserver::InferBaseContext::needPreprocess
bool needPreprocess() const
nvdsinferserver::UniqPostprocessor
std::unique_ptr< BasePostprocessor > UniqPostprocessor
Processor interfaces.
Definition: sources/libs/nvdsinferserver/infer_common.h:103
nvdsinferserver::SharedCuStream
std::shared_ptr< CudaStream > SharedCuStream
Cuda based pointers.
Definition: sources/libs/nvdsinferserver/infer_common.h:89
nvdsinferserver::SharedOptions
std::shared_ptr< IOptions > SharedOptions
Definition: sources/libs/nvdsinferserver/infer_common.h:78
nvdsinferserver::InferBaseContext::deinit
NvDsInferStatus deinit() override
nvdsinferserver::UniqBackend
std::unique_ptr< BaseBackend > UniqBackend
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:217
nvdsinferserver::InferBaseContext::backend
BaseBackend * backend()
Definition: sources/libs/nvdsinferserver/infer_base_context.h:106
nvdsinferserver::InferBaseContext::print
void print(NvDsInferLogLevel l, const char *msg)
nvdsinferserver::InferBaseContext::config
const ic::InferenceConfig & config() const
Definition: sources/libs/nvdsinferserver/infer_base_context.h:103
nvdsinferserver::InferBaseContext::~InferBaseContext
~InferBaseContext() override
nvdsinferserver::InferCompleted
std::function< void(NvDsInferStatus, SharedBatchArray)> InferCompleted
Definition: sources/libs/nvdsinferserver/infer_base_context.h:50
nvdsinferserver::SharedIBatchArray
std::shared_ptr< IBatchArray > SharedIBatchArray
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:210
nvdsinferserver::BaseBackend
Base class of inference backend processing.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:45
nvdsinferserver::InferBaseContext::InferBaseContext
InferBaseContext()
nvdsinferserver::InferBaseContext::run
NvDsInferStatus run(SharedIBatchArray input, InferOutputCb outputCb) final
nvdsinferserver::IPreprocessor
Preprocessor interface class.
Definition: sources/libs/nvdsinferserver/infer_iprocess.h:46
infer_base_backend.h
Header file for inference processing backend base class.
nvdsinferserver::InferBaseContext::mainStream
virtual SharedCuStream & mainStream()=0
nvdsinferserver::InferBaseContext::uniqueId
int uniqueId() const
Definition: 9.1/sources/libs/nvdsinferserver/infer_base_context.h:105
nvdsinferserver::InferBaseContext::needCopyInputToHost
bool needCopyInputToHost() const
nvdsinferserver::InferBaseContext::backendConsumedInputs
virtual void backendConsumedInputs(SharedBatchArray inputs)
Definition: 9.1/sources/libs/nvdsinferserver/infer_base_context.h:98
NvDsInferStatus
NvDsInferStatus
Enum for the status codes returned by NvDsInferContext.
Definition: sources/includes/nvdsinfer.h:231