Skip to content

determine fht_avx.c/fht_neon.c based on CMake target processor #8346

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 35 commits into from
Feb 26, 2025
Merged
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
1 change: 1 addition & 0 deletions build/Test.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function(et_cxx_test target_name)
cmake_parse_arguments(ET_CXX_TEST "" "" "${multi_arg_names}" ${ARGN})

add_executable(${target_name} ${ET_CXX_TEST_SOURCES} ${EXECUTORCH_ROOT}/runtime/core/exec_aten/testing_util/tensor_util.cpp)
find_package(GTest)
# Includes gtest, gmock, executorch by default
target_link_libraries(
${target_name} GTest::gtest GTest::gtest_main GTest::gmock executorch
Expand Down
47 changes: 25 additions & 22 deletions build/Utils.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -329,29 +329,25 @@ function(resolve_python_executable)
endfunction()

# find_package(Torch CONFIG REQUIRED) replacement for targets that have a
# header-only Torch dependency. Because find_package sets variables in the
# parent scope, we use a macro to preserve this rather than maintaining our own
# list of those variables.
macro(find_package_torch_headers)
# We cannot simply use CMAKE_FIND_ROOT_PATH_BOTH, because that does not
# propagate into TorchConfig.cmake.
foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE)
set(OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}
${CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}
)
set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind} BOTH)
endforeach()
find_package_torch()
foreach(mode_kind IN ITEMS PACKAGE LIBRARY INCLUDE)
set(CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}
${OLD_CMAKE_FIND_ROOT_PATH_MODE_${mode_kind}}
)
endforeach()
endmacro()
# header-only Torch dependency.
#
# Unlike find_package(Torch ...), this will only set
# TORCH_INCLUDE_DIRS in the parent scope. In particular, it will NOT
# set any of the following:
# - TORCH_FOUND
# - TORCH_LIBRARY
# - TORCH_CXX_FLAGS
function(find_package_torch_headers)
# We implement this way rather than using find_package so that
# cross-compilation can still use the host's installed copy of
# torch, since the headers should be fine.
get_torch_base_path(TORCH_BASE_PATH)
set(TORCH_INCLUDE_DIRS "${TORCH_BASE_PATH}/include;${TORCH_BASE_PATH}/include/torch/csrc/api/include" PARENT_SCOPE)
endfunction()

# Add the Torch CMake configuration to CMAKE_PREFIX_PATH so that find_package
# can find Torch.
function(add_torch_to_cmake_prefix_path)
# Return the base path to the installed Torch Python library in
# outVar.
function(get_torch_base_path outVar)
if(NOT PYTHON_EXECUTABLE)
resolve_python_executable()
endif()
Expand All @@ -370,6 +366,13 @@ function(add_torch_to_cmake_prefix_path)
message("Output:\n${_tmp_torch_path}")
message(FATAL_ERROR "Error:\n${_tmp_torch_path_error}")
endif()
set(${outVar} ${_tmp_torch_path} PARENT_SCOPE)
endfunction()

