Caffe2 - Python API
A deep learning, cross platform ML framework
device_reduce_sum_bench.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 ## @package device_reduce_sum_bench
17 # Module caffe2.experiments.python.device_reduce_sum_bench
18 from __future__ import absolute_import
19 from __future__ import division
20 from __future__ import print_function
21 from __future__ import unicode_literals
22 
23 import argparse
24 import itertools
25 import logging
26 import os
27 
28 from six import add_metaclass
29 import numpy as np
30 
31 from caffe2.python import workspace, core
32 from caffe2.python.hypothesis_test_util import runOpBenchmark, gpu_do
33 
34 logging.basicConfig()
35 logger = logging.getLogger(os.path.basename(__file__))
36 logger.setLevel(logging.INFO)
37 
38 ALL_BENCHMARKS = {}
39 
40 
41 class BenchmarkMeta(type):
42  def __new__(metacls, name, bases, class_dict):
43  cls = type.__new__(metacls, name, bases, class_dict)
44  if name != 'Benchmark':
45  ALL_BENCHMARKS[name] = cls
46  return cls
47 
48 
49 @add_metaclass(BenchmarkMeta)
50 class Benchmark(object):
51 
52  def __init__(self):
53  self.results = []
54 
55  def display(self):
56  print('Results ({}):'.format(type(self).__name__))
57  print('input size ms/iter')
58  print('------------------------------ -----------')
59  for size, ms in self.results:
60  print('{!s:<30} {:.4f}'.format(size, ms))
61 
62 
64  def run(self):
65  op = core.CreateOperator(
66  "SumElements",
67  ["X"],
68  ["y"]
69  )
70 
71  for n in itertools.imap(pow, itertools.cycle([10]), range(10)):
72  X = np.random.rand(n).astype(np.float32)
73  logger.info('Running benchmark for n = {}'.format(n))
74  ret = runOpBenchmark(gpu_do, op, inputs=[X])
75  self.results.append((n, ret[1]))
76 
77 
79  def run(self):
80  op = core.CreateOperator(
81  "SumSqrElements",
82  ["X"],
83  ["y"]
84  )
85 
86  for n in itertools.imap(pow, itertools.cycle([10]), range(10)):
87  X = np.random.rand(n).astype(np.float32)
88  logger.info('Running benchmark for n = {}'.format(n))
89  ret = runOpBenchmark(gpu_do, op, inputs=[X])
90  self.results.append((n, ret[1]))
91 
92 
94  def run(self):
95  op = core.CreateOperator(
96  "SoftmaxWithLoss",
97  ["X", "label"],
98  ["probs", "avgloss"],
99  )
100 
101  for n in itertools.imap(pow, itertools.cycle([10]), range(8)):
102  for D in itertools.imap(pow, itertools.cycle([10]), range(3)):
103  X = np.random.rand(n, D).astype(np.float32)
104  label = (np.random.rand(n) * D).astype(np.int32)
105  logger.info('Running benchmark for n = {}, D= {}'.format(n, D))
106  ret = runOpBenchmark(gpu_do, op, inputs=[X, label])
107  self.results.append(((n, D), ret[1]))
108 
109 
110 def parse_args():
111  parser = argparse.ArgumentParser(os.path.basename(__file__))
112  parser.add_argument('-b', '--benchmarks', nargs='+',
113  default=ALL_BENCHMARKS.keys(),
114  help='benchmarks to run (default: %(default)s))')
115  return parser.parse_args()
116 
117 
118 def main():
119  args = parse_args()
120 
121  benchmarks = [ALL_BENCHMARKS[name]() for name in args.benchmarks]
122  for bench in benchmarks:
123  bench.run()
124  for bench in benchmarks:
125  bench.display()
126 
127 
128 if __name__ == '__main__':
129  workspace.GlobalInit(['caffe2', '--caffe2_log_level=2'])
130  main()