Skip to content

Commit

Permalink
DebugLog changes (#2073)
Browse files Browse the repository at this point in the history
@tensorflow/micro

Change DebugLog to allow stdio style formatted output using either <cstdio> or a third party printf library.

Remove micro_string code.

bug=fixes #2083
  • Loading branch information
ddavis-2015 authored Jul 6, 2023
1 parent 7837d79 commit ef0a7b0
Show file tree
Hide file tree
Showing 25 changed files with 260 additions and 613 deletions.
7 changes: 3 additions & 4 deletions tensorflow/lite/kernels/op_macros.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,13 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_KERNELS_OP_MACROS_H_
#define TENSORFLOW_LITE_KERNELS_OP_MACROS_H_

#include "tensorflow/lite/micro/debug_log.h"

#if !defined(TF_LITE_MCU_DEBUG_LOG)
#include <cstdlib>
#define TFLITE_ABORT abort()
#else
#include "tensorflow/lite/micro/micro_log.h"
inline void AbortImpl() {
DebugLog("HALTED\n");
MicroPrintf("HALTED");
while (1) {
}
}
Expand Down
23 changes: 0 additions & 23 deletions tensorflow/lite/micro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,6 @@ cc_library(
copts = micro_copts(),
deps = [
":debug_log",
":micro_string",
],
)

Expand All @@ -291,17 +290,6 @@ cc_library(
],
)

cc_library(
name = "micro_string",
srcs = [
"micro_string.cc",
],
hdrs = [
"micro_string.h",
],
copts = micro_copts(),
)

