Caffe2 - Python API
A deep learning, cross platform ML framework
lstm_comparison.py
1 # Copyright (c) 2016-present, Facebook, Inc.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14 ##############################################################################
15 
16 from __future__ import absolute_import
17 from __future__ import division
18 from __future__ import print_function
19 from __future__ import unicode_literals
20 from caffe2.proto import caffe2_pb2
21 from caffe2.python import workspace, core, lstm_benchmark, utils
22 from copy import copy
23 
24 @utils.debug
25 def Compare(args):
26  results = []
27  num_iters = 1000
28  args.gpu = True
29  with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
30  for batch_size in [64, 128, 256]:
31  for seq_length in [20, 100]:
32  for hidden_dim in [40, 100, 400, 800]:
33  args.batch_size = batch_size
34  args.seq_length = seq_length
35  args.hidden_dim = hidden_dim
36  args.data_size = batch_size * seq_length * num_iters
37  args.iters_to_report = num_iters // 3
38 
39  args.implementation = 'own'
40  t_own = lstm_benchmark.Benchmark(args)
41  workspace.ResetWorkspace()
42  args.implementation = 'cudnn'
43  t_cudnn = lstm_benchmark.Benchmark(args)
44  workspace.ResetWorkspace()
45  results.append((copy(args), float(t_own), float(t_cudnn)))
46  print(args)
47  print("t_cudnn / t_own: {}".format(t_cudnn / t_own))
48 
49  for args, t_own, t_cudnn in results:
50  print("{}: cudnn time: {}, own time: {}, ratio: {}".format(
51  str(args), t_cudnn, t_own, t_cudnn / t_own))
52 
53  ratio_sum = 0
54  for args, t_own, t_cudnn in results:
55  ratio = float(t_cudnn) / t_own
56  ratio_sum += ratio
57  print("hidden_dim: {}, seq_lengths: {}, batch_size: {}, num_layers: {}:"
58  " cudnn time: {}, own time: {}, ratio: {}".format(
59  args.hidden_dim, args.seq_length, args.batch_size,
60  args.num_layers, t_cudnn, t_own, ratio))
61 
62  print("Ratio average: {}".format(ratio_sum / len(results)))
63 
64 
65 if __name__ == '__main__':
66  args = lstm_benchmark.GetArgumentParser().parse_args()
67 
68  workspace.GlobalInit([
69  'caffe2',
70  '--caffe2_log_level=0',
71  '--caffe2_print_blob_sizes_at_exit=0',
72  '--caffe2_gpu_memory_tracking=1'])
73 
74  Compare(args)