Skip to content

Commit d4c78ab

Browse files
Add torchao kernels to xcframework (#10963)
### Summary In accordance with the following [ticket](#10694), we want to enable building the torchao kernel through the main ExecuTorch CMakeLists.txt. This PR should cover all the necessary steps required to build low-bit kernels on the iOS app. #### List of changes - Provide an optional flag `EXECUTORCH_BUILD_KERNELS_TORCHAO` that enables building torchao through the CMakeLists.txt. - Update the apple framework script to include the torchao build. - Updated the apple framework defaults to include torchao. ### Test plan - Code has been successfully tested on the iPhone 16 simulator --------- Co-authored-by: Scott Roy <[email protected]>
1 parent bdf658b commit d4c78ab

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

CMakeLists.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,30 @@ if(EXECUTORCH_BUILD_PTHREADPOOL)
278278
)
279279
endif()
280280

281+
if(EXECUTORCH_BUILD_KERNELS_TORCHAO)
282+
set(TORCHAO_BUILD_ATEN_OPS OFF)
283+
set(TORCHAO_BUILD_EXECUTORCH_OPS ON)
284+
set(TORCHAO_BUILD_CPU_AARCH64 ON)
285+
set(TORCHAO_ENABLE_ARM_NEON_DOT ON)
286+
287+
list(APPEND TORCHAO_INCLUDE_DIRS
288+
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/pthreadpool/include
289+
${EXECUTORCH_ROOT}/backends/xnnpack/third-party/cpuinfo/include
290+
${EXECUTORCH_ROOT}/third-party/ao
291+
)
292+
293+
set(EXECUTORCH_INCLUDE_DIRS ${TORCHAO_INCLUDE_DIRS})
294+
295+
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/third-party/ao/torchao/experimental)
296+
executorch_target_link_options_shared_lib(torchao_ops_executorch)
297+
list(APPEND _executorch_kernels torchao_ops_executorch)
298+
endif()
299+
300+
if(EXECUTORCH_BUILD_TESTS)
301+
set(EXECUTORCH_BUILD_EXTENSION_FLAT_TENSOR ON)
302+
include(CTest)
303+
endif()
304+
281305
# TODO(dbort): Fix these warnings and remove this flag.
282306
set(_common_compile_options -Wno-deprecated-declarations -fPIC)
283307

scripts/build_apple_frameworks.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ libquantized_kernels.a,\
125125
libquantized_ops_lib.a,\
126126
:"
127127

128+
FRAMEWORK_KERNELS_TORCHAO="kernels_torchao:\
129+
libtorchao_ops_executorch.a,\
130+
libtorchao_kernels_aarch64.a,\
131+
:"
132+
128133
usage() {
129134
echo "Usage: $0 [OPTIONS]"
130135
echo "Build frameworks for Apple platforms."
@@ -137,6 +142,7 @@ usage() {
137142
echo " --mps Only build the Metal Performance Shaders backend."
138143
echo " --optimized Only build the Optimized kernels."
139144
echo " --quantized Only build the Quantized kernels."
145+
echo " --torchao Only build the TorchAO kernels."
140146
echo " --xnnpack Only build the XNNPACK backend."
141147
echo
142148
exit 0
@@ -154,6 +160,7 @@ set_cmake_options_override() {
154160
"-DEXECUTORCH_BUILD_MPS=OFF"
155161
"-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=OFF"
156162
"-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=OFF"
163+
"-DEXECUTORCH_BUILD_KERNELS_TORCHAO=OFF"
157164
"-DEXECUTORCH_BUILD_XNNPACK=OFF"
158165
)
159166
fi
@@ -184,6 +191,7 @@ for arg in "$@"; do
184191
--mps) set_cmake_options_override "EXECUTORCH_BUILD_MPS" ;;
185192
--optimized) set_cmake_options_override "EXECUTORCH_BUILD_KERNELS_OPTIMIZED" ;;
186193
--quantized) set_cmake_options_override "EXECUTORCH_BUILD_KERNELS_QUANTIZED" ;;
194+
--torchao) set_cmake_options_override "EXECUTORCH_BUILD_KERNELS_TORCHAO" ;;
187195
--xnnpack) set_cmake_options_override "EXECUTORCH_BUILD_XNNPACK" ;;
188196
*)
189197
echo -e "\033[31m[error] unknown option: ${arg}\033[0m"
@@ -311,6 +319,7 @@ for mode in "${MODES[@]}"; do
311319
append_framework_flag "EXECUTORCH_BUILD_KERNELS_LLM" "$FRAMEWORK_KERNELS_LLM" "$mode"
312320
append_framework_flag "EXECUTORCH_BUILD_KERNELS_OPTIMIZED" "$FRAMEWORK_KERNELS_OPTIMIZED" "$mode"
313321
append_framework_flag "EXECUTORCH_BUILD_KERNELS_QUANTIZED" "$FRAMEWORK_KERNELS_QUANTIZED" "$mode"
322+
append_framework_flag "EXECUTORCH_BUILD_KERNELS_TORCHAO" "$FRAMEWORK_KERNELS_TORCHAO" "$mode"
314323

315324
cd "${OUTPUT_DIR}"
316325
"$SOURCE_ROOT_DIR"/scripts/create_frameworks.sh "${FRAMEWORK_FLAGS[@]}"

tools/cmake/executorch-config.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ set(optional_lib_list
8484
quantized_kernels
8585
quantized_ops_lib
8686
quantized_ops_aot_lib
87+
torchao_ops_executorch
88+
torchao_kernels_aarch64
8789
)
8890

8991
foreach(lib ${optional_lib_list})

tools/cmake/preset/apple_common.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ set_overridable_option(EXECUTORCH_BUILD_EXTENSION_TENSOR ON)
2929
set_overridable_option(EXECUTORCH_BUILD_KERNELS_LLM ON)
3030
set_overridable_option(EXECUTORCH_BUILD_KERNELS_OPTIMIZED ON)
3131
set_overridable_option(EXECUTORCH_BUILD_KERNELS_QUANTIZED ON)
32+
set_overridable_option(EXECUTORCH_BUILD_KERNELS_TORCHAO ON)

0 commit comments

Comments
 (0)