Skip to content

Commit b623687

Browse files
authored
[ExecuTorch][llm] Add quantized_moe_ffn custom op with C++ kernel (pytorch#21533)
This PR was created by the merge bot to help merge the original PR into the main branch. ghstack PR number: pytorch#21119 by @digantdesai ^ Please use this as the source of truth for the PR details, comments, and reviews ghstack PR base: https://github.com/pytorch/executorch/tree/gh/digantdesai/69/base ghstack PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/69/head Merge bot PR base: https://github.com/pytorch/executorch/tree/main Merge bot PR head: https://github.com/pytorch/executorch/tree/gh/digantdesai/69/orig Differential Revision: [D102382000](https://our.internmc.facebook.com/intern/diff/D102382000/) @diff-train-skip-merge Co-authored-by: Digant Desai <digantdesai@meta.com>
1 parent fdb88c3 commit b623687

7 files changed

Lines changed: 1092 additions & 3 deletions

File tree

‎extension/llm/custom_ops/CMakeLists.txt‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,33 @@ target_include_directories(
8383
)
8484
target_link_libraries(custom_ops PUBLIC ${custom_ops_libs} executorch_core)
8585

86+
# The MoE kernel always compiles with a reference fallback (unpack + dequant +
87+
# cpublas::gemm) using the torchao weight_packing headers from third-party/ao
88+
# (already on the include path). Pass
89+
# -DEXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE=ON to additionally link the
90+
# optimized torchao linear_operator (fused INT8-dyn-act GEMM, aarch64 NEON
91+
# dotprod).
92+
option(EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE
93+
"Link the optimized torchao linear kernel for llama::quantized_moe_ffn"
94+
OFF
95+
)
96+
if(EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE)
97+
if(NOT TARGET torchao_ops_linear_8bit_act_xbit_weight_executorch)
98+
message(
99+
FATAL_ERROR
100+
"EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE=ON requires target "
101+
"torchao_ops_linear_8bit_act_xbit_weight_executorch, which is not "
102+
"defined. Build the torchao ops or set this option OFF."
103+
)
104+
endif()
105+
# Compile definition and link must be gated on the same condition, else the
106+
# ENABLE_QUANTIZED_MOE_FFN path compiles without the library that defines it.
107+
target_compile_definitions(custom_ops PUBLIC ENABLE_QUANTIZED_MOE_FFN=1)
108+
target_link_libraries(
109+
custom_ops PUBLIC torchao_ops_linear_8bit_act_xbit_weight_executorch
110+
)
111+
endif()
112+
86113
target_compile_options(custom_ops PUBLIC ${_common_compile_options})
87114

88115
install(
@@ -98,6 +125,7 @@ if(EXECUTORCH_BUILD_KERNELS_LLM_AOT)
98125
custom_ops_aot_lib SHARED
99126
${_custom_ops__srcs}
100127
${CMAKE_CURRENT_SOURCE_DIR}/op_sdpa_aot.cpp
128+
${CMAKE_CURRENT_SOURCE_DIR}/op_moe_aot.cpp
101129
${CMAKE_CURRENT_SOURCE_DIR}/op_fast_hadamard_transform_aten.cpp
102130
${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop.cpp
103131
${CMAKE_CURRENT_SOURCE_DIR}/op_tile_crop_aot.cpp
@@ -134,6 +162,23 @@ if(EXECUTORCH_BUILD_KERNELS_LLM_AOT)
134162
custom_ops_aot_lib PUBLIC cpublas torch extension_tensor
135163
extension_threadpool
136164
)
165+
if(EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE)
166+
if(NOT TARGET torchao_ops_linear_8bit_act_xbit_weight_executorch)
167+
message(
168+
FATAL_ERROR
169+
"EXECUTORCH_BUILD_KERNELS_LLM_QUANTIZED_MOE=ON requires target "
170+
"torchao_ops_linear_8bit_act_xbit_weight_executorch, which is not "
171+
"defined. Build the torchao ops or set this option OFF."
172+
)
173+
endif()
174+
target_compile_definitions(
175+
custom_ops_aot_lib PUBLIC ENABLE_QUANTIZED_MOE_FFN=1
176+
)
177+
target_link_libraries(
178+
custom_ops_aot_lib
179+
PUBLIC torchao_ops_linear_8bit_act_xbit_weight_executorch
180+
)
181+
endif()
137182
if(WIN32)
138183
# There is no direct replacement for libpthread.so on Windows. For the
139184
# Windows build, link directly against pthreadpool and cpuinfo.

0 commit comments

Comments
 (0)