Skip to content

Commit 067a829

Browse files
shoumikhinfacebook-github-bot
authored andcommitted
Build custom ops framework. (#2765)
Summary: Pull Request resolved: #2765 Reviewed By: kirklandsign Differential Revision: D55535503 fbshipit-source-id: 7d03323b9e0ede3385b1761af4aa571deaffd2a8
1 parent 21944ef commit 067a829

File tree

3 files changed

+91
-49
lines changed

3 files changed

+91
-49
lines changed

CMakeLists.txt

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,21 +58,41 @@ endif()
5858

5959
# --- cpuinfo
6060
set(CPUINFO_SOURCE_DIR "backends/xnnpack/third-party/cpuinfo")
61-
set(CPUINFO_BUILD_TOOLS OFF CACHE BOOL "")
62-
set(CPUINFO_BUILD_UNIT_TESTS OFF CACHE BOOL "")
63-
set(CPUINFO_BUILD_MOCK_TESTS OFF CACHE BOOL "")
64-
set(CPUINFO_BUILD_BENCHMARKS OFF CACHE BOOL "")
65-
set(CPUINFO_LIBRARY_TYPE "static" CACHE STRING "")
66-
set(CPUINFO_LOG_LEVEL "error" CACHE STRING "")
61+
set(CPUINFO_BUILD_TOOLS
62+
OFF
63+
CACHE BOOL "")
64+
set(CPUINFO_BUILD_UNIT_TESTS
65+
OFF
66+
CACHE BOOL "")
67+
set(CPUINFO_BUILD_MOCK_TESTS
68+
OFF
69+
CACHE BOOL "")
70+
set(CPUINFO_BUILD_BENCHMARKS
71+
OFF
72+
CACHE BOOL "")
73+
set(CPUINFO_LIBRARY_TYPE
74+
"static"
75+
CACHE STRING "")
76+
set(CPUINFO_LOG_LEVEL
77+
"error"
78+
CACHE STRING "")
6779
set(CLOG_SOURCE_DIR "${CPUINFO_SOURCE_DIR}/deps/clog")
6880
add_subdirectory("${CPUINFO_SOURCE_DIR}")
6981

7082
# --- pthreadpool
7183
set(PTHREADPOOL_SOURCE_DIR "backends/xnnpack/third-party/pthreadpool")
72-
set(PTHREADPOOL_BUILD_TESTS OFF CACHE BOOL "")
73-
set(PTHREADPOOL_BUILD_BENCHMARKS OFF CACHE BOOL "")
74-
set(PTHREADPOOL_LIBRARY_TYPE "static" CACHE STRING "")
75-
set(PTHREADPOOL_ALLOW_DEPRECATED_API ON CACHE BOOL "")
84+
set(PTHREADPOOL_BUILD_TESTS
85+
OFF
86+
CACHE BOOL "")
87+
set(PTHREADPOOL_BUILD_BENCHMARKS
88+
OFF
89+
CACHE BOOL "")
90+
set(PTHREADPOOL_LIBRARY_TYPE
91+
"static"
92+
CACHE STRING "")
93+
set(PTHREADPOOL_ALLOW_DEPRECATED_API
94+
ON
95+
CACHE BOOL "")
7696
add_subdirectory("${PTHREADPOOL_SOURCE_DIR}")
7797

7898
# ------------------------------ OPTIONS -------------------------------------
@@ -161,6 +181,8 @@ option(EXECUTORCH_BUILD_ARM_BAREMETAL
161181

162182
option(EXECUTORCH_BUILD_COREML "Build the Core ML backend" OFF)
163183

184+
option(EXECUTORCH_BUILD_CUSTOM "Build the custom kernels" OFF)
185+
164186
option(EXECUTORCH_BUILD_EXTENSION_DATA_LOADER "Build the Data Loader extension"
165187
OFF)
166188

@@ -335,6 +357,12 @@ endif()
335357
#
336358
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/portable)
337359

360+
if(EXECUTORCH_BUILD_CUSTOM)
361+
# TODO: move all custom kernels to ${CMAKE_CURRENT_SOURCE_DIR}/kernels/custom
362+
add_subdirectory(
363+
${CMAKE_CURRENT_SOURCE_DIR}/examples/models/llama2/custom_ops)
364+
endif()
365+
338366
if(EXECUTORCH_BUILD_OPTIMIZED)
339367
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/kernels/optimized)
340368
endif()
@@ -470,7 +498,7 @@ if(EXECUTORCH_BUILD_PYBIND)
470498
# find pytorch lib, to allow pybind to take at::Tensor as input/output
471499
find_package(Torch CONFIG REQUIRED)
472500
find_library(TORCH_PYTHON_LIBRARY torch_python
473-
PATHS "${TORCH_INSTALL_PREFIX}/lib")
501+
PATHS "${TORCH_INSTALL_PREFIX}/lib")
474502

