diff --git a/backends/cadence/CMakeLists.txt b/backends/cadence/CMakeLists.txt index d786142f085..773dd0e52d5 100644 --- a/backends/cadence/CMakeLists.txt +++ b/backends/cadence/CMakeLists.txt @@ -20,7 +20,6 @@ if(NOT EXECUTORCH_ROOT) endif() include(${EXECUTORCH_ROOT}/build/Utils.cmake) -include(${EXECUTORCH_ROOT}/build/Codegen.cmake) # Let files say "include ". set(_common_include_directories ${EXECUTORCH_ROOT}/..) @@ -30,54 +29,6 @@ if(EXECUTORCH_NNLIB_OPT) set(TARGET_DIR hifi) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/third-party/nnlib) endif() -set(CMAKE_EXPORT_COMPILE_COMMANDS ON) - -# Source root directory for executorch. -if(NOT EXECUTORCH_ROOT) - set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) -endif() - -if(NOT PYTHON_EXECUTABLE) - resolve_python_executable() -endif() - -set(_common_compile_options -Wno-deprecated-declarations -fPIC) - -# Find prebuilt libraries. executorch package should contain portable_ops_lib, -# etdump, bundled_program. -find_package(executorch CONFIG REQUIRED) -target_link_options_shared_lib(executorch) -target_link_options_shared_lib(portable_ops_lib) - -target_include_directories(executorch INTERFACE ${_common_include_directories}) - -find_package( - gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../third-party -) - -add_executable(cadence_runner cadence_runner/cadence_runner.cpp) -target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators) add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/kernels) - -target_include_directories( - etdump INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../sdk/include - ${EXECUTORCH_ROOT}/third-party/flatcc/include -) - -target_include_directories( - cadence_runner PUBLIC ${ROOT_DIR}/.. ${CMAKE_BINARY_DIR} - ${_common_include_directories} -) - -target_link_libraries( - cadence_runner - executorch - gflags - etdump - extension_data_loader - bundled_program - cadence_ops_lib - flatccrt -) diff --git a/backends/cadence/build_cadence_xtensa.sh b/backends/cadence/build_cadence_xtensa.sh index f96436e65d5..eebd0707d19 100644 --- a/backends/cadence/build_cadence_xtensa.sh +++ b/backends/cadence/build_cadence_xtensa.sh @@ -65,8 +65,11 @@ else -DEXECUTORCH_BUILD_HOST_TARGETS=ON \ -DEXECUTORCH_BUILD_EXECUTOR_RUNNER=ON \ -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ - -DEXECUTORCH_BUILD_CADENCE=OFF \ + -DEXECUTORCH_BUILD_CPUINFO=OFF \ + -DEXECUTORCH_BUILD_FLATC=OFF \ + -DEXECUTORCH_BUILD_CADENCE=ON \ -DFLATC_EXECUTABLE="$(which flatc)" \ + -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ -DEXECUTORCH_ENABLE_LOGGING=ON \ -DEXECUTORCH_ENABLE_PROGRAM_VERIFICATION=ON \ -DEXECUTORCH_USE_DL=OFF \ @@ -74,11 +77,7 @@ else -DEXECUTORCH_BUILD_KERNELS_CUSTOM=OFF \ -DPYTHON_EXECUTABLE=python3 \ -DEXECUTORCH_NNLIB_OPT=ON \ - -DEXECUTORCH_BUILD_GFLAGS=ON \ -DHAVE_FNMATCH_H=OFF \ - -DEXECUTORCH_ENABLE_EVENT_TRACER=OFF \ - -DEXECUTORCH_BUILD_EXTENSION_RUNNER_UTIL=ON \ - -DEXECUTORCH_BUILD_CPUINFO=OFF \ -Bcmake-out cmake --build cmake-out --target install --config Release -j16 fi diff --git a/backends/cadence/cadence_runner/CMakeLists.txt b/backends/cadence/cadence_runner/CMakeLists.txt new file mode 100644 index 00000000000..2595e8e62f2 --- /dev/null +++ b/backends/cadence/cadence_runner/CMakeLists.txt @@ -0,0 +1,72 @@ +# Copyright (c) Meta Platforms, Inc. and affiliates. +# All rights reserved. +# +# This source code is licensed under the BSD-style license found in the +# LICENSE file in the root directory of this source tree. + +# Set the minimum required version of CMake for this project. +cmake_minimum_required(VERSION 3.10) + +if(NOT CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 17) +endif() + +# Set the project name. +project(cadence_backend) + +# Source root directory for executorch. +if(NOT EXECUTORCH_ROOT) + set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..) +endif() + +include(${EXECUTORCH_ROOT}/build/Utils.cmake) +include(${EXECUTORCH_ROOT}/build/Codegen.cmake) + +# Let files say "include ". +set(_common_include_directories ${EXECUTORCH_ROOT}/..) +set(TARGET_DIR reference) + +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +if(NOT PYTHON_EXECUTABLE) + resolve_python_executable() +endif() + +# Find prebuilt libraries. executorch package should contain portable_ops_lib, +# etdump, bundled_program. +find_package(executorch CONFIG REQUIRED) +target_link_options_shared_lib(executorch) +target_link_options_shared_lib(portable_ops_lib) + +target_include_directories(executorch INTERFACE ${_common_include_directories}) + +find_package( + gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party +) + +add_executable(cadence_runner cadence_runner.cpp) +target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED) + +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators) +add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/kernels) + +target_include_directories( + etdump INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../../devtools/include + ${EXECUTORCH_ROOT}/third-party/flatcc/include +) + +target_include_directories( + cadence_runner PUBLIC ${ROOT_DIR}/../.. ${CMAKE_BINARY_DIR} + ${_common_include_directories} +) + +target_link_libraries( + cadence_runner + executorch + gflags + etdump + extension_data_loader + bundled_program + cadence_ops_lib + flatccrt +) diff --git a/backends/cadence/build_cadence_runner.sh b/backends/cadence/cadence_runner/build_cadence_runner.sh similarity index 87% rename from backends/cadence/build_cadence_runner.sh rename to backends/cadence/cadence_runner/build_cadence_runner.sh index 693a320bdf4..40b4eb37de2 100755 --- a/backends/cadence/build_cadence_runner.sh +++ b/backends/cadence/cadence_runner/build_cadence_runner.sh @@ -12,7 +12,7 @@ set -euo pipefail SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )" readonly SCRIPT_DIR -readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../.." +readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../../.." # Allow overriding the number of build jobs. Default to 9. export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-9}" @@ -32,8 +32,9 @@ main() { -DEXECUTORCH_BUILD_PTHREADPOOL=OFF \ -DEXECUTORCH_BUILD_CPUINFO=OFF \ -DEXECUTORCH_ENABLE_LOGGING=ON \ - -Bcmake-out . - cmake --build cmake-out --target install --config Release + -DEXECUTORCH_NNLIB_OPT=OFF \ + -Bcmake-out + cmake --build cmake-out --target install --config Release -j16 local example_dir=backends/cadence local build_dir="cmake-out/${example_dir}" @@ -43,7 +44,7 @@ main() { -DCMAKE_BUILD_TYPE=Release \ -B"${build_dir}" \ "${example_dir}" - cmake --build "${build_dir}" --config Release + cmake --build "${build_dir}" --config Release -j16 local runner="${PWD}/${build_dir}/cadence_runner" if [[ ! -f "${runner}" ]]; then diff --git a/backends/cadence/hifi/kernels/CMakeLists.txt b/backends/cadence/hifi/kernels/CMakeLists.txt index 15d1a4ddd52..d03bb1c01ef 100644 --- a/backends/cadence/hifi/kernels/CMakeLists.txt +++ b/backends/cadence/hifi/kernels/CMakeLists.txt @@ -10,6 +10,8 @@ add_library( kernels.cpp ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/matmul_asym8uxasym8u_asym8u.cpp ) +# Let files say "include ". +set(_common_include_directories ${EXECUTORCH_ROOT}/..) target_include_directories( cadence_kernels @@ -19,6 +21,7 @@ target_include_directories( ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/include/nnlib ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/include ${EXECUTORCH_ROOT}/backends/cadence/hifi/third-party/nnlib/nnlib-hifi4/xa_nnlib/algo/ndsp/hifi4/include/ + ${_common_include_directories} ) target_link_libraries(cadence_kernels PRIVATE xa_nnlib) diff --git a/backends/cadence/hifi/kernels/kernels.cpp b/backends/cadence/hifi/kernels/kernels.cpp index 4d9183e4cc2..12b430db644 100644 --- a/backends/cadence/hifi/kernels/kernels.cpp +++ b/backends/cadence/hifi/kernels/kernels.cpp @@ -7,6 +7,7 @@ */ #include + #include #include diff --git a/backends/cadence/hifi/operators/CMakeLists.txt b/backends/cadence/hifi/operators/CMakeLists.txt index 8da6169cda1..78413ef312e 100644 --- a/backends/cadence/hifi/operators/CMakeLists.txt +++ b/backends/cadence/hifi/operators/CMakeLists.txt @@ -28,6 +28,7 @@ set(_aten_ops__srcs "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/matmul_ops_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/reduce_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/repeat_util.cpp" + "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/slice_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/op_add.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/op_bmm.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/op_cat.cpp" diff --git a/backends/cadence/reference/kernels/CMakeLists.txt b/backends/cadence/reference/kernels/CMakeLists.txt index fba66e9b27a..07394cbe834 100644 --- a/backends/cadence/reference/kernels/CMakeLists.txt +++ b/backends/cadence/reference/kernels/CMakeLists.txt @@ -7,4 +7,9 @@ # lint_cmake: -linelength add_library(cadence_kernels kernels.cpp) -target_include_directories(cadence_kernels PUBLIC .) +# Let files say "include ". +set(_common_include_directories ${EXECUTORCH_ROOT}/..) + +target_include_directories(cadence_kernels PUBLIC . + ${_common_include_directories} +) diff --git a/backends/cadence/reference/kernels/kernels.cpp b/backends/cadence/reference/kernels/kernels.cpp index ae3e1bad2db..229f19906e2 100644 --- a/backends/cadence/reference/kernels/kernels.cpp +++ b/backends/cadence/reference/kernels/kernels.cpp @@ -6,9 +6,12 @@ * LICENSE file in the root directory of this source tree. */ +#include + #include #include #include +#include #include namespace impl { @@ -17,8 +20,7 @@ namespace kernels { // Quantize a fp32 value to an int8_t/uint8_t value template -__attribute__((always_inline)) T -quantize(const float x, float scale, int32_t zero_point) { +T quantize(const float x, float scale, int32_t zero_point) { constexpr float min_val = std::numeric_limits::min(); constexpr float max_val = std::numeric_limits::max(); float tmp = roundf(x * scale + zero_point); @@ -40,8 +42,7 @@ void quantize( // Dequantize an int8_t/uint8_t value to an fp32 value template -__attribute__((always_inline)) float -dequantize(const T x, float scale, int32_t zero_point) { +float dequantize(const T x, float scale, int32_t zero_point) { return scale * (x - zero_point); } @@ -60,9 +61,8 @@ void dequantize( // explicit template instantiation -#define typed_quantize_val(dtype) \ - template __attribute__((always_inline)) dtype quantize( \ - const float x, float inv_scale, int32_t zero_point); +#define typed_quantize_val(dtype) \ + template dtype quantize(const float x, float inv_scale, int32_t zero_point); typed_quantize_val(int8_t); typed_quantize_val(uint8_t); typed_quantize_val(int16_t); @@ -82,9 +82,8 @@ typed_quantize_vec(int16_t); typed_quantize_vec(int32_t); #undef typed_quantize_vec -#define typed_dequantize_val(dtype) \ - template __attribute__((always_inline)) float dequantize( \ - const dtype x, float scale, int32_t zero_point); +#define typed_dequantize_val(dtype) \ + template float dequantize(const dtype x, float scale, int32_t zero_point); typed_dequantize_val(int8_t); typed_dequantize_val(uint8_t); typed_dequantize_val(int16_t); diff --git a/backends/cadence/reference/operators/CMakeLists.txt b/backends/cadence/reference/operators/CMakeLists.txt index 605c43ef715..c814a58fe8d 100644 --- a/backends/cadence/reference/operators/CMakeLists.txt +++ b/backends/cadence/reference/operators/CMakeLists.txt @@ -32,6 +32,7 @@ set(_aten_ops__srcs "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/matmul_ops_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/reduce_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/repeat_util.cpp" + "${EXECUTORCH_ROOT}/kernels/portable/cpu/util/slice_util.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/pattern/unary_ufunc_realhb_to_floath.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/op_bmm.cpp" "${EXECUTORCH_ROOT}/kernels/portable/cpu/op_cat.cpp" diff --git a/backends/cadence/runtime/executor.py b/backends/cadence/runtime/executor.py index d07b1b6a52e..6b173a97809 100644 --- a/backends/cadence/runtime/executor.py +++ b/backends/cadence/runtime/executor.py @@ -106,7 +106,9 @@ def __init__( working_dir: str = "", ): self.working_dir = working_dir - self.executor_builder = "./backends/cadence/build_cadence_runner.sh" + self.executor_builder = ( + "./backends/cadence/cadence_runner/build_cadence_runner.sh" + ) self.execute_runner = "./cmake-out/backends/cadence/cadence_runner" self.bundled_program_path: str = "CadenceDemoModel.bpte"