okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5649b9e52
main
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 ) 8if ( NOT DEFINED CMAKE_HIP_ARCHITECTURES) 9set ( CMAKE_HIP_ARCHITECTURES gfx906 CACHE STRING "HIP architectures" FORCE ) 10endif () 11enable_language ( HIP ) 12find_package ( hip REQUIRED ) 13find_package ( hipblas REQUIRED ) 14add_library ( CUDA::cudart INTERFACE IMPORTED ) 15target_link_libraries ( CUDA::cudart INTERFACE hip::host ) 16target_compile_definitions ( CUDA::cudart INTERFACE STRATA_HIP_GFX906=1 __HIP_PLATFORM_AMD__=1 ) 17target_include_directories ( CUDA::cudart BEFORE INTERFACE ${ CMAKE_CURRENT_SOURCE_DIR } /include/strata/platform/hip_compat ) 18add_library ( CUDA::cublas INTERFACE IMPORTED ) 19target_link_libraries ( CUDA::cublas INTERFACE roc::hipblas CUDA::cudart ) 20set ( CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES ${ CMAKE_CURRENT_SOURCE_DIR } /include/strata/platform/hip_compat ) 21include_directories ( BEFORE ${ CMAKE_CURRENT_SOURCE_DIR } /include/strata/platform/hip_compat ) 22add_compile_definitions ( STRATA_HIP_GFX906=1 ) 23message ( STATUS "Strata: HIP (gfx906 compat) enabled, arch ${ CMAKE_HIP_ARCHITECTURES } " ) 24set ( _strata_gpu_runtime_target CUDA::cudart ) 25set ( _strata_gpu_blas_target CUDA::cublas ) 26set ( _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. 33set ( _strata_gg " ${ _strata_ggml_root } /ggml" ) 34set ( _strata_mmq " ${ _strata_gg } /src/ggml-cuda" ) 35set ( _strata_quantize_cu " ${ _strata_mmq } /quantize.cu" ) # the overlay's copy when the trellis overlay is on: it includes mmq.cuh 36if ( _strata_mmq_tq_overlay) 37set ( _strata_quantize_cu " ${ _strata_tq_overlay_dir } /quantize.cu" ) 38endif () 39if ( NOT EXISTS " ${ _strata_mmq } /common.cuh" OR NOT EXISTS " ${ _strata_mmq } /quantize.cu" ) 40message ( FATAL_ERROR "The selected GGML source is missing the ggml-cuda MMQ sources: ${ _strata_mmq } " ) 41endif () 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) 43set ( _strata_mmq_sources 44src/prefill/moe_mmq.cu 45src/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) 48set ( _strata_pf_own_sources 49src/prefill/moe_fused.cu 50src/prefill/moe_fused_iq.cu 51src/prefill/moe_fused_iq_q36.cu # the same kernels again for Qwen3.6's expert shape (gfx12: strata-q36's fused prompt experts) 52src/prefill/moe_f16_kq.cu 53src/prefill/moe_p1a.cu 54src/prefill/moe_helpers.cu ) # the gathers, SwiGLU passes and row table (cut from moe_mmq.cu) 55if ( 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 58list ( APPEND _strata_mmq_types q4_k q5_k q5_1 q6_k ) 59endif () 60foreach ( _t IN LISTS _strata_mmq_types) 61list ( APPEND _strata_mmq_sources " ${ _strata_mmq } /template-instances/mmq-instance- ${ _t } .cu" ) 62endforeach () 63if ( _strata_mmq_tq_overlay) 64foreach ( _t tq2_t tqk6 tqk7 tqp5 tqp6 tqp7) 65list ( APPEND _strata_mmq_sources " ${ _strata_tq_overlay_dir } /template-instances/mmq-instance- ${ _t } .cu" ) 66endforeach () 67endif () 68set_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 70set_source_files_properties ( $ { _strata_pf_own_sources } 71PROPERTIES INCLUDE_DIRECTORIES " ${ STRATA_HIP_COMPAT_INCLUDE_DIR } " ) 72add_library ( strata_mmq STATIC ${ _strata_mmq_sources }) 73add_library ( strata_pf_own STATIC ${ _strata_pf_own_sources }) 74target_include_directories ( strata_mmq 75PRIVATE " ${ _strata_gg } /include" " ${ _strata_gg } /src" " ${ _strata_mmq } " 76PUBLIC ${ CMAKE_CURRENT_SOURCE_DIR } /include ) 77if ( _strata_mmq_tq_overlay) 78target_include_directories ( strata_mmq BEFORE PRIVATE ${ _strata_tq_overlay_dir } ${ _strata_tq_overlay_dir } /include ) 79endif () 80target_compile_definitions ( strata_mmq PRIVATE GGML_USE_HIP=1 ) 81if ( STRATA_MMQ_KQUANTS) 82target_compile_definitions ( strata_mmq PRIVATE STRATA_MMQ_KQUANTS=1 ) 83endif () 84if ( STRATA_ORCA_Q4KS_MMQ) 85target_compile_definitions ( strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1 ) 86endif () 87if ( _strata_mmq_tq) 88target_compile_definitions ( strata_mmq PRIVATE STRATA_MMQ_TQ=1 ) 89endif () 90if ( _strata_mmq_tqp) 91target_compile_definitions ( strata_mmq PRIVATE STRATA_MMQ_TQP=1 ) 92endif () 93target_link_libraries ( strata_mmq PUBLIC ggml-base hip::host roc::hipblas ) 94strata_pf_own_mirror () 95target_link_libraries ( strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas ) 96target_link_libraries ( strata_mmq PUBLIC strata_pf_own ) 97target_link_libraries ( strata_prefill PUBLIC strata_mmq ) 98target_compile_definitions ( strata_prefill PUBLIC STRATA_PREFILL_MMQ=1 STRATA_PREFILL_FUSED=1 ) 99set ( _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 ) 105set ( _strata_gg " ${ _strata_ggml_root } /ggml" ) 106set ( _strata_pf_own_sources 107src/prefill/moe_fused.cu 108src/prefill/moe_fused_iq.cu 109src/prefill/moe_fused_iq_q36.cu 110src/prefill/moe_f16_kq.cu 111src/prefill/moe_p1a.cu 112src/prefill/moe_helpers.cu ) 113set_source_files_properties ( $ { _strata_pf_own_sources } PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES " ${ STRATA_HIP_COMPAT_INCLUDE_DIR } " ) 114add_library ( strata_pf_own STATIC ${ _strata_pf_own_sources } src/prefill/mmq_absent.cpp ) 115target_include_directories ( strata_pf_own PRIVATE " ${ _strata_gg } /include" " ${ _strata_gg } /src" PUBLIC ${ CMAKE_CURRENT_SOURCE_DIR } /include ) 116target_compile_definitions ( strata_pf_own PRIVATE GGML_USE_HIP=1 ) 117target_link_libraries ( strata_pf_own PUBLIC strata_quant ggml-base hip::host roc::hipblas ) 118target_link_libraries ( strata_prefill PUBLIC strata_pf_own ) 119target_compile_definitions ( strata_prefill PUBLIC STRATA_PREFILL_FUSED=1 ) 120set ( _strata_pf_tests_lib strata_pf_own ) 121message ( 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 ) 126if ( 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) 128find_package ( Python3 QUIET COMPONENTS Interpreter ) 129if ( Python3_Interpreter_FOUND) 130add_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> ) 131set_tests_properties ( no_mmq_symbols PROPERTIES TIMEOUT 900 ) 132endif () 133endif () 134# Aurora: the gfx11 fused experts against the FP64 reference and MMQ (tests/cuda's test; exit 77 off gfx11) 135add_executable ( hip_prefill_fused_moe tests/cuda/prefill_fused_moe_test.cpp ) 136target_include_directories ( hip_prefill_fused_moe PRIVATE " ${ _strata_gg } /include" ) 137target_link_libraries ( hip_prefill_fused_moe PRIVATE ${ _strata_pf_tests_lib } ggml-base strata_hip_runtime ) 138add_test ( NAME hip_prefill_fused_moe COMMAND hip_prefill_fused_moe ) 139set_tests_properties ( hip_prefill_fused_moe PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300 ) 140list ( APPEND _strata_hip_tests hip_prefill_fused_moe ) 141add_executable ( hip_prefill_fused_iq tests/cuda/prefill_fused_iq_test.cpp ) 142target_include_directories ( hip_prefill_fused_iq PRIVATE " ${ _strata_gg } /include" ) 143target_link_libraries ( hip_prefill_fused_iq PRIVATE ${ _strata_pf_tests_lib } ggml-base strata_hip_runtime ) 144add_test ( NAME hip_prefill_fused_iq COMMAND hip_prefill_fused_iq ) 145set_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) 147add_test ( NAME hip_prefill_fused_iq_f16 COMMAND hip_prefill_fused_iq ) 148set_tests_properties ( hip_prefill_fused_iq_f16 PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ENVIRONMENT "STRATA_PF_F16=1;STRATA_PF_F16_KQ=1" ) 149list ( 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) 152add_executable ( hip_prefill_fused_q36 tests/cuda/prefill_fused_q36_test.cpp ) 153target_include_directories ( hip_prefill_fused_q36 PRIVATE " ${ _strata_gg } /include" ) 154target_link_libraries ( hip_prefill_fused_q36 PRIVATE ${ _strata_pf_tests_lib } ggml-base strata_hip_runtime ) 155add_test ( NAME hip_prefill_fused_q36 COMMAND hip_prefill_fused_q36 ) 156set_tests_properties ( hip_prefill_fused_q36 PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900 ) 157list ( 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 159add_executable ( hip_prefill_expert_paths_bench tests/cuda/prefill_expert_paths_bench.cpp ) 160target_include_directories ( hip_prefill_expert_paths_bench PRIVATE " ${ _strata_gg } /include" ) 161target_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 ) 162add_test ( NAME hip_prefill_expert_paths_bench COMMAND hip_prefill_expert_paths_bench --quick ) 163set_tests_properties ( hip_prefill_expert_paths_bench PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600 ) 164list ( 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 167set_source_files_properties ( src/prefill/gr_upmix_parity.cu PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES " ${ STRATA_HIP_COMPAT_INCLUDE_DIR } " ) 168add_executable ( hip_gr_upmix_parity src/prefill/gr_upmix_parity.cu ) 169target_link_libraries ( hip_gr_upmix_parity PRIVATE strata_prefill strata_hip_runtime ) 170add_test ( NAME hip_gr_upmix_parity COMMAND hip_gr_upmix_parity --max-t 8192 ) 171set_tests_properties ( hip_gr_upmix_parity PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300 ) 172list ( 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) 175add_test ( NAME hip_prefill_fused_iq_q8d COMMAND hip_prefill_fused_iq "--only=S / Q8_0" --edge --no-timing --no-kq ) 176set_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 178add_test ( NAME hip_prefill_fused_iq_q8gu COMMAND hip_prefill_fused_iq "--only=Q8_0 / Q8_0" --edge --no-timing --no-kq ) 179set_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) 181add_test ( NAME hip_prefill_fused_iq_q6k COMMAND hip_prefill_fused_iq "--only=Q6_K + Q8_0" --edge --no-timing ) 182set_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 184add_test ( NAME hip_prefill_fused_iq_tq COMMAND hip_prefill_fused_iq "--only=+ T" --edge --no-timing ) 185set_tests_properties ( hip_prefill_fused_iq_tq PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900 ENVIRONMENT "STRATA_PF_FUSED_TQ=1" ) 186add_test ( NAME hip_prefill_fused_iq_tqh COMMAND hip_prefill_fused_iq "--only=+ T" --folded --edge --no-timing ) 187set_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) 190set_source_files_properties ( tests/cuda/prefill_decode_golden.cu PROPERTIES LANGUAGE HIP INCLUDE_DIRECTORIES " ${ STRATA_HIP_COMPAT_INCLUDE_DIR } " ) 191add_executable ( hip_prefill_decode_golden tests/cuda/prefill_decode_golden.cu ) 192target_include_directories ( hip_prefill_decode_golden PRIVATE $ <TARGET_PROPERTY:strata_pf_own,INCLUDE_DIRECTORIES> ) 193target_compile_definitions ( hip_prefill_decode_golden PRIVATE $ <TARGET_PROPERTY:strata_pf_own,COMPILE_DEFINITIONS> ) 194target_compile_options ( hip_prefill_decode_golden PRIVATE $ <TARGET_PROPERTY:strata_pf_own,COMPILE_OPTIONS> ) 195target_link_libraries ( hip_prefill_decode_golden PRIVATE ggml-base strata_hip_runtime ) 196add_test ( NAME hip_prefill_decode_golden COMMAND hip_prefill_decode_golden ${ CMAKE_CURRENT_SOURCE_DIR } /tests/cuda/prefill_decode_golden.txt ) 197set_tests_properties ( hip_prefill_decode_golden PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120 ) 198list ( APPEND _strata_hip_tests hip_prefill_decode_golden ) 199 200if ( 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. 202add_executable ( hip_prefill_mmq_parity tests/hip/prefill_mmq_parity.cpp ) 203target_include_directories ( hip_prefill_mmq_parity PRIVATE " ${ _strata_gg } /include" ) 204target_link_libraries ( hip_prefill_mmq_parity PRIVATE strata_prefill ) 205add_test ( NAME hip_prefill_mmq_parity COMMAND hip_prefill_mmq_parity ) 206set_tests_properties ( hip_prefill_mmq_parity PROPERTIES TIMEOUT 120 ) 207list ( APPEND _strata_hip_tests hip_prefill_mmq_parity ) 208endif () 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) 214file ( 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 ) 216if ( _strata_mmq) 217file ( GLOB _strata_mmq_cu ${ _strata_mmq } /*.cu ${ _strata_mmq } /template-instances/*.cu ) 218list ( APPEND _strata_cu ${ _strata_mmq_cu }) 219target_compile_definitions ( strata_mmq PRIVATE GGML_USE_HIP ) 220target_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. 223if ( TARGET strata_mmq_w4a4) 224target_compile_definitions ( strata_mmq_w4a4 PRIVATE GGML_USE_HIP ) 225endif () 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) 230if ( _strata_mmq_tq_overlay) 231set ( _strata_tq_hip ${ _strata_tq_overlay_dir } /quantize.cu ) 232foreach ( _t tq2_t tqk6 tqk7 tqp5 tqp6 tqp7) 233list ( APPEND _strata_tq_hip ${ _strata_tq_overlay_dir } /template-instances/mmq-instance- ${ _t } .cu ) 234endforeach () 235list ( APPEND _strata_cu ${ _strata_tq_hip }) 236endif () 237target_sources ( strata_pf_own PRIVATE ${ CMAKE_CURRENT_SOURCE_DIR } /src/prefill/moe_f16_kq.cu ) # (Strata's own: beside moe_fused_iq.cu) 238target_sources ( strata_pf_own PRIVATE ${ CMAKE_CURRENT_SOURCE_DIR } /src/prefill/moe_p1a.cu ) # (the same: moe_fused_iq.cu calls it) 239endif () 240set_source_files_properties ( $ { _strata_cu } PROPERTIES LANGUAGE HIP ) 241endif () 242 243if ( STRATA_HIP_GFX906) 244add_executable ( gfx906_qsa_query_swizzle_probe bench/gfx906_qsa_query_swizzle_probe.cpp ) 245target_link_libraries ( gfx906_qsa_query_swizzle_probe PRIVATE strata_kernels ${ _strata_gpu_runtime_target }) 246endif () 247endmacro ()