NVIDIA DeepStream SDK API Reference

9.1 Release
sources/libs/nvdsinferserver/infer_base_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 
27 #ifndef __NVDSINFER_BASE_BACKEND_H__
28 #define __NVDSINFER_BASE_BACKEND_H__
29 
30 #include <stdarg.h>
31 #include <condition_variable>
32 #include <memory>
33 #include <mutex>
34 #include <queue>
35 
36 #include "infer_datatypes.h"
37 #include "infer_ibackend.h"
38 #include "infer_utils.h"
39 
40 namespace nvdsinferserver {
41 
45 class BaseBackend : public IBackend {
46 public:
50  ~BaseBackend() override = default;
54  InferTensorOrder getInputTensorOrder() const final { return m_InputOrder; }
55 
59  void setUniqueId(uint32_t id) { m_UniqueId = id; }
60 
64  int uniqueId() const { return m_UniqueId; }
65 
69  void setFirstDimBatch(bool flag) { m_IsFirstDimBatch = flag; }
70 
74  bool isFirstDimBatch() const final { return m_IsFirstDimBatch; }
75 
80  uint32_t getLayerSize() const final {
81  assert(!m_AllLayers.empty());
82  return (int)m_AllLayers.size();
83  }
84 
88  uint32_t getInputLayerSize() const final {
89  return m_InputSize;
90  }
91 
96  const std::string& bindingName) const final;
97 
101  LayersTuple getInputLayers() const final;
102 
106  LayersTuple getOutputLayers() const final;
107 
112  bool checkInputDims(const InputShapes& shapes) const;
113 
118  const LayerDescriptionList& allLayers() const { return m_AllLayers; }
119 
123  void setKeepInputs(bool enable) {
124  m_KeepInputs = enable;
125  }
126 
130  int32_t maxBatchSize() const final { return m_MaxBatchSize; }
131 
135  bool isNonBatching() const { return isNonBatch(maxBatchSize()); }
136 
137 protected:
141  using LayerIdxMap = std::unordered_map<std::string, int>;
142 
153  void resetLayers(LayerDescriptionList layers, int inputSize);
154 
158  LayerDescription* mutableLayerInfo(const std::string& bindingName)
159  {
160  const LayerDescription* info = getLayerInfo(bindingName);
161  return const_cast<LayerDescription*>(info);
162  }
163 
167  void setInputTensorOrder(InferTensorOrder order) { m_InputOrder = order; }
168 
172  bool needKeepInputs() const {
173  return m_KeepInputs;
174  }
175 
179  void setMaxBatchSize(uint32_t size) { m_MaxBatchSize = size; }
180 
181 private:
185  LayerDescriptionList m_AllLayers;
189  LayerIdxMap m_LayerNameToIdx;
193  uint32_t m_InputSize = 0;
197  int32_t m_MaxBatchSize = 0;
201  bool m_IsFirstDimBatch = false;
202 
210  uint32_t m_UniqueId = 0;
214  bool m_KeepInputs = false;
215 };
216 
217 using UniqBackend = std::unique_ptr<BaseBackend>;
218 
219 } // namespace nvdsinferserver
220 #endif
nvdsinferserver::BaseBackend::getOutputLayers
LayersTuple getOutputLayers() const final
Get the LayersTuple for output layers.
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
INFER_EXPORT_API::isNonBatch
bool isNonBatch(T b)
Checks if the input batch size is zero.
Definition: sources/libs/nvdsinferserver/infer_utils.h:106
nvdsinferserver::BaseBackend::setMaxBatchSize
void setMaxBatchSize(uint32_t size)
Set the maximum batch size to be used for the backend.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:179
nvdsinferserver::BaseBackend::~BaseBackend
~BaseBackend() override=default
Destructor, default.
nvdsinferserver::BaseBackend::uniqueId
int uniqueId() const
Get the unique ID of the object instance.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:64
nvdsinferserver::BaseBackend::resetLayers
void resetLayers(LayerDescriptionList layers, int inputSize)
Set the layer description list of the backend.
nvdsinferserver::IBackend::InputShapes
std::vector< InputShapeTuple > InputShapes
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:89
nvdsinferserver::IBackend
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:65
nvdsinferserver::InferTensorOrder
InferTensorOrder
The type of tensor order.
Definition: sources/includes/nvdsinferserver/infer_datatypes.h:46
nvdsinferserver::BaseBackend::mutableLayerInfo
LayerDescription * mutableLayerInfo(const std::string &bindingName)
Get the mutable layer description structure for the layer name.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:158
nvdsinferserver::LayerDescription
Stores the information of a layer in the inference model.
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:39
nvdsinferserver::IBackend::LayersTuple
std::tuple< const LayerDescription *, int > LayersTuple
Tuple containing pointer to layer descriptions and the number of layers.
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:82
nvdsinferserver::BaseBackend::LayerIdxMap
std::unordered_map< std::string, int > LayerIdxMap
Map of layer name to layer index.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:141
nvdsinferserver::BaseBackend::isFirstDimBatch
bool isFirstDimBatch() const final
Returns boolean indicating if batched input is expected.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:74
nvdsinferserver::BaseBackend::getLayerInfo
const LayerDescription * getLayerInfo(const std::string &bindingName) const final
Retrieve the layer information from the layer name.
nvdsinferserver::BaseBackend::getLayerSize
uint32_t getLayerSize() const final
Returns the total number of layers (input + output) for the model.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:80
nvdsinferserver::BaseBackend::setUniqueId
void setUniqueId(uint32_t id)
Set the unique ID for the object instance.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:59
nvdsinferserver::BaseBackend::isNonBatching
bool isNonBatching() const
Checks if the batch size indicates batched processing or no.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:135
nvdsinferserver::UniqBackend
std::unique_ptr< BaseBackend > UniqBackend
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:217
nvdsinferserver::BaseBackend::setFirstDimBatch
void setFirstDimBatch(bool flag)
Set the flag indicating that it is a batch input.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:69
nvdsinferserver::LayerDescriptionList
std::vector< LayerDescription > LayerDescriptionList
Definition: sources/libs/nvdsinferserver/infer_ibackend.h:63
nvdsinferserver::BaseBackend::getInputLayers
LayersTuple getInputLayers() const final
Get the LayersTuple for input layers.
nvdsinferserver::BaseBackend::maxBatchSize
int32_t maxBatchSize() const final
Returns the maximum batch size set for the backend.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:130
infer_ibackend.h
Inference processing backend interface header file.
nvdsinferserver::InferTensorOrder::kNone
@ kNone
infer_utils.h
Header file containing utility functions and classes used by the nvinferserver low level library.
nvdsinferserver::BaseBackend::needKeepInputs
bool needKeepInputs() const
Check if the keep input flag is set.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:172
nvdsinferserver::BaseBackend
Base class of inference backend processing.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:45
nvdsinferserver::BaseBackend::allLayers
const LayerDescriptionList & allLayers() const
Returns the list of all descriptions of all layers, input and output.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:118
nvdsinferserver::BaseBackend::checkInputDims
bool checkInputDims(const InputShapes &shapes) const
Check that the list of input shapes have fixed dimensions and corresponding layers are marked as inpu...
nvdsinferserver::BaseBackend::getInputTensorOrder
InferTensorOrder getInputTensorOrder() const final
Returns the input tensor order.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:54
nvdsinferserver::BaseBackend::setInputTensorOrder
void setInputTensorOrder(InferTensorOrder order)
Set the tensor order for the input layers.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:167
nvdsinferserver::BaseBackend::setKeepInputs
void setKeepInputs(bool enable)
Set the flag indicating whether to keep inputs buffers.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:123
nvdsinferserver::BaseBackend::getInputLayerSize
uint32_t getInputLayerSize() const final
Returns the number of input layers for the model.
Definition: sources/libs/nvdsinferserver/infer_base_backend.h:88