okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-mmq-default-off: build the engine without llama.cpp's MMQ kernels by default (-DSTRATA_MMQ=ON keeps the old path; linux-aarch64 unchanged)5649b9e52

main
17.6 KiB247 linesraw
1# cmake/backends/hip.cmake - the AMD (HIP) parts of the root project: the gfx906 compat toolchain and sources, MMQ on HIP.
2#
3# Every block below is a macro: it runs in the scope of the caller (CMakeLists.txt calls it where the block used to be), so variables, targets and
4# their order are those of the single-file CMakeLists.txt it was cut from (cleanup 5.4; the build graph is identical).
5
6# HIP for gfx906 through the CUDA compat layer: the toolchain and the CUDA:: stand-in targets
7macro(strata_hip_gfx906_toolchain)
8  if(NOT DEFINED CMAKE_HIP_ARCHITECTURES)
9    set(CMAKE_HIP_ARCHITECTURES gfx906 CACHE STRING "HIP architectures" FORCE)
10  endif()
11  enable_language(HIP)
12  find_package(hip REQUIRED)
13  find_package(hipblas REQUIRED)
14  add_library(CUDA::cudart INTERFACE IMPORTED)
15  target_link_libraries(CUDA::cudart INTERFACE hip::host)
16  target_compile_definitions(CUDA::cudart INTERFACE STRATA_HIP_GFX906=1 __HIP_PLATFORM_AMD__=1)
17  target_include_directories(CUDA::cudart BEFORE INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include/strata/platform/hip_compat)
18  add_library(CUDA::cublas INTERFACE IMPORTED)
19  target_link_libraries(CUDA::cublas INTERFACE roc::hipblas CUDA::cudart)
20  set(CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES ${CMAKE_CURRENT_SOURCE_DIR}/include/strata/platform/hip_compat)
21  include_directories(BEFORE ${CMAKE_CURRENT_SOURCE_DIR}/include/strata/platform/hip_compat)
22  add_compile_definitions(STRATA_HIP_GFX906=1)
23  message(STATUS "Strata: HIP (gfx906 compat) enabled, arch ${CMAKE_HIP_ARCHITECTURES}")
24  set(_strata_gpu_runtime_target CUDA::cudart)
25  set(_strata_gpu_blas_target CUDA::cublas)
26  set(_strata_gpu_include_directories ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
27endmacro()
28
29# strata_mmq on HIP (gfx11 / gfx12 / gfx906): the same MMQ sources compiled as HIP, and their tests
30macro(strata_hip_mmq)
31    # Compile only the pinned GGML MMQ and activation-quantizer sources used by prefill. This uses
32    # common.cuh's native HIP path without enabling a second GGML HIP backend.
33    set(_strata_gg "${_strata_ggml_root}/ggml")
34    set(_strata_mmq "${_strata_gg}/src/ggml-cuda")
35    set(_strata_quantize_cu "${_strata_mmq}/quantize.cu")   # the overlay's copy when the trellis overlay is on: it includes mmq.cuh
36    if(_strata_mmq_tq_overlay)
37      set(_strata_quantize_cu "${_strata_tq_overlay_dir}/quantize.cu")
38    endif()
39    if(NOT EXISTS "${_strata_mmq}/common.cuh" OR NOT EXISTS "${_strata_mmq}/quantize.cu")
40      message(FATAL_ERROR "The selected GGML source is missing the ggml-cuda MMQ sources: ${_strata_mmq}")
41    endif()
42    # Aurora: moe_fused.cu's gfx11 and moe_fused_iq.cu's gfx11 / gfx12 WMMA kernels for the prompt experts (opt-in STRATA_PF_FUSED=1)
43    set(_strata_mmq_sources
44      src/prefill/moe_mmq.cu
45      src/prefill/ggml_cuda_host.cu
46      "${_strata_quantize_cu}")
47    # P4-SPLIT: Strata's own prompt kernels in a library of their own (strata_pf_own, as on CUDA)
48    set(_strata_pf_own_sources
49      src/prefill/moe_fused.cu
50      src/prefill/moe_fused_iq.cu
51      src/prefill/moe_fused_iq_q36.cu   # the same kernels again for Qwen3.6's expert shape (gfx12: strata-q36's fused prompt experts)
52      src/prefill/moe_f16_kq.cu
53      src/prefill/moe_p1a.cu
54      src/prefill/moe_helpers.cu)   # the gathers, SwiGLU passes and row table (cut from moe_mmq.cu)
55    if(STRATA_MMQ_KQUANTS)
56      # the same K-quant instances as the CUDA branch: on HIP they serve the dense GGUF projections
57      # of Gemm::native (STRATA_DENSE_MMQ=1), the mixed-quant packs' attention/PLE tensors
58      list(APPEND _strata_mmq_types q4_k q5_k q5_1 q6_k)
59    endif()
60    foreach(_t IN LISTS _strata_mmq_types)
61      list(APPEND _strata_mmq_sources "${_strata_mmq}/template-instances/mmq-instance-${_t}.cu")
62    endforeach()
63    if(_strata_mmq_tq_overlay)
64      foreach(_t tq2_t tqk6 tqk7 tqp5 tqp6 tqp7)
65        list(APPEND _strata_mmq_sources "${_strata_tq_overlay_dir}/template-instances/mmq-instance-${_t}.cu")
66      endforeach()
67    endif()
68    set_source_files_properties(${_strata_mmq_sources} ${_strata_pf_own_sources} PROPERTIES LANGUAGE HIP)
69    # moe_fused.cu is Strata's own CUDA-shaped source: it takes the HIP compat headers, ggml's MMQ sources do not
70    set_source_files_properties(${_strata_pf_own_sources}
71                                PROPERTIES INCLUDE_DIRECTORIES "${STRATA_HIP_COMPAT_INCLUDE_DIR}")
72    add_library(strata_mmq STATIC ${_strata_mmq_sources})
73    add_library(strata_pf_own STATIC ${_strata_pf_own_sources})
74    target_include_directories(strata_mmq
75      PRIVATE "${_strata_gg}/include" "${_strata_gg}/src" "${_strata_mmq}"
76      PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
77    if(_strata_mmq_tq_overlay)
78      target_include_directories(strata_mmq BEFORE PRIVATE ${_strata_tq_overlay_dir} ${_strata_tq_overlay_dir}/include)
79    endif()
80    target_compile_definitions(strata_mmq PRIVATE GGML_USE_HIP=1)
81    if(STRATA_MMQ_KQUANTS)
82      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_KQUANTS=1)
83    endif()
84    if(STRATA_ORCA_Q4KS_MMQ)
85      target_compile_definitions(strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1)
86    endif()
87    if(_strata_mmq_tq)
88      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TQ=1)
89    endif()
90    if(_strata_mmq_tqp)
91      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TQP=1)
92    endif()
93    target_link_libraries(strata_mmq PUBLIC ggml-base hip::host roc::hipblas)
94    strata_pf_own_mirror()
95    target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas)
96    target_link_libraries(strata_mmq PUBLIC strata_pf_own)
97    target_link_libraries(strata_prefill PUBLIC strata_mmq)
98    target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_MMQ=1 STRATA_PREFILL_FUSED=1)
99    set(_strata_pf_tests_lib strata_mmq)
100endmacro()
101
102# P4-SPLIT, HIP without llama.cpp's MMQ (STRATA_MMQ=OFF, the default, or STRATA_PREFILL_MMQ off): Strata's own prompt kernels alone (strata_pf_own, with
103# mmq_absent.cpp for the MMQ half of moe_mmq.hpp), compiled as strata_hip_mmq compiles them, with the ggml-base headers only
104macro(strata_hip_pf_own)
105    set(_strata_gg "${_strata_ggml_root}/ggml")
106    set(_strata_pf_own_sources
107      src/prefill/moe_fused.cu
108      src/prefill/moe_fused_iq.cu
109      src/prefill/moe_fused_iq_q36.cu
110      src/prefill/moe_f16_kq.cu
111      src/prefill/moe_p1a.cu
112      src/prefill/moe_helpers.cu)
113    set_source_files_properties(${_strata_pf_own_sources} PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES "${STRATA_HIP_COMPAT_INCLUDE_DIR}")
114    add_library(strata_pf_own STATIC ${_strata_pf_own_sources} src/prefill/mmq_absent.cpp)
115    target_include_directories(strata_pf_own PRIVATE "${_strata_gg}/include" "${_strata_gg}/src" PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
116    target_compile_definitions(strata_pf_own PRIVATE GGML_USE_HIP=1)
117    target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas)
118    target_link_libraries(strata_prefill PUBLIC strata_pf_own)
119    target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_FUSED=1)
120    set(_strata_pf_tests_lib strata_pf_own)
121    message(STATUS "Strata: no llama.cpp MMQ prompt kernels (HIP): Strata's own prompt kernels and the FP16 path")
122endmacro()
123
124# the prompt kernels' tests on HIP: the fused experts' (linked to ${_strata_pf_tests_lib}) and, with MMQ, MMQ's own
125macro(strata_hip_prompt_tests)
126    if(NOT TARGET strata_mmq)
127      # P4-SPLIT: the engine and Strata's prompt library hold none of llama.cpp's MMQ (names in the bytes, nm; tools/check_no_mmq.py)
128      find_package(Python3 QUIET COMPONENTS Interpreter)
129      if(Python3_Interpreter_FOUND)
130        add_test(NAME no_mmq_symbols COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/check_no_mmq.py $<TARGET_FILE:strata> $<TARGET_FILE:strata_pf_own>)
131        set_tests_properties(no_mmq_symbols PROPERTIES TIMEOUT 900)
132      endif()
133    endif()
134    # Aurora: the gfx11 fused experts against the FP64 reference and MMQ (tests/cuda's test; exit 77 off gfx11)
135    add_executable(hip_prefill_fused_moe tests/cuda/prefill_fused_moe_test.cpp)
136    target_include_directories(hip_prefill_fused_moe PRIVATE "${_strata_gg}/include")
137    target_link_libraries(hip_prefill_fused_moe PRIVATE ${_strata_pf_tests_lib} ggml-base strata_hip_runtime)
138    add_test(NAME hip_prefill_fused_moe COMMAND hip_prefill_fused_moe)
139    set_tests_properties(hip_prefill_fused_moe PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
140    list(APPEND _strata_hip_tests hip_prefill_fused_moe)
141    add_executable(hip_prefill_fused_iq tests/cuda/prefill_fused_iq_test.cpp)
142    target_include_directories(hip_prefill_fused_iq PRIVATE "${_strata_gg}/include")
143    target_link_libraries(hip_prefill_fused_iq PRIVATE ${_strata_pf_tests_lib} ggml-base strata_hip_runtime)
144    add_test(NAME hip_prefill_fused_iq COMMAND hip_prefill_fused_iq)
145    set_tests_properties(hip_prefill_fused_iq PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
146    # the same binary on F16 weights (gfx11: the covered formats take moe_f16_kq.cu / native_f16_kernel; elsewhere it is the default run again)
147    add_test(NAME hip_prefill_fused_iq_f16 COMMAND hip_prefill_fused_iq)
148    set_tests_properties(hip_prefill_fused_iq_f16 PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ENVIRONMENT "STRATA_PF_F16=1;STRATA_PF_F16_KQ=1")
149    list(APPEND _strata_hip_tests hip_prefill_fused_iq)
150    # the same kernels at Qwen3.6's expert shape (n_embd 2048, n_ff 512: the copy moe_fused_iq_q36.cu compiles, gfx12): UD-Q4_K_XL's pairs (Q4_K / Q5_K and Q5_K gate/up
151    # with Q5_K / Q6_K downs), UD-Q6_K_XL's Q6_K + Q8_0 and the Q8_0 file's Q8_0 / Q8_0 against the reference and the int8 model (77 off gfx12 and without a card)
152    add_executable(hip_prefill_fused_q36 tests/cuda/prefill_fused_q36_test.cpp)
153    target_include_directories(hip_prefill_fused_q36 PRIVATE "${_strata_gg}/include")
154    target_link_libraries(hip_prefill_fused_q36 PRIVATE ${_strata_pf_tests_lib} ggml-base strata_hip_runtime)
155    add_test(NAME hip_prefill_fused_q36 COMMAND hip_prefill_fused_q36)
156    set_tests_properties(hip_prefill_fused_q36 PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900)
157    list(APPEND _strata_hip_tests hip_prefill_fused_q36)
158    # one prompt layer of experts timed on every path the engine can take (see tests/cuda/prefill_expert_paths_bench.cpp); any AMD card, a small smoke run here
159    add_executable(hip_prefill_expert_paths_bench tests/cuda/prefill_expert_paths_bench.cpp)
160    target_include_directories(hip_prefill_expert_paths_bench PRIVATE "${_strata_gg}/include")
161    target_link_libraries(hip_prefill_expert_paths_bench PRIVATE strata_prefill ${_strata_pf_tests_lib} $<TARGET_NAME_IF_EXISTS:strata_quant> ggml-base strata_hip_runtime)
162    add_test(NAME hip_prefill_expert_paths_bench COMMAND hip_prefill_expert_paths_bench --quick)
163    set_tests_properties(hip_prefill_expert_paths_bench PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
164    list(APPEND _strata_hip_tests hip_prefill_expert_paths_bench)
165    # the hyper-connection read's fused up projection + mix (gr_upmix, STRATA_HC_UPMIX) on gfx11: BF16 images against the default pair, and the FP16 prompt
166    # path's FP16 images (STRATA_HIP_PROMPT_F16=1) against the FP64 reference; exit 77 off gfx11 / without a card
167    set_source_files_properties(src/prefill/gr_upmix_parity.cu PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES "${STRATA_HIP_COMPAT_INCLUDE_DIR}")
168    add_executable(hip_gr_upmix_parity src/prefill/gr_upmix_parity.cu)
169    target_link_libraries(hip_gr_upmix_parity PRIVATE strata_prefill strata_hip_runtime)
170    add_test(NAME hip_gr_upmix_parity COMMAND hip_gr_upmix_parity --max-t 8192)
171    set_tests_properties(hip_gr_upmix_parity PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
172    list(APPEND _strata_hip_tests hip_gr_upmix_parity)
173    # STRATA_PF_FUSED_Q8D=1 (gfx12, opt-in): the pinned UD-IQ4_XS's i-quant / Q8_0 pairs on the fused kernels without the K-quant switch (--no-kq);
174    # off gfx12 the pairs are not covered without STRATA_PF_FUSED_KQ and the test skips (77, as without a card)
175    add_test(NAME hip_prefill_fused_iq_q8d COMMAND hip_prefill_fused_iq "--only=S / Q8_0" --edge --no-timing --no-kq)
176    set_tests_properties(hip_prefill_fused_iq_q8d PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ENVIRONMENT "STRATA_PF_FUSED_Q8D=1")
177    # STRATA_PF_FUSED_Q8GU=1 (gfx12, opt-in): the Q8_0 set's Q8_0 / Q8_0 pair (UD-Q6_K_XL's one Q8_0 layer), as CUDA's _q8gu; --no-kq: the K-quant switch stays unset
178    add_test(NAME hip_prefill_fused_iq_q8gu COMMAND hip_prefill_fused_iq "--only=Q8_0 / Q8_0" --edge --no-timing --no-kq)
179    set_tests_properties(hip_prefill_fused_iq_q8gu PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ENVIRONMENT "STRATA_PF_FUSED_Q8GU=1")
180    # STRATA_PF_FUSED_Q6K=1 (gfx12, opt-in): UD-Q6_K_XL's Q6_K gate/up + Q8_0 down, as CUDA's _q6k; MMQ for Q6_K on HIP is the K-quant library (STRATA_MMQ_KQ=1)
181    add_test(NAME hip_prefill_fused_iq_q6k COMMAND hip_prefill_fused_iq "--only=Q6_K + Q8_0" --edge --no-timing)
182    set_tests_properties(hip_prefill_fused_iq_q6k PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ENVIRONMENT "STRATA_PF_FUSED_Q6K=1;STRATA_MMQ_KQ=1")
183    # STRATA_PF_FUSED_TQ=1 (gfx12, opt-in): the Gyro trellis pairs (TQK6 + TQK7, TQ2_T + TQ2_T), plain and Hadamard-folded (--folded), as CUDA's _tq / _tqh
184    add_test(NAME hip_prefill_fused_iq_tq COMMAND hip_prefill_fused_iq "--only=+ T" --edge --no-timing)
185    set_tests_properties(hip_prefill_fused_iq_tq PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900 ENVIRONMENT "STRATA_PF_FUSED_TQ=1")
186    add_test(NAME hip_prefill_fused_iq_tqh COMMAND hip_prefill_fused_iq "--only=+ T" --folded --edge --no-timing)
187    set_tests_properties(hip_prefill_fused_iq_tqh PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900 ENVIRONMENT "STRATA_PF_FUSED_TQ=1")
188    # the fused prompt kernels' weight decode against the digests CUDA wrote (tests/cuda/prefill_decode_golden.cu): the same file on every vendor
189    # (moe_fused_iq.cu's decode compiled as strata_mmq compiles it: its include directories, definitions and options; exit 77 without a card)
190    set_source_files_properties(tests/cuda/prefill_decode_golden.cu PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES "${STRATA_HIP_COMPAT_INCLUDE_DIR}")
191    add_executable(hip_prefill_decode_golden tests/cuda/prefill_decode_golden.cu)
192    target_include_directories(hip_prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,INCLUDE_DIRECTORIES>)
193    target_compile_definitions(hip_prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,COMPILE_DEFINITIONS>)
194    target_compile_options(hip_prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,COMPILE_OPTIONS>)
195    target_link_libraries(hip_prefill_decode_golden PRIVATE ggml-base strata_hip_runtime)
196    add_test(NAME hip_prefill_decode_golden COMMAND hip_prefill_decode_golden ${CMAKE_CURRENT_SOURCE_DIR}/tests/cuda/prefill_decode_golden.txt)
197    set_tests_properties(hip_prefill_decode_golden PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
198    list(APPEND _strata_hip_tests hip_prefill_decode_golden)
199
200    if(TARGET strata_mmq)   # (P4-SPLIT: MMQ's own)
201    # Numerical diagnostic: synthetic layouts by default, optional first-expert IQ3_XXS/IQ4_NL check with a pack.
202    add_executable(hip_prefill_mmq_parity tests/hip/prefill_mmq_parity.cpp)
203    target_include_directories(hip_prefill_mmq_parity PRIVATE "${_strata_gg}/include")
204    target_link_libraries(hip_prefill_mmq_parity PRIVATE strata_prefill)
205    add_test(NAME hip_prefill_mmq_parity COMMAND hip_prefill_mmq_parity)
206    set_tests_properties(hip_prefill_mmq_parity PROPERTIES TIMEOUT 120)
207    list(APPEND _strata_hip_tests hip_prefill_mmq_parity)
208    endif()
209endmacro()
210
211# HIP gfx906: every CUDA source of the tree (and the ggml-cuda MMQ sources) compiled as HIP
212macro(strata_hip_gfx906_sources)
213if(STRATA_HIP_GFX906)
214  file(GLOB_RECURSE _strata_cu CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/src/*.cu ${CMAKE_CURRENT_SOURCE_DIR}/bench/micro/*.cu
215       ${CMAKE_CURRENT_SOURCE_DIR}/tools/vision/*.cu)
216  if(_strata_mmq)
217    file(GLOB _strata_mmq_cu ${_strata_mmq}/*.cu ${_strata_mmq}/template-instances/*.cu)
218    list(APPEND _strata_cu ${_strata_mmq_cu})
219    target_compile_definitions(strata_mmq PRIVATE GGML_USE_HIP)
220    target_compile_definitions(strata_pf_own PRIVATE GGML_USE_HIP)   # (P4-SPLIT: the files that were in strata_mmq keep its definitions)
221    # #3172: strata_mmq_w4a4 is built from llama.cpp's headers too; without GGML_USE_HIP its common.cuh took the CUDA vendor header
222    # (cuda.h, cuda_bf16.h: not found). Here it only reports that W4A4 (RTX 50's FP4 MMA) is not available.
223    if(TARGET strata_mmq_w4a4)
224      target_compile_definitions(strata_mmq_w4a4 PRIVATE GGML_USE_HIP)
225    endif()
226    # #3271: three more link errors of a 0.1.44+ gfx906 build, all sources that strata_mmq lists but the glob above never saw:
227    # the trellis overlay's quantize.cu and instances (they are generated into the build tree, so the glob missed them: CMake drops a
228    # .cu source whose language is not enabled) and moe_f16_kq.cu, which moe_fused_iq.cu calls (a stub here: no gfx11 device code, so
229    # f16kq::covers() answers false and the int8 kernels run, as on every other card)
230    if(_strata_mmq_tq_overlay)
231      set(_strata_tq_hip ${_strata_tq_overlay_dir}/quantize.cu)
232      foreach(_t tq2_t tqk6 tqk7 tqp5 tqp6 tqp7)
233        list(APPEND _strata_tq_hip ${_strata_tq_overlay_dir}/template-instances/mmq-instance-${_t}.cu)
234      endforeach()
235      list(APPEND _strata_cu ${_strata_tq_hip})
236    endif()
237    target_sources(strata_pf_own PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_f16_kq.cu)   # (Strata's own: beside moe_fused_iq.cu)
238    target_sources(strata_pf_own PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_p1a.cu)    # (the same: moe_fused_iq.cu calls it)
239  endif()
240  set_source_files_properties(${_strata_cu} PROPERTIES LANGUAGE HIP)
241endif()
242
243if(STRATA_HIP_GFX906)
244  add_executable(gfx906_qsa_query_swizzle_probe bench/gfx906_qsa_query_swizzle_probe.cpp)
245  target_link_libraries(gfx906_qsa_query_swizzle_probe PRIVATE strata_kernels ${_strata_gpu_runtime_target})
246endif()
247endmacro()