475503
# compile options for pybind
476504

build/Utils.cmake

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ function(executorch_print_configuration_summary)
4444
message(
4545
STATUS
4646
" EXECUTORCH_BUILD_COREML : ${EXECUTORCH_BUILD_COREML}")
47+
message(STATUS " EXECUTORCH_BUILD_CUSTOM : "
48+
"${EXECUTORCH_BUILD_CUSTOM}")
4749
message(STATUS " EXECUTORCH_BUILD_EXECUTOR_RUNNER : "
4850
"${EXECUTORCH_BUILD_EXECUTOR_RUNNER}")
4951
message(STATUS " EXECUTORCH_BUILD_EXTENSION_DATA_LOADER : "
@@ -135,9 +137,10 @@ function(extract_sources sources_file)
135137
endif()
136138

137139
execute_process(
138-
COMMAND ${PYTHON_EXECUTABLE} ${executorch_root}/build/extract_sources.py
139-
--config=${executorch_root}/build/cmake_deps.toml --out=${sources_file}
140-
--buck2=${BUCK2}
140+
COMMAND
141+
${PYTHON_EXECUTABLE} ${executorch_root}/build/extract_sources.py
142+
--config=${executorch_root}/build/cmake_deps.toml --out=${sources_file}
143+
--buck2=${BUCK2}
141144
OUTPUT_VARIABLE gen_srcs_output
142145
ERROR_VARIABLE gen_srcs_error
143146
RESULT_VARIABLE gen_srcs_exit_code
@@ -153,16 +156,14 @@ function(extract_sources sources_file)
153156
endif()
154157
endfunction()
155158

156-
# Sets the value of the BUCK2 variable by searching for a buck2 binary
157-
# with the correct version.
159+
# Sets the value of the BUCK2 variable by searching for a buck2 binary with the
160+
# correct version.
158161
#
159-
# The resolve_buck.py script uses the following logic to find buck2:
160-
# 1) If BUCK2 argument is set explicitly, use it. Warn if the version is
161-
# incorrect.
162-
# 2) Look for a binary named buck2 on the system path. Take it if it is
163-
# the correct version.
164-
# 3) Check for a previously downloaded buck2 binary (from step 4).
165-
# 4) Download and cache correct version of buck2.
162+
# The resolve_buck.py script uses the following logic to find buck2: 1) If BUCK2
163+
# argument is set explicitly, use it. Warn if the version is incorrect. 2) Look
164+
# for a binary named buck2 on the system path. Take it if it is the correct
165+
# version. 3) Check for a previously downloaded buck2 binary (from step 4). 4)
166+
# Download and cache correct version of buck2.
166167
function(resolve_buck2)
167168
if(EXECUTORCH_ROOT)
168169
set(executorch_root ${EXECUTORCH_ROOT})
@@ -171,8 +172,8 @@ function(resolve_buck2)
171172
endif()
172173

173174
set(resolve_buck2_command
174-
${PYTHON_EXECUTABLE} ${executorch_root}/build/resolve_buck.py
175-
--cache_dir=${CMAKE_CURRENT_BINARY_DIR}/buck2-bin)
175+
${PYTHON_EXECUTABLE} ${executorch_root}/build/resolve_buck.py
176+
--cache_dir=${CMAKE_CURRENT_BINARY_DIR}/buck2-bin)
176177

177178
if(NOT ${BUCK2} STREQUAL "")
178179
list(APPEND resolve_buck2_command --buck2=${BUCK2})
@@ -186,35 +187,42 @@ function(resolve_buck2)
186187
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
187188
OUTPUT_STRIP_TRAILING_WHITESPACE)
188189

