@@ -36,9 +36,13 @@ endif()
3636add_compile_options ("-Wall" "-Werror" "-Wno-deprecated" "-Wno-shorten-64-to-32" )
3737
3838include (CMakePrintHelpers)
39+ include (${CMAKE_CURRENT_SOURCE_DIR} /Utils.cmake)
40+
3941message ("TORCHAO_INCLUDE_DIRS: ${TORCHAO_INCLUDE_DIRS} " )
4042include_directories (${TORCHAO_INCLUDE_DIRS} )
4143
44+
45+ # Build cpu/aarch64 kernels
4246if (TORCHAO_BUILD_CPU_AARCH64)
4347 message (STATUS "Building with cpu/aarch64" )
4448 add_compile_definitions (TORCHAO_BUILD_CPU_AARCH64)
@@ -77,29 +81,50 @@ if(TORCHAO_BUILD_CPU_AARCH64)
7781 add_compile_definitions (TORCHAO_ENABLE_ARM_I8MM)
7882 endif ()
7983
80- # Defines torchao_kernels_aarch64
81- add_subdirectory (kernels/cpu/aarch64)
82-
8384 if (TORCHAO_BUILD_KLEIDIAI)
8485 message (STATUS "Building with Arm KleidiAI library" )
8586 add_compile_definitions (TORCHAO_ENABLE_KLEIDI)
8687 endif ()
88+
89+ # Defines torchao_kernels_aarch64
90+ add_subdirectory (kernels/cpu/aarch64)
8791endif ()
8892
89- # Add quantized operation dir
90- add_subdirectory (ops/linear_8bit_act_xbit_weight)
91- add_subdirectory (ops/embedding_xbit)
92-
93- # ATen ops lib
94- if (TORCHAO_BUILD_ATEN_OPS)
95- add_library (torchao_ops_aten SHARED)
96- target_link_libraries (
97- torchao_ops_aten PRIVATE
98- torchao_ops_linear_8bit_act_xbit_weight_aten
99- torchao_ops_embedding_xbit_aten
93+
94+
95+ if (NOT TARGET cpuinfo)
96+ # For some reason cpuinfo package has unused functions/variables
97+ # TODO (T215533422): fix upstream
98+ add_compile_options (-Wno-unused-function -Wno-unused-variable )
99+ include (FetchContent)
100+ FetchContent_Declare(cpuinfo
101+ GIT_REPOSITORY https://github.com/pytorch/cpuinfo.git
102+ GIT_TAG c61fe919607bbc534d7a5a5707bdd7041e72c5ff)
103+ FetchContent_MakeAvailable(
104+ cpuinfo)
105+ endif ()
106+
107+ # Build ATen ops
108+ if (TORCHAO_BUILD_ATEN_OPS)
109+ find_package (Torch REQUIRED)
110+ set (_torchao_op_srcs_aten)
111+ list (APPEND _torchao_op_srcs_aten
112+ ops/embedding_xbit/op_embedding_xbit_aten.cpp
113+ ops/linear_8bit_act_xbit_weight/linear_8bit_act_xbit_weight.cpp
114+ ops/linear_8bit_act_xbit_weight/op_linear_8bit_act_xbit_weight_aten.cpp
100115 )
116+ list (TRANSFORM _torchao_op_srcs_aten PREPEND "${CMAKE_CURRENT_SOURCE_DIR} /" )
117+ add_library (torchao_ops_aten SHARED ${_torchao_op_srcs_aten} )
118+ target_link_torchao_parallel_backend(torchao_ops_aten "${TORCHAO_PARALLEL_BACKEND} " )
119+ if (TORCHAO_BUILD_CPU_AARCH64)
120+ target_link_libraries (torchao_ops_aten PRIVATE torchao_kernels_aarch64)
121+ endif ()
122+ target_link_libraries (torchao_ops_aten PRIVATE cpuinfo)
123+ target_include_directories (torchao_ops_aten PRIVATE "${TORCH_INCLUDE_DIRS} " )
124+ target_link_libraries (torchao_ops_aten PRIVATE "${TORCH_LIBRARIES} " )
125+ target_compile_definitions (torchao_ops_aten PRIVATE USE_ATEN=1)
101126
102- # Add MPS support if enabled
127+ # Add MPS support if enabled
103128 if (TORCHAO_BUILD_MPS_OPS)
104129 message (STATUS "Building with MPS support" )
105130 add_subdirectory (ops/mps)
@@ -114,18 +139,38 @@ if (TORCHAO_BUILD_ATEN_OPS)
114139 )
115140endif ()
116141
117- # Build executorch lib if enabled
142+
143+ # Build ExecuTorch ops
118144if (TORCHAO_BUILD_EXECUTORCH_OPS)
119- add_library (torchao_ops_executorch STATIC )
120- target_link_libraries (torchao_ops_executorch PRIVATE
121- torchao_ops_linear_8bit_act_xbit_weight_executorch
122- torchao_ops_embedding_xbit_executorch
145+ # ExecuTorch package is not required, but EXECUTORCH_INCLUDE_DIRS and EXECUTORCH_LIBRARIES must
146+ # be defined and EXECUTORCH_LIBRARIES must include the following libraries installed by ExecuTorch:
147+ # libexecutorch.a
148+ # libextension_threadpool.a
149+ # libcpuinfo.a
150+ # libpthreadpool.a
151+ if (NOT DEFINED EXECUTORCH_INCLUDE_DIRS AND NOT DEFINED EXECUTORCH_LIBRARIES)
152+ message (WARNING "EXECUTORCH_INCLUDE_DIRS and EXECUTORCH_LIBRARIES are not defined. Looking for ExecuTorch." )
153+ find_package (ExecuTorch HINTS ${CMAKE_PREFIX_PATH} /executorch/share/cmake)
154+ endif ()
155+ set (_torchao_op_srcs_executorch)
156+ list (APPEND _torchao_op_srcs_executorch
157+ ops/embedding_xbit/op_embedding_xbit_executorch.cpp
158+ ops/linear_8bit_act_xbit_weight/linear_8bit_act_xbit_weight.cpp
159+ ops/linear_8bit_act_xbit_weight/op_linear_8bit_act_xbit_weight_executorch.cpp
123160 )
161+ list (TRANSFORM _torchao_op_srcs_executorch PREPEND "${CMAKE_CURRENT_SOURCE_DIR} /" )
162+ add_library (torchao_ops_executorch STATIC ${_torchao_op_srcs_executorch} )
163+ target_link_torchao_parallel_backend(torchao_ops_executorch executorch)
164+ target_include_directories (torchao_ops_executorch PRIVATE "${EXECUTORCH_INCLUDE_DIRS} " )
165+ target_compile_definitions (torchao_ops_executorch PRIVATE USE_EXECUTORCH=1)
166+ target_link_libraries (torchao_ops_executorch PRIVATE "${EXECUTORCH_LIBRARIES} " )
167+ if (TORCHAO_BUILD_CPU_AARCH64)
168+ target_link_libraries (torchao_ops_executorch PRIVATE torchao_kernels_aarch64)
169+ endif ()
170+ target_link_libraries (torchao_ops_executorch PRIVATE cpuinfo)
124171 install (
125172 TARGETS
126173 torchao_ops_executorch
127- torchao_ops_linear_8bit_act_xbit_weight_executorch
128- torchao_ops_embedding_xbit_executorch
129174 EXPORT _targets
130175 DESTINATION lib
131176 )
0 commit comments