-
Notifications
You must be signed in to change notification settings - Fork 167
/
speed_gpu.py
executable file
·209 lines (161 loc) · 8.4 KB
/
speed_gpu.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
import time
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import optim
from torch.autograd import Variable
import torch.onnx
import onnx
from onnx import optimizer
import os
import tensorrt as trt
import numpy as np
import pycuda.autoinit
import pycuda.driver as cuda
import time
from utils import AverageMeter, calculate_accuracy
from models import squeezenet, shufflenetv2, shufflenet, mobilenet, mobilenetv2, c3d, resnetl
model_folder = 'results'
model_name = 'onnx_model'
onnx_model = 'results/ortho_model_shufflenetv2_unet_1120x224_best2.onnx'
# onnx_model = os.path.join(model_folder, model_name + '.onnx')
# # model = shufflenet.get_model(groups=3, width_mult=0.5, num_classes=600)#1
# # model = shufflenetv2.get_model( width_mult=0.25, num_classes=600, sample_size = 112)#2
# # model = mobilenet.get_model( width_mult=0.5, num_classes=600, sample_size = 112)#3
# # model = mobilenetv2.get_model( width_mult=0.2, num_classes=600, sample_size = 112)#4
# # model = shufflenet.get_model(groups=3, width_mult=1.0, num_classes=600)#5
# # model = shufflenetv2.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#6
# # model = mobilenet.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#7
# # model = mobilenetv2.get_model( width_mult=0.45, num_classes=600, sample_size = 112)#8
# # model = shufflenet.get_model(groups=3, width_mult=1.5, num_classes=600)#9
# # model = shufflenetv2.get_model( width_mult=1.5, num_classes=600, sample_size = 112)#10
# # model = mobilenet.get_model( width_mult=1.5, num_classes=600, sample_size = 112)#11
# # model = mobilenetv2.get_model( width_mult=0.7, num_classes=600, sample_size = 112)#12
# # model = shufflenet.get_model(groups=3, width_mult=2.0, num_classes=600)#13
# model = shufflenetv2.get_model( width_mult=2.0, num_classes=600, sample_size = 112)#14
# # model = mobilenet.get_model( width_mult=2.0, num_classes=600, sample_size = 112)#15
# # model = mobilenetv2.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#16
# # model = squeezenet.get_model( version=1.1, num_classes=600, sample_size = 112, sample_duration = 8)
# # model = resnetl.resnetl10(num_classes=2, sample_size = 112, sample_duration = 8)
# # model = model.cuda()
# # model = nn.DataParallel(model, device_ids=None)
# print(model)
# # create the imput placeholder for the model
# # input_placeholder = torch.randn(1, 3, 16, 112, 112)
# input_placeholder = torch.randn(1, 3, 1120, 224)
# # export
# torch.onnx.export(model, input_placeholder, onnx_model)
# print('{} exported!'.format(onnx_model))
TRT_LOGGER = trt.Logger(trt.Logger.WARNING)
def build_engine(model_path):
with trt.Builder(TRT_LOGGER) as builder, \
builder.create_network() as network, \
trt.OnnxParser(network, TRT_LOGGER) as parser:
builder.max_workspace_size = 1<<30
builder.max_batch_size = 1
with open(model_path, "rb") as f:
parser.parse(f.read())
engine = builder.build_cuda_engine(network)
return engine
def alloc_buf(engine):
# host cpu mem
h_in_size = trt.volume(engine.get_binding_shape(0))
h_out_size = trt.volume(engine.get_binding_shape(1))
h_in_dtype = trt.nptype(engine.get_binding_dtype(0))
h_out_dtype = trt.nptype(engine.get_binding_dtype(1))
in_cpu = cuda.pagelocked_empty(h_in_size, h_in_dtype)
out_cpu = cuda.pagelocked_empty(h_out_size, h_out_dtype)
# allocate gpu mem
in_gpu = cuda.mem_alloc(in_cpu.nbytes)
out_gpu = cuda.mem_alloc(out_cpu.nbytes)
stream = cuda.Stream()
return in_cpu, out_cpu, in_gpu, out_gpu, stream
def inference(engine, context, inputs, out_cpu, in_gpu, out_gpu, stream):
# async version
# with engine.create_execution_context() as context: # cost time to initialize
# cuda.memcpy_htod_async(in_gpu, inputs, stream)
# context.execute_async(1, [int(in_gpu), int(out_gpu)], stream.handle, None)
# cuda.memcpy_dtoh_async(out_cpu, out_gpu, stream)
# stream.synchronize()
# sync version
cuda.memcpy_htod(in_gpu, inputs)
context.execute(1, [int(in_gpu), int(out_gpu)])
cuda.memcpy_dtoh(out_cpu, out_gpu)
return out_cpu
if __name__ == "__main__":
# inputs = np.random.random((1, 3, 16, 112, 112)).astype(np.float32)
inputs = np.random.random((1, 3, 1120, 224)).astype(np.float32)
engine = build_engine(onnx_model)
context = engine.create_execution_context()
for _ in range(10):
t1 = time.time()
in_cpu, out_cpu, in_gpu, out_gpu, stream = alloc_buf(engine)
res = inference(engine, context, inputs.reshape(-1), out_cpu, in_gpu, out_gpu, stream)
print(res)
print("cost time: ", time.time()-t1)
# tensorrt docker image: docker pull nvcr.io/nvidia/tensorrt:19.09-py3 (See: https://ngc.nvidia.com/catalog/containers/nvidia:tensorrt/tags)
# NOTE: cuda driver >= 418
# batch_time = AverageMeter()
# input_var = Variable(torch.randn(1, 3, 8, 112, 112).cuda())
# end_time = time.time()
# for i in range(10000):
# output = model(input_var)
# batch_time.update(time.time() - end_time)
# end_time = time.time()
# print("Current average time: ", batch_time.avg, "Speed (vps): ", 1 / (batch_time.avg / 1.0) )
# print("Average time for GPU: ", batch_time.avg, "Speed (vps): ", 1 / (batch_time.avg / 1.0))
# import time
# import torch
# import torch.nn as nn
# import torch.nn.functional as F
# from torch import optim
# from torch.autograd import Variable
# from utils import AverageMeter, calculate_accuracy
# from models import squeezenet, shufflenetv2, shufflenet, mobilenet, mobilenetv2, c3d, resnetl
# try:
# from apex.parallel import DistributedDataParallel as DDP
# from apex.fp16_utils import *
# from apex import amp, optimizers
# from apex.multi_tensor_apply import multi_tensor_applier
# except ImportError:
# raise ImportError("Please install apex from https://www.github.com/nvidia/apex to run this example.")
# # model = shufflenet.get_model(groups=3, width_mult=0.5, num_classes=600)#1
# # model = shufflenetv2.get_model( width_mult=0.25, num_classes=600, sample_size = 112)#2
# # model = mobilenet.get_model( width_mult=0.5, num_classes=600, sample_size = 112)#3
# # model = mobilenetv2.get_model( width_mult=0.2, num_classes=600, sample_size = 112)#4
# # model = shufflenet.get_model(groups=3, width_mult=1.0, num_classes=600)#5
# # model = shufflenetv2.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#6
# # model = mobilenet.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#7
# # model = mobilenetv2.get_model( width_mult=0.45, num_classes=600, sample_size = 112)#8
# # model = shufflenet.get_model(groups=3, width_mult=1.5, num_classes=600)#9
# # model = shufflenetv2.get_model( width_mult=1.5, num_classes=600, sample_size = 112)#10
# # model = mobilenet.get_model( width_mult=1.5, num_classes=600, sample_size = 112)#11
# # model = mobilenetv2.get_model( width_mult=0.7, num_classes=600, sample_size = 112)#12
# # model = shufflenet.get_model(groups=3, width_mult=2.0, num_classes=600)#13
# # model = shufflenetv2.get_model( width_mult=2.0, num_classes=600, sample_size = 112)#14
# # model = mobilenet.get_model( width_mult=2.0, num_classes=600, sample_size = 112)#15
# # model = mobilenetv2.get_model( width_mult=1.0, num_classes=600, sample_size = 112)#16
# # model = squeezenet.get_model( version=1.1, num_classes=600, sample_size = 112, sample_duration = 8)
# model = resnetl.resnetl10(num_classes=2, sample_size = 112, sample_duration = 8)
# model = model.cuda()
# #model = nn.DataParallel(model, device_ids=None)
# optimizer = optim.SGD(
# model.parameters(),
# lr=0.001)
# print(model)
# print("\nCUDNN VERSION: {}\n".format(torch.backends.cudnn.version()))
# assert torch.backends.cudnn.enabled, "Amp requires cudnn backend to be enabled."
# model, optimizer = amp.initialize(model, optimizer,
# opt_level='O3',
# keep_batchnorm_fp32=True,
# loss_scale=None
# )
# batch_time = AverageMeter()
# input_var = Variable(torch.randn(1, 3, 8, 112, 112).cuda())
# end_time = time.time()
# for i in range(10000):
# output = model(input_var)
# batch_time.update(time.time() - end_time)
# end_time = time.time()
# print("Current average time: ", batch_time.avg, "Speed (vps): ", 1 / (batch_time.avg / 1.0) )
# print("Average time for GPU: ", batch_time.avg, "Speed (vps): ", 1 / (batch_time.avg / 1.0))