Skip to content

Commit 7ab977e

Browse files
zonglinpengfacebook-github-bot
authored andcommitted
build cadence cpu flow as a stand-alone cmake dependency (#5555)
Summary: Pull Request resolved: #5555 This PR - Re-enable cadence e2e flow (quant ops, models) on CPU - move cadence runner builder to cadence runner - create executor cmake to build runner separately from hifi - update to cmake builder script for the cpu runner - add slice_util to reference ops because its missing - disabled __attribute__((always_inline)) in reference kernel because of {F1886927939} Reviewed By: mcremon-meta Differential Revision: D63284100 fbshipit-source-id: 10955f2ade74788da10aa441d164977b44e4eec5
1 parent 52f9e03 commit 7ab977e

File tree

5 files changed

+88
-15
lines changed

5 files changed

+88
-15
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Copyright (c) Meta Platforms, Inc. and affiliates.
2+
# All rights reserved.
3+
#
4+
# This source code is licensed under the BSD-style license found in the
5+
# LICENSE file in the root directory of this source tree.
6+
7+
# Set the minimum required version of CMake for this project.
8+
cmake_minimum_required(VERSION 3.10)
9+
10+
if(NOT CMAKE_CXX_STANDARD)
11+
set(CMAKE_CXX_STANDARD 17)
12+
endif()
13+
14+
# Set the project name.
15+
project(cadence_backend)
16+
17+
# Source root directory for executorch.
18+
if(NOT EXECUTORCH_ROOT)
19+
set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../..)
20+
endif()
21+
22+
include(${EXECUTORCH_ROOT}/build/Utils.cmake)
23+
include(${EXECUTORCH_ROOT}/build/Codegen.cmake)
24+
25+
# Let files say "include <executorch/path/to/header.h>".
26+
set(_common_include_directories ${EXECUTORCH_ROOT}/..)
27+
set(TARGET_DIR reference)
28+
29+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
30+
31+
if(NOT PYTHON_EXECUTABLE)
32+
resolve_python_executable()
33+
endif()
34+
35+
# Find prebuilt libraries. executorch package should contain portable_ops_lib,
36+
# etdump, bundled_program.
37+
find_package(executorch CONFIG REQUIRED)
38+
target_link_options_shared_lib(executorch)
39+
target_link_options_shared_lib(portable_ops_lib)
40+
41+
target_include_directories(executorch INTERFACE ${_common_include_directories})
42+
43+
find_package(
44+
gflags REQUIRED PATHS ${CMAKE_CURRENT_BINARY_DIR}/../../../third-party
45+
)
46+
47+
add_executable(cadence_runner cadence_runner.cpp)
48+
target_compile_options(executorch INTERFACE -DET_EVENT_TRACER_ENABLED)
49+
50+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/operators)
51+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/${TARGET_DIR}/kernels)
52+
53+
target_include_directories(
54+
etdump INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/../../../devtools/include
55+
${EXECUTORCH_ROOT}/third-party/flatcc/include
56+
)
57+
58+
target_include_directories(
59+
cadence_runner PUBLIC ${ROOT_DIR}/../.. ${CMAKE_BINARY_DIR}
60+
${_common_include_directories}
61+
)
62+
63+
target_link_libraries(
64+
cadence_runner
65+
executorch
66+
gflags
67+
etdump
68+
extension_data_loader
69+
bundled_program
70+
cadence_ops_lib
71+
flatccrt
72+
)

backends/cadence/build_cadence_runner.sh renamed to backends/cadence/cadence_runner/build_cadence_runner.sh

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ set -euo pipefail
1212
SCRIPT_DIR="$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
1313
readonly SCRIPT_DIR
1414

15-
readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../.."
15+
readonly EXECUTORCH_ROOT="${SCRIPT_DIR}/../../.."
1616