189-
if(resolve_buck2_exit_code EQUAL 0)
190-
set(BUCK2 ${resolve_buck2_output} PARENT_SCOPE)
191-
message(STATUS "Resolved buck2 as ${resolve_buck2_output}.")
192-
elseif(resolve_buck2_exit_code EQUAL 2)
193-
# Wrong buck version used. Stop here to ensure that the user sees
194-
# the error.
195-
message(FATAL_ERROR "Failed to resolve buck2.\n${resolve_buck2_error}")
196-
else()
197-
# Unexpected failure of the script. Warn.
198-
message(WARNING "Failed to resolve buck2.")
199-
message(WARNING "${resolve_buck2_error}")
190+
if(resolve_buck2_exit_code EQUAL 0)
191+
set(BUCK2
192+
${resolve_buck2_output}
193+
PARENT_SCOPE)
194+
message(STATUS "Resolved buck2 as ${resolve_buck2_output}.")
195+
elseif(resolve_buck2_exit_code EQUAL 2)
196+
# Wrong buck version used. Stop here to ensure that the user sees the error.
197+
message(FATAL_ERROR "Failed to resolve buck2.\n${resolve_buck2_error}")
198+
else()
199+
# Unexpected failure of the script. Warn.
200+
message(WARNING "Failed to resolve buck2.")
201+
message(WARNING "${resolve_buck2_error}")
200202

201-
if("${BUCK2}" STREQUAL "")
202-
set(BUCK2 "buck2" PARENT_SCOPE)
203-
endif()
203+
if("${BUCK2}" STREQUAL "")
204+
set(BUCK2
205+
"buck2"
206+
PARENT_SCOPE)
204207
endif()
208+
endif()
205209
endfunction()
206210

207-
# Sets the value of the PYTHON_EXECUTABLE variable to 'python' if in
208-
# an active (non-base) conda environment, and 'python3' otherwise. This
209-
# maintains backwards compatibility for non-conda users and avoids conda
210-
# users needing to explicitly set PYTHON_EXECUTABLE=python.
211+
# Sets the value of the PYTHON_EXECUTABLE variable to 'python' if in an active
212+
# (non-base) conda environment, and 'python3' otherwise. This maintains
213+
# backwards compatibility for non-conda users and avoids conda users needing to
214+
# explicitly set PYTHON_EXECUTABLE=python.
211215
function(resolve_python_executable)
212-
# Counter-intuitively, CONDA_DEFAULT_ENV contains the name of the
213-
# active environment.
214-
if(DEFINED ENV{CONDA_DEFAULT_ENV} AND
215-
NOT $ENV{CONDA_DEFAULT_ENV} STREQUAL "base")
216-
set(PYTHON_EXECUTABLE python PARENT_SCOPE)
216+
# Counter-intuitively, CONDA_DEFAULT_ENV contains the name of the active
217+
# environment.
218+
if(DEFINED ENV{CONDA_DEFAULT_ENV} AND NOT $ENV{CONDA_DEFAULT_ENV} STREQUAL
219+
"base")
220+
set(PYTHON_EXECUTABLE
221+
python
222+
PARENT_SCOPE)
217223
else()
218-
set(PYTHON_EXECUTABLE python3 PARENT_SCOPE)
224+
set(PYTHON_EXECUTABLE
225+
python3
226+
PARENT_SCOPE)
219227
endif()
220228
endfunction()

