Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an InitializeTarget function that can be specialized for a given target. #47077

Merged
merged 1 commit into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions tensorflow/lite/micro/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,17 @@ cc_library(
],
)

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

cc_test(
name = "micro_error_reporter_test",
srcs = [
Expand Down
33 changes: 8 additions & 25 deletions tensorflow/lite/micro/arduino/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 2021 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 @@ -12,27 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

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

#include "Arduino.h"

// The Arduino DUE uses a different object for the default serial port shown in
// the monitor than most other models, so make sure we pick the right one. See
// https://github.com/arduino/Arduino/issues/3088#issuecomment-406655244
#if defined(__SAM3X8E__)
#define DEBUG_SERIAL_OBJECT (SerialUSB)
#else
#define DEBUG_SERIAL_OBJECT (Serial)
#endif

// On Arduino platforms, we set up a serial port and write to it for debug
// logging.
extern "C" void DebugLog(const char* s) {
static bool is_initialized = false;
if (!is_initialized) {
DEBUG_SERIAL_OBJECT.begin(9600);
is_initialized = true;
}
DEBUG_SERIAL_OBJECT.print(s);
}
// This file is empty to ensure that a specialized implementation of
// debug_log.h is used (instead of the default implementation from
// tensorflow/lite/micro/debug_log.cc).
//
// The actual target-specific implementation of debug_log.h is in
// system_setup.cc since that allows us to consolidate all the target-specific
// specializations into one source file.
36 changes: 36 additions & 0 deletions tensorflow/lite/micro/arduino/system_setup.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* Copyright 2021 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.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

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

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

// The Arduino DUE uses a different object for the default serial port shown in
// the monitor than most other models, so make sure we pick the right one. See
// https://github.com/arduino/Arduino/issues/3088#issuecomment-406655244
#if defined(__SAM3X8E__)
#define DEBUG_SERIAL_OBJECT (SerialUSB)
#else
#define DEBUG_SERIAL_OBJECT (Serial)
#endif

extern "C" void DebugLog(const char* s) { DEBUG_SERIAL_OBJECT.print(s); }

namespace tflite {

void InitializeTarget() { DEBUG_SERIAL_OBJECT.begin(9600); }

} // namespace tflite
2 changes: 2 additions & 0 deletions tensorflow/lite/micro/benchmarks/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/micro/kernels:fully_connected",
],
)
Expand All @@ -63,6 +64,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:micro_utils",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/micro/examples/person_detection:model_settings",
"//tensorflow/lite/micro/examples/person_detection:person_detect_model_data",
"//tensorflow/lite/micro/examples/person_detection:simple_images_test_data",
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/lite/micro/benchmarks/keyword_benchmark.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/micro_profiler.h"
#include "tensorflow/lite/micro/system_setup.h"

