Skip to content
Closed
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,6 @@
[submodule "third-party/ios-cmake"]
path = third-party/ios-cmake
url = https://github.com/leetal/ios-cmake
[submodule "backends/cadence/hifi/third-party/nnlib/nnlib-hifi4"]
path = backends/cadence/hifi/third-party/nnlib/nnlib-hifi4
url = https://github.com/foss-xtensa/nnlib-hifi4.git
10 changes: 9 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ target_link_libraries(executorch_no_prim_ops PRIVATE program_schema)
# Check if dl exists for this toolchain and only then link it.
find_library(DL_LIBRARY_EXISTS NAMES dl)
# Check if the library was found
if(DL_LIBRARY_EXISTS)
if(DL_LIBRARY_EXISTS AND NOT EXECUTORCH_BUILD_CADENCE)
target_link_libraries(executorch_no_prim_ops PRIVATE dl) # For dladdr()
endif()
target_include_directories(
Expand All @@ -443,7 +443,9 @@ target_link_options_shared_lib(executorch)
# Real integrations should supply their own YAML file that only lists the
# operators necessary for the models that will run.
#
if(NOT EXECUTORCH_BUILD_CADENCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable)
endif()

if(EXECUTORCH_BUILD_KERNELS_CUSTOM)
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
Expand Down Expand Up @@ -496,6 +498,8 @@ if(EXECUTORCH_BUILD_EXECUTOR_RUNNER)

if(EXECUTORCH_BUILD_KERNELS_OPTIMIZED)
list(APPEND _executor_runner_libs optimized_native_cpu_ops_lib)
elseif(EXECUTORCH_BUILD_CADENCE)
list(APPEND _executor_runner_libs cadence_ops_lib)
else()
list(APPEND _executor_runner_libs portable_ops_lib)
endif()
Expand Down Expand Up @@ -566,6 +570,10 @@ if(EXECUTORCH_BUILD_COREML)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/apple/coreml)
endif()

if(EXECUTORCH_BUILD_CADENCE)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/backends/cadence)
endif()

if(EXECUTORCH_BUILD_PYBIND)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/pybind11)

Expand Down
118 changes: 9 additions & 109 deletions backends/cadence/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ if(NOT CMAKE_CXX_STANDARD)
endif()

# Set the project name.
project(cadence_executorch_example)
project(cadence_backend)

# Source root directory for executorch.
if(NOT EXECUTORCH_ROOT)
Expand All @@ -21,121 +21,21 @@ endif()

include(${EXECUTORCH_ROOT}/build/Utils.cmake)

if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()

# Let files say "include <executorch/path/to/header.h>".
set(_common_include_directories ${EXECUTORCH_ROOT}/..)

# Find prebuilt executorch lib
find_package(executorch CONFIG REQUIRED)

add_compile_options(
-DSDK_DEBUGCONSOLE=1
-DSERIAL_PORT_TYPE_UART=1
-DDEBUG_CONSOLE_RX_ENABLE=0
-DDEBUG
-DCPU_MIMXRT685SFVKB_dsp
-DMCUXPRESSO_SDK
-g
-O0
-Wall
-fsigned-char
-Wno-missing-braces
-fmessage-length=0
-DPRINTF_FLOAT_ENABLE=1
)

if(NOT DEFINED NXP_SDK_ROOT_DIR)
message(FATAL_ERROR "NXP_SDK_ROOT_DIR is not set")
endif()

# lint_cmake: -linelength
set(SOURCES
${NXP_SDK_ROOT_DIR}/components/lists/fsl_component_generic_list.c
${NXP_SDK_ROOT_DIR}/components/uart/fsl_adapter_usart.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_clock.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_common.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_common_dsp.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_flexcomm.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_gpio.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_mu.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_reset.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers/fsl_usart.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/system_MIMXRT685S_dsp.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite/fsl_assert.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite/fsl_debug_console.c
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp/board_hifi4.c
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp/pin_mux.c
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/str/fsl_str.c
)

add_library(dsp_mu_polling_libs STATIC ${SOURCES})

target_include_directories(
dsp_mu_polling_libs
PUBLIC ${NXP_SDK_ROOT_DIR}
${NXP_SDK_ROOT_DIR}/components/uart
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/drivers
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/debug_console_lite
${NXP_SDK_ROOT_DIR}/components/lists
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S
${NXP_SDK_ROOT_DIR}/CMSIS/Core/Include
${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/utilities/str
${NXP_SDK_ROOT_DIR}/boards/evkmimxrt685/dsp_examples/mu_polling/dsp
)

add_library(extension_runner_util STATIC IMPORTED)
set_property(
TARGET extension_runner_util
PROPERTY
IMPORTED_LOCATION
"${CMAKE_CURRENT_LIST_DIR}/../../cmake-out/extension/runner_util/libextension_runner_util.a"
)

add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/third-party/nnlib)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/operators)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/hifi/kernels)