1717
# Allow overriding the number of build jobs. Default to 9.
1818
export CMAKE_BUILD_PARALLEL_LEVEL="${CMAKE_BUILD_PARALLEL_LEVEL:-9}"
@@ -32,8 +32,9 @@ main() {
3232
-DEXECUTORCH_BUILD_PTHREADPOOL=OFF \
3333
-DEXECUTORCH_BUILD_CPUINFO=OFF \
3434
-DEXECUTORCH_ENABLE_LOGGING=ON \
35-
-Bcmake-out .
36-
cmake --build cmake-out --target install --config Release
35+
-DEXECUTORCH_NNLIB_OPT=OFF \
36+
-Bcmake-out
37+
cmake --build cmake-out --target install --config Release -j16
3738

3839
local example_dir=backends/cadence
3940
local build_dir="cmake-out/${example_dir}"
@@ -43,7 +44,7 @@ main() {
4344
-DCMAKE_BUILD_TYPE=Release \
4445
-B"${build_dir}" \
4546
"${example_dir}"
46-
cmake --build "${build_dir}" --config Release
47+
cmake --build "${build_dir}" --config Release -j16
4748

4849
local runner="${PWD}/${build_dir}/cadence_runner"
4950
if [[ ! -f "${runner}" ]]; then

backends/cadence/reference/kernels/kernels.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include <math.h>
1010
#include <algorithm>
1111
#include <cstring>
12+
#include <limits>
1213
#include <numeric>
1314

1415
namespace impl {
@@ -17,8 +18,7 @@ namespace kernels {
1718

1819
// Quantize a fp32 value to an int8_t/uint8_t value
1920
template <typename T>
20-
__attribute__((always_inline)) T
21-
quantize(const float x, float scale, int32_t zero_point) {
21+
T quantize(const float x, float scale, int32_t zero_point) {
2222
constexpr float min_val = std::numeric_limits<T>::min();
2323
constexpr float max_val = std::numeric_limits<T>::max();
2424
float tmp = roundf(x * scale + zero_point);
@@ -40,8 +40,7 @@ void quantize(
4040

4141
// Dequantize an int8_t/uint8_t value to an fp32 value
4242
template <typename T>
43-
__attribute__((always_inline)) float
44-
dequantize(const T x, float scale, int32_t zero_point) {
43+
float dequantize(const T x, float scale, int32_t zero_point) {
4544
return scale * (x - zero_point);
4645
}
4746

@@ -60,9 +59,8 @@ void dequantize(
6059

6160
// explicit template instantiation
6261

63-
#define typed_quantize_val(dtype) \
64-
template __attribute__((always_inline)) dtype quantize( \
65-
const float x, float inv_scale, int32_t zero_point);
62+
#define typed_quantize_val(dtype) \
63+
template dtype quantize(const float x, float inv_scale, int32_t zero_point);
6664
typed_quantize_val(int8_t);
6765
typed_quantize_val(uint8_t);
6866
typed_quantize_val(int16_t);
@@ -82,9 +80,8 @@ typed_quantize_vec(int16_t);
8280
typed_quantize_vec(int32_t);
8381
#undef typed_quantize_vec
8482

85-
#define typed_dequantize_val(dtype) \
86-
template __attribute__((always_inline)) float dequantize( \
87-
const dtype x, float scale, int32_t zero_point);
83+
#define typed_dequantize_val(dtype) \
84+
template float dequantize(const dtype x, float scale, int32_t zero_point);
8885
typed_dequantize_val(int8_t);
8986
typed_dequantize_val(uint8_t);
9087
typed_dequantize_val(int16_t);

backends/cadence/reference/operators/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ set(_aten_ops__srcs
3232
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/matmul_ops_util.cpp"
3333
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/reduce_util.cpp"
3434
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/repeat_util.cpp"
35+
"${EXECUTORCH_ROOT}/kernels/portable/cpu/util/slice_util.cpp"
3536
"${EXECUTORCH_ROOT}/kernels/portable/cpu/pattern/unary_ufunc_realhb_to_floath.cpp"
3637
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_bmm.cpp"
3738
"${EXECUTORCH_ROOT}/kernels/portable/cpu/op_cat.cpp"

backends/cadence/runtime/executor.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,9 @@ def __init__(
106106
working_dir: str = "",
107107
):
108108
self.working_dir = working_dir
109-
self.executor_builder = "./backends/cadence/build_cadence_runner.sh"
109+
self.executor_builder = (
110+
"./backends/cadence/cadence_runner/build_cadence_runner.sh"
111+
)
110112
self.execute_runner = "./cmake-out/backends/cadence/cadence_runner"
111113
self.bundled_program_path: str = "CadenceDemoModel.bpte"
112114

0 commit comments

Comments
 (0)