forked from dongfangduoshou123/YoloV3-TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrtNetWork.h
320 lines (257 loc) · 7.5 KB
/
trtNetWork.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
#pragma once
#include "NvCaffeParser.h"
#include "NvOnnxParser.h"
#include "NvUffParser.h"
#include "NvInfer.h"
#include "NvOnnxParser.h"
#include "logger.h"
#include <string>
#include <unistd.h>
#include <assert.h>
#include <iostream>
#include <thread>
#include <algorithm>
#include <cuda_runtime.h>
#include <unordered_map>
class CudaEvent;
inline void cudaCheck(cudaError_t ret, std::ostream& err = std::cerr){
if(ret != cudaSuccess){
err << "Cuda failure:" << cudaGetErrorString(ret) << std::endl;
abort();
}
}
namespace
{
void cudaSleep(void* sleep)
{
std::this_thread::sleep_for(std::chrono::duration<int, std::milli>(*static_cast<int*>(sleep)));
}
}
class CudaStream{
public:
CudaStream(){cudaCheck(cudaStreamCreate(&stream));}
CudaStream(const CudaStream&) = delete;
CudaStream(CudaStream&&) = delete;
CudaStream& operator =(const CudaStream&) = delete;
CudaStream& operator =(CudaStream&&) = delete;
~CudaStream() {cudaCheck(cudaStreamDestroy(stream));}
cudaStream_t get() const{
return stream;
}
void wait(CudaEvent& event);
void sleep(int* ms){
cudaCheck(cudaLaunchHostFunc(stream, cudaSleep, ms));
}
private:
cudaStream_t stream{};
};
class CudaEvent{
public:
CudaEvent(unsigned int flags){
cudaCheck(cudaEventCreateWithFlags(&event,flags));
}
CudaEvent(const CudaEvent&) = delete;
CudaEvent(CudaEvent&&) = delete;
CudaEvent& operator=(CudaEvent&&) = delete;
CudaEvent& operator=(const CudaEvent&) = delete;
~CudaEvent(){
cudaCheck(cudaEventDestroy(event));
}
cudaEvent_t get() const{
return event;
}
void record(const CudaStream& stream) {
cudaCheck(cudaEventRecord(event, stream.get()));
}
void synchronize(){
cudaCheck(cudaEventSynchronize(event));
}
void reset(unsigned int flags = cudaEventDefault){
cudaCheck(cudaEventDestroy(event));
cudaCheck(cudaEventCreateWithFlags(&event, flags));
}
float operator-(const CudaEvent& ohs){
float time(0);
cudaCheck(cudaEventElapsedTime(&time, ohs.get(), get()));
return time;
}
private:
cudaEvent_t event{};
};
inline void CudaStream::wait(CudaEvent &event){
cudaCheck(cudaStreamWaitEvent(stream, event.get(),0));
}
template <typename Allocator, typename Deletor>
class Buffer{
public:
Buffer() = default;
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
Buffer(Buffer&& otr){
reset(otr.get());
otr.ptr = nullptr;
}
Buffer& operator=(Buffer&& oth){
reset(oth.get());
oth.ptr = nullptr;
}
Buffer(size_t size){
Allocator()(&ptr, size);
siz = size;
}
~Buffer() {
reset();
}
void allocate(size_t size){
reset();
Allocator()(&ptr, size);
siz = size;
}
void reset(void* p=nullptr){
if(ptr != nullptr){
Deletor()(ptr);
}
ptr = p;
}
void* get(){
return ptr;
}
size_t size(){
return siz;
}
private:
void* ptr{nullptr};
size_t siz;
};
struct DeviceAllocator
{
void operator()(void** ptr, size_t size) { cudaCheck(cudaMalloc(ptr, size)); }
};
struct DeviceDeallocator
{
void operator()(void* ptr) { cudaCheck(cudaFree(ptr)); }
};
struct HostAllocator
{
void operator()(void** ptr, size_t size) { cudaCheck(cudaMallocHost(ptr, size)); }
};
struct HostDeallocator
{
void operator()(void* ptr) { cudaCheck(cudaFreeHost(ptr)); }
};
using DeviceBuffer = Buffer<DeviceAllocator, DeviceDeallocator>;
using HostBuffer = Buffer<HostAllocator, HostDeallocator>;
struct TrtBinds{
std::vector<DeviceBuffer> deviceBufferBinds;
std::vector<HostBuffer> hostBufferBinds;
// std::unordered_map<std::string, int> tensor2Index;
// std::unordered_map<int, std::string> index2Tensor;
};
template<typename T>
struct trtDeletor{
void operator()(T* p){p->destroy();}
};
template<typename T> using trtUniquePtr=std::unique_ptr<T, trtDeletor<T> >;
struct Parser
{
trtUniquePtr<nvcaffeparser1::ICaffeParser> caffeParser;
trtUniquePtr<nvuffparser::IUffParser> uffParser;
trtUniquePtr<nvonnxparser::IParser> onnxParser;
};
namespace trt{
enum class ModelFormat
{
kCAFFE,
kONNX,
kUFF
};
struct UffInput
{
std::vector<std::pair<std::string, nvinfer1::Dims>> inputs;
bool NHWC{false};
};
using ShapeMinOptMax = std::array<nvinfer1::Dims, 3>;
struct ModelInfo{
ModelFormat format;
//for onnx
std::string onnxmodelfile;
//for caffe
std::string caffeprotoxt;
std::string caffemodel;
//for uff
std::string uffmodel;
UffInput uffInputs;
//caffe model and uffmodel must implement, onnx model does not need
std::vector<std::string> outputs;
//optional
std::unordered_map<std::string, ShapeMinOptMax> shapes;
//general the number of inputsformat and outputsformat must be equal to the network's inputs number and outputs number.
std::vector<std::pair<nvinfer1::DataType, nvinfer1::TensorFormat> > inputsformat;
std::vector<std::pair<nvinfer1::DataType, nvinfer1::TensorFormat> > outputsformat;
};
struct BuildInfo{
BuildInfo() {}
long maxWorkspaceSize;
int contextNum;
nvinfer1::BuilderFlag flag;
std::shared_ptr<nvinfer1::IInt8EntropyCalibrator2> Int8Calibrator{nullptr};
bool safe;
int dlacore;
int maxBatch;
BuildInfo(const BuildInfo& l) {
maxWorkspaceSize = l.maxWorkspaceSize;
contextNum = l.contextNum;
flag = l.flag;
Int8Calibrator = l.Int8Calibrator;
safe = l.safe;
dlacore = l.dlacore;
maxBatch = l.maxBatch;
}
};
inline int volume(const nvinfer1::Dims& d)
{
return std::accumulate(d.d, d.d + d.nbDims, 1, std::multiplies<int>());
}
class trtNetWork{
public:
trtNetWork(std::string engineFilePath, ModelInfo minfo,BuildInfo binfo)
:engineFilePath(engineFilePath), modelInfo(minfo),safe(binfo.safe),DLACore(binfo.dlacore),buildinfo(binfo)
{
builder.reset(nvinfer1::createInferBuilder(gLogger.getTRTLogger()));
network.reset(builder->createNetworkV2(1U));
}
virtual ~trtNetWork() {
Destroy();
}
//inference output is not returned and the output can get from the output memory bindings.
virtual void Inference(void*data,int contextIndex=0, int batch=-1);
virtual bool CreateNetwork();
virtual bool CreateEngineAndSerialize(long maxWorkspaceSize,nvinfer1::BuilderFlag flag, int maxBatch);
virtual bool EngineDeserialize();
virtual void Destroy();
void GetEngine(){
if(access(engineFilePath.c_str(),0) == 0){
EngineDeserialize();
}else{
CreateNetwork();
CreateEngineAndSerialize(buildinfo.maxWorkspaceSize, buildinfo.flag, buildinfo.maxBatch);
}
}
protected:
std::string engineFilePath;
trtUniquePtr<nvinfer1::IBuilder> builder{nullptr};
trtUniquePtr<nvinfer1::ICudaEngine> engine{nullptr};
trtUniquePtr<nvinfer1::INetworkDefinition> network{nullptr};
trtUniquePtr<nvinfer1::IRuntime> runtime{nullptr};
std::vector<trtUniquePtr<nvinfer1::IExecutionContext>> exeContexts;
std::vector<TrtBinds> binds;
//store the engine's input output pointers, these pointers are not mangaged by TrtBinds
std::vector<std::vector<void*> >m_DeviceBuffers;
std::vector<CudaStream*> streams;
Parser parser;
ModelInfo modelInfo;
bool safe{false};
int DLACore{-1};
BuildInfo buildinfo;
};
}//namespace trt