forked from wang-xinyu/tensorrtx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlp.cpp
323 lines (259 loc) · 10.1 KB
/
mlp.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
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
#include "NvInfer.h" // TensorRT library
#include "iostream" // Standard input/output library
#include "logging.h" // logging file -- by NVIDIA
#include <map> // for weight maps
#include <fstream> // for file-handling
#include <chrono> // for timing the execution
// provided by nvidia for using TensorRT APIs
using namespace nvinfer1;
// Logger from TRT API
static Logger gLogger;
const int INPUT_SIZE = 1;
const int OUTPUT_SIZE = 1;
/** ////////////////////////////
// DEPLOYMENT RELATED /////////
////////////////////////////*/
std::map<std::string, Weights> loadWeights(const std::string file) {
/**
* Parse the .wts file and store weights in dict format.
*
* @param file path to .wts file
* @return weight_map: dictionary containing weights and their values
*/
std::cout << "[INFO]: Loading weights..." << file << std::endl;
std::map<std::string, Weights> weightMap;
// Open Weight file
std::ifstream input(file);
assert(input.is_open() && "[ERROR]: Unable to load weight file...");
// Read number of weights
int32_t count;
input >> count;
assert(count > 0 && "Invalid weight map file.");
// Loop through number of line, actually the number of weights & biases
while (count--) {
// TensorRT weights
Weights wt{DataType::kFLOAT, nullptr, 0};
uint32_t size;
// Read name and type of weights
std::string w_name;
input >> w_name >> std::dec >> size;
wt.type = DataType::kFLOAT;
uint32_t *val = reinterpret_cast<uint32_t *>(malloc(sizeof(val) * size));
for (uint32_t x = 0, y = size; x < y; ++x) {
// Change hex values to uint32 (for higher values)
input >> std::hex >> val[x];
}
wt.values = val;
wt.count = size;
// Add weight values against its name (key)
weightMap[w_name] = wt;
}
return weightMap;
}
ICudaEngine *createMLPEngine(unsigned int maxBatchSize, IBuilder *builder, IBuilderConfig *config, DataType dt) {
/**
* Create Multi-Layer Perceptron using the TRT Builder and Configurations
*
* @param maxBatchSize: batch size for built TRT model
* @param builder: to build engine and networks
* @param config: configuration related to Hardware
* @param dt: datatype for model layers
* @return engine: TRT model
*/
std::cout << "[INFO]: Creating MLP using TensorRT..." << std::endl;
// Load Weights from relevant file
std::map<std::string, Weights> weightMap = loadWeights("../mlp.wts");
// Create an empty network
INetworkDefinition *network = builder->createNetworkV2(0U);
// Create an input with proper *name
ITensor *data = network->addInput("data", DataType::kFLOAT, Dims3{1, 1, 1});
assert(data);
// Add layer for MLP
IFullyConnectedLayer *fc1 = network->addFullyConnected(*data, 1,
weightMap["linear.weight"],
weightMap["linear.bias"]);
assert(fc1);
// set output with *name
fc1->getOutput(0)->setName("out");
// mark the output
network->markOutput(*fc1->getOutput(0));
// Set configurations
builder->setMaxBatchSize(1);
// Set workspace size
config->setMaxWorkspaceSize(1 << 20);
// Build CUDA Engine using network and configurations
ICudaEngine *engine = builder->buildEngineWithConfig(*network, *config);
assert(engine != nullptr);
// Don't need the network any more
// free captured memory
network->destroy();
// Release host memory
for (auto &mem: weightMap) {
free((void *) (mem.second.values));
}
return engine;
}
void APIToModel(unsigned int maxBatchSize, IHostMemory **modelStream) {
/**
* Create engine using TensorRT APIs
*
* @param maxBatchSize: for the deployed model configs
* @param modelStream: shared memory to store serialized model
*/
// Create builder with the help of logger
IBuilder *builder = createInferBuilder(gLogger);
// Create hardware configs
IBuilderConfig *config = builder->createBuilderConfig();
// Build an engine
ICudaEngine *engine = createMLPEngine(maxBatchSize, builder, config, DataType::kFLOAT);
assert(engine != nullptr);
// serialize the engine into binary stream
(*modelStream) = engine->serialize();
// free up the memory
engine->destroy();
builder->destroy();
}
void performSerialization() {
/**
* Serialization Function
*/
// Shared memory object
IHostMemory *modelStream{nullptr};
// Write model into stream
APIToModel(1, &modelStream);
assert(modelStream != nullptr);
std::cout << "[INFO]: Writing engine into binary..." << std::endl;
// Open the file and write the contents there in binary format
std::ofstream p("../mlp.engine", std::ios::binary);
if (!p) {
std::cerr << "could not open plan output file" << std::endl;
return;
}
p.write(reinterpret_cast<const char *>(modelStream->data()), modelStream->size());
// Release the memory
modelStream->destroy();
std::cout << "[INFO]: Successfully created TensorRT engine..." << std::endl;
std::cout << "\n\tRun inference using `./mlp -d`" << std::endl;
}
/** ////////////////////////////
// INFERENCE RELATED //////////
////////////////////////////*/
void doInference(IExecutionContext &context, float *input, float *output, int batchSize) {
/**
* Perform inference using the CUDA context
*
* @param context: context created by engine
* @param input: input from the host
* @param output: output to save on host
* @param batchSize: batch size for TRT model
*/
// Get engine from the context
const ICudaEngine &engine = context.getEngine();
// Pointers to input and output device buffers to pass to engine.
// Engine requires exactly IEngine::getNbBindings() number of buffers.
assert(engine.getNbBindings() == 2);
void *buffers[2];
// In order to bind the buffers, we need to know the names of the input and output tensors.
// Note that indices are guaranteed to be less than IEngine::getNbBindings()
const int inputIndex = engine.getBindingIndex("data");
const int outputIndex = engine.getBindingIndex("out");
// Create GPU buffers on device -- allocate memory for input and output
cudaMalloc(&buffers[inputIndex], batchSize * INPUT_SIZE * sizeof(float));
cudaMalloc(&buffers[outputIndex], batchSize * OUTPUT_SIZE * sizeof(float));
// create CUDA stream for simultaneous CUDA operations
cudaStream_t stream;
cudaStreamCreate(&stream);
// copy input from host (CPU) to device (GPU) in stream
cudaMemcpyAsync(buffers[inputIndex], input, batchSize * INPUT_SIZE * sizeof(float), cudaMemcpyHostToDevice, stream);
// execute inference using context provided by engine
context.enqueue(batchSize, buffers, stream, nullptr);
// copy output back from device (GPU) to host (CPU)
cudaMemcpyAsync(output, buffers[outputIndex], batchSize * OUTPUT_SIZE * sizeof(float), cudaMemcpyDeviceToHost,
stream);
// synchronize the stream to prevent issues
// (block CUDA and wait for CUDA operations to be completed)
cudaStreamSynchronize(stream);
// Release stream and buffers (memory)
cudaStreamDestroy(stream);
cudaFree(buffers[inputIndex]);
cudaFree(buffers[outputIndex]);
}
void performInference() {
/**
* Get inference using the pre-trained model
*/
// stream to write model
char *trtModelStream{nullptr};
size_t size{0};
// read model from the engine file
std::ifstream file("../mlp.engine", std::ios::binary);
if (file.good()) {
file.seekg(0, file.end);
size = file.tellg();
file.seekg(0, file.beg);
trtModelStream = new char[size];
assert(trtModelStream);
file.read(trtModelStream, size);
file.close();
}
// create a runtime (required for deserialization of model) with NVIDIA's logger
IRuntime *runtime = createInferRuntime(gLogger);
assert(runtime != nullptr);
// deserialize engine for using the char-stream
ICudaEngine *engine = runtime->deserializeCudaEngine(trtModelStream, size, nullptr);
assert(engine != nullptr);
// create execution context -- required for inference executions
IExecutionContext *context = engine->createExecutionContext();
assert(context != nullptr);
float out[1]; // array for output
float data[1]; // array for input
for (float &i: data)
i = 12.0; // put any value for input
// time the execution
auto start = std::chrono::system_clock::now();
// do inference using the parameters
doInference(*context, data, out, 1);
// time the execution
auto end = std::chrono::system_clock::now();
std::cout << "\n[INFO]: Time taken by execution: "
<< std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << "ms" << std::endl;
// free the captured space
context->destroy();
engine->destroy();
runtime->destroy();
std::cout << "\nInput:\t" << data[0];
std::cout << "\nOutput:\t";
for (float i: out) {
std::cout << i;
}
std::cout << std::endl;
}
int checkArgs(int argc, char **argv) {
/**
* Parse command line arguments
*
* @param argc: argument count
* @param argv: arguments vector
* @return int: a flag to perform operation
*/
if (argc != 2) {
std::cerr << "[ERROR]: Arguments not right!" << std::endl;
std::cerr << "./mlp -s // serialize model to plan file" << std::endl;
std::cerr << "./mlp -d // deserialize plan file and run inference" << std::endl;
return -1;
}
if (std::string(argv[1]) == "-s") {
return 1;
} else if (std::string(argv[1]) == "-d") {
return 2;
}
return -1;
}
int main(int argc, char **argv) {
int args = checkArgs(argc, argv);
if (args == 1)
performSerialization();
else if (args == 2)
performInference();
return 0;
}