From e3790a64226b30bd8df31f636c65f20a8f40c1a8 Mon Sep 17 00:00:00 2001 From: Chao Mei Date: Tue, 3 Mar 2020 04:48:42 -0800 Subject: [PATCH] Add an option to use platform-wide tracing via TFLite platform profiler. PiperOrigin-RevId: 298577942 Change-Id: I749dfdf89f4560128b128ac0493e30429f7aad36 --- tensorflow/lite/tools/benchmark/BUILD | 1 + tensorflow/lite/tools/benchmark/README.md | 5 ++++ .../lite/tools/benchmark/benchmark_test.cc | 2 ++ .../tools/benchmark/benchmark_tflite_model.cc | 30 ++++++++++++++++++- 4 files changed, 37 insertions(+), 1 deletion(-) diff --git a/tensorflow/lite/tools/benchmark/BUILD b/tensorflow/lite/tools/benchmark/BUILD index 5a413112e2f..77bd723bb10 100644 --- a/tensorflow/lite/tools/benchmark/BUILD +++ b/tensorflow/lite/tools/benchmark/BUILD @@ -148,6 +148,7 @@ cc_library( "//tensorflow/lite:string_util", "//tensorflow/lite/experimental/ruy/profiler", "//tensorflow/lite/kernels:builtin_ops", + "//tensorflow/lite/profiling:platform_profiler", "//tensorflow/lite/profiling:profiler", "//tensorflow/lite/profiling:profile_summary_formatter", "//tensorflow/lite/tools/evaluation:utils", diff --git a/tensorflow/lite/tools/benchmark/README.md b/tensorflow/lite/tools/benchmark/README.md index 286ddf69cab..a0891859b0c 100644 --- a/tensorflow/lite/tools/benchmark/README.md +++ b/tensorflow/lite/tools/benchmark/README.md @@ -82,6 +82,11 @@ and the following optional parameters: blank, passive mode is used by default. * `enable_op_profiling`: `bool` (default=false) \ Whether to enable per-operator profiling measurement. +* `enable_platform_tracing`: `bool` (default=false) \ + Whether to enable platform-wide tracing. Needs to be combined with + 'enable_op_profiling'. Note, the platform-wide tracing might not work if + the tool runs as a commandline native binary. For example, on Android, the + ATrace-based tracing only works when the tool is launched as an APK. * `hexagon_profiling`: `bool` (default=false) \ Whether to profile ops running on hexagon. Needs to be combined with `enable_op_profiling`. When this is set to true the profile of ops diff --git a/tensorflow/lite/tools/benchmark/benchmark_test.cc b/tensorflow/lite/tools/benchmark/benchmark_test.cc index 8ff8c4ad57c..db1acd0c475 100644 --- a/tensorflow/lite/tools/benchmark/benchmark_test.cc +++ b/tensorflow/lite/tools/benchmark/benchmark_test.cc @@ -84,6 +84,8 @@ BenchmarkParams CreateParams(int32_t num_runs, float min_secs, float max_secs, params.AddParam("max_delegated_partitions", BenchmarkParam::Create(0)); params.AddParam("profiling_output_csv_file", BenchmarkParam::Create("")); + params.AddParam("enable_platform_tracing", + BenchmarkParam::Create(false)); return params; } diff --git a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc index f05cfb9fc80..e62e60cb4c7 100644 --- a/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc +++ b/tensorflow/lite/tools/benchmark/benchmark_tflite_model.cc @@ -32,6 +32,7 @@ limitations under the License. #include "tensorflow/lite/kernels/register.h" #include "tensorflow/lite/model.h" #include "tensorflow/lite/op_resolver.h" +#include "tensorflow/lite/profiling/platform_profiler.h" #include "tensorflow/lite/profiling/profile_summary_formatter.h" #include "tensorflow/lite/string_util.h" #include "tensorflow/lite/tools/benchmark/benchmark_utils.h" @@ -59,6 +60,20 @@ constexpr int kOpProfilingEnabledDefault = true; constexpr int kOpProfilingEnabledDefault = false; #endif +// Dumps platform-wide tracing files via a platform-based profiler that's built +// upon platform tracing tools, like ATrace on Android etc. +class PlatformProfilingListener : public BenchmarkListener { + public: + explicit PlatformProfilingListener(Interpreter* interpreter) { + TFLITE_BENCHMARK_CHECK(interpreter); + platform_profiler_ = profiling::CreatePlatformProfiler(); + interpreter->SetProfiler(platform_profiler_.get()); + } + + private: + std::unique_ptr platform_profiler_; +}; + // Dumps ruy profiling events if the ruy profiler is enabled. class RuyProfileListener : public BenchmarkListener { public: @@ -261,6 +276,8 @@ BenchmarkParams BenchmarkTfLiteModel::DefaultParams() { BenchmarkParam::Create("")); default_params.AddParam("max_delegated_partitions", BenchmarkParam::Create(0)); + default_params.AddParam("enable_platform_tracing", + BenchmarkParam::Create(false)); for (const auto& delegate_util : GetRegisteredDelegateProviders()) { delegate_util->AddParams(&default_params); @@ -313,7 +330,10 @@ std::vector BenchmarkTfLiteModel::GetFlags() { "File path to export profile data as CSV, if not set " "prints to stdout."), CreateFlag("max_delegated_partitions", ¶ms_, - "Max partitions to be delegated.")}; + "Max partitions to be delegated."), + CreateFlag("enable_platform_tracing", ¶ms_, + "enable platform-wide tracing, only meaningful when " + "--enable_op_profiling is set to true.")}; flags.insert(flags.end(), specific_flags.begin(), specific_flags.end()); @@ -356,6 +376,8 @@ void BenchmarkTfLiteModel::LogParams() { << "]"; TFLITE_LOG(INFO) << "Max number of delegated partitions : [" << params_.Get("max_delegated_partitions") << "]"; + TFLITE_LOG(INFO) << "Enable platform-wide tracing: [" + << params_.Get("enable_platform_tracing") << "]"; for (const auto& delegate_util : GetRegisteredDelegateProviders()) { delegate_util->LogParams(params_); @@ -684,6 +706,12 @@ std::unique_ptr BenchmarkTfLiteModel::GetOpResolver() std::unique_ptr BenchmarkTfLiteModel::MayCreateProfilingListener() const { if (!params_.Get("enable_op_profiling")) return nullptr; + + if (params_.Get("enable_platform_tracing")) { + return std::unique_ptr( + new PlatformProfilingListener(interpreter_.get())); + } + return std::unique_ptr(new ProfilingListener( interpreter_.get(), params_.Get("max_profiling_buffer_entries"), params_.Get("profiling_output_csv_file"),