-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTrtBuffer.cpp
180 lines (147 loc) · 5.8 KB
/
TrtBuffer.cpp
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
#include "TrtBuffer.h"
#include <iostream>
#include <fstream>
#include <string>
bool TrtBuffer::DeserializeEngine(const std::string &engineFile) {
std::ifstream in(engineFile.c_str(), std::ifstream::binary);
if (in.is_open()) {
spdlog::info("deserialize engine from {}", engineFile);
auto const start_pos = in.tellg();
in.ignore(std::numeric_limits<std::streamsize>::max());
size_t bufCount = in.gcount();
in.seekg(start_pos);
std::unique_ptr<char[]> engineBuf(new char[bufCount]);
in.read(engineBuf.get(), bufCount);
initLibNvInferPlugins(&mLogger, "");
nvinfer1::IRuntime *runtime = nvinfer1::createInferRuntime(mLogger);
_engine = runtime->deserializeCudaEngine((void *) engineBuf.get(), bufCount, nullptr);
assert(_engine != nullptr);
runtime->destroy();
return true;
}
return false;
}
void TrtBuffer::InitEngine(int profileIndex, nvinfer1::Dims maxDims) {
spdlog::info("init engine...");
context = _engine->createExecutionContext();
context->setOptimizationProfile(0);
assert(context != nullptr);
// 设置最大bindshape,已获取最大输出大小
context->setBindingDimensions(0, maxDims);
spdlog::info("create cuda stream");
CUDA_CHECK(cudaStreamCreate(&stream));
spdlog::info("malloc device memory");
int nbBindings = _engine->getNbBindings();
// std::cout << "nbBingdings: " << nbBindings << std::endl;
bindingDevice.resize(nbBindings);
bindingHost.resize(nbBindings);
bindingSize.resize(nbBindings);
bindingDims.resize(nbBindings);
bindingDataType.resize(nbBindings);
for (int i = 0; i < nbBindings; i++) {
nvinfer1::Dims dims = context->getBindingDimensions(i);
nvinfer1::DataType dtype = _engine->getBindingDataType(i);
const char *name = _engine->getBindingName(i);
int64_t totalSize = volume(dims) * getElementSize(dtype);
bindingDims[i] = dims;
bindingDataType[i] = dtype;
bindingSize[i] = totalSize;
if (_engine->bindingIsInput(i)) {
spdlog::info("input: ");
inputBindIndex.push_back(i);
} else {
spdlog::info("output: ");
outputBindIndex.push_back(i);
}
spdlog::info("bindingDevice bindIndex: {}, name: {}, size in byte: {}", i, name, totalSize);
spdlog::info("bindingDevice dims with {} dimemsion", dims.nbDims);
bindingDevice[i] = safeCudaMalloc(totalSize);
bindingHost[i] = safeCudaHostAlloc(totalSize);
}
spdlog::info("engine init finish");
}
void TrtBuffer::InitEngine() {
spdlog::info("init engine...");
context = _engine->createExecutionContext();
assert(context != nullptr);
spdlog::info("create cuda stream");
CUDA_CHECK(cudaStreamCreate(&stream));
spdlog::info("malloc device memory");
int nbBindings = _engine->getNbBindings();
bindingDevice.resize(nbBindings);
bindingHost.resize(nbBindings);
bindingSize.resize(nbBindings);
bindingDims.resize(nbBindings);
bindingDataType.resize(nbBindings);
for (int i = 0; i < nbBindings; i++) {
nvinfer1::Dims dims = _engine->getBindingDimensions(i);
nvinfer1::DataType dtype = _engine->getBindingDataType(i);
const char *name = _engine->getBindingName(i);
int64_t totalSize = volume(dims) * getElementSize(dtype);
bindingDims[i] = dims;
bindingDataType[i] = dtype;
bindingSize[i] = totalSize;
if (_engine->bindingIsInput(i)) {
spdlog::info("input: ");
inputBindIndex.push_back(i);
} else {
spdlog::info("output: ");
outputBindIndex.push_back(i);
}
spdlog::info("bindingDevice bindIndex: {}, name: {}, size in byte: {}", i, name, totalSize);
spdlog::info("bindingDevice dims with {} dimemsion", dims.nbDims);
bindingDevice[i] = safeCudaMalloc(totalSize);
bindingHost[i] = safeCudaHostAlloc(totalSize);
}
spdlog::info("engine init finish");
}
TrtBuffer::TrtBuffer(const std::string &engineFile) {
DeserializeEngine(engineFile);
InitEngine();
}
TrtBuffer::TrtBuffer(const std::string &engineFile, int profileIndex, nvinfer1::Dims maxDims) {
DeserializeEngine(engineFile);
InitEngine(profileIndex, maxDims);
}
TrtBuffer::~TrtBuffer() {
if (context != nullptr) {
context->destroy();
context = nullptr;
}
if (_engine != nullptr) {
_engine->destroy();
_engine = nullptr;
}
for (auto bind: bindingDevice) {
safeCudaFree(bind);
}
for (auto bind: bindingHost) {
safeCudaHostFree(bind);
}
}
void TrtBuffer::DataTransferAsync(int size, int bindIndex, bool isHostToDevice) {
auto host = bindingHost[bindIndex];
if (isHostToDevice) {
assert(size * getElementSize(bindingDataType[bindIndex]) <= unsigned(bindingSize[bindIndex]));
CUDA_CHECK(cudaMemcpyAsync(
bindingDevice[bindIndex], host, size * getElementSize(bindingDataType[bindIndex]), cudaMemcpyHostToDevice, stream));
} else {
CUDA_CHECK(cudaMemcpyAsync(
host, bindingDevice[bindIndex], size * getElementSize(bindingDataType[bindIndex]), cudaMemcpyDeviceToHost, stream));
}
}
void TrtBuffer::ForwardAsync(nvinfer1::Dims &dim) {
context->setBindingDimensions(0, dim);
context->enqueueV2(bindingDevice.data(), stream, nullptr);
}
void TrtBuffer::ForwardAsync() {
context->enqueueV2(bindingDevice.data(), stream, nullptr);
}
void TrtBuffer::GetOutput() {
for (unsigned int idx = 0; idx < outputBindIndex.size(); ++idx) {
int bindIndex = outputBindIndex[idx];
//auto hostOutput = bindingHost[bindIndex];
// 数据 GPU -> host
DataTransferAsync(GetRuntimeBindingSize(bindIndex), bindIndex, false);
}
}