forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlstm_comparison.py
58 lines (45 loc) · 2.03 KB
/
lstm_comparison.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
from caffe2.python import workspace, core, lstm_benchmark, utils
from copy import copy
@utils.debug
def Compare(args):
results = []
num_iters = 1000
args.gpu = True
with core.DeviceScope(core.DeviceOption(workspace.GpuDeviceType, 0)):
for batch_size in [64, 128, 256]:
for seq_length in [20, 100]:
for hidden_dim in [40, 100, 400, 800]:
args.batch_size = batch_size
args.seq_length = seq_length
args.hidden_dim = hidden_dim
args.data_size = batch_size * seq_length * num_iters
args.iters_to_report = num_iters // 3
args.implementation = 'own'
t_own = lstm_benchmark.Benchmark(args)
workspace.ResetWorkspace()
args.implementation = 'cudnn'
t_cudnn = lstm_benchmark.Benchmark(args)
workspace.ResetWorkspace()
results.append((copy(args), float(t_own), float(t_cudnn)))
print(args)
print("t_cudnn / t_own: {}".format(t_cudnn / t_own))
for args, t_own, t_cudnn in results:
print("{}: cudnn time: {}, own time: {}, ratio: {}".format(
str(args), t_cudnn, t_own, t_cudnn / t_own))
ratio_sum = 0
for args, t_own, t_cudnn in results:
ratio = float(t_cudnn) / t_own
ratio_sum += ratio
print("hidden_dim: {}, seq_lengths: {}, batch_size: {}, num_layers: {}:"
" cudnn time: {}, own time: {}, ratio: {}".format(
args.hidden_dim, args.seq_length, args.batch_size,
args.num_layers, t_cudnn, t_own, ratio))
print("Ratio average: {}".format(ratio_sum / len(results)))
if __name__ == '__main__':
args = lstm_benchmark.GetArgumentParser().parse_args()
workspace.GlobalInit([
'caffe2',
'--caffe2_log_level=0',
'--caffe2_print_blob_sizes_at_exit=0',
'--caffe2_gpu_memory_tracking=1'])
Compare(args)