-
Notifications
You must be signed in to change notification settings - Fork 2
/
nvdsinfer_backend.h
346 lines (290 loc) · 9.72 KB
/
nvdsinfer_backend.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/**
* Copyright (c) 2019-2020, NVIDIA CORPORATION. All rights reserved.
*
* NVIDIA Corporation and its licensors retain all intellectual property
* and proprietary rights in and to this software, related documentation
* and any modifications thereto. Any use, reproduction, disclosure or
* distribution of this software and related documentation without an express
* license agreement from NVIDIA Corporation is strictly prohibited.
*
*/
#ifndef __NVDSINFER_BACKEND_H__
#define __NVDSINFER_BACKEND_H__
#include <stdarg.h>
#include <condition_variable>
#include <memory>
#include <mutex>
#include <queue>
#include <cuda_runtime_api.h>
#include <NvCaffeParser.h>
#include <NvInfer.h>
#include <NvInferRuntime.h>
#include "nvdsinfer_func_utils.h"
/* This file provides backend inference interface for abstracting implementation
* details in various cases like inferencing on implicit batch dims/full dims
* network, inferencing on DLA etc. This file also provides helper classes for
* managing the lifecycle of CUDA resources like streams, buffers, events. */
namespace nvdsinfer {
/**
* Helper class for managing Cuda Streams.
*/
class CudaStream
{
public:
explicit CudaStream(uint flag = cudaStreamDefault, int priority = 0);
~CudaStream();
operator cudaStream_t() { return m_Stream; }
cudaStream_t& ptr() { return m_Stream; }
SIMPLE_MOVE_COPY(CudaStream)
private:
void move_copy(CudaStream&& o)
{
m_Stream = o.m_Stream;
o.m_Stream = nullptr;
}
DISABLE_CLASS_COPY(CudaStream);
cudaStream_t m_Stream = nullptr;
};
/**
* Helper class for managing Cuda events.
*/
class CudaEvent
{
public:
explicit CudaEvent(uint flag = cudaEventDefault);
~CudaEvent();
operator cudaEvent_t() { return m_Event; }
cudaEvent_t& ptr() { return m_Event; }
SIMPLE_MOVE_COPY(CudaEvent)
private:
void move_copy(CudaEvent&& o)
{
m_Event = o.m_Event;
o.m_Event = nullptr;
}
DISABLE_CLASS_COPY(CudaEvent);
cudaEvent_t m_Event = nullptr;
};
/**
* Helper base class for managing Cuda allocated buffers.
*/
class CudaBuffer
{
public:
virtual ~CudaBuffer() = default;
size_t bytes() const { return m_Size; }
template <typename T>
T* ptr()
{
return (T*)m_Buf;
}
void* ptr() { return m_Buf; }
SIMPLE_MOVE_COPY(CudaBuffer)
protected:
explicit CudaBuffer(size_t s) : m_Size(s) {}
void move_copy(CudaBuffer&& o)
{
m_Buf = o.m_Buf;
o.m_Buf = nullptr;
m_Size = o.m_Size;
o.m_Size = 0;
}
DISABLE_CLASS_COPY(CudaBuffer);
void* m_Buf = nullptr;
size_t m_Size = 0;
};
/**
* CUDA device buffers.
*/
class CudaDeviceBuffer : public CudaBuffer
{
public:
explicit CudaDeviceBuffer(size_t size);
~CudaDeviceBuffer();
};
/**
* CUDA host buffers.
*/
class CudaHostBuffer : public CudaBuffer
{
public:
explicit CudaHostBuffer(size_t size);
~CudaHostBuffer();
};
/**
* Abstract interface to manage a batched buffer for inference.
*/
class InferBatchBuffer
{
public:
InferBatchBuffer() = default;
virtual ~InferBatchBuffer() = default;
/* Get device buffer pointers for bound layers associated with this batch. */
virtual std::vector<void*>& getDeviceBuffers() = 0;
/* Get the data type of the buffer(layer) for a bound layer having index
* `bindingIndex`. */
virtual NvDsInferDataType getDataType(int bindingIndex = 0) const = 0;
/* Get the batch dimensions for the buffer allocated for a bound layer having
* index `bindingIndex. */
virtual NvDsInferBatchDims getBatchDims(int bindingIndex = 0) const = 0;
private:
DISABLE_CLASS_COPY(InferBatchBuffer);
};
/**
* Abstract interface for managing the actual inferencing implementation. This
* interface abstracts away the low-level implementation details required for
* inferencing with implicit batch dimensions network/full dimensions network on
* GPU and inferencing on DLA.
*
* Actual instance of a BackendContext can be created using `createBackendContext`
* function. This function will create the appropriate BackendContext
* (ImplicitTrtBackendContext/FullDimTrtBackendContext/DlaTrtBackendContext)
* based on the parameters used to build the network/engine.
*/
class BackendContext
{
public:
BackendContext() = default;
virtual ~BackendContext() = default;
/* Initialize the backend context. */
virtual NvDsInferStatus initialize() = 0;
/* Get the number of bound layers for the engine. */
virtual int getNumBoundLayers() = 0;
/* Get information for a bound layer with index `bindingIdx`. */
virtual const NvDsInferBatchDimsLayerInfo& getLayerInfo(int bindingIdx) = 0;
/* Get binding index for a bound layer with name `bindingName`. */
virtual int getLayerIdx(const std::string& bindingName) = 0;
/* Returns if the bound layer at index `bindingIdx` can support the
* provided batch dimensions. */
virtual bool canSupportBatchDims(
int bindingIdx, const NvDsInferBatchDims& batchDims) = 0;
/* Get the min/max/optimal batch dimensions for a bound layer. */
virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx) = 0;
virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx) = 0;
virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx) = 0;
/* Enqueue a batched buffer for inference. */
virtual NvDsInferStatus enqueueBuffer(
const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
CudaEvent* consumeEvent) = 0;
private:
DISABLE_CLASS_COPY(BackendContext);
};
class TrtEngine;
/**
* Base class for implementations of the BackendContext interface. Implements
* functionality common to all backends.
*/
class TrtBackendContext : public BackendContext
{
public:
~TrtBackendContext();
protected:
TrtBackendContext(UniquePtrWDestroy<nvinfer1::IExecutionContext>&& ctx,
std::shared_ptr<TrtEngine> engine);
int getLayerIdx(const std::string& bindingName) override;
int getNumBoundLayers() override;
const NvDsInferBatchDimsLayerInfo& getLayerInfo(int bindingIdx) override
{
assert(bindingIdx < (int)m_AllLayers.size());
return m_AllLayers[bindingIdx];
}
bool canSupportBatchDims(
int bindingIdx, const NvDsInferBatchDims& batchDims) override;
virtual NvDsInferBatchDims getMaxBatchDims(int bindingIdx) override
{
assert(bindingIdx < (int)m_AllLayers.size());
return m_AllLayers[bindingIdx].profileDims[kSELECTOR_MAX];
}
virtual NvDsInferBatchDims getMinBatchDims(int bindingIdx) override
{
assert(bindingIdx < (int)m_AllLayers.size());
return m_AllLayers[bindingIdx].profileDims[kSELECTOR_MIN];
}
virtual NvDsInferBatchDims getOptBatchDims(int bindingIdx) override
{
assert(bindingIdx < (int)m_AllLayers.size());
return m_AllLayers[bindingIdx].profileDims[kSELECTOR_OPT];
}
protected:
UniquePtrWDestroy<nvinfer1::IExecutionContext> m_Context;
std::shared_ptr<TrtEngine> m_CudaEngine;
std::vector<NvDsInferBatchDimsLayerInfo> m_AllLayers;
int m_GpuId = -1;
static std::mutex sDLAExecutionMutex;
};
/**
* Backend context for implicit batch dimension network.
*/
class ImplicitTrtBackendContext : public TrtBackendContext
{
public:
ImplicitTrtBackendContext(
UniquePtrWDestroy<nvinfer1::IExecutionContext>&& ctx,
std::shared_ptr<TrtEngine> engine);
private:
NvDsInferStatus initialize() override;
bool canSupportBatchDims(
int bindingIdx, const NvDsInferBatchDims& batchDims) override;
NvDsInferStatus enqueueBuffer(
const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
CudaEvent* consumeEvent) override;
protected:
int m_MaxBatchSize = 0;
};
/**
* Backend context for full dimensions network.
*/
class FullDimTrtBackendContext : public TrtBackendContext
{
public:
FullDimTrtBackendContext(
UniquePtrWDestroy<nvinfer1::IExecutionContext>&& ctx,
std::shared_ptr<TrtEngine> engine, int profile = 0);
private:
NvDsInferStatus initialize() override;
NvDsInferStatus enqueueBuffer(
const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
CudaEvent* consumeEvent) override;
protected:
// Only idx 0 profile supported.
const int m_ProfileIndex = 0;
};
/**
* Backend context for implicit batch dimension network inferencing on DLA.
*/
class DlaImplicitTrtBackendContext : public ImplicitTrtBackendContext
{
public:
DlaImplicitTrtBackendContext(UniquePtrWDestroy<nvinfer1::IExecutionContext>&& ctx,
std::shared_ptr<TrtEngine> engine);
NvDsInferStatus enqueueBuffer(
const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
CudaEvent* consumeEvent) override;
};
/**
* Backend context for implicit batch dimension network inferencing on DLA.
*/
class DlaFullDimTrtBackendContext : public FullDimTrtBackendContext
{
public:
DlaFullDimTrtBackendContext(UniquePtrWDestroy<nvinfer1::IExecutionContext>&& ctx,
std::shared_ptr<TrtEngine> engine, int profile = 0);
NvDsInferStatus enqueueBuffer(
const std::shared_ptr<InferBatchBuffer>& buffer, CudaStream& stream,
CudaEvent* consumeEvent) override;
private:
static std::mutex sExecutionMutex;
};
/**
* Create an instance of a BackendContext.
*
* ImplicitTrtBackendContext - created when TRT CudaEngine/network is built with
* implicit batch dimensions
* FullDimTrtBackendContext - created when TRT CudaEngine/network is built with
* full dimensions support
* DlaTrtBackendContext - created when TRT CudaEngine is built for DLA
*/
std::unique_ptr<TrtBackendContext> createBackendContext(
const std::shared_ptr<TrtEngine>& engine);
} // end of namespace nvdsinfer
#endif