cc_library(
name = "micro_time",
srcs = [
Expand Down Expand Up @@ -549,17 +537,6 @@ cc_test(
],
)

cc_test(
name = "micro_string_test",
srcs = [
"micro_string_test.cc",
],
deps = [
":micro_string",
"//tensorflow/lite/micro/testing:micro_test",
],
)

cc_test(
name = "micro_time_test",
srcs = [
Expand Down
26 changes: 20 additions & 6 deletions tensorflow/lite/micro/arc_emsdp/debug_log.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -19,6 +19,12 @@ limitations under the License.
#include <cstdio>
#include <cstring>

#ifndef TF_LITE_STRIP_ERROR_STRINGS
#include "eyalroz_printf/src/printf/printf.h"
#endif

namespace {

// Print to debug console by default. One can define next to extend destinations
// set: EMSDP_LOG_TO_MEMORY
// : fill .debug_log memory region (data section) with passed chars.
Expand Down Expand Up @@ -89,9 +95,7 @@ void LogToMem(const char* s) {
debug_log_mem[cursor] = '^';
}

extern "C" void DebugLog(const char* s) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS

void LogDebugString(const char* s) {
#if defined EMSDP_LOG_TO_UART
DbgUartSendStr(s);
#endif
Expand All @@ -106,6 +110,16 @@ extern "C" void DebugLog(const char* s) {
#warning "EMSDP_LOG_TO_HOST is defined. Ensure hostlib is linked."
fprintf(stderr, "%s", s);
#endif

#endif // TF_LITE_STRIP_ERROR_STRINGS
}

} // namespace

extern "C" void DebugLog(const char* format, va_list args) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
constexpr int kMaxLogLen = 256;
char log_buffer[kMaxLogLen];

vsnprintf_(log_buffer, kMaxLogLen, format, args);
LogDebugString(log_buffer);
#endif
}
24 changes: 22 additions & 2 deletions tensorflow/lite/micro/bluepill/debug_log.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,13 +15,33 @@ limitations under the License.

#include "tensorflow/lite/micro/debug_log.h"

#ifndef TF_LITE_STRIP_ERROR_STRINGS
#include "eyalroz_printf/src/printf/printf.h"
#endif

namespace {

#ifndef TF_LITE_STRIP_ERROR_STRINGS
// For Arm Cortex-M devices, calling SYS_WRITE0 will output the zero-terminated
// string pointed to by R1 to any debug console that's attached to the system.
extern "C" void DebugLog(const char* s) {
void SysWriteDebugConsole(const char* s) {
asm("mov r0, #0x04\n" // SYS_WRITE0
"mov r1, %[str]\n"
"bkpt #0xAB\n"
:
: [str] "r"(s)
: "r0", "r1");
}
#endif // TF_LITE_STRIP_ERROR_STRINGS

} // namespace

extern "C" void DebugLog(const char* format, va_list args) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
constexpr int kMaxLogLen = 256;
char log_buffer[kMaxLogLen];

vsnprintf_(log_buffer, kMaxLogLen, format, args);
SysWriteDebugConsole(log_buffer);
#endif // TF_LITE_STRIP_ERROR_STRINGS
}
18 changes: 14 additions & 4 deletions tensorflow/lite/micro/chre/debug_log.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,16 @@ limitations under the License.

#include <chre.h>

extern "C" void DebugLog(const char* s) {
chreLog(CHRE_LOG_DEBUG, "[TFL_MICRO] %s", s);
}
#ifndef TF_LITE_STRIP_ERROR_STRINGS
#include "eyalroz_printf/src/printf/printf.h"
#endif

extern "C" void DebugLog(const char* format, va_list args) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
constexpr int kMaxLogLen = 256;
char log_buffer[kMaxLogLen];

vsnprintf_(log_buffer, kMaxLogLen, format, args);
chreLog(CHRE_LOG_DEBUG, "[TFL_MICRO] %s", log_buffer);
#endif
}
26 changes: 21 additions & 5 deletions tensorflow/lite/micro/cortex_m_generic/debug_log.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,17 +24,33 @@ extern "C" {

#include "tensorflow/lite/micro/cortex_m_generic/debug_log_callback.h"

#ifndef TF_LITE_STRIP_ERROR_STRINGS
#include "eyalroz_printf/src/printf/printf.h"
#endif

static DebugLogCallback debug_log_callback = nullptr;

namespace {

void InvokeDebugLogCallback(const char* s) {
if (debug_log_callback != nullptr) {
debug_log_callback(s);
}
}

} // namespace

void RegisterDebugLogCallback(void (*cb)(const char* s)) {
debug_log_callback = cb;
}

void DebugLog(const char* s) {
void DebugLog(const char* format, va_list args) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
if (debug_log_callback != nullptr) {
debug_log_callback(s);
}
constexpr int kMaxLogLen = 256;
char log_buffer[kMaxLogLen];

vsnprintf_(log_buffer, kMaxLogLen, format, args);
InvokeDebugLogCallback(log_buffer);
#endif
}

Expand Down
32 changes: 14 additions & 18 deletions tensorflow/lite/micro/debug_log.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -18,33 +18,29 @@ limitations under the License.
// the only function that's absolutely required to be available on a target
// device, since it's used for communicating test results back to the host so
// that we can verify the implementation is working correctly.
// It's designed to be as easy as possible to supply an implementation though.
// On platforms that have a POSIX stack or C library, it can be written as a
// single call to `fprintf(stderr, "%s", s)` to output a string to the error
// stream of the console, but if there's no OS or C library available, there's
// almost always an equivalent way to write out a string to some serial
// interface that can be used instead. For example on Arm M-series MCUs, calling
// the `bkpt #0xAB` assembler instruction will output the string in r1 to
// whatever debug serial connection is available. If you're running mbed, you
// can do the same by creating `Serial pc(USBTX, USBRX)` and then calling
// `pc.printf("%s", s)`.
// To add an equivalent function for your own platform, create your own
// implementation file, and place it in a subfolder with named after the OS
// you're targeting. For example, see the Cortex M bare metal version in
// tensorflow/lite/micro/bluepill/debug_log.cc or the mbed one on
// tensorflow/lite/micro/mbed/debug_log.cc.
// This function should support standard C/C++ stdio style formatting
// operations. It's designed to be as easy as possible to supply an
// implementation though. On platforms that have a POSIX stack or C library, it
// can be written as a single call to `vfprintf(stderr, format, args)` to output
// a string to the error stream of the console, but if there's no OS or C
// library available, there's almost always an equivalent way to write out a
// string to some serial interface that can be used instead. To add an
// equivalent function for your own platform, create your own implementation
// file, and place it in a subfolder with named after the OS you're targeting.
// For example, see the Cortex M bare metal version in the
// tensorflow/lite/micro/bluepill/debug_log.cc file.

#include "tensorflow/lite/micro/debug_log.h"

#ifndef TF_LITE_STRIP_ERROR_STRINGS
#include <cstdio>
#endif

extern "C" void DebugLog(const char* s) {
extern "C" void DebugLog(const char* format, va_list args) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
// Reusing TF_LITE_STRIP_ERROR_STRINGS to disable DebugLog completely to get
// maximum reduction in binary size. This is because we have DebugLog calls
// via TF_LITE_CHECK that are not stubbed out by TF_LITE_REPORT_ERROR.
fprintf(stderr, "%s", s);
vfprintf(stderr, format, args);
#endif
}
13 changes: 10 additions & 3 deletions tensorflow/lite/micro/debug_log.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,21 @@ limitations under the License.
#ifndef TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_
#define TENSORFLOW_LITE_MICRO_DEBUG_LOG_H_

#ifdef __cplusplus
#include <cstdarg>
#else
#include <stdarg.h>
#endif // __cplusplus

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

// This function should be implemented by each target platform, and provide a
// way for strings to be output to some text stream. For more information, see
// tensorflow/lite/micro/debug_log.cc.
void DebugLog(const char* s);
// the tensorflow/lite/micro/debug_log.cc file. This function should support
// standard C/C++ stdio style formatting operations.
void DebugLog(const char* format, va_list args);

#ifdef __cplusplus
} // extern "C"
Expand Down
1 change: 0 additions & 1 deletion tensorflow/lite/micro/memory_planner/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ cc_library(
":micro_memory_planner",
"//tensorflow/lite/micro:micro_compatibility",
"//tensorflow/lite/micro:micro_log",
"//tensorflow/lite/micro:micro_string",
],
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2019 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -16,7 +16,6 @@ limitations under the License.
#include "tensorflow/lite/micro/memory_planner/greedy_memory_planner.h"

#include "tensorflow/lite/micro/micro_log.h"
#include "tensorflow/lite/micro/micro_string.h"

namespace tflite {

Expand Down
4 changes: 2 additions & 2 deletions tensorflow/lite/micro/micro_context.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
/* Copyright 2023 The TensorFlow Authors. All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -142,7 +142,7 @@ void MicroContextReportOpError(struct TfLiteContext* context,
const char* format, ...) {
va_list args;
va_start(args, format);
Log(format, args);
VMicroPrintf(format, args);
va_end(args);
}

Expand Down
Loading

0 comments on commit ef0a7b0

Please sign in to comment.