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

Updated CMake for latest Pico-SDK, including Pico2 support (issue #18) #19

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
48 changes: 34 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,31 @@
# Generated Cmake Pico project file

cmake_minimum_required(VERSION 3.12)

# Pull in PICO SDK (must be before project)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

# == DO NEVER EDIT THE NEXT LINES for Raspberry Pi Pico VS Code Extension to work ==
if(WIN32)
set(USERHOME $ENV{USERPROFILE})
else()
set(USERHOME $ENV{HOME})
endif()
set(sdkVersion 2.0.0)
set(toolchainVersion 13_2_Rel1)
set(picotoolVersion 2.0.0)
set(picoVscode ${USERHOME}/.pico-sdk/cmake/pico-vscode.cmake)
if (EXISTS ${picoVscode})
include(${picoVscode})
endif()
# ====================================================================================

set(PICO_BOARD pico2 CACHE STRING "Board type")

# Pull in Raspberry Pi Pico SDK (must be before project)
include(pico_sdk_import.cmake)

project(pico-tflmicro C CXX ASM)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)

pico_sdk_init()

Expand Down Expand Up @@ -35,14 +54,15 @@ target_compile_definitions(
COMPILE_DEFINITIONS TFLITE_USE_CTIME=1
)

set_target_properties(
target_compile_options(
pico-tflmicro
PROPERTIES
COMPILE_FLAGS -Os
COMPILE_FLAGS -fno-rtti
COMPILE_FLAGS -fno-exceptions
COMPILE_FLAGS -fno-threadsafe-statics
COMPILE_FLAGS -nostdlib
PUBLIC
-Os
-fno-exceptions
-nostdlib
$<$<COMPILE_LANGUAGE:CXX>:
-fno-rtti
-fno-threadsafe-statics>
)

target_link_libraries(
Expand Down Expand Up @@ -670,7 +690,7 @@ add_subdirectory("tests/arena_allocator_persistent_arena_buffer_allocator_test")
add_subdirectory("tests/arena_allocator_recording_single_arena_buffer_allocator_test")
add_subdirectory("tests/arena_allocator_single_arena_buffer_allocator_test")
add_subdirectory("tests/fake_micro_context_test")
add_subdirectory("tests/flatbuffer_utils_test")
#add_subdirectory("tests/flatbuffer_utils_test")
add_subdirectory("tests/kernels_activations_test")
add_subdirectory("tests/kernels_add_n_test")
add_subdirectory("tests/kernels_add_test")
Expand All @@ -692,7 +712,7 @@ add_subdirectory("tests/kernels_depthwise_conv_test")
add_subdirectory("tests/kernels_dequantize_test")
add_subdirectory("tests/kernels_detection_postprocess_test")
add_subdirectory("tests/kernels_div_test")
add_subdirectory("tests/kernels_elementwise_test")
#add_subdirectory("tests/kernels_elementwise_test")
add_subdirectory("tests/kernels_elu_test")
add_subdirectory("tests/kernels_embedding_lookup_test")
add_subdirectory("tests/kernels_exp_test")
Expand All @@ -706,8 +726,8 @@ add_subdirectory("tests/kernels_gather_nd_test")
add_subdirectory("tests/kernels_gather_test")
add_subdirectory("tests/kernels_hard_swish_test")
add_subdirectory("tests/kernels_if_test")
add_subdirectory("tests/kernels_l2_pool_2d_test")
add_subdirectory("tests/kernels_l2norm_test")
#add_subdirectory("tests/kernels_l2_pool_2d_test")
#add_subdirectory("tests/kernels_l2norm_test")
add_subdirectory("tests/kernels_leaky_relu_test")
add_subdirectory("tests/kernels_log_softmax_test")
add_subdirectory("tests/kernels_logical_test")
Expand Down
12 changes: 4 additions & 8 deletions examples/hello_world/main_functions.cpp
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, looks like this is a fix for a separate bug from the Pico2 support? I'll dig into this and see where the incorrect code is coming from. If it's upstream, I'll add a patch in the sync process, otherwise I'll pull this part of the PR in directly. Thanks for the update!

Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ void loop() {
static_cast<float>(kInferencesPerCycle);
float x = position * kXrange;

// Quantize the input from floating-point to integer
int8_t x_quantized = x / input->params.scale + input->params.zero_point;
// Place the quantized input in the model's input tensor
input->data.int8[0] = x_quantized;
// Obtain the input from floating-point to integer
input->data.f[0] = x;

// Run inference, and report any error
TfLiteStatus invoke_status = interpreter->Invoke();
Expand All @@ -101,10 +99,8 @@ void loop() {
return;
}

// Obtain the quantized output from model's output tensor
int8_t y_quantized = output->data.int8[0];
// Dequantize the output from integer to floating-point
float y = (y_quantized - output->params.zero_point) * output->params.scale;
// Obtain the output from model's output tensor
float y = output->data.f[0];

// Output the results. A custom HandleOutput function can be implemented
// for each supported hardware target.
Expand Down
46 changes: 33 additions & 13 deletions pico_sdk_import.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

# todo document

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
Expand All @@ -20,9 +18,20 @@ if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_P
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_TAG} AND (NOT PICO_SDK_FETCH_FROM_GIT_TAG))
set(PICO_SDK_FETCH_FROM_GIT_TAG $ENV{PICO_SDK_FETCH_FROM_GIT_TAG})
message("Using PICO_SDK_FETCH_FROM_GIT_TAG from environment ('${PICO_SDK_FETCH_FROM_GIT_TAG}')")
endif ()

if (PICO_SDK_FETCH_FROM_GIT AND NOT PICO_SDK_FETCH_FROM_GIT_TAG)
set(PICO_SDK_FETCH_FROM_GIT_TAG "master")
message("Using master as default value for PICO_SDK_FETCH_FROM_GIT_TAG")
endif()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the Raspberry Pi Pico SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")
set(PICO_SDK_FETCH_FROM_GIT_TAG "${PICO_SDK_FETCH_FROM_GIT_TAG}" CACHE FILEPATH "release tag for SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
Expand All @@ -31,20 +40,31 @@ if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
# GIT_SUBMODULES_RECURSE was added in 3.17
if (${CMAKE_VERSION} VERSION_GREATER_EQUAL "3.17.0")
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
GIT_SUBMODULES_RECURSE FALSE
)
else ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG ${PICO_SDK_FETCH_FROM_GIT_TAG}
)
endif ()

if (NOT pico_sdk)
message("Downloading PICO SDK")
message("Downloading Raspberry Pi Pico SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
"SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()
Expand All @@ -56,9 +76,9 @@ endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the Raspberry Pi Pico SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)
set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the Raspberry Pi Pico SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})