-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrt_helper.py
673 lines (506 loc) · 24.9 KB
/
trt_helper.py
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# import torch
import tensorrt as trt
import numpy as np
import ctypes
import math
import time
from typing import Optional, Tuple
import pycuda.driver as cuda
import pycuda.autoinit
class TrtNetworkHelper():
"""TensorRT Network Definition helper for Pytorch"""
def __init__(self, network, plugin_registry, logger):
self.network = network
self.plugin_registry = plugin_registry
self.logger = logger
self.input_num = 0
def set_layer_name(self, layer, name):
"""
Tool function. Set the name of trt layer or plugin and print output shapes.
"""
if not layer:
raise RuntimeError("Could not name")
layer.name = str(self.network.num_layers) + "_" + name
for i in range(0, layer.num_outputs):
shape = layer.get_output(i).shape
self.logger.log(trt.Logger.INFO, "[Network] " + layer.name + ", output[" + str(i) + "] shape= " + str(shape))
return None
def check_trt_layer(self, trt_layer):
"""
Tool function. check trt layer,
"""
if not trt_layer:
raise RuntimeError("add " + str(trt_layer) + " failed!")
for i in range(0, trt_layer.num_outputs):
shape = trt_layer.get_output(i).shape
# print(trt.volume(shape))
# if len(shape) is 1:
# raise RuntimeError("add " + layer.name + " failed!")
def layer_post_process(self, trt_layer, layer_name, precision):
"""
Tool function. set precision, set_layer_name and check_trt_layer
"""
if precision is not None:
trt_layer.precision = precision
self.set_layer_name(trt_layer, layer_name)
self.check_trt_layer(trt_layer)
def addInput(self, name, dtype, shape):
if name is None:
name = "input" + str(self.input_num)
self.input_num = self.input_num + 1
trt_input = self.network.add_input(name=name, dtype=dtype, shape=shape)
if not trt_input:
raise RuntimeError("addInput failed!")
self.logger.log(trt.Logger.INFO, "[Network] add input:" + name + ", shape=" + str(shape))
return trt_input
def markOutput(self, x: trt.ITensor):
self.network.mark_output(x)
self.logger.log(trt.Logger.INFO, "[Network] mark output:" + x.name + ", shape=" + str(x.shape))
def addEmbedding(self, indices, weight, layer_name=None, precision=None):
constant_layer = self.network.add_constant(weight.shape, trt.Weights(weight))
gather_layer = self.network.add_gather(constant_layer.get_output(0),
indices, axis=0)
if layer_name is None:
layer_name = "nn.Embedding"
else:
layer_name = "nn.Embedding." + layer_name
self.layer_post_process(gather_layer, layer_name, precision)
return gather_layer.get_output(0)
def addGELU(self, x, layer_name=None, precision=None):
POW = self.network.add_constant((1, 1, 1), trt.Weights(np.ascontiguousarray([3.0], dtype=np.float32)))
MULTIPLY = self.network.add_constant((1, 1, 1), trt.Weights(np.ascontiguousarray([0.044715], dtype=np.float32)))
SQRT = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([0.79788456080286535587989211986876], dtype=np.float32))))
ONE = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([1.0], dtype=np.float32))))
HALF = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([0.5], dtype=np.float32))))
X_pow = self.network.add_elementwise(x, POW.get_output(0), trt.ElementWiseOperation.POW)
X_pow_t = X_pow.get_output(0)
X_mul = self.network.add_elementwise(X_pow_t, MULTIPLY.get_output(0), trt.ElementWiseOperation.PROD)
X_add = self.network.add_elementwise(x, X_mul.get_output(0), trt.ElementWiseOperation.SUM)
X_sqrt = self.network.add_elementwise(X_add.get_output(0), SQRT.get_output(0), trt.ElementWiseOperation.PROD)
X_sqrt_tensor = X_sqrt.get_output(0)
X_tanh = self.network.add_activation(X_sqrt_tensor, trt.ActivationType.TANH)
X_tanh_tensor = X_tanh.get_output(0)
X_one = self.network.add_elementwise(X_tanh_tensor, ONE.get_output(0), trt.ElementWiseOperation.SUM)
CDF = self.network.add_elementwise(X_one.get_output(0), HALF.get_output(0), trt.ElementWiseOperation.PROD)
gelu_layer = self.network.add_elementwise(CDF.get_output(0), x, trt.ElementWiseOperation.PROD)
if layer_name is None:
layer_name = "nn.GELU"
else:
layer_name = "nn.GELU." + layer_name
self.layer_post_process(gelu_layer, layer_name, precision)
return gelu_layer.get_output(0)
# 自己定义的,应该没问题
def addLayerNorm(self, x, gamma, beta, layer_name=None, precision=None):
# create your layer norm plugin
# ln_plg_creator = self.plugin_registry.get_plugin_creator("LayerNorm", "1", "")
# gamma_field = trt.PluginField("gamma_field", gamma, trt.PluginFieldType.FLOAT32)
# beta_field = trt.PluginField("beta_field", beta, trt.PluginFieldType.FLOAT32)
# field_collection = trt.PluginFieldCollection([gamma_field, beta_field])
# plugin = ln_plg_creator.create_plugin("LayerNorm", field_collection)
# trt_layer = self.network.add_plugin_v2([x], plugin)
# if layer_name is None:
# layer_name = "Custom.LayerNorm"
# self.layer_post_process(trt_layer, layer_name, precision)
# x = trt_layer.get_output(0)
pfc = trt.PluginFieldCollection([])
LN_creator = self.plugin_registry.get_plugin_creator("LayerNorm", "1", "")
LN_plug = LN_creator.create_plugin("LayerNorm", pfc)
GAMMA = self.network.add_constant(gamma.shape, trt.Weights(np.ascontiguousarray(gamma, dtype=np.float32))).get_output(0)
BETA = self.network.add_constant(beta.shape, trt.Weights(np.ascontiguousarray(beta, dtype=np.float32))).get_output(0)
# scale_layer = self.network.add_elementwise(LN.get_output(0), GAMMA.get_output(0), trt.ElementWiseOperation.PROD)
# trt_layer = self.network.add_elementwise(scale_layer.get_output(0), BETA.get_output(0), trt.ElementWiseOperation.SUM)
trt_layer = self.network.add_plugin_v2([x, GAMMA, BETA], LN_plug)
if layer_name is None:
layer_name = "LayerNorm"
else:
layer_name = "LayerNorm." + layer_name
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
# 自己定义的,没问题
def addLinear(self, x, weight, bias, layer_name=None, precision=None):
# add Linear
W = self.network.add_constant((1, weight.shape[0], weight.shape[1]), trt.Weights(np.ascontiguousarray(weight, dtype=np.float32)))
B = self.network.add_constant((1, 1, bias.shape[0]), trt.Weights(np.ascontiguousarray(bias, dtype=np.float32)))
matmul_layer = self.network.add_matrix_multiply(x, trt.MatrixOperation.NONE, W.get_output(0), trt.MatrixOperation.NONE)
trt_layer = self.network.add_elementwise(matmul_layer.get_output(0), B.get_output(0), trt.ElementWiseOperation.SUM)
x = trt_layer.get_output(0)
return x
def addReLU(self, layer, x, layer_name=None, precision=None):
trt_layer = self.network.add_activation(x, type=trt.ActivationType.RELU)
if layer_name is None:
layer_name = "nn.ReLU"
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
# 自己定义的,没问题
def addSoftmax(self, x: trt.ITensor, dim: int = -1, layer_name=None, precision=None) -> trt.ITensor:
# add softmax
trt_layer = self.network.add_softmax(x)
if dim == -1:
trt_layer.axes = 1 << 3
if layer_name is None:
layer_name = "Custom.Softmax"
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
################## unary op ###################
def addLog(self, x: trt.ITensor, layer_name=None, precision=None):
trt_layer = self.network.add_unary(x, trt.UnaryOperation.LOG)
if layer_name is None:
layer_name = "unary.log"
else:
layer_name = "unary.log." + layer_name
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
################## elementwise op ###################
# 自己定义的,没问题
def addAdd(self, a, b, layer_name=None, precision=None):
# add Add
trt_layer = self.network.add_elementwise(a, b, trt.ElementWiseOperation.SUM)
if layer_name is None:
layer_name = "Custom.Add"
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
# 自己定义的,没问题
# tensor and scalar op
def addScale(
self,
x: trt.ITensor,
scale: float,
layer_name: str = None,
precision: trt.DataType = None
) -> trt.ITensor:
"""scale"""
# add scale
scale = trt.Weights(np.ascontiguousarray([scale], dtype=np.float32))
trt_layer = self.network.add_scale(x, trt.ScaleMode.UNIFORM, None, scale, None)
if layer_name is None:
layer_name = "Custom.Scale"
self.layer_post_process(trt_layer, layer_name, precision)
x = trt_layer.get_output(0)
return x
# 自己定义的,没问题
def addMatMul(self, a: trt.ITensor, b: trt.ITensor, layer_name: Optional[str] = None) -> trt.ITensor:
# add MatMul
trt_layer = self.network.add_matrix_multiply(a, trt.MatrixOperation.NONE, b, trt.MatrixOperation.NONE)
if layer_name is None:
layer_name = "Custom.MatMul"
self.layer_post_process(trt_layer, layer_name, precision=None)
x = trt_layer.get_output(0)
return x
def addConstant(self, w, layer_name: Optional[str] = None) -> trt.ITensor:
trt_layer = self.network.add_constant(w.shape, trt.Weights(np.ascontiguousarray(w)))
if layer_name is None:
layer_name = "trt.Constant"
else:
layer_name = "trt.Constant." + layer_name
self.layer_post_process(trt_layer, layer_name, None)
x = trt_layer.get_output(0)
return x
def addShuffle(
self,
x: trt.ITensor,
first_transpose: trt.Permutation,
reshape_dims: trt.Dims,
second_transpose: trt.Permutation,
layer_name: Optional[str] = None
) -> trt.ITensor:
""""""
trt_layer = self.network.add_shuffle(x)
if first_transpose is not None:
trt_layer.first_transpose = first_transpose
if reshape_dims is not None:
trt_layer.reshape_dims = reshape_dims
if second_transpose is not None:
trt_layer.second_transpose = second_transpose
if layer_name is None:
layer_name = "trt.Shuffle"
else:
layer_name = "trt.Shuffle." + layer_name
self.layer_post_process(trt_layer, layer_name, None)
x = trt_layer.get_output(0)
return x
class InferHelper():
""""""
def __init__(self, plan_name, trt_logger):
""""""
self.logger = trt_logger
self.runtime = trt.Runtime(trt_logger)
with open(plan_name, 'rb') as f:
self.engine = self.runtime.deserialize_cuda_engine(f.read())
self.context = self.engine.create_execution_context()
self.context.active_optimization_profile = 0
def infer(self, inputs: list):
nInput = len(inputs)
bufferD = []
# alloc memory
for i in range(nInput):
bufferD.append(cuda.mem_alloc(inputs[i].nbytes))
cuda.memcpy_htod(bufferD[i], inputs[i].ravel())
self.context.set_binding_shape(i, tuple(inputs[i].shape))
# print(inputs[i].nbytes)
# for i in range(0, self.engine.num_bindings):
# print("get_binding_shape:" + str(self.context.get_binding_shape(i)))
outputs = []
for i in range(len(inputs), self.engine.num_bindings):
outputs.append(np.zeros(self.context.get_binding_shape(i)).astype(np.float32))
nOutput = len(outputs)
for i in range(nOutput):
bufferD.append(cuda.mem_alloc(outputs[i].nbytes))
# print(outputs[i].nbytes)
for i in range(len(inputs), self.engine.num_bindings):
trt_output_shape = self.context.get_binding_shape(i)
output_idx = i - len(inputs)
if not (list(trt_output_shape) == list(outputs[output_idx].shape)):
self.logger.log(trt.Logger.ERROR, "[Infer] output shape is error!")
self.logger.log(trt.Logger.ERROR, "trt_output.shape = " + str(trt_output_shape))
self.logger.log(trt.Logger.ERROR, "base_output.shape = " + str(outputs[output_idx].shape))
assert(0)
# warm up
self.context.execute_v2(bufferD)
T1 = time.perf_counter()
self.context.execute_v2(bufferD)
T2 =time.perf_counter()
print("time=" + str((T2-T1) * 1000) + "ms")
for i in range(nInput, nInput + nOutput):
cuda.memcpy_dtoh(outputs[i - nInput].ravel(), bufferD[i])
for i in range(0, len(outputs)):
print("outputs.shape:" + str(outputs[i].shape))
print("outputs.sum:" + str(outputs[i].sum()))
# print(outputs[i])
# print("trt_output.shape:" + str(trt_output.shape))
# print("trt_output.sum:" + str(trt_output.sum()))
# print(trt_output.view(-1)[0:10])
# print("torch.allclose result:" + str(torch.allclose(base_output, trt_output, 1e-05, 1e-03)))
# print("====================")
return outputs
# return torch.allclose(base_output, trt_output, 1e-05, 1e-03)
# # import torch
# import tensorrt as trt
# import numpy as np
# import ctypes
# import math
# import time
# from typing import Optional, Tuple
# import pycuda.driver as cuda
# import pycuda.autoinit
# class TrtNetworkHelper():
# """TensorRT Network Definition helper for Pytorch"""
# def __init__(self, network, plugin_registry, logger):
# self.network = network
# self.plugin_registry = plugin_registry
# self.logger = logger
# self.input_num = 0
# def set_layer_name(self, layer, name):
# """
# Tool function. Set the name of trt layer or plugin and print output shapes.
# """
# if not layer:
# raise RuntimeError("Could not name")
# layer.name = str(self.network.num_layers) + "_" + name
# for i in range(0, layer.num_outputs):
# shape = layer.get_output(i).shape
# self.logger.log(trt.Logger.INFO, "[Network] " + layer.name + ", output[" + str(i) + "] shape= " + str(shape))
# return None
# def check_trt_layer(self, trt_layer):
# """
# Tool function. check trt layer,
# """
# if not trt_layer:
# raise RuntimeError("add " + str(trt_layer) + " failed!")
# for i in range(0, trt_layer.num_outputs):
# shape = trt_layer.get_output(i).shape
# # print(trt.volume(shape))
# # if len(shape) is 1:
# # raise RuntimeError("add " + layer.name + " failed!")
# def layer_post_process(self, trt_layer, layer_name, precision):
# """
# Tool function. set precision, set_layer_name and check_trt_layer
# """
# if precision is not None:
# trt_layer.precision = precision
# self.set_layer_name(trt_layer, layer_name)
# self.check_trt_layer(trt_layer)
# def addInput(self, name, dtype, shape):
# if name is None:
# name = "input" + str(self.input_num)
# self.input_num = self.input_num + 1
# trt_input = self.network.add_input(name=name, dtype=dtype, shape=shape)
# if not trt_input:
# raise RuntimeError("addInput failed!")
# self.logger.log(trt.Logger.INFO, "[Network] add input:" + name + ", shape=" + str(shape))
# return trt_input
# def markOutput(self, x: trt.ITensor):
# self.network.mark_output(x)
# self.logger.log(trt.Logger.INFO, "[Network] mark output:" + x.name + ", shape=" + str(x.shape))
# def addEmbedding(self, indices, weight, layer_name=None, precision=None):
# constant_layer = self.network.add_constant(weight.shape, trt.Weights(weight))
# gather_layer = self.network.add_gather(constant_layer.get_output(0),
# indices, axis=0)
# if layer_name is None:
# layer_name = "nn.Embedding"
# else:
# layer_name = "nn.Embedding." + layer_name
# self.layer_post_process(gather_layer, layer_name, precision)
# return gather_layer.get_output(0)
# def addGELU(self, x, layer_name=None, precision=None):
# POW = self.network.add_constant((1, 1, 1), trt.Weights(np.ascontiguousarray([3.0], dtype=np.float32)))
# MULTIPLY = self.network.add_constant((1, 1, 1), trt.Weights(np.ascontiguousarray([0.044715], dtype=np.float32)))
# SQRT = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([0.79788456080286535587989211986876], dtype=np.float32))))
# ONE = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([1.0], dtype=np.float32))))
# HALF = self.network.add_constant((1, 1, 1), trt.Weights((np.ascontiguousarray([0.5], dtype=np.float32))))
# X_pow = self.network.add_elementwise(x, POW.get_output(0), trt.ElementWiseOperation.POW)
# X_pow_t = X_pow.get_output(0)
# X_mul = self.network.add_elementwise(X_pow_t, MULTIPLY.get_output(0), trt.ElementWiseOperation.PROD)
# X_add = self.network.add_elementwise(x, X_mul.get_output(0), trt.ElementWiseOperation.SUM)
# X_sqrt = self.network.add_elementwise(X_add.get_output(0), SQRT.get_output(0), trt.ElementWiseOperation.PROD)
# X_sqrt_tensor = X_sqrt.get_output(0)
# X_tanh = self.network.add_activation(X_sqrt_tensor, trt.ActivationType.TANH)
# X_tanh_tensor = X_tanh.get_output(0)
# X_one = self.network.add_elementwise(X_tanh_tensor, ONE.get_output(0), trt.ElementWiseOperation.SUM)
# CDF = self.network.add_elementwise(X_one.get_output(0), HALF.get_output(0), trt.ElementWiseOperation.PROD)
# gelu_layer = self.network.add_elementwise(CDF.get_output(0), x, trt.ElementWiseOperation.PROD)
# if layer_name is None:
# layer_name = "nn.GELU"
# else:
# layer_name = "nn.GELU." + layer_name
# self.layer_post_process(gelu_layer, layer_name, precision)
# return gelu_layer.get_output(0)
# def addLayerNorm(self, x, gamma, beta, layer_name=None, precision=None):
# # TODO: create your layer norm plugin
# return trt_layer.get_output(0)
# def addLinear(self, x, weight, bias, layer_name=None, precision=None):
# # TODO: add Linear
# return x
# def addReLU(self, layer, x, layer_name=None, precision=None):
# trt_layer = self.network.add_activation(x, type=trt.ActivationType.RELU)
# if layer_name is None:
# layer_name = "nn.ReLU"
# self.layer_post_process(trt_layer, layer_name, precision)
# x = trt_layer.get_output(0)
# return x
# def addSoftmax(self, x: trt.ITensor, dim: int = -1, layer_name=None, precision=None) -> trt.ITensor:
# # TODO: add softmax
# return x
# ################## unary op ###################
# def addLog(self, x: trt.ITensor, layer_name=None, precision=None):
# trt_layer = self.network.add_unary(x, trt.UnaryOperation.LOG)
# if layer_name is None:
# layer_name = "unary.log"
# else:
# layer_name = "unary.log." + layer_name
# self.layer_post_process(trt_layer, layer_name, precision)
# x = trt_layer.get_output(0)
# return x
# ################## elementwise op ###################
# def addAdd(self, a, b, layer_name=None, precision=None):
# # add Add
# return x
# # tensor and scalar op
# def addScale(
# self,
# x: trt.ITensor,
# scale: float,
# layer_name: str = None,
# precision: trt.DataType = None
# ) -> trt.ITensor:
# """scale"""
# # TODOL add scale
# return x
# def addMatMul(self, a: trt.ITensor, b: trt.ITensor, layer_name: Optional[str] = None) -> trt.ITensor:
# # add MatMul
# return x
# def addConstant(self, w, layer_name: Optional[str] = None) -> trt.ITensor:
# trt_layer = self.network.add_constant(w.shape, w)
# if layer_name is None:
# layer_name = "trt.Constant"
# else:
# layer_name = "trt.Constant." + layer_name
# self.layer_post_process(trt_layer, layer_name, None)
# x = trt_layer.get_output(0)
# return x
# def addShuffle(
# self,
# x: trt.ITensor,
# first_transpose: trt.Permutation,
# reshape_dims: trt.Dims,
# second_transpose: trt.Permutation,
# layer_name: Optional[str] = None
# ) -> trt.ITensor:
# """"""
# trt_layer = self.network.add_shuffle(x)
# if first_transpose is not None:
# trt_layer.first_transpose = first_transpose
# if reshape_dims is not None:
# trt_layer.reshape_dims = reshape_dims
# if second_transpose is not None:
# trt_layer.second_transpose = second_transpose
# if layer_name is None:
# layer_name = "trt.Shuffle"
# else:
# layer_name = "trt.Shuffle." + layer_name
# self.layer_post_process(trt_layer, layer_name, None)
# x = trt_layer.get_output(0)
# return x
# class InferHelper():
# """"""
# def __init__(self, plan_name, trt_logger):
# """"""
# self.logger = trt_logger
# self.runtime = trt.Runtime(trt_logger)
# with open(plan_name, 'rb') as f:
# self.engine = self.runtime.deserialize_cuda_engine(f.read())
# self.context = self.engine.create_execution_context()
# self.context.active_optimization_profile = 0
# def infer(self, inputs: list):
# nInput = len(inputs)
# bufferD = []
# # alloc memory
# for i in range(nInput):
# bufferD.append(cuda.mem_alloc(inputs[i].nbytes))
# cuda.memcpy_htod(bufferD[i], inputs[i].ravel())
# self.context.set_binding_shape(i, tuple(inputs[i].shape))
# # print(inputs[i].nbytes)
# # for i in range(0, self.engine.num_bindings):
# # print("get_binding_shape:" + str(self.context.get_binding_shape(i)))
# outputs = []
# for i in range(len(inputs), self.engine.num_bindings):
# outputs.append(np.zeros(self.context.get_binding_shape(i)).astype(np.float32))
# nOutput = len(outputs)
# for i in range(nOutput):
# bufferD.append(cuda.mem_alloc(outputs[i].nbytes))
# # print(outputs[i].nbytes)
# for i in range(len(inputs), self.engine.num_bindings):
# trt_output_shape = self.context.get_binding_shape(i)
# output_idx = i - len(inputs)
# if not (list(trt_output_shape) == list(outputs[output_idx].shape)):
# self.logger.log(trt.Logger.ERROR, "[Infer] output shape is error!")
# self.logger.log(trt.Logger.ERROR, "trt_output.shape = " + str(trt_output_shape))
# self.logger.log(trt.Logger.ERROR, "base_output.shape = " + str(outputs[output_idx].shape))
# assert(0)
# # warm up
# self.context.execute_v2(bufferD)
# T1 = time.perf_counter()
# self.context.execute_v2(bufferD)
# T2 =time.perf_counter()
# print("time=" + str((T2-T1) * 1000) + "ms")
# for i in range(nInput, nInput + nOutput):
# cuda.memcpy_dtoh(outputs[i - nInput].ravel(), bufferD[i])
# for i in range(0, len(outputs)):
# print("outputs.shape:" + str(outputs[i].shape))
# print("outputs.sum:" + str(outputs[i].sum()))
# # print(outputs[i])
# # print("trt_output.shape:" + str(trt_output.shape))
# # print("trt_output.sum:" + str(trt_output.sum()))
# # print(trt_output.view(-1)[0:10])
# # print("torch.allclose result:" + str(torch.allclose(base_output, trt_output, 1e-05, 1e-03)))
# # print("====================")
# return outputs
# # return torch.allclose(base_output, trt_output, 1e-05, 1e-03)