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 ==============================================================================*/ 15 16 // Contains benchmark functions used with the code-generated benchmarks that can 17 // be used to test a model on android. See also code generation rules in 18 // tfcompile.bzl. 19 // 20 // This is separate from the built-in micro-benchmarks, because we want to: 21 // 1. show a binary with minimal dependencies, to show a close-to-lower-bound 22 // binary size. 23 // 2. compile on Android. 24 #ifndef TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ 25 #define TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ 26 27 #include <functional> 28 #include <string> 29 #include <vector> 30 31 #include "tensorflow/core/platform/types.h" 32 33 namespace tensorflow { 34 namespace tfcompile { 35 namespace benchmark { 36 37 // Options specifies options for benchmarks of functions generated by tfcompile. 38 struct Options { 39 // kDefaultMicros specifies the default time to run the benchmark, and is used 40 // if neither max_iters nor max_micros is set. 41 static constexpr int64 kDefaultMicros = 3000000; 42 43 int64 max_iters = 0; // Maximum iterations to run, ignored if <= 0. 44 int64 max_micros = 0; // Maximum microseconds to run, ignored if <= 0. 45 }; 46 47 // Stats holds statistics collected during benchmarking. 48 struct Stats { 49 std::vector<int64> per_iter_us; // Per-iteration deltas in us. 50 int64 total_us; // Total time in us. 51 StatsStats52 Stats() : total_us(0) { per_iter_us.reserve(5000); } 53 }; 54 55 // DumpStatsToStdout printfs to stdout stats in a multi-line human-friendly 56 // form. 57 void DumpStatsToStdout(const Stats& stats); 58 59 // BenchmarkFn is the signature of the function generated by tfcompile. 60 typedef std::function<void()> BenchmarkFn; 61 62 // Benchmark runs a benchmark of the function `fn`, collecting stats in `stats`. 63 // Use `options` to configure benchmarking options. 64 void Benchmark(const Options& options, const BenchmarkFn& fn, Stats* stats); 65 66 } // namespace benchmark 67 } // namespace tfcompile 68 } // namespace tensorflow 69 70 #endif // TENSORFLOW_COMPILER_AOT_BENCHMARK_H_ 71