-
Notifications
You must be signed in to change notification settings - Fork 13.6k
ggml-cpu: Add CPU backend support for KleidiAI library #11390
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
ggerganov
merged 10 commits into
ggml-org:master
from
chaxu01:feature/kleidiai-cpu-backend
Feb 20, 2025
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
6adca19
ggml-cpu: Add CPU backend support for KleidiAI library
chaxu01 119d3bf
Add environmental variable GGML_KLEIDIAI_SME
chaxu01 f4eb1b3
Add support for multithread LHS conversion
chaxu01 3e08f37
Switch kernel selection order to dotprod and i8mm
chaxu01 9edd107
updates for review comments
chaxu01 e04880f
More updates for review comments
chaxu01 ca0c8b6
Reorganize and rename KleidiAI files
chaxu01 02315a8
Move ggml-cpu-traits.h to source file
chaxu01 09436f4
Update cmake for SME build and add alignment for SME
chaxu01 710552f
Remove append GGML_USE_CPU_KLEIDIAI to the GGML_CDEF_PUBLIC list
chaxu01 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -111,21 +111,23 @@ function(ggml_add_cpu_backend_variant_impl tag_name) | |
| function(check_arm_feature tag code) | ||
| set(CMAKE_REQUIRED_FLAGS_SAVE ${CMAKE_REQUIRED_FLAGS}) | ||
| set(CMAKE_REQUIRED_FLAGS "${ARM_MCPU_FLAG}+${tag}") | ||
| check_cxx_source_runs( | ||
| "${code}" | ||
| GGML_MACHINE_SUPPORTS_${tag} | ||
| ) | ||
| check_cxx_source_runs("${code}" GGML_MACHINE_SUPPORTS_${tag}) | ||
| if (GGML_MACHINE_SUPPORTS_${tag}) | ||
| set(ARM_MCPU_FLAG_FIX "${ARM_MCPU_FLAG_FIX}+${tag}" PARENT_SCOPE) | ||
| else() | ||
| set(ARM_MCPU_FLAG_FIX "${ARM_MCPU_FLAG_FIX}+no${tag}" PARENT_SCOPE) | ||
| set(CMAKE_REQUIRED_FLAGS "${ARM_MCPU_FLAG}+no${tag}") | ||
| check_cxx_source_compiles("${code}" GGML_MACHINE_SUPPORTS_no${tag}) | ||
| if (GGML_MACHINE_SUPPORTS_no${tag}) | ||
| set(ARM_MCPU_FLAG_FIX "${ARM_MCPU_FLAG_FIX}+no${tag}" PARENT_SCOPE) | ||
| endif() | ||
| endif() | ||
| set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_SAVE}) | ||
| endfunction() | ||
|
|
||
| check_arm_feature(dotprod "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vdotq_s32(_s, _a, _b); return 0; }") | ||
| check_arm_feature(i8mm "#include <arm_neon.h>\nint main() { int8x16_t _a, _b; volatile int32x4_t _s = vmmlaq_s32(_s, _a, _b); return 0; }") | ||
| check_arm_feature(sve "#include <arm_sve.h>\nint main() { svfloat32_t _a, _b; volatile svfloat32_t _c = svadd_f32_z(svptrue_b8(), _a, _b); return 0; }") | ||
| check_arm_feature(sme "#include <arm_sme.h>\n__arm_locally_streaming int main() { __asm__ volatile(\"smstart; smstop;\"); return 0; }") | ||
|
|
||
| list(APPEND ARCH_FLAGS "${ARM_MCPU_FLAG}${ARM_MCPU_FLAG_FIX}") | ||
| else() | ||
|
|
@@ -150,7 +152,7 @@ function(ggml_add_cpu_backend_variant_impl tag_name) | |
| if (ARM_FEATURE_RESULT) | ||
| message(WARNING "Failed to get ARM features") | ||
| else() | ||
| foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC) | ||
| foreach(feature DOTPROD SVE MATMUL_INT8 FMA FP16_VECTOR_ARITHMETIC SME) | ||
| string(FIND "${ARM_FEATURE}" "__ARM_FEATURE_${feature} 1" feature_pos) | ||
| if (NOT ${feature_pos} EQUAL -1) | ||
| message(STATUS "ARM feature ${feature} enabled") | ||
|
|
@@ -316,6 +318,95 @@ function(ggml_add_cpu_backend_variant_impl tag_name) | |
| target_compile_definitions(${GGML_CPU_NAME} PRIVATE GGML_USE_CPU_AARCH64) | ||
| endif() | ||
|
|
||
| if (GGML_CPU_KLEIDIAI) | ||
| message(STATUS "Using KleidiAI optimized kernels if applicable") | ||
|
|
||
| # Disable the KleidiAI tests | ||
| set(KLEIDIAI_BUILD_TESTS OFF) | ||
|
|
||
| # Fetch KleidiAI sources: | ||
| include(FetchContent) | ||
| set(KLEIDIAI_COMMIT_TAG "v1.3.0") | ||
| set(KLEIDIAI_DOWNLOAD_URL "https://github.com/ARM-software/kleidiai/archive/refs/tags/${KLEIDIAI_COMMIT_TAG}.tar.gz") | ||
| set(KLEIDIAI_ARCHIVE_MD5 "060bd2dc64642b091f461cc8dd7426d9") | ||
|
|
||
| if (POLICY CMP0135) | ||
| cmake_policy(SET CMP0135 NEW) | ||
| endif() | ||
|
|
||
| FetchContent_Declare(KleidiAI_Download | ||
| URL ${KLEIDIAI_DOWNLOAD_URL} | ||
| DOWNLOAD_EXTRACT_TIMESTAMP NEW | ||
| URL_HASH MD5=${KLEIDIAI_ARCHIVE_MD5}) | ||
|
|
||
| FetchContent_MakeAvailable(KleidiAI_Download) | ||
| FetchContent_GetProperties(KleidiAI_Download | ||
| SOURCE_DIR KLEIDIAI_SRC | ||
| POPULATED KLEIDIAI_POPULATED) | ||
|
|
||
| if (NOT KLEIDIAI_POPULATED) | ||
| message(FATAL_ERROR "KleidiAI source downloaded failed.") | ||
| endif() | ||
|
|
||
| add_compile_definitions(GGML_USE_CPU_KLEIDIAI) | ||
|
|
||
| # Remove kleidiai target after fetching it | ||
| if (TARGET kleidiai) | ||
| set_target_properties(kleidiai PROPERTIES EXCLUDE_FROM_ALL TRUE) | ||
| endif() | ||
|
|
||
| list(APPEND GGML_CPU_SOURCES | ||
| ggml-cpu/ggml-kleidiai/ggml-kleidiai.cpp | ||
| ggml-cpu/ggml-kleidiai/kleidiai_kernels.cpp | ||
| ggml-cpu/ggml-kleidiai/ggml-kleidiai.h | ||
| ggml-cpu/ggml-kleidiai/kleidiai_kernels.h | ||
| ) | ||
|
|
||
| # KleidiAI | ||
| include_directories( | ||
| ${KLEIDIAI_SRC}/ | ||
| ${KLEIDIAI_SRC}/kai/ | ||
| ${KLEIDIAI_SRC}/kai/ukernels/ | ||
| ${KLEIDIAI_SRC}/kai/ukernels/matmul/ | ||
| ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/ | ||
| ${KLEIDIAI_SRC}/kai/ukernels/matmul/pack/) | ||
|
|
||
| set(ARCH_FLAGS_TEMP "${ARCH_FLAGS}") | ||
| if (NOT ARCH_FLAGS_TEMP) | ||
| string(REGEX MATCH "-march=[^ ]+" ARCH_FLAGS_TEMP "${CMAKE_C_FLAGS}") | ||
| endif() | ||
| string(FIND "${ARCH_FLAGS_TEMP}" "+dotprod" DOTPROD_ENABLED) | ||
| string(FIND "${ARCH_FLAGS_TEMP}" "+i8mm" I8MM_ENABLED) | ||
| string(FIND "${ARCH_FLAGS_TEMP}" "+sme" SME_ENABLED) | ||
|
|
||
| set(PRIVATE_ARCH_FLAGS ${ARCH_FLAGS}) | ||
|
|
||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p_f32.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32ps1s0scalef16_qsu4c32s16s0_neon.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/pack/kai_lhs_quant_pack_qsi8d32p_f32_neon.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/pack/kai_rhs_pack_nxk_qsi4c32pscalef16_qsu4c32s16s0.c) | ||
|
|
||
| if (NOT DOTPROD_ENABLED MATCHES -1) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x8_qsi4c32p4x8_1x4x32_neon_dotprod.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p4x4_1x4_neon_dotprod.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x4_qsi4c32p4x4_16x4_neon_dotprod.c) | ||
| endif() | ||
|
|
||
| if (NOT I8MM_ENABLED MATCHES -1) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p4x8_qsi4c32p4x8_16x4_neon_i8mm.c) | ||
| endif() | ||
|
|
||
| if (NOT SME_ENABLED MATCHES -1) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1vlx4_qsi4c32p4vlx4_1vlx4vl_sme2_mopa.c) | ||
| list(APPEND GGML_KLEIDIAI_SOURCES ${KLEIDIAI_SRC}/kai/ukernels/matmul/matmul_clamp_f32_qsi8d32p_qsi4c32p/kai_matmul_clamp_f32_qsi8d32p1x4_qsi4c32p4vlx4_1x4vl_sme2_sdot.c) | ||
| set(PRIVATE_ARCH_FLAGS "${PRIVATE_ARCH_FLAGS}+sve+sve2") | ||
| endif() | ||
|
|
||
| list(APPEND GGML_CDEF_PUBLIC GGML_USE_CPU_KLEIDIAI) | ||
|
||
| set_source_files_properties("${GGML_KLEIDIAI_SOURCES}" PROPERTIES COMPILE_OPTIONS "${PRIVATE_ARCH_FLAGS}") | ||
| list(APPEND GGML_CPU_SOURCES "${GGML_KLEIDIAI_SOURCES}") | ||
| endif() | ||
|
|
||
| message(STATUS "Adding CPU backend variant ${GGML_CPU_NAME}: ${ARCH_FLAGS} ${ARCH_DEFINITIONS}") | ||
| target_sources(${GGML_CPU_NAME} PRIVATE ${GGML_CPU_SOURCES}) | ||
| target_compile_options(${GGML_CPU_NAME} PRIVATE ${ARCH_FLAGS}) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.