# Add the Torch CMake configuration to CMAKE_PREFIX_PATH so that find_package
# can find Torch.
function(add_torch_to_cmake_prefix_path)
get_torch_base_path(_tmp_torch_path)
list(APPEND CMAKE_PREFIX_PATH "${_tmp_torch_path}")
set(CMAKE_PREFIX_PATH
"${CMAKE_PREFIX_PATH}"
Expand Down
2 changes: 2 additions & 0 deletions build/build_android_llm_demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ build_android_native_library() {
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI="${ANDROID_ABI}" \
-DANDROID_PLATFORM=android-26 \
-DBUILD_TESTING=OFF \
-DEXECUTORCH_ENABLE_LOGGING=ON \
-DEXECUTORCH_LOG_LEVEL=Info \
-DEXECUTORCH_BUILD_XNNPACK=ON \
Expand Down Expand Up @@ -73,6 +74,7 @@ build_android_native_library() {
-DCMAKE_TOOLCHAIN_FILE=${ANDROID_NDK}/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="${ANDROID_ABI}" \
-DANDROID_PLATFORM=android-26 \
-DBUILD_TESTING=OFF \
-DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
-DEXECUTORCH_ENABLE_LOGGING=ON \
-DEXECUTORCH_LOG_LEVEL=Info \
Expand Down
5 changes: 1 addition & 4 deletions build/cmake_deps.toml
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,7 @@ buck_targets = [
"//extension/llm/custom_ops:custom_ops",
]
filters = [
# Second clause is to pick up fht_neon.c/fht_avx.c from FFHT. TODO:
# remove filters and patch extract_sources.py's Buck query to fetch
# srcs; presumably filters is here to remove .h files.
"(.cpp$)|(fht.*\\.c$)",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment is irrelevant since you are removing it, but this second regex seemed wrong?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks right to me. Dot is a regex metacharacter so we need to escape it with a backslash. Backslash is a string metacharacter so we need to escape it with a second backslash. Or is there a different problem I missed entirely?

".cpp$",
]
excludes = [
"^codegen",
Expand Down
2 changes: 2 additions & 0 deletions extension/android_test/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ build_native_library() {
cmake . -DCMAKE_INSTALL_PREFIX="${CMAKE_OUT}" \
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}/build/cmake/android.toolchain.cmake" \
-DANDROID_ABI="${ANDROID_ABI}" \
-DBUILD_TESTING=OFF \
-DEXECUTORCH_BUILD_XNNPACK=ON \
-DEXECUTORCH_XNNPACK_SHARED_WORKSPACE=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
Expand All @@ -36,6 +37,7 @@ build_native_library() {
cmake extension/android \
-DCMAKE_TOOLCHAIN_FILE="${ANDROID_NDK}"/build/cmake/android.toolchain.cmake \
-DANDROID_ABI="${ANDROID_ABI}" \
-DBUILD_TESTING=OFF \
-DCMAKE_INSTALL_PREFIX=c"${CMAKE_OUT}" \
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
-DEXECUTORCH_BUILD_LLAMA_JNI=ON \
Expand Down
21 changes: 21 additions & 0 deletions extension/llm/custom_ops/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@ list(APPEND custom_ops_libs cpuinfo)
list(APPEND custom_ops_libs cpublas)
list(APPEND custom_ops_libs eigen_blas)

if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(aarch64|arm64|armv7)$")
list(APPEND _custom_ops__srcs
"extension/llm/custom_ops/spinquant/third-party/FFHT/fht_neon.c"
)
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64")
list(APPEND _custom_ops__srcs
"extension/llm/custom_ops/spinquant/third-party/FFHT/fht_avx.c"
)
else()
message(
FATAL_ERROR
"Unsupported CMAKE_SYSTEM_PROCESSOR ${CMAKE_SYSTEM_PROCESSOR}. (If \
32-bit x86, try using fht_avx.c and send a PR if it works!)"
)
endif()

list(TRANSFORM _custom_ops__srcs PREPEND "${EXECUTORCH_ROOT}/")

if(NOT EXECUTORCH_BUILD_XNNPACK)
Expand Down Expand Up @@ -121,3 +137,8 @@ if(EXECUTORCH_BUILD_KERNELS_CUSTOM_AOT)
LIBRARY DESTINATION executorch/extension/llm/custom_ops
)
endif()

add_subdirectory(spinquant/third-party/FFHT)
if(BUILD_TESTING)
add_subdirectory(spinquant/test)
endif()
30 changes: 30 additions & 0 deletions extension/llm/custom_ops/spinquant/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# 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.

# @generated by test/utils/generate_gtest_cmakelists.py
#
# This file should be formatted with
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~
# It should also be cmake-lint clean.
#

cmake_minimum_required(VERSION 3.19)

set(EXECUTORCH_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../../../../..)

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

set(_test_srcs
fast_hadamard_transform_test.cpp fast_hadamard_transform_test_impl.cpp
op_fast_hadamard_transform_test.cpp
)

et_cxx_test(
extension_llm_custom_ops_spinquant_test SOURCES ${_test_srcs} EXTRA_LIBS
custom_ops dumb_fht
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# 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.

# Please this file formatted by running:
# ~~~
# cmake-format -i CMakeLists.txt
# ~~~

add_library(dumb_fht dumb_fht.c)
1 change: 1 addition & 0 deletions test/run_oss_cpp_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ build_executorch() {
cmake . \
-DCMAKE_INSTALL_PREFIX=cmake-out \
-DEXECUTORCH_USE_CPP_CODE_COVERAGE=ON \
-DEXECUTORCH_BUILD_KERNELS_CUSTOM=ON \
-DEXECUTORCH_BUILD_KERNELS_OPTIMIZED=ON \
-DEXECUTORCH_BUILD_KERNELS_QUANTIZED=ON \
-DEXECUTORCH_BUILD_EXTENSION_DATA_LOADER=ON \
Expand Down
11 changes: 11 additions & 0 deletions test/utils/OSSTestConfig.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
{ "tests": [
{
"directory": "extension/llm/custom_ops/spinquant/test",
"sources": [
"fast_hadamard_transform_test.cpp",
"fast_hadamard_transform_test_impl.cpp",
"op_fast_hadamard_transform_test.cpp"
],
"additional_libs": [
"custom_ops"
]
},
{
"directory": "extension/data_loader/test",
"sources": [
Expand Down
Loading