/*
* Keyword Spotting Benchmark for performance optimizations. The model used in
Expand Down Expand Up @@ -77,6 +78,7 @@ void KeywordRunNIerations(int iterations, const char* tag,
} // namespace tflite

int main(int argc, char** argv) {
tflite::InitializeTarget();
tflite::MicroProfiler profiler;

uint32_t event_handle = profiler.BeginEvent("InitializeKeywordRunner");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_utils.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

/*
Expand Down Expand Up @@ -74,6 +75,8 @@ void PersonDetectionNIerations(const int8_t* input, int iterations,
} // namespace tflite

int main(int argc, char** argv) {
tflite::InitializeTarget();

tflite::MicroProfiler profiler;

uint32_t event_handle = profiler.BeginEvent("InitializeBenchmarkRunner");
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/micro/examples/hello_world/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/schema:schema_fbs",
],
)
Expand Down
3 changes: 3 additions & 0 deletions tensorflow/lite/micro/examples/hello_world/main_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ limitations under the License.
#include "tensorflow/lite/micro/examples/hello_world/output_handler.h"
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Globals, used for compatibility with Arduino-style sketches.
Expand All @@ -38,6 +39,8 @@ uint8_t tensor_arena[kTensorArenaSize];

// The name of this function is important for Arduino compatibility.
void setup() {
tflite::InitializeTarget();

// Set up logging. Google style is to avoid globals or statics because of
// lifetime uncertainty, but since this has a trivial destructor it's okay.
// NOLINTNEXTLINE(runtime-global-variables)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

#define NUM_OUT_CH 3
Expand All @@ -34,6 +35,7 @@ static const char* labels[] = {"Plane", "Car", "Bird", "Cat", "Deer",
"Dog", "Frog", "Horse", "Ship", "Truck"};

int main(int argc, char** argv) {
tflite::InitializeTarget();
init_lcd();
wait_ms(100);

Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/micro/examples/magic_wand/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/schema:schema_fbs",
],
)
3 changes: 3 additions & 0 deletions tensorflow/lite/micro/examples/magic_wand/main_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Globals, used for compatibility with Arduino-style sketches.
Expand All @@ -42,6 +43,8 @@ uint8_t tensor_arena[kTensorArenaSize];

// The name of this function is important for Arduino compatibility.
void setup() {
tflite::InitializeTarget();

// Set up logging. Google style is to avoid globals or statics because of
// lifetime uncertainty, but since this has a trivial destructor it's okay.
static tflite::MicroErrorReporter micro_error_reporter; // NOLINT
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/lite/micro/examples/micro_speech/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/micro/examples/micro_speech/micro_features:micro_model_settings",
"//tensorflow/lite/micro/examples/micro_speech/micro_features:model",
"//tensorflow/lite/schema:schema_fbs",
Expand All @@ -383,6 +384,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/micro/examples/micro_speech/micro_features:micro_model_settings",
"//tensorflow/lite/micro/examples/micro_speech/micro_features:model",
"//tensorflow/lite/schema:schema_fbs",
Expand Down
3 changes: 3 additions & 0 deletions tensorflow/lite/micro/examples/micro_speech/main_functions.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Globals, used for compatibility with Arduino-style sketches.
Expand All @@ -47,6 +48,8 @@ int8_t* model_input_buffer = nullptr;

// The name of this function is important for Arduino compatibility.
void setup() {
tflite::InitializeTarget();

// Set up logging. Google style is to avoid globals or statics because of
// lifetime uncertainty, but since this has a trivial destructor it's okay.
// NOLINTNEXTLINE(runtime-global-variables)
Expand Down
1 change: 1 addition & 0 deletions tensorflow/lite/micro/examples/person_detection/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ cc_binary(
"//tensorflow/lite/micro:micro_error_reporter",
"//tensorflow/lite/micro:micro_framework",
"//tensorflow/lite/micro:op_resolvers",
"//tensorflow/lite/micro:system_setup",
"//tensorflow/lite/schema:schema_fbs",
],
)
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ limitations under the License.
#include "tensorflow/lite/micro/micro_error_reporter.h"
#include "tensorflow/lite/micro/micro_interpreter.h"
#include "tensorflow/lite/micro/micro_mutable_op_resolver.h"
#include "tensorflow/lite/micro/system_setup.h"
#include "tensorflow/lite/schema/schema_generated.h"

// Globals, used for compatibility with Arduino-style sketches.
Expand All @@ -45,6 +46,8 @@ static uint8_t tensor_arena[kTensorArenaSize];

// The name of this function is important for Arduino compatibility.
void setup() {
tflite::InitializeTarget();

// Set up logging. Google style is to avoid globals or statics because of
// lifetime uncertainty, but since this has a trivial destructor it's okay.
// NOLINTNEXTLINE(runtime-global-variables)
Expand Down
30 changes: 8 additions & 22 deletions tensorflow/lite/micro/sparkfun_edge/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 2021 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 @@ -12,24 +12,10 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

// Implementation for the DebugLog() function that prints to the UART on the
// SparkFun Edge microcontroller. The same should work for other targets using
// the Ambiq Apollo 3.

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

#include "am_bsp.h" // NOLINT
#include "am_util.h" // NOLINT

extern "C" void DebugLog(const char* s) {
#ifndef TF_LITE_STRIP_ERROR_STRINGS
static bool is_initialized = false;
if (!is_initialized) {
am_bsp_uart_printf_enable();
is_initialized = true;
}

am_util_stdio_printf("%s", s);
#endif
}
// This file is empty to ensure that a specialized implementation of
// debug_log.h is used (instead of the default implementation from
// tensorflow/lite/micro/debug_log.cc).
//
// The actual target-specific implementation of debug_log.h is in
// system_setup.cc since that allows us to consolidate all the target-specific
// specializations into one source file.
Loading