build/build_apple_frameworks.sh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ PYTHON=$(which python3)
1818
FLATC="flatc"
1919
IOS_DEPLOYMENT_TARGET="17.0"
2020
COREML=OFF
21+
CUSTOM=OFF
2122
MPS=OFF
2223
OPTIMIZED=OFF
2324
PORTABLE=OFF
@@ -26,6 +27,7 @@ XNNPACK=OFF
2627
HEADERS_PATH="include"
2728
EXECUTORCH_FRAMEWORK="executorch:libexecutorch.a,libextension_apple.a,libextension_data_loader.a,libextension_module.a:$HEADERS_PATH"
2829
COREML_FRAMEWORK="coreml_backend:libcoremldelegate.a:"
30+
CUSTOM_FRAMEWORK="custom_backend:libcustom_ops.a,libcustom_ops_lib.a:"
2931
MPS_FRAMEWORK="mps_backend:libmpsdelegate.a:"
3032
OPTIMIZED_FRAMEWORK="optimized_backend:liboptimized_kernels.a,liboptimized_ops_lib.a:"
3133
PORTABLE_FRAMEWORK="portable_backend:libportable_kernels.a,libportable_ops_lib.a:"
@@ -45,6 +47,7 @@ usage() {
4547
echo " --python=FILE Python executable path. Default: Path of python3 found in the current \$PATH"
4648
echo " --flatc=FILE FlatBuffers Compiler executable path. Default: '\$SOURCE_ROOT_DIR/third-party/flatbuffers/cmake-out/flatc'"
4749
echo " --coreml Include this flag to build the Core ML backend."
50+
echo " --custom Include this flag to build the Custom backend."
4851
echo " --mps Include this flag to build the Metal Performance Shaders backend."
4952
echo " --optimized Include this flag to build the Optimized backend."
5053
echo " --portable Include this flag to build the Portable backend."
@@ -67,6 +70,7 @@ for arg in "$@"; do
6770
--flatc=*) FLATC="${arg#*=}" ;;
6871
--ios-deployment-target=*) IOS_DEPLOYMENT_TARGET="${arg#*=}" ;;
6972
--coreml) COREML=ON ;;
73+
--custom) CUSTOM=ON ;;
7074
--mps) MPS=ON ;;
7175
--optimized) OPTIMIZED=ON ;;
7276
--portable) PORTABLE=ON ;;
@@ -130,6 +134,7 @@ cmake_build() {
130134
-DCMAKE_ARCHIVE_OUTPUT_DIRECTORY="$(pwd)" \
131135
-DIOS_DEPLOYMENT_TARGET="$IOS_DEPLOYMENT_TARGET" \
132136
-DEXECUTORCH_BUILD_COREML=$COREML \
137+
-DEXECUTORCH_BUILD_CUSTOM=$CUSTOM \
133138
-DEXECUTORCH_BUILD_MPS=$MPS \
134139
-DEXECUTORCH_BUILD_OPTIMIZED=$OPTIMIZED \
135140
-DEXECUTORCH_BUILD_QUANTIZED=$QUANTIZED \
@@ -172,6 +177,7 @@ append_framework_flag() {
172177

173178
append_framework_flag "ON" "$EXECUTORCH_FRAMEWORK"
174179
append_framework_flag "$COREML" "$COREML_FRAMEWORK"
180+
append_framework_flag "$CUSTOM" "$CUSTOM_FRAMEWORK"
175181
append_framework_flag "$MPS" "$MPS_FRAMEWORK"
176182
append_framework_flag "$OPTIMIZED" "$OPTIMIZED_FRAMEWORK"
177183
append_framework_flag "$PORTABLE" "$PORTABLE_FRAMEWORK"

0 commit comments

Comments
 (0)