# Generate the model header file
add_custom_command(
OUTPUT ${CMAKE_BINARY_DIR}/model_pte.h
COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/utils/gen_header.py
--model_path ${MODEL_PATH} --header_output_path ${CMAKE_BINARY_DIR}
COMMENT "Converting .pte model to header file..."
DEPENDS ${CMAKE_CURRENT_LIST_DIR}/utils/gen_header.py
)

add_custom_target(gen_model_header DEPENDS ${CMAKE_BINARY_DIR}/model_pte.h)

add_executable(cadence_executorch_example executor_runner.cpp)
add_dependencies(cadence_executorch_example gen_model_header)

# lint_cmake: -linelength
target_include_directories(
cadence_executorch_example PUBLIC ${ROOT_DIR}/.. ${CMAKE_BINARY_DIR}
${_common_include_directories}
install(
TARGETS cadence_ops_lib
DESTINATION lib
INCLUDES
DESTINATION ${_common_include_directories}
)

target_link_options(
cadence_executorch_example PRIVATE
-mlsp=${NXP_SDK_ROOT_DIR}/devices/MIMXRT685S/xtensa/min-rt
)
target_link_libraries(
cadence_executorch_example dsp_mu_polling_libs cadence_ops_lib
extension_runner_util executorch
)

add_custom_command(
TARGET cadence_executorch_example
POST_BUILD
COMMAND
${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/utils/post_compilation.py
${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME} ${CMAKE_BINARY_DIR}
COMMENT
"Generating .bin files that can be used to flash the DSP with. Copy over
the dsp_text_release.bin and dsp_data_release.bin that are generated into
your NXP MCUXpresso IDE workspace and flash the DSP with these binaries."
DEPENDS
${CMAKE_CURRENT_LIST_DIR}/utils/post_compilation.py
)


4 changes: 2 additions & 2 deletions backends/cadence/cadence.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ set(CMAKE_CROSSCOMPILING TRUE)
set(CMAKE_C_COMPILER ${TOOLCHAIN_HOME}/bin/${CROSS_COMPILE_TARGET}-clang)
set(CMAKE_CXX_COMPILER ${TOOLCHAIN_HOME}/bin/${CROSS_COMPILE_TARGET}-clang++)

set(CMAKE_C_FLAGS_INIT "-stdlib=libc++")
set(CMAKE_CXX_FLAGS_INIT "-stdlib=libc++")
set(CMAKE_C_FLAGS_INIT "-stdlib=libc++ -mtext-section-literals -mlongcalls")
set(CMAKE_CXX_FLAGS_INIT "-stdlib=libc++ -mtext-section-literals -mlongcalls")
set(CMAKE_SYSROOT ${TOOLCHAIN_HOME}/${SYSROOT_TARGET})
set(CMAKE_LINKER ${TOOLCHAIN_HOME}/bin/xt-ld)
add_link_options(-lm -stdlib=libc++ -Wl,--no-as-needed -static)
Expand Down
2 changes: 2 additions & 0 deletions backends/cadence/hifi/kernels/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ target_include_directories(
${NN_LIB_BASE_DIR}/xa_nnlib/algo/ndsp/hifi4/include/
${NXP_SDK_ROOT_DIR}/middleware/dsp/naturedsp/hifi4/include/
)

target_link_libraries(cadence_kernels PRIVATE xa_nnlib)
25 changes: 25 additions & 0 deletions backends/cadence/hifi/kernels/kernels.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,31 @@
#include "stddef.h"
#include "xa_type_def.h"


extern "C" WORD32 xa_nn_elm_quantize_f32_asym8s(WORD8 * __restrict__ p_out,
const FLOAT32 * __restrict__ p_inp,
FLOAT32 out_scale,
WORD32 out_zero_bias,
WORD32 num_elm);

/*extern "C" WORD32 xa_nn_elm_quantize_f32_asym8u(UWORD8 * __restrict__ p_out,
const FLOAT32 * __restrict__ p_inp,
FLOAT32 out_scale,
WORD32 out_zero_bias,
WORD32 num_elm); */

extern "C" WORD32 xa_nn_elm_dequantize_asym8s_f32(FLOAT32 * __restrict__ p_out,
const WORD8 * __restrict__ p_inp,
WORD32 inp_zero_bias,
FLOAT32 inp_scale,
WORD32 num_elm);

/*extern "C" WORD32 xa_nn_elm_dequantize_asym8u_f32(FLOAT32 * __restrict__ p_out,
const UWORD8 * __restrict__ p_inp,
WORD32 inp_zero_bias,
FLOAT32 inp_scale,
WORD32 num_elm);*/

namespace impl {
namespace HiFi {
namespace kernels {
Expand Down
17 changes: 11 additions & 6 deletions backends/cadence/hifi/operators/dequantize_per_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,21 @@ void dequantize_per_tensor_out(

if (input.scalar_type() == ScalarType::Byte) {
const uint8_t* input_data = input.const_data_ptr<uint8_t>();
impl::HiFi::kernels::dequantize<uint8_t>(
out_data, input_data, scale, zero_point, numel);
#if 0 //NNLIB_OPT (not available in nnlib)
xa_nn_elm_dequantize_asym8u_f32(out_data, input_data, zero_point, scale, numel);
#else
impl::HiFi::kernels::dequantize<uint8_t>(out_data, input_data, scale, zero_point, numel);
#endif
} else if (input.scalar_type() == ScalarType::Char) {
const int8_t* input_data = input.const_data_ptr<int8_t>();
impl::HiFi::kernels::dequantize<int8_t>(
out_data, input_data, scale, zero_point, numel);
#if NNLIB_OPT
xa_nn_elm_dequantize_asym8s_f32(out_data, input_data, zero_point, scale, numel);
#else
impl::HiFi::kernels::dequantize<int8_t>(out_data, input_data, scale, zero_point, numel);
#endif
} else if (input.scalar_type() == ScalarType::Int) {
const int32_t* input_data = input.const_data_ptr<int32_t>();
impl::HiFi::kernels::dequantize<int32_t>(
out_data, input_data, scale, zero_point, numel);
impl::HiFi::kernels::dequantize<int32_t>(out_data, input_data, scale, zero_point, numel);
} else {
ET_CHECK_MSG(false, "Unhandled input dtype %hhd", input.scalar_type());
}
Expand Down
17 changes: 11 additions & 6 deletions backends/cadence/hifi/operators/quantize_per_tensor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,21 @@ void quantize_per_tensor_out(

if (out.scalar_type() == ScalarType::Byte) {
uint8_t* out_data = out.mutable_data_ptr<uint8_t>();
impl::HiFi::kernels::quantize<uint8_t>(
out_data, input_data, 1. / scale, zero_point, numel);
#if 0 //NNLIB_OPT (not available in nnlib)
xa_nn_elm_quantize_f32_asym8u(out_data, input_data, scale, zero_point, numel);
#else
impl::HiFi::kernels::quantize<uint8_t>(out_data, input_data, 1. / scale, zero_point, numel);
#endif
} else if (out.scalar_type() == ScalarType::Char) {
int8_t* out_data = out.mutable_data_ptr<int8_t>();
impl::HiFi::kernels::quantize<int8_t>(
out_data, input_data, 1. / scale, zero_point, numel);
#if NNLIB_OPT
xa_nn_elm_quantize_f32_asym8s(out_data, input_data, scale, zero_point, numel);
#else
impl::HiFi::kernels::quantize<int8_t>(out_data, input_data, 1. / scale, zero_point, numel);
#endif
} else if (out.scalar_type() == ScalarType::Int) {
int32_t* out_data = out.mutable_data_ptr<int32_t>();
impl::HiFi::kernels::quantize<int32_t>(
out_data, input_data, 1. / scale, zero_point, numel);
impl::HiFi::kernels::quantize<int32_t>(out_data, input_data, 1. / scale, zero_point, numel);
} else {
ET_CHECK_MSG(false, "Unhandled input dtype %hhd", out.scalar_type());
}
Expand Down
33 changes: 33 additions & 0 deletions backends/cadence/hifi/third-party/nnlib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

cmake_minimum_required(VERSION 3.10.0)
project(cadence_nnlib)


add_custom_target( nnlib_target ALL COMMAND
make install_nnlib -f makefile -C ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/build
OBJDIR=${CMAKE_CURRENT_BINARY_DIR}/obj
LIBDIR=${CMAKE_CURRENT_BINARY_DIR}/lib
-j8 )

add_library(xa_nnlib STATIC IMPORTED GLOBAL)
add_dependencies(xa_nnlib nnlib_target)

message("NNLIB")
message("${CMAKE_CURRENT_BINARY_DIR}")

set_property(
TARGET xa_nnlib
PROPERTY
IMPORTED_LOCATION "${CMAKE_CURRENT_BINARY_DIR}/lib/xa_nnlib.a"
)











1 change: 1 addition & 0 deletions backends/cadence/hifi/third-party/nnlib/nnlib-hifi4
Submodule nnlib-hifi4 added at 6a9ea4