1# Copyright 2017 The TensorFlow Authors. All Rights Reserved. 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# ============================================================================== 15r"""Benchmark base to run and report benchmark results.""" 16 17from __future__ import absolute_import as _absolute_import 18from __future__ import division as _division 19from __future__ import print_function as _print_function 20 21import os 22import uuid 23 24from tensorflow.python.eager import test 25from tensorflow.python.platform import flags 26from tensorflow.python.profiler import profiler_v2 as profiler 27 28flags.DEFINE_bool("xprof", False, "Run and report benchmarks with xprof on") 29flags.DEFINE_string("logdir", "/tmp/xprof/", "Directory to store xprof data") 30 31 32class MicroBenchmarksBase(test.Benchmark): 33 """Run and report benchmark results. 34 35 The first run is without any profilng. 36 Second run is with xprof and python trace. Third run is with xprof without 37 python trace. Note: xprof runs are with fewer iterations. 38 """ 39 40 def run_with_xprof(self, enable_python_trace, run_benchmark, func, 41 num_iters_xprof, execution_mode, suid): 42 if enable_python_trace: 43 options = profiler.ProfilerOptions(python_tracer_level=1) 44 logdir = os.path.join(flags.FLAGS.logdir, suid + "_with_python") 45 else: 46 options = profiler.ProfilerOptions(python_tracer_level=0) 47 logdir = os.path.join(flags.FLAGS.logdir, suid) 48 with profiler.Profile(logdir, options): 49 total_time = run_benchmark(func, num_iters_xprof, execution_mode) 50 us_per_example = float("{0:.3f}".format(total_time * 1e6 / num_iters_xprof)) 51 return logdir, us_per_example 52 53 def run_report(self, run_benchmark, func, num_iters, execution_mode=None): 54 """Run and report benchmark results.""" 55 total_time = run_benchmark(func, num_iters, execution_mode) 56 mean_us = total_time * 1e6 / num_iters 57 extras = { 58 "examples_per_sec": float("{0:.3f}".format(num_iters / total_time)), 59 "us_per_example": float("{0:.3f}".format(total_time * 1e6 / num_iters)) 60 } 61 62 if flags.FLAGS.xprof: 63 suid = str(uuid.uuid4()) 64 # Re-run with xprof and python trace. 65 num_iters_xprof = min(100, num_iters) 66 xprof_link, us_per_example = self.run_with_xprof(True, run_benchmark, 67 func, num_iters_xprof, 68 execution_mode, suid) 69 extras["xprof link with python trace"] = xprof_link 70 extras["us_per_example with xprof and python"] = us_per_example 71 72 # Re-run with xprof but no python trace. 73 xprof_link, us_per_example = self.run_with_xprof(False, run_benchmark, 74 func, num_iters_xprof, 75 execution_mode, suid) 76 extras["xprof link"] = xprof_link 77 extras["us_per_example with xprof"] = us_per_example 78 79 benchmark_name = self._get_benchmark_name() 80 self.report_benchmark( 81 iters=num_iters, wall_time=mean_us, extras=extras, name=benchmark_name) 82