Only prod log interpreter creation on mobile platforms

PiperOrigin-RevId: 263674744
This commit is contained in:
Jared Duke 2019-08-15 17:11:51 -07:00 committed by TensorFlower Gardener
parent 4225c4f697
commit 4735b9bc02
4 changed files with 41 additions and 1 deletions

View File

@ -29,6 +29,20 @@ limitations under the License.
#include "tensorflow/lite/schema/schema_generated.h"
#include "tensorflow/lite/util.h"
// TODO(b/139446230): Move to portable platform header.
#if defined(__ANDROID__)
#define TFLITE_IS_MOBILE_PLATFORM
#endif // defined(__ANDROID__)
#if defined(__APPLE__)
#include "TargetConditionals.h"
#if TARGET_IPHONE_SIMULATOR
#define TFLITE_IS_MOBILE_PLATFORM
#elif TARGET_OS_IPHONE
#define TFLITE_IS_MOBILE_PLATFORM
#endif
#endif // defined(__APPLE__)
// TODO(b/132087118): move static_assert to c_api_internal when compiled with
// C++.
static_assert(sizeof(TfLiteFloat16) == sizeof(uint16_t),
@ -60,7 +74,13 @@ Interpreter::Interpreter(ErrorReporter* error_reporter)
: error_reporter_(error_reporter ? error_reporter
: DefaultErrorReporter()) {
// TODO(b/128420794): Include the TFLite runtime version in the log.
// Prod logging is useful for mobile platforms where scraping console logs is
// critical for debugging.
#if defined(TFLITE_IS_MOBILE_PLATFORM)
TFLITE_LOG_PROD_ONCE(TFLITE_LOG_INFO, "Initialized TensorFlow Lite runtime.");
#else
TFLITE_LOG_ONCE(TFLITE_LOG_INFO, "Initialized TensorFlow Lite runtime.");
#endif
// There's always at least 1 subgraph which is the primary subgraph.
AddSubgraphs(1);

View File

@ -65,8 +65,14 @@ TEST(BasicInterpreter, ZeroInterpreter) {
testing::internal::CaptureStderr();
Interpreter interpreter;
#ifndef NDEBUG
const char* kExpectedLog = "INFO: Initialized TensorFlow Lite runtime";
#else
const char* kExpectedLog = "";
#endif
EXPECT_THAT(testing::internal::GetCapturedStderr(),
testing::HasSubstr("INFO: Initialized TensorFlow Lite runtime"));
testing::HasSubstr(kExpectedLog));
interpreter.SetInputs({});
interpreter.SetOutputs({});

View File

@ -68,12 +68,14 @@ class MinimalLogger {
#ifndef NDEBUG
// In debug builds, always log.
#define TFLITE_LOG TFLITE_LOG_PROD
#define TFLITE_LOG_ONCE TFLITE_LOG_PROD_ONCE
#else
// In prod builds, never log, but ensure the code is well-formed and compiles.
#define TFLITE_LOG(severity, format, ...) \
while (false) { \
TFLITE_LOG_PROD(severity, format, ##__VA_ARGS__); \
}
#define TFLITE_LOG_ONCE TFLITE_LOG
#endif
#endif // TENSORFLOW_LITE_MINIMAL_LOGGING_H_

View File

@ -73,6 +73,18 @@ TEST(MinimalLogging, Debug) {
#endif
}
TEST(MinimalLogging, DebugOnce) {
testing::internal::CaptureStderr();
for (int i = 0; i < 10; ++i) {
TFLITE_LOG_ONCE(TFLITE_LOG_INFO, "Count: %d", i);
}
#ifndef NDEBUG
EXPECT_EQ("INFO: Count: 0\n", testing::internal::GetCapturedStderr());
#else
EXPECT_TRUE(testing::internal::GetCapturedStderr().empty());
#endif
}
} // namespace tflite
int main(int argc, char** argv) {