-
Notifications
You must be signed in to change notification settings - Fork 45
/
evaluate_timing.py
165 lines (139 loc) · 4.91 KB
/
evaluate_timing.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
'''
Script for timing models in eval mode and torchscript eval modes.
'''
import os
import logging
import sys
import time
from argparse import ArgumentParser
import importlib
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
torch.backends.cudnn.benchmark = True
def opts_parser():
usage = 'Pass the model and'
parser = ArgumentParser(description=usage)
parser.add_argument(
'--num_iter', type=int, default=50,
help='Number of iterations to average over.')
parser.add_argument(
'--model_class', type=str, default='selecsls', metavar='FILE',
help='Select model type to use (DenseNet, SelecSLS, ResNet etc.)')
parser.add_argument(
'--model_config', type=str, default='SelecSLS84', metavar='NET_CONFIG',
help='Select the model configuration')
parser.add_argument(
'--model_weights', type=str, default='./weights/SelecSLS84_statedict.pth', metavar='FILE',
help='Path to model weights')
parser.add_argument(
'--input_size', type=int, default=400,
help='Input image size.')
parser.add_argument(
'--gpu_id', type=int, default=0,
help='Which GPU to use.')
parser.add_argument(
'--pruned_and_fused', type=bool, default=False,
help='Whether to zero out features with gamma below a certain threshold')
parser.add_argument(
'--gamma_thresh', type=float, default=1e-3,
help='gamma threshold to use for simulating pruning. Set this to -1 to only fuse BN without pruning')
return parser
def measure_cpu(model, x):
# synchronize gpu time and measure fp
model.eval()
with torch.no_grad():
t0 = time.time()
y_pred = model(x)
elapsed_fp_nograd = time.time()-t0
return elapsed_fp_nograd
def measure_gpu(model, x):
# synchronize gpu time and measure fp
model.eval()
with torch.no_grad():
torch.cuda.synchronize()
t0 = time.time()
y_pred = model(x)
torch.cuda.synchronize()
elapsed_fp_nograd = time.time()-t0
return elapsed_fp_nograd
def benchmark(model_class, model_config, gpu_id, num_iter, model_weights, input_size, pruned_and_fused, gamma_thresh):
# Import the model module
model_module = importlib.import_module('models.'+model_class)
net = model_module.Net(nClasses=1000, config=model_config)
net.load_state_dict(torch.load(model_weights, map_location= lambda storage, loc: storage))
if pruned_and_fused:
print('Pruning and fusing the model')
net.prune_and_fuse(gamma_thresh, True)
device = torch.device("cuda:"+str(gpu_id) if torch.cuda.is_available() else "cpu")
net = net.to(device)
print('\nEvaluating on GPU {}'.format(device))
print('\nGPU, Batch Size: 1')
x = torch.randn(1, 3, input_size, input_size)
#Warm up
for i in range(10):
_ = measure_gpu(net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_gpu(net, x.to(device))
fp.append(t)
print('Model FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
jit_net = torch.jit.trace(net, x.to(device))
for i in range(10):
_ = measure_gpu(jit_net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_gpu(jit_net, x.to(device))
fp.append(t)
print('JIT FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
print('\nGPU, Batch Size: 16')
x = torch.randn(16, 3, input_size, input_size)
#Warm up
for i in range(10):
_ = measure_gpu(net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_gpu(net, x.to(device))
fp.append(t)
print('Model FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
jit_net = torch.jit.trace(net, x.to(device))
for i in range(10):
_ = measure_gpu(jit_net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_gpu(jit_net, x.to(device))
fp.append(t)
print('JIT FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
device = torch.device("cpu")
print('\nEvaluating on {}'.format(device))
net = net.to(device)
print('\nCPU, Batch Size: 1')
x = torch.randn(1, 3, input_size, input_size)
#Warm up
for i in range(10):
_ = measure_cpu(net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_cpu(net, x.to(device))
fp.append(t)
print('Model FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
jit_net = torch.jit.trace(net, x.to(device))
for i in range(10):
_ = measure_cpu(jit_net, x.to(device))
fp = []
for i in range(num_iter):
t = measure_cpu(jit_net, x.to(device))
fp.append(t)
print('JIT FP: '+str(np.mean(np.asarray(fp)*1000))+'ms')
def main():
# parse command line
torch.manual_seed(1234)
parser = opts_parser()
args = parser.parse_args()
# run
benchmark(**vars(args))
if __name__ == '__main__':
main()