Force enabling TFLite tracing with TFLITE_ENABLE_DEFAULT_PROFILER define.

Manual setting such as debug.tflite.trace Android system property is not needed when compiled with this option.

PiperOrigin-RevId: 329398936
Change-Id: I7bcf62bb32676a5c206d025ffa5d668d93f38548
This commit is contained in:
Juho Ha 2020-08-31 16:13:36 -07:00 committed by TensorFlower Gardener
parent 78766489df
commit 3a3f914539
4 changed files with 21 additions and 10 deletions

View File

@ -29,10 +29,6 @@ limitations under the License.
#include "tensorflow/lite/util.h"
#include "tensorflow/lite/version.h"
#if defined(TFLITE_ENABLE_DEFAULT_PROFILER)
#include "tensorflow/lite/profiling/platform_profiler.h"
#endif
namespace tflite {
namespace {

View File

@ -93,6 +93,9 @@ class ATraceProfiler : public tflite::Profiler {
};
std::unique_ptr<tflite::Profiler> MaybeCreateATraceProfiler() {
#if defined(TFLITE_ENABLE_DEFAULT_PROFILER)
return std::unique_ptr<tflite::Profiler>(new ATraceProfiler());
#else // TFLITE_ENABLE_DEFAULT_PROFILER
#if defined(__ANDROID__)
constexpr char kTraceProp[] = "debug.tflite.trace";
char trace_enabled[PROP_VALUE_MAX] = "";
@ -102,6 +105,7 @@ std::unique_ptr<tflite::Profiler> MaybeCreateATraceProfiler() {
}
#endif // __ANDROID__
return nullptr;
#endif // TFLITE_ENABLE_DEFAULT_PROFILER
}
} // namespace profiling

View File

@ -22,6 +22,9 @@ limitations under the License.
namespace tflite {
namespace profiling {
// Creates a profiler which reports the traced events to the Android ATrace.
// Nullptr will be returned if the Android system property 'debug.tflite.trace'
// is not set or the property value is not 1.
std::unique_ptr<tflite::Profiler> MaybeCreateATraceProfiler();
} // namespace profiling

View File

@ -27,18 +27,26 @@ namespace profiling {
namespace {
TEST(ATraceProfilerTest, MaybeCreateATraceProfiler) {
auto default_profiler = MaybeCreateATraceProfiler();
EXPECT_EQ(nullptr, default_profiler.get());
auto initial_state_profiler = MaybeCreateATraceProfiler();
#if !defined(TFLITE_ENABLE_DEFAULT_PROFILER)
EXPECT_EQ(nullptr, initial_state_profiler.get());
#else
EXPECT_NE(nullptr, initial_state_profiler.get());
#endif
#if defined(__ANDROID__)
if (__system_property_set("debug.tflite.trace", "1") == 0) {
auto profiler = MaybeCreateATraceProfiler();
EXPECT_NE(nullptr, profiler.get());
auto on_state_profiler = MaybeCreateATraceProfiler();
EXPECT_NE(nullptr, on_state_profiler.get());
}
if (__system_property_set("debug.tflite.trace", "0") == 0) {
auto no_profiler = MaybeCreateATraceProfiler();
EXPECT_EQ(nullptr, no_profiler.get());
auto off_state_profiler = MaybeCreateATraceProfiler();
#if !defined(TFLITE_ENABLE_DEFAULT_PROFILER)
EXPECT_EQ(nullptr, off_state_profiler.get());
#else
EXPECT_NE(nullptr, off_state_profiler.get());
#endif
}
#endif // __ANDROID__
}