Skip to content

Commit

Permalink
lite: Add VERBOSE in LogSeverity and add SetMinimumLogSeverity/GetMin…
Browse files Browse the repository at this point in the history
…imumLogSeverity in the minimal logger. Log only when the severity is larger or equal to the minimum log severity.

PiperOrigin-RevId: 455663649
  • Loading branch information
yishuangP authored and tensorflower-gardener committed Jun 17, 2022
1 parent 2a2c293 commit 21368c6
Show file tree
Hide file tree
Showing 11 changed files with 24 additions and 170 deletions.
9 changes: 1 addition & 8 deletions tensorflow/lite/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ FRAMEWORK_LIB_HDRS = STABLE_FRAMEWORK_LIB_HDRS + [
"core/subgraph.h",
"graph_info.h",
"interpreter_options.h",
"logger.h",
"optional_debug_tools.h",
"signature_runner.h",
]
Expand Down Expand Up @@ -506,14 +505,12 @@ cc_library(
"core/subgraph.h",
"interpreter.cc",
"interpreter_builder.cc",
"logger.cc",
"signature_runner.h",
],
hdrs = [
"interpreter.h",
"interpreter_builder.h",
"interpreter_options.h",
"logger.h",
"model.h",
"model_builder.h",
],
Expand Down Expand Up @@ -578,7 +575,6 @@ cc_library(
"interpreter.h",
"interpreter_builder.h",
"interpreter_options.h",
"logger.h",
"model.h",
"model_builder.h",
"signature_runner.h",
Expand Down Expand Up @@ -1165,10 +1161,7 @@ cc_library(
"minimal_logging_default.cc",
],
}),
hdrs = [
"logger.h",
"minimal_logging.h",
],
hdrs = ["minimal_logging.h"],
compatible_with = get_compatible_with_portable(),
copts = tflite_copts_warnings() + tflite_copts(),
linkopts = select({
Expand Down
17 changes: 14 additions & 3 deletions tensorflow/lite/core/subgraph.cc
Original file line number Diff line number Diff line change
Expand Up @@ -411,15 +411,26 @@ TfLiteStatus Subgraph::ReplaceNodeSubsetsWithDelegateKernels(
PartitionGraphIntoIndependentNodeSubsets(&info, nodes_to_replace,
&node_subsets);

#ifdef __ANDROID__
// On Android the log message below is used for diagnosing delegation success
// also in production builds. Use VERBOSE here so that the logging is turned
// off in production builds on other platforms.
// also in production builds. Delegation happens sufficiently rarely that the
// message isn't spammy.
TFLITE_LOG_PROD(
tflite::TFLITE_LOG_VERBOSE,
tflite::TFLITE_LOG_INFO,
"Replacing %d node(s) with delegate (%s) node, yielding %zu partitions.",
nodes_to_replace->size,
registration.custom_name ? registration.custom_name : "unknown",
node_subsets.size());
#else // !__ANDROID__
// Server-side, delegation may happen so often as to make logging spammy + we
// don't have a clear need for the diagnostic in production builds.
TFLITE_LOG(
tflite::TFLITE_LOG_INFO,
"Replacing %d node(s) with delegate (%s) node, yielding %zu partitions.",
nodes_to_replace->size,
registration.custom_name ? registration.custom_name : "unknown",
node_subsets.size());
#endif // __ANDROID__

execution_plan_.clear();

Expand Down
31 changes: 0 additions & 31 deletions tensorflow/lite/logger.cc

This file was deleted.

50 changes: 0 additions & 50 deletions tensorflow/lite/logger.h

This file was deleted.

12 changes: 0 additions & 12 deletions tensorflow/lite/minimal_logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ void MinimalLogger::Log(LogSeverity severity, const char* format, ...) {

const char* MinimalLogger::GetSeverityName(LogSeverity severity) {
switch (severity) {
case TFLITE_LOG_VERBOSE:
return "VERBOSE";
case TFLITE_LOG_INFO:
return "INFO";
case TFLITE_LOG_WARNING:
Expand All @@ -41,15 +39,5 @@ const char* MinimalLogger::GetSeverityName(LogSeverity severity) {
return "<Unknown severity>";
}

LogSeverity MinimalLogger::GetMinimumLogSeverity() {
return MinimalLogger::minimum_log_severity_;
}

LogSeverity MinimalLogger::SetMinimumLogSeverity(LogSeverity new_severity) {
LogSeverity old_severity = MinimalLogger::minimum_log_severity_;
MinimalLogger::minimum_log_severity_ = new_severity;
return old_severity;
}

} // namespace logging_internal
} // namespace tflite
27 changes: 8 additions & 19 deletions tensorflow/lite/minimal_logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.

#include <cstdarg>

#include "tensorflow/lite/logger.h"

namespace tflite {

namespace logging_internal {
enum LogSeverity {
TFLITE_LOG_INFO = 0,
TFLITE_LOG_WARNING = 1,
TFLITE_LOG_ERROR = 2,
};

using tflite::LogSeverity;
namespace logging_internal {

// Helper class for simple platform-specific console logging. Note that we
// explicitly avoid the convenience of ostream-style logging to minimize binary
Expand All @@ -37,17 +39,8 @@ class MinimalLogger {
static void LogFormatted(LogSeverity severity, const char* format,
va_list args);

// Get the minimum severity level for logging. Default is INFO in prod builds
// and VERBOSE in debug builds.
// Note: Default is always VERBOSE on Android.
static LogSeverity GetMinimumLogSeverity();

// Set the minimum severity level for logging, returning the old severity.
static LogSeverity SetMinimumLogSeverity(LogSeverity new_severity);

private:
static const char* GetSeverityName(LogSeverity severity);
static LogSeverity minimum_log_severity_;
};

} // namespace logging_internal
Expand All @@ -58,12 +51,8 @@ class MinimalLogger {
// stripped in release optimized builds. In general, prefer the error reporting
// APIs for developer-facing errors, and only use this for diagnostic output
// that should always be logged in user builds.
#define TFLITE_LOG_PROD(severity, format, ...) \
if (severity >= \
tflite::logging_internal::MinimalLogger::GetMinimumLogSeverity()) { \
tflite::logging_internal::MinimalLogger::Log(severity, format, \
##__VA_ARGS__); \
}
#define TFLITE_LOG_PROD(severity, format, ...) \
tflite::logging_internal::MinimalLogger::Log(severity, format, ##__VA_ARGS__);

// Convenience macro for logging a statement *once* for a given process lifetime
// in production builds.
Expand Down
3 changes: 0 additions & 3 deletions tensorflow/lite/minimal_logging_android.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ namespace {

int GetPlatformSeverity(LogSeverity severity) {
switch (severity) {
case TFLITE_LOG_VERBOSE:
case TFLITE_LOG_INFO:
return ANDROID_LOG_INFO;
case TFLITE_LOG_WARNING:
Expand All @@ -39,8 +38,6 @@ int GetPlatformSeverity(LogSeverity severity) {

} // namespace

LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_VERBOSE;

void MinimalLogger::LogFormatted(LogSeverity severity, const char* format,
va_list args) {
// First log to Android's explicit log(cat) API.
Expand Down
8 changes: 0 additions & 8 deletions tensorflow/lite/minimal_logging_default.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,6 @@ limitations under the License.
namespace tflite {
namespace logging_internal {

#ifndef NDEBUG
// In debug builds, default is VERBOSE.
LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_VERBOSE;
#else
// In prod builds, default is INFO.
LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_INFO;
#endif

void MinimalLogger::LogFormatted(LogSeverity severity, const char* format,
va_list args) {
fprintf(stderr, "%s: ", GetSeverityName(severity));
Expand Down
9 changes: 0 additions & 9 deletions tensorflow/lite/minimal_logging_ios.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ namespace {

int GetPlatformSeverity(LogSeverity severity) {
switch (severity) {
case TFLITE_LOG_VERBOSE:
case TFLITE_LOG_INFO:
return LOG_INFO;
case TFLITE_LOG_WARNING:
Expand All @@ -40,14 +39,6 @@ int GetPlatformSeverity(LogSeverity severity) {

} // namespace

#ifndef NDEBUG
// In debug builds, default is VERBOSE.
LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_VERBOSE;
#else
// In prod builds, default is INFO.
LogSeverity MinimalLogger::minimum_log_severity_ = TFLITE_LOG_INFO;
#endif

void MinimalLogger::LogFormatted(LogSeverity severity, const char* format,
va_list args) {
// First log to iOS system logging API.
Expand Down
27 changes: 0 additions & 27 deletions tensorflow/lite/minimal_logging_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ limitations under the License.
#include "tensorflow/lite/minimal_logging.h"

#include <gtest/gtest.h>
#include "tensorflow/lite/logger.h"

namespace tflite {

Expand Down Expand Up @@ -46,35 +45,9 @@ TEST(MinimalLogging, Error) {

TEST(MinimalLogging, UnknownSeverity) {
testing::internal::CaptureStderr();
LogSeverity default_log_severity = TFLITE_LOG_INFO;
#if defined(__ANDROID__) || !defined(NDEBUG)
default_log_severity = TFLITE_LOG_VERBOSE;
#endif
EXPECT_EQ(tflite::logging_internal::MinimalLogger::SetMinimumLogSeverity(
static_cast<LogSeverity>(-1)),
default_log_severity);
TFLITE_LOG_PROD(static_cast<LogSeverity>(-1), "Three");
EXPECT_EQ("<Unknown severity>: Three\n",
testing::internal::GetCapturedStderr());
tflite::logging_internal::MinimalLogger::SetMinimumLogSeverity(
default_log_severity);
}

TEST(MinimalLogging, MinimumSeverity) {
testing::internal::CaptureStderr();
LogSeverity default_log_severity = TFLITE_LOG_INFO;
#if defined(__ANDROID__) || !defined(NDEBUG)
default_log_severity = TFLITE_LOG_VERBOSE;
#endif

EXPECT_EQ(tflite::logging_internal::MinimalLogger::SetMinimumLogSeverity(
TFLITE_LOG_WARNING),
default_log_severity);
TFLITE_LOG_PROD(TFLITE_LOG_WARNING, "Foo");
TFLITE_LOG_PROD(default_log_severity, "Bar");
EXPECT_EQ("WARNING: Foo\n", testing::internal::GetCapturedStderr());
tflite::logging_internal::MinimalLogger::SetMinimumLogSeverity(
default_log_severity);
}

TEST(MinimalLogging, Once) {
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/nnapi/sl/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ cc_library(
compatible_with = get_compatible_with_portable(),
visibility = _DEFAULT_VISIBILITY + nnapi_sl_visibility_allowlist(),
deps = [
"//tensorflow/lite:minimal_logging",
"//tensorflow/lite/kernels/internal:compatibility",
"//tensorflow/lite/nnapi:nnapi_lib",
],
Expand Down

0 comments on commit 21368c6

Please sign in to comment.