okami.mom/strata

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

waveringanaport: keep upstream MMQ_GROUP=16 and staged-walk setup; latent paths use LAT_GROUP=64 and their own staging; geometry test names edge074d12d663

main
45.7 KiB585 linesraw
1# cmake/backends/cuda.cmake - the NVIDIA (CUDA) parts of the root project: toolchain setup, llama.cpp's MMQ kernels (strata_mmq, strata_kq), the spill guard.
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# CUDA: runtime library, architectures (enable_language), the architecture checks, the gpu target variables
7macro(strata_cuda_toolchain)
8  # #585: CMake links CUDA code with the STATIC runtime by default, and the targets below also link CUDA::cudart (the
9  # DLL's import library).  MSVC with a CUDA 12.x toolkit - the experimental Pascal/Volta build - then fails with
10  # LNK1169 (every cudart symbol defined twice); GNU ld lets it pass.  That build links the shared runtime alone.  The
11  # ready-made engine (CUDA 13, no STRATA_EXPERIMENTAL_SM60) is unchanged; -DCMAKE_CUDA_RUNTIME_LIBRARY overrides.
12  if(MSVC AND STRATA_EXPERIMENTAL_SM60 AND NOT DEFINED CMAKE_CUDA_RUNTIME_LIBRARY)
13    set(CMAKE_CUDA_RUNTIME_LIBRARY Shared)
14  endif()
15  # #4023: the default below has to be set BEFORE enable_language(CUDA): since CMake 3.18 that call fills an unset
16  # CMAKE_CUDA_ARCHITECTURES with nvcc's own default (sm_75 for CUDA 13), so a test after it never saw "undefined" and
17  # a build with no option was an sm_75 build.  -DCMAKE_CUDA_ARCHITECTURES and the CUDAARCHS environment variable win.
18  set(_strata_cuda_arch_defaulted OFF)
19  if(NOT DEFINED CMAKE_CUDA_ARCHITECTURES AND NOT DEFINED ENV{CUDAARCHS})
20    set(CMAKE_CUDA_ARCHITECTURES 120 CACHE STRING "CUDA architectures")
21    set(_strata_cuda_arch_defaulted ON)
22  endif()
23  enable_language(CUDA)
24  # `enable_language(CUDA)` gives the .cu compile rules but NOT the imported targets, so `CUDA::cudart` has to
25  # be brought in explicitly for the host-side tests that call cudaMalloc themselves.
26  find_package(CUDAToolkit REQUIRED)
27  option(STRATA_EXPERIMENTAL_SM60 "community build for GPUs below compute capability 7.5: Pascal sm_60, Volta sm_70 (#236)" OFF)
28  if(_strata_cuda_arch_defaulted)
29    # a recommendation, not a refusal: the release engine is built for 75;86;89;120, a hand build names its card
30    message(WARNING "Strata: CMAKE_CUDA_ARCHITECTURES was not set, so this build is for ${CMAKE_CUDA_ARCHITECTURES} only (sm_120 = RTX 50). For another "
31                    "card pass it: -DCMAKE_CUDA_ARCHITECTURES=native (the GPUs in this PC), 75 (RTX 20), 86 (RTX 30), "
32                    "89 (RTX 40), or a list like \"86;120\".")
33  endif()
34  set(_strata_cuda_below75 OFF)   # an architecture below sm_75 in the list (only with STRATA_EXPERIMENTAL_SM60): tests of sm_75+ device code are not built
35  foreach(_a IN LISTS CMAKE_CUDA_ARCHITECTURES)
36    string(REGEX REPLACE "-.*$" "" _base "${_a}")     # 120-real / 120-virtual are still 120
37    if(_base MATCHES "^[0-9]+$" AND _base LESS 75)
38      set(_strata_cuda_below75 ON)
39    endif()
40    # Turing port: developed and measured on sm_120 (RTX 50); the tf32 mma in the QSA scorer has a
41    # portable fp32-FMA fallback, so RTX 20 (75), 30 (86), RTX 40 (89) and RTX 50 (120) build too.
42    # Pre-sm_75 is refused.
43    if(_base STREQUAL "native" OR _base STREQUAL "all" OR _base STREQUAL "all-major")
44      continue()
45    endif()
46    # STRATA_EXPERIMENTAL_SM60: a Pascal (sm_60, GP100-class) or Volta (sm_70, V100 / Titan V, #236) build.
47    # These sit below every architecture the kernels were written for, so two intrinsics are replaced below
48    # their arch - `__dp4a` (6.1+) and `__nanosleep` (7.0+) - in include/strata/kernels/dp4a.hpp, which is
49    # bit-exact for the first and merely a shorter pause for the second (sm_70 has both).  Community-tested,
50    # not in the ready-made engine; RTX 20/30/40/50 cards must use the release engine instead.  It needs a CUDA
51    # 12.x toolkit: CUDA 13 dropped sm_60 and sm_70 (nvcc fails CMake's compiler test with "compute_70").
52    if(_base LESS 75 AND NOT STRATA_EXPERIMENTAL_SM60)
53      message(FATAL_ERROR
54        "Strata needs an NVIDIA GPU of compute capability 7.5 or newer (RTX 20 / 30 / 40 / 50 series), but "
55        "CMAKE_CUDA_ARCHITECTURES is '${CMAKE_CUDA_ARCHITECTURES}'.  (A Pascal sm_60 or Volta sm_70 build is "
56        "-DSTRATA_EXPERIMENTAL_SM60=ON; upstream does not support it.)")
57    endif()
58    if(_base LESS 52)
59      message(FATAL_ERROR "CMAKE_CUDA_ARCHITECTURES '${CMAKE_CUDA_ARCHITECTURES}': even the experimental build "
60                          "needs compute capability 5.2 (sm_52, Maxwell) or newer")
61    endif()
62    # Maxwell (sm_52, #3218): one reporter built and ran this on two Quadro M6000 (CUDA 12.x, Qwen3.8-Flash-Next
63    # IQ1, about 22 t/s).  It has no native half-precision arithmetic, so it is slower per byte than Pascal; only
64    # this opt-in build gets the floor.  Warn, do not refuse.
65    if(_base LESS 60)
66      message(WARNING "Building for sm_${_base} (Maxwell): community-tested on one reporter's Quadro M6000 only, "
67                      "no native half-precision arithmetic, CUDA 12.x toolkit required (docs/OLDER_GPUS.md).")
68    endif()
69    # #220 / #224: RTX 50 engines built with CUDA 12.8 crashed in the prompt path on Linux (13.0 and newer did not)
70    if(_base GREATER_EQUAL 120 AND CMAKE_CUDA_COMPILER_VERSION VERSION_LESS 13.0)
71      message(WARNING "Building for sm_${_base} (RTX 50) with CUDA ${CMAKE_CUDA_COMPILER_VERSION}: engines built with "
72                      "CUDA 12.8 crashed on long prompts on these cards (issues #220, #224). Use CUDA 13.0 or newer.")
73    endif()
74  endforeach()
75  if(STRATA_EXPERIMENTAL_SM60)
76    # every target sees it: the Pascal / Volta code paths (the Volta prompt attention, PR #600) are compiled into this
77    # build only, so the ready-made engine (sm_75+) keeps exactly the code it had
78    add_compile_definitions(STRATA_EXPERIMENTAL_SM60=1)
79  endif()
80  message(STATUS "Strata: CUDA enabled, arch ${CMAKE_CUDA_ARCHITECTURES}")
81  set(_strata_gpu_runtime_target CUDA::cudart)
82  set(_strata_gpu_blas_target CUDA::cublas)
83  set(_strata_gpu_include_directories ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES})
84endmacro()
85
86# strata_pf_own (Strata's own prompt kernels) with strata_mmq's include directories (in their order, the trellis overlay's
87# first), definitions and options: the same command line for the files that left strata_mmq (CUDA and HIP)
88macro(strata_pf_own_mirror)
89  foreach(_strata_p INCLUDE_DIRECTORIES COMPILE_DEFINITIONS COMPILE_OPTIONS)
90    get_target_property(_strata_v strata_mmq ${_strata_p})
91    if(_strata_v)
92      set_property(TARGET strata_pf_own PROPERTY ${_strata_p} "${_strata_v}")
93    endif()
94  endforeach()
95  target_include_directories(strata_pf_own INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}/include)
96endmacro()
97
98# strata_mmq, strata_mmq_w4a4 and libstrata_kq: llama.cpp's MMQ kernels for the prompt path (CUDA), and their tests
99macro(strata_cuda_mmq)
100    # prompt-speed plan step 2b: llama.cpp's MMQ kernels (ggml-cuda, MIT) for the prompt path's experts, compiled
101    # from the same checkout as ggml above - only mmq.cuh's instances for the packs' types and the quantizer, with
102    # the few host symbols of ggml-cuda.cu they use in src/prefill/ggml_cuda_host.cu
103    set(_strata_gg ${_strata_ggml_root}/ggml)
104    set(_strata_mmq ${_strata_gg}/src/ggml-cuda)
105    set(_strata_quantize_cu ${_strata_mmq}/quantize.cu)   # the overlay's copy when the trellis overlay is on: it includes mmq.cuh
106    if(_strata_mmq_tq_overlay)
107      set(_strata_quantize_cu ${_strata_tq_overlay_dir}/quantize.cu)
108    endif()
109    # #136: the Q2_0 pack's prompt experts on Strata's own int8 kernels (moe_fused.cu, opt-in STRATA_PF_FUSED=1; CUDA
110    # only: the HIP branch below keeps MMQ, and an sm_75 device or an sm_75-only build reports them unavailable)
111    add_library(strata_mmq STATIC src/prefill/moe_mmq.cu src/prefill/moe_iq4_w4a8.cu
112                src/prefill/mmq_nvfp4_w4a8.cu
113                src/prefill/ggml_cuda_host.cu ${_strata_quantize_cu})
114    # M1 of the q3k latent A stage: the down half (IQ4_NL, K = 640) on moe_fused.cu's latent kernel structure
115    target_sources(strata_mmq PRIVATE src/prefill/lat_q3k_down.cu)
116    # M2a of the q3k latent A stage: the gate/up half (Q3_K, K = 640 features of gate and up)
117    target_sources(strata_mmq PRIVATE src/prefill/lat_q3k_gu.cu)
118    # M2b of the q3k latent A stage: SwiGLU + the q8_1 of H
119    target_sources(strata_mmq PRIVATE src/prefill/lat_q3k_swiglu.cu)
120    # M3 of the q3k latent A stage: the whole stage in one call (two launches, the down is lat_q3k_down's)
121    target_sources(strata_mmq PRIVATE src/prefill/lat_q3k_fused.cu)
122    # The latent Q3_K variant uses the same flags as the other MMQ sources.
123    target_sources(strata_mmq PRIVATE src/prefill/moe_fused_q3k.cu)
124    # P4-SPLIT: Strata's own prompt kernels (the fused int8 experts) are a library of their own, strata_pf_own, so that a
125    # build without llama.cpp's MMQ keeps them.  With MMQ they are compiled with strata_mmq's command line (strata_pf_own_mirror
126    # below) and strata_mmq hands them on (PUBLIC): every target that links strata_mmq gets the same objects as before.
127    add_library(strata_pf_own STATIC src/prefill/moe_fused.cu src/prefill/moe_fused_iq.cu src/prefill/moe_helpers.cu
128                src/prefill/moe_fused_iq_q36.cu)   # (the same kernels again for Qwen3.6's expert shape, for src/qwen35/q36_engine.cu)
129    if(STRATA_EXPERIMENTAL_SM60 AND NOT STRATA_HIP_GFX906)
130      # GP100 (sm_60): a resident layer's experts weight-stationary, one launch per product (moe_ws.hpp; opt-in at
131      # run time, STRATA_MMQ_WS=1, and only where every visible GPU is cc 6.0)
132      target_sources(strata_mmq PRIVATE src/prefill/moe_ws.cu src/prefill/moe_ws_gu_iq2.cu src/prefill/moe_ws_gu_iq3.cu
133                     src/prefill/moe_ws_down.cu)
134    endif()
135    # q8_0: the drafter's matrices (E-9); STRATA_ORCA_Q4KS_MMQ adds q5_0 (#296, in _strata_mmq_types), and
136    # STRATA_MMQ_KQUANTS q4_k q5_k q5_1 q6_k, the experts of Unsloth's UD-Q4/Q5/Q6_K_XL (docs/QUANTS.md; the dispatch
137    # follows eddoursul/Strata 7706c1b).  MODEL-UPDATE Phase 1: on CUDA they are a library of their own, strata_kq
138    # (below), loaded only for a model with these experts: the engine loads every kernel of its own binary at start
139    # (CUDA_MODULE_LOADING=EAGER), and these four took ~3.8 MB of VRAM (3 expert slots of IQ3_XXS on a P100) from every
140    # model.  gfx906 (HIP: code objects load on first use) keeps them in strata_mmq.
141    set(_strata_mmq_cuda_types ${_strata_mmq_types})
142    set(_strata_kq_types q4_k q5_k q5_1 q6_k)
143    set(_strata_kq_dso OFF)
144    if(STRATA_MMQ_KQUANTS AND STRATA_HIP_GFX906)
145      list(APPEND _strata_mmq_cuda_types ${_strata_kq_types})
146    elseif(STRATA_MMQ_KQUANTS)
147      set(_strata_kq_dso ON)
148    endif()
149    foreach(_t IN LISTS _strata_mmq_cuda_types)
150      target_sources(strata_mmq PRIVATE ${_strata_mmq}/template-instances/mmq-instance-${_t}.cu)
151    endforeach()
152    if(_strata_mmq_tq_overlay)
153      foreach(_t tq2_t tqk6 tqk7 tqp5 tqp6 tqp7)
154        target_sources(strata_mmq PRIVATE ${_strata_tq_overlay_dir}/template-instances/mmq-instance-${_t}.cu)
155      endforeach()
156      # first on the include path: the patched mmq.cuh / ggml.h shadow the pinned ones for this target only
157      target_include_directories(strata_mmq BEFORE PRIVATE ${_strata_tq_overlay_dir} ${_strata_tq_overlay_dir}/include)
158    endif()
159    # NVFP4: W4A8 (default) in mmq_nvfp4_w4a8.cu with the plain architectures; the opt-in W4A4 needs RTX 50's FP4 MMA,
160    # which exists only in the arch-specific target, so its one unit alone is built with 12x -> 12xa (the default
161    # binary is unchanged and runs on sm_121; a card without that image falls back to W4A8 at run time)
162    set(_strata_w4a4_archs ${CMAKE_CUDA_ARCHITECTURES})
163    # `native` is first replaced by the detected architectures (e.g. 120-real), so the 12x -> 12xa rewrite below applies
164    # to it too: with plain sm_120 ptxas refuses the FP4 MMA of this one unit (#4023)
165    if("native" IN_LIST _strata_w4a4_archs AND CMAKE_CUDA_ARCHITECTURES_NATIVE)
166      list(REMOVE_ITEM _strata_w4a4_archs native)
167      list(APPEND _strata_w4a4_archs ${CMAKE_CUDA_ARCHITECTURES_NATIVE})
168    endif()
169    # (two steps: CMake before 3.29 rejects a \2 whose optional group did not match, "120" alone)
170    list(TRANSFORM _strata_w4a4_archs REPLACE "^(12[0-9])$" "\\1a")
171    list(TRANSFORM _strata_w4a4_archs REPLACE "^(12[0-9])-(real|virtual)$" "\\1a-\\2")
172    add_library(strata_mmq_w4a4 STATIC src/prefill/mmq_nvfp4_w4a4.cu)
173    set_target_properties(strata_mmq_w4a4 PROPERTIES CUDA_ARCHITECTURES "${_strata_w4a4_archs}")
174    target_include_directories(strata_mmq_w4a4 PRIVATE ${_strata_gg}/include ${_strata_gg}/src ${_strata_mmq})
175    target_compile_options(strata_mmq_w4a4 PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda -use_fast_math>)
176    if(MSVC)
177      target_compile_options(strata_mmq_w4a4 PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/bigobj>)
178    endif()
179    target_link_libraries(strata_mmq_w4a4 PUBLIC ggml-base ${_strata_gpu_runtime_target})
180    target_link_libraries(strata_mmq PRIVATE strata_mmq_w4a4)
181    # STRATA_MMQ_TAB: the pointer-table copy of MMQ's scheduler mirrors mmq.cuh from mul_mat_q_process_tile through
182    # mul_mat_q_case (struct mmq_args included).  A fingerprint of that text instead of a compile probe: nothing is
183    # compiled at configure time, and a llama.cpp tree with another scheduler turns the feature off instead of
184    # failing the build.  _strata_mmq_tab_known: the hashes moe_mmq_tab.cuh was verified against (3cf03257, the
185    # pinned commit, which is also what third_party/strata-ggml holds).
186    set(_strata_mmq_tab_on OFF)
187    if(STRATA_MMQ_TAB AND NOT STRATA_HIP_GFX906)   # gfx906 compiles no .cu from here (its HIP glob): no tab at all
188      if(NOT STRATA_GGML_DIR)
189        # the vendored third_party/strata-ggml is the tree moe_mmq_tab.cuh was verified against (its mmq.cuh is the pinned file, unedited), and
190        # tools/check_vendored.py keeps its bytes: no fingerprint of mmq.cuh is needed
191        set(_strata_mmq_tab_on ON)
192      else()   # a -DSTRATA_GGML_DIR checkout: only the verified scheduler is accepted
193        set(_strata_mmq_tab_known 36e4dc716c504470ec09cc0852d29a6ac5eace257f6e5e72db3805ca4f043eb0)
194        set(_strata_mmq_tab_hash "")
195        if(EXISTS ${_strata_mmq}/mmq.cuh)
196          file(READ ${_strata_mmq}/mmq.cuh _strata_mmq_src)
197          string(REPLACE "\r\n" "\n" _strata_mmq_src "${_strata_mmq_src}")
198          string(FIND "${_strata_mmq_src}" "static __device__ __forceinline__ void mul_mat_q_process_tile" _strata_tab_a)
199          string(FIND "${_strata_mmq_src}" "#define DECL_MMQ_CASE" _strata_tab_b)
200          if(_strata_tab_a GREATER -1 AND _strata_tab_b GREATER _strata_tab_a)
201            math(EXPR _strata_tab_len "${_strata_tab_b} - ${_strata_tab_a}")
202            string(SUBSTRING "${_strata_mmq_src}" ${_strata_tab_a} ${_strata_tab_len} _strata_tab_region)
203            string(SHA256 _strata_mmq_tab_hash "${_strata_tab_region}")
204          endif()
205          unset(_strata_mmq_src)
206          unset(_strata_tab_region)
207        endif()
208        if(_strata_mmq_tab_hash AND _strata_mmq_tab_hash IN_LIST _strata_mmq_tab_known)
209          set(_strata_mmq_tab_on ON)
210        else()
211          message(STATUS "STRATA_MMQ_TAB off: mmq.cuh differs from the mirrored scheduler (${_strata_mmq_tab_hash})")
212        endif()
213      endif()
214    endif()
215    if(_strata_mmq_tab_on)
216      set(_strata_tab_cuh ${CMAKE_CURRENT_SOURCE_DIR}/src/prefill/moe_mmq_tab.cuh)
217      # the native packs' nine types, the ones prefill_mmq_tab_test checks bit for bit; the optional Q5_0 / K-quant
218      # instances (STRATA_ORCA_Q4KS_MMQ, STRATA_MMQ_KQUANTS) get no pointer table and keep the gather
219      set(_strata_mmq_tab_types q2_0 iq2_xxs iq2_xs iq2_s iq3_xxs iq3_s iq4_nl iq4_xs q8_0)
220      foreach(_t IN LISTS _strata_mmq_tab_types)
221        string(TOUPPER ${_t} _strata_tab_T)
222        configure_file(src/prefill/mmq_tab_instance.cu.in ${CMAKE_BINARY_DIR}/mmq_tab/mmq-tab-${_t}.cu @ONLY)
223        target_sources(strata_mmq PRIVATE ${CMAKE_BINARY_DIR}/mmq_tab/mmq-tab-${_t}.cu)
224      endforeach()
225      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TAB=1)
226      message(STATUS "STRATA_MMQ_TAB on: pointer-table MMQ for ${_strata_mmq_tab_types}")
227    endif()
228    target_include_directories(strata_mmq PRIVATE ${_strata_gg}/include ${_strata_gg}/src ${_strata_mmq}
229                               PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
230    target_compile_options(strata_mmq PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda -use_fast_math>)
231    if(MSVC)
232      target_compile_options(strata_mmq PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/bigobj>)
233    endif()
234    # GGML's CUDA error reporting calls cuGetErrorString from the driver API.
235    target_link_libraries(strata_mmq PUBLIC ggml-base ${_strata_gpu_runtime_target})
236    if(TARGET CUDA::cuda_driver)   # not in the HIP (gfx906) build, whose CUDA::cudart is a stand-in
237      target_link_libraries(strata_mmq PUBLIC CUDA::cuda_driver)
238    endif()
239    if(STRATA_MMQ_KQUANTS)
240      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_KQUANTS=1)
241    endif()
242    if(_strata_kq_dso)
243      # strata_kq: the K-quant MMQ instances with their own copy of ggml_cuda_host.cu's symbols and the static CUDA
244      # runtime, next to the engine (libstrata_kq.so / strata_kq.dll); moe_mmq.cu opens it when a model asks for one
245      # of these formats (STRATA_MMQ_KQ=1).  Without the file the engine runs them on the FP16 prompt path, as before.
246      target_compile_definitions(strata_mmq PRIVATE STRATA_KQ_DSO=1)
247      target_sources(strata_mmq PRIVATE src/prefill/mmq_kq_loader.cpp)
248      if(NOT WIN32)
249        target_link_libraries(strata_mmq PUBLIC ${CMAKE_DL_LIBS})
250      endif()
251      add_library(strata_kq SHARED src/prefill/mmq_kq_lib.cu src/prefill/ggml_cuda_host.cu)
252      foreach(_t IN LISTS _strata_kq_types)
253        target_sources(strata_kq PRIVATE ${_strata_mmq}/template-instances/mmq-instance-${_t}.cu)
254      endforeach()
255      target_include_directories(strata_kq PRIVATE ${_strata_gg}/include ${_strata_gg}/src ${_strata_mmq}
256                                 ${CMAKE_CURRENT_SOURCE_DIR}/include)
257      target_compile_options(strata_kq PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda -use_fast_math>)
258      if(MSVC)
259        target_compile_options(strata_kq PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/bigobj>)
260      endif()
261      set_target_properties(strata_kq PROPERTIES CUDA_RUNTIME_LIBRARY Static POSITION_INDEPENDENT_CODE ON
262                            CXX_VISIBILITY_PRESET hidden CUDA_VISIBILITY_PRESET hidden)
263      # ggml-base is static: position-independent so a shared library can hold it (the same code otherwise)
264      set_target_properties(ggml-base PROPERTIES POSITION_INDEPENDENT_CODE ON)
265      target_link_libraries(strata_kq PRIVATE ggml-base)
266      if(TARGET CUDA::cuda_driver)
267        target_link_libraries(strata_kq PRIVATE CUDA::cuda_driver)
268      endif()
269      add_dependencies(strata_mmq strata_kq)
270    endif()
271    if(STRATA_ORCA_Q4KS_MMQ)
272      target_compile_definitions(strata_mmq PRIVATE STRATA_ORCA_Q4KS_MMQ=1)
273    endif()
274    if(_strata_mmq_tq)
275      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TQ=1)
276    endif()
277    if(_strata_mmq_tqp)
278      target_compile_definitions(strata_mmq PRIVATE STRATA_MMQ_TQP=1)
279    endif()
280    strata_pf_own_mirror()
281    target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base ${_strata_gpu_runtime_target})
282    if(TARGET CUDA::cuda_driver)
283      target_link_libraries(strata_pf_own PUBLIC CUDA::cuda_driver)
284    endif()
285    target_link_libraries(strata_mmq PUBLIC strata_pf_own)
286    target_link_libraries(strata_prefill PUBLIC strata_mmq)
287    target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_MMQ=1 STRATA_PREFILL_FUSED=1)
288    if(STRATA_EXPERIMENTAL_SM60 AND NOT STRATA_HIP_GFX906)
289      # the weight-stationary experts (moe_ws.hpp) are in strata_mmq: the prompt path may take them (STRATA_MMQ_WS=1)
290      target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_WS=1)
291    endif()
292    set(_strata_pf_tests_lib strata_mmq)
293endmacro()
294
295# P4-SPLIT, STRATA_MMQ=OFF (the default): Strata's own prompt kernels without llama.cpp's MMQ (strata_pf_own alone, with mmq_absent.cpp for the MMQ half
296# of moe_mmq.hpp); the same sources and options as strata_cuda_mmq gives them, the ggml-base headers only
297macro(strata_cuda_pf_own)
298    set(_strata_gg ${_strata_ggml_root}/ggml)
299    add_library(strata_pf_own STATIC src/prefill/moe_fused.cu src/prefill/moe_fused_iq.cu src/prefill/moe_helpers.cu
300                src/prefill/moe_fused_iq_q36.cu src/prefill/mmq_absent.cpp)
301    target_include_directories(strata_pf_own PRIVATE ${_strata_gg}/include ${_strata_gg}/src PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)
302    target_compile_options(strata_pf_own PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--extended-lambda -use_fast_math>)
303    if(MSVC)
304      target_compile_options(strata_pf_own PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:-Xcompiler=/bigobj>)
305    endif()
306    target_link_libraries(strata_pf_own PUBLIC strata_quant ggml-base ${_strata_gpu_runtime_target})
307    if(TARGET CUDA::cuda_driver)
308      target_link_libraries(strata_pf_own PUBLIC CUDA::cuda_driver)
309    endif()
310    if(STRATA_HIP_GFX906)   # (strata_hip_gfx906_sources adds these two to strata_pf_own only with MMQ)
311      target_sources(strata_pf_own PRIVATE src/prefill/moe_f16_kq.cu)
312      target_sources(strata_pf_own PRIVATE src/prefill/moe_p1a.cu)
313      target_compile_definitions(strata_pf_own PRIVATE GGML_USE_HIP)
314    endif()
315    target_link_libraries(strata_prefill PUBLIC strata_pf_own)
316    target_compile_definitions(strata_prefill PUBLIC STRATA_PREFILL_FUSED=1)
317    set(_strata_pf_tests_lib strata_pf_own)
318    message(STATUS "Strata: no llama.cpp MMQ prompt kernels (STRATA_MMQ=OFF): Strata's own prompt kernels and the FP16 path")
319endmacro()
320
321# the prompt kernels' tests: the fused experts' (linked to ${_strata_pf_tests_lib}) and, with MMQ, MMQ's own
322macro(strata_cuda_prompt_tests)
323    if(STRATA_BUILD_TESTS AND NOT STRATA_MMQ)
324      # P4-SPLIT: the engine and Strata's prompt library hold none of llama.cpp's MMQ (names in the bytes, nm, cuobjdump; tools/check_no_mmq.py)
325      find_package(Python3 QUIET COMPONENTS Interpreter)
326      if(Python3_Interpreter_FOUND)
327        add_test(NAME no_mmq_symbols COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/check_no_mmq.py
328                 --cuobjdump ${CUDAToolkit_BIN_DIR}/cuobjdump${CMAKE_EXECUTABLE_SUFFIX} $<TARGET_FILE:strata> $<TARGET_FILE:strata_pf_own>)
329        set_tests_properties(no_mmq_symbols PROPERTIES TIMEOUT 900)
330      endif()
331    endif()
332    if(STRATA_BUILD_TESTS)
333      # the fused prompt kernels' weight decode (moe_fused_iq.cu's load_unit / convert, compiled as the library compiles them) against
334      # stored digests: the same file on CUDA and HIP (exit 77 without a card).  Those device functions exist only for sm_75 and newer
335      # (moe_fused_iq.cu's __CUDA_ARCH__ >= 750 sections), so a list with an older architecture (STRATA_EXPERIMENTAL_SM60) does not build it
336      if(_strata_cuda_below75)
337        message(STATUS "Strata: prefill_decode_golden not built: ${CMAKE_CUDA_ARCHITECTURES} has an architecture below sm_75")
338      else()
339        add_executable(prefill_decode_golden tests/cuda/prefill_decode_golden.cu)
340        target_include_directories(prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,INCLUDE_DIRECTORIES>)
341        target_compile_definitions(prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,COMPILE_DEFINITIONS>)
342        target_compile_options(prefill_decode_golden PRIVATE $<TARGET_PROPERTY:strata_pf_own,COMPILE_OPTIONS>)
343        target_link_libraries(prefill_decode_golden PRIVATE ggml-base ${_strata_gpu_runtime_target})
344        add_test(NAME prefill_decode_golden COMMAND prefill_decode_golden ${CMAKE_CURRENT_SOURCE_DIR}/tests/cuda/prefill_decode_golden.txt)
345        set_tests_properties(prefill_decode_golden PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
346      endif()
347      # #136: the fused int8 experts against a double-precision reference and the MMQ path (exit 77 below sm_80)
348      add_executable(prefill_fused_moe_test tests/cuda/prefill_fused_moe_test.cpp)
349      target_link_libraries(prefill_fused_moe_test PRIVATE ${_strata_pf_tests_lib} ggml-base ${_strata_gpu_runtime_target})
350      add_test(NAME prefill_fused_moe_test COMMAND prefill_fused_moe_test)
351      set_tests_properties(prefill_fused_moe_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
352      # #136: the same for the native packs' formats (IQ2_XXS IQ2_XS IQ2_S IQ3_XXS IQ3_S IQ4_XS, down Q2_0 IQ4_NL)
353      add_executable(prefill_fused_iq_test tests/cuda/prefill_fused_iq_test.cpp)
354      target_link_libraries(prefill_fused_iq_test PRIVATE ${_strata_pf_tests_lib} ggml-base ${_strata_gpu_runtime_target})
355      add_test(NAME prefill_fused_iq_test COMMAND prefill_fused_iq_test)
356      set_tests_properties(prefill_fused_iq_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
357      # one prompt layer of experts timed on every path the engine can take (MMQ where it is built, the dequantize + FP16 GEMM path with and without
358      # the few-row experts, the grouped kernels, the fused kernels where the card has them), hash and error of each against a CPU reference; runs on
359      # any card (77 only without one).  The ctest entry is a small smoke run; the grid is `prefill_expert_paths_bench` itself (see its source's head)
360      add_executable(prefill_expert_paths_bench tests/cuda/prefill_expert_paths_bench.cpp)
361      target_link_libraries(prefill_expert_paths_bench PRIVATE strata_prefill ${_strata_pf_tests_lib} $<TARGET_NAME_IF_EXISTS:strata_quant> ggml-base ${_strata_gpu_runtime_target})
362      add_test(NAME prefill_expert_paths_bench COMMAND prefill_expert_paths_bench --quick)
363      set_tests_properties(prefill_expert_paths_bench PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
364      # P4-SMALL: the fused experts for one gathered group (the layer-major walk's entry) against the layer's fused path (bits) and MMQ in the walk
365      # (P4-INT: MMQ builds only: the test returns 1 where mmq::built() is false, it has no no-MMQ rule yet)
366      if(STRATA_MMQ)
367        add_executable(prefill_fused_group_test tests/cuda/prefill_fused_group_test.cpp)
368        target_link_libraries(prefill_fused_group_test PRIVATE ${_strata_pf_tests_lib} ggml-base ${_strata_gpu_runtime_target})
369        add_test(NAME prefill_fused_group_test COMMAND prefill_fused_group_test)
370        set_tests_properties(prefill_fused_group_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
371      endif()
372      # the same kernels at Qwen3.6's expert shape (n_embd 2048, n_ff 512: the copy moe_fused_iq_q36.cu compiles): UD-Q6_K_XL's and the
373      # Q8_0 file's pairs against the reference, the int8 model and MMQ (77 below sm_80)
374      add_executable(prefill_fused_q36_test tests/cuda/prefill_fused_q36_test.cpp)
375      target_link_libraries(prefill_fused_q36_test PRIVATE ${_strata_pf_tests_lib} ggml-base ${_strata_gpu_runtime_target})
376      add_test(NAME prefill_fused_q36_test COMMAND prefill_fused_q36_test)
377      set_tests_properties(prefill_fused_q36_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900)
378      # a launch whose expert blobs are not in device memory takes the unstaged kernel instead of ending the process (a staged K-quant down: timing part only, one layer of 2,048 tokens)
379      add_test(NAME prefill_fused_q36_test_hostblobs COMMAND prefill_fused_q36_test "--only=Q4_K / Q5_K" --no-ref --chunks=2048 --host-blobs=1 --expect-fallback)
380      set_tests_properties(prefill_fused_q36_test_hostblobs PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
381      # the opt-in STRATA_PF_FUSED_Q2N=1 on its own (the test leaves STRATA_PF_FUSED unset): the native Q2_0 pack's Q2_0 / Q2_0
382      # layers on sm_80+ (77 below sm_80 and without a card)
383      add_test(NAME prefill_fused_iq_test_q2n COMMAND prefill_fused_iq_test "--only=Q2_0 / Q2_0" --unset-pf-fused)
384      set_tests_properties(prefill_fused_iq_test_q2n PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q2N=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
385      # the Turing set (STRATA_PF_FUSED_SM75=1): the canonical Q2_0 pair and the i-quant pairs with kernels on sm_75, against the reference and the int8 model with the edge shapes.
386      # 77 on any card that is not a compute capability 7.5 one and does not emulate it (STRATA_EMULATE_CC=75 on a bigger card, with a build whose device code has PTX for sm_75)
387      add_test(NAME prefill_fused_iq_test_sm75 COMMAND prefill_fused_iq_test --turing --edge --no-timing)
388      set_tests_properties(prefill_fused_iq_test_sm75 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_SM75=1" SKIP_RETURN_CODE 77 TIMEOUT 900)
389      # the opt-in STRATA_PF_FUSED_NT=256 kernels (256 threads, two blocks a multiprocessor, two-pass weight loads) against the
390      # same references; the batches of the test reach them through the Batch overload of experts_native (77 below sm_80)
391      add_test(NAME prefill_fused_iq_test_nt256 COMMAND prefill_fused_iq_test)
392      set_tests_properties(prefill_fused_iq_test_nt256 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_NT=256" SKIP_RETURN_CODE 77 TIMEOUT 600)
393      # the opt-in STRATA_PF_FUSED_F16=1 (f16 mma, rounding level) and STRATA_PF_FUSED_WS=1 (warp-specialized, the same F16 products;
394      # about 101 KB of shared memory at its defaults, more than an sm_86 / sm_89 / sm_120 card's 99 KB, so it is set up on
395      # sm_80 and sm_90 cards and the test skips (77) elsewhere)
396      add_test(NAME prefill_fused_iq_test_f16 COMMAND prefill_fused_iq_test)
397      set_tests_properties(prefill_fused_iq_test_f16 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_F16=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
398      add_test(NAME prefill_fused_iq_test_ws COMMAND prefill_fused_iq_test)
399      set_tests_properties(prefill_fused_iq_test_ws PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_WS=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
400      # the opt-in STRATA_PF_FUSED_Q50=1 (phase 4 step 1): UD-IQ4_XS v31's Q5_0 down layers on the native kernels, against the reference
401      # (this build has no Q5_0 MMQ), with the tails and tile boundaries the test adds for every Q5_0 pair (77 below sm_80)
402      add_test(NAME prefill_fused_iq_test_q50 COMMAND prefill_fused_iq_test "--only=Q5_0" --no-timing)
403      set_tests_properties(prefill_fused_iq_test_q50 PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q50=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
404      # a launch whose expert blobs are not in device memory takes the unstaged kernel instead of ending the process (timing part only: one layer of 2,048 tokens, S20_NOMMQ because MMQ does not read host blobs)
405      add_test(NAME prefill_fused_iq_test_hostblobs COMMAND prefill_fused_iq_test "--only=IQ3_S / IQ4_NL" --no-ref --chunks=2048 --host-blobs=1 --expect-fallback)
406      set_tests_properties(prefill_fused_iq_test_hostblobs PROPERTIES ENVIRONMENT "S20_NOMMQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
407      # the opt-in STRATA_PF_FUSED_KQ=1 (phase 4): UD-Q4_K_XL's Q4_K / Q5_K gate/up with Q5_1 / Q8_0 down on the native kernels, against the
408      # reference and the int8 model at every shape (--edge), MMQ's K-quant library alongside (STRATA_MMQ_KQ=1; 77 below sm_80)
409      add_test(NAME prefill_fused_iq_test_kq COMMAND prefill_fused_iq_test "--only=K / Q" --edge --no-timing)
410      set_tests_properties(prefill_fused_iq_test_kq PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_KQ=1;STRATA_MMQ_KQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
411      # the weight-staging kernels of UD-Q4_K_XL's pairs (Q5_1 and Q8_0 downs) against the unstaged kernel: the same experts and routing run with device blobs (staged)
412      # and host-mapped blobs (the unstaged fallback), every output bit equal (tails, a tile boundary and a 2,048-token chunk; 77 below sm_80)
413      add_test(NAME prefill_fused_iq_test_kqbits COMMAND prefill_fused_iq_test "--only=K / Q" --bits-staged)
414      set_tests_properties(prefill_fused_iq_test_kqbits PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_KQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
415      # the opt-in STRATA_PF_FUSED_Q8D=1 (phase 4): the pinned UD-IQ4_XS's IQ3_S / Q8_0 and IQ4_XS / Q8_0 layers on the native kernels, against the
416      # reference, the int8 model and MMQ at every shape (--edge; 77 below sm_80)
417      add_test(NAME prefill_fused_iq_test_q8d COMMAND prefill_fused_iq_test "--only=S / Q8_0" --edge --no-timing)
418      set_tests_properties(prefill_fused_iq_test_q8d PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q8D=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
419      # the opt-in STRATA_PF_FUSED_Q8GU=1 (phase 4): the Q8_0 set's Q8_0 / Q8_0 layers on the native kernels, against the reference, the int8
420      # model and MMQ at every shape (--edge; 77 below sm_80)
421      add_test(NAME prefill_fused_iq_test_q8gu COMMAND prefill_fused_iq_test "--only=Q8_0 / Q8_0" --edge --no-timing)
422      set_tests_properties(prefill_fused_iq_test_q8gu PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q8GU=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
423      # the opt-in STRATA_PF_FUSED_Q6K=1 (phase 4): UD-Q6_K_XL's Q6_K gate/up with a Q8_0 down on the native kernels, against the reference, the
424      # int8 model and MMQ's K-quant library (STRATA_MMQ_KQ=1) at every shape (--edge; 77 below sm_80)
425      add_test(NAME prefill_fused_iq_test_q6k COMMAND prefill_fused_iq_test "--only=Q6_K + Q8_0" --edge --no-timing)
426      set_tests_properties(prefill_fused_iq_test_q6k PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_Q6K=1;STRATA_MMQ_KQ=1" SKIP_RETURN_CODE 77 TIMEOUT 600)
427      # the opt-in STRATA_PF_FUSED_TQ=1 (phase 4): Gyro-S's TQK6 / TQK7 and Gyro-M's TQ2_T / TQ2_T layers on the native kernels, against a reference
428      # from the format definition (fp16 codebook), the int8 model (the int8 codebook) and the overlay's trellis MMQ at every shape (--edge; 77 below sm_80)
429      add_test(NAME prefill_fused_iq_test_tq COMMAND prefill_fused_iq_test "--only=+ T" --edge --no-timing)
430      set_tests_properties(prefill_fused_iq_test_tq PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_TQ=1" SKIP_RETURN_CODE 77 TIMEOUT 900)
431      # the same pairs as Hadamard-folded experts (the Gyro packs' form): the gate/up input and H rotated per 128 values with random signs; the
432      # fused path's H rotation (set_down_rotation) against the host-side rotation of the reference and MMQ with its H rotated on the device
433      add_test(NAME prefill_fused_iq_test_tqh COMMAND prefill_fused_iq_test "--only=+ T" --folded --edge --no-timing)
434      set_tests_properties(prefill_fused_iq_test_tqh PROPERTIES ENVIRONMENT "STRATA_PF_FUSED_TQ=1" SKIP_RETURN_CODE 77 TIMEOUT 900)
435      # NVFP4 / NVFP4 (ModelOpt packs) on the native kernels: weights decoded from the 36-byte blocks, the blob's tail scales on the FP32 products; edge shapes too
436      add_test(NAME prefill_fused_iq_test_nvfp4 COMMAND prefill_fused_iq_test "--only=NVFP4" --edge)
437      set_tests_properties(prefill_fused_iq_test_nvfp4 PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 900)
438    endif()
439    if(STRATA_BUILD_TESTS AND STRATA_MMQ)   # (P4-SPLIT: MMQ's own tests, below too)
440      # the MoE input quantized per token and scattered to its rows (mmq::quantize_scatter) against the per-row
441      # gather: the same q8_1 bytes for every MMQ layout (--bench: both at 2K, 8K and 32K tokens)
442      # the Unsloth v3.1 sets' expert formats through MMQ (Q5_0 down only in a STRATA_ORCA_Q4KS_MMQ build), and --bench
443      add_executable(prefill_mmq_v31_test tests/cuda/prefill_mmq_v31_test.cpp)
444      target_link_libraries(prefill_mmq_v31_test PRIVATE strata_mmq strata_kernels ggml-base ${_strata_gpu_blas_target} ${_strata_gpu_runtime_target})
445      add_test(NAME prefill_mmq_v31_test COMMAND prefill_mmq_v31_test)
446      set_tests_properties(prefill_mmq_v31_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
447      add_executable(prefill_mmq_scatter_test tests/cuda/prefill_mmq_scatter_test.cpp)
448      target_link_libraries(prefill_mmq_scatter_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
449      add_test(NAME prefill_mmq_scatter_test COMMAND prefill_mmq_scatter_test)
450      set_tests_properties(prefill_mmq_scatter_test PROPERTIES TIMEOUT 120)
451      if(STRATA_EXPERIMENTAL_SM60)
452        # mmq::swiglu_quant: swiglu + the down product's q8_1 rows in one kernel against the two, byte for byte
453        add_executable(prefill_swiglu_quant_test tests/cuda/prefill_swiglu_quant_test.cpp)
454        target_link_libraries(prefill_swiglu_quant_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
455        add_test(NAME prefill_swiglu_quant_test COMMAND prefill_swiglu_quant_test)
456        set_tests_properties(prefill_swiglu_quant_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 120)
457        # the weight-stationary experts (STRATA_MMQ_WS) against the per-group MMQ, byte for byte, on random experts; the
458        # verdict is given on a compute capability 6.0 card only (77 elsewhere)
459        add_executable(prefill_ws_parity_test tests/cuda/prefill_ws_parity_test.cpp)
460        target_link_libraries(prefill_ws_parity_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
461        add_test(NAME prefill_ws_parity_test COMMAND prefill_ws_parity_test)
462        set_tests_properties(prefill_ws_parity_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
463      endif()
464    endif()
465    if(STRATA_BUILD_TESTS AND STRATA_MMQ AND NOT STRATA_HIP_GFX906)
466      # STRATA_IQ4_W4A8: the IQ4 W4A8 expert kernel against MMQ, bit for bit, on the group buffers and (with
467      # STRATA_MMQ_TAB) the pointer table (exit 77 without a card or off sm_80..sm_88; gfx906: a stub, no test)
468      add_executable(prefill_iq4_w4a8_test tests/cuda/prefill_iq4_w4a8_test.cu)
469      target_link_libraries(prefill_iq4_w4a8_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
470      add_test(NAME prefill_iq4_w4a8_test COMMAND prefill_iq4_w4a8_test)
471      set_tests_properties(prefill_iq4_w4a8_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
472    endif()
473    if(STRATA_BUILD_TESTS AND _strata_mmq_tab_on)
474      # STRATA_MMQ_DIRECT: the pointer-table MMQ bit for bit against the gathered MMQ (exit 77 below sm_80)
475      add_executable(prefill_mmq_tab_test tests/cuda/prefill_mmq_tab_test.cu)
476      target_link_libraries(prefill_mmq_tab_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
477      add_test(NAME prefill_mmq_tab_test COMMAND prefill_mmq_tab_test)
478      set_tests_properties(prefill_mmq_tab_test PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
479    endif()
480    if(STRATA_BUILD_TESTS AND STRATA_MMQ AND STRATA_MMQ_KQUANTS)
481      # the UD-Q4_K_XL expert formats through MMQ against ggml's dequantized weights (synthetic, a few seconds)
482      add_executable(prefill_mmq_kquant_test tests/cuda/prefill_mmq_kquant_test.cpp)
483      target_link_libraries(prefill_mmq_kquant_test PRIVATE strata_mmq ggml-base ${_strata_gpu_runtime_target})
484      add_test(NAME prefill_mmq_kquant_test COMMAND prefill_mmq_kquant_test)
485      # the K-quant MMQ runs when STRATA_MMQ_KQ=1 (the engine's default on NVIDIA 8.0+ and Linux gfx12; see the option above), and on CUDA it is strata_kq next to the test
486      set_tests_properties(prefill_mmq_kquant_test PROPERTIES TIMEOUT 120 ENVIRONMENT STRATA_MMQ_KQ=1)
487    endif()
488    # the NVFP4 expert products (w4a8 / w4a4, STRATA_PREFILL_NVFP4) against a double-precision reference
489    if(STRATA_MMQ AND CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|AMD64|amd64|i[3-6]86)$")   # its FP16 rounding helper is the x86 F16C intrinsics: not built on aarch64
490      add_executable(mmq_nvfp4_parity src/prefill/mmq_nvfp4_parity.cpp)
491      target_link_libraries(mmq_nvfp4_parity PRIVATE strata_mmq ggml-base CUDA::cudart)
492      if(NOT MSVC)   # GCC/Clang need the target flag
493        target_compile_options(mmq_nvfp4_parity PRIVATE $<$<COMPILE_LANGUAGE:CXX>:-mf16c>)
494      endif()
495    endif()
496    # the routed-expert products (MMQ) timed at the prompt path's shapes (16 experts a launch)
497    if(STRATA_MMQ)
498    add_executable(lat_mmq_bench src/prefill/lat_mmq_bench.cpp)
499    # latent D projections on partial tiles and identical rows
500    add_executable(latent_dense_tail_test src/prefill/latent_dense_tail_test.cpp)
501    target_link_libraries(latent_dense_tail_test PRIVATE strata_mmq strata_kernels ggml-base CUDA::cudart)
502    target_include_directories(latent_dense_tail_test PRIVATE include)
503    # one q3k expert through the verified MMQ products: the target the fused Q3_K dot must match
504    add_executable(lat_fused_q3k_test src/prefill/lat_fused_q3k_test.cpp)
505    target_link_libraries(lat_fused_q3k_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
506    target_include_directories(lat_fused_q3k_test PRIVATE include)
507    # M1 of the q3k latent A stage: the down half against native_expert_grouped's own down (one unit test)
508    add_executable(lat_q3k_down_test src/prefill/lat_q3k_down_test.cpp)
509    target_link_libraries(lat_q3k_down_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
510    target_include_directories(lat_q3k_down_test PRIVATE include)
511    # M2a: the Q3_K gate/up half against a double-precision host dequantization (+ the per-super-block decode gate)
512    add_executable(lat_q3k_gu_test src/prefill/lat_q3k_gu_test.cpp)
513    target_link_libraries(lat_q3k_gu_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
514    target_include_directories(lat_q3k_gu_test PRIVATE include)
515    # M2b: SwiGLU + q8_1 of H against the engine's own quantizer rule on the host
516    add_executable(lat_q3k_swiglu_test src/prefill/lat_q3k_swiglu_test.cpp)
517    target_link_libraries(lat_q3k_swiglu_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
518    target_include_directories(lat_q3k_swiglu_test PRIVATE include)
519    # M2c step 1: the gu half on the down half's four shapes (mine vs native gate/up only)
520    add_executable(lat_q3k_gu_shape_test src/prefill/lat_q3k_gu_shape_test.cpp)
521    target_link_libraries(lat_q3k_gu_shape_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
522    target_include_directories(lat_q3k_gu_shape_test PRIVATE include)
523    # M3: the fused A stage against the serial chain (memcmp) and against native's whole step, four shapes
524    add_executable(lat_q3k_fused_test src/prefill/lat_q3k_fused_test.cpp)
525    add_executable(lat_cache_test src/prefill/lat_cache_test.cpp)
526    target_link_libraries(lat_cache_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
527    target_include_directories(lat_cache_test PRIVATE include)
528    target_link_libraries(lat_q3k_fused_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
529    target_include_directories(lat_q3k_fused_test PRIVATE include)
530    # M3b: explicit per-group counts (nullptr = the old starts-derived path), scrambled starts, fused == serial
531    add_executable(lat_q3k_counts_test src/prefill/lat_q3k_counts_test.cpp)
532    target_link_libraries(lat_q3k_counts_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
533    target_include_directories(lat_q3k_counts_test PRIVATE include)
534    # M4's unit gate: ent_tok / ent_dst (native_expert_grouped's two entry maps)
535    add_executable(lat_q3k_maps_test src/prefill/lat_q3k_maps_test.cpp)
536    target_link_libraries(lat_q3k_maps_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
537    target_include_directories(lat_q3k_maps_test PRIVATE include)
538    # the engine's real A-stage shape (512 groups, 42450 entries, shared xl rows, non-monotonic off[])
539    add_executable(lat_q3k_engine_shape_test src/prefill/lat_q3k_engine_shape_test.cpp)
540    target_link_libraries(lat_q3k_engine_shape_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
541    target_include_directories(lat_q3k_engine_shape_test PRIVATE include)
542    # L2a of the q3k acceptance sheet: one batch of kMaxBatch experts vs one launch a launch, same inputs
543    add_executable(lat_q3k_batch_self_test src/prefill/lat_q3k_batch_self_test.cpp)
544    target_link_libraries(lat_q3k_batch_self_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
545    target_include_directories(lat_q3k_batch_self_test PRIVATE include)
546    # one group with thousands of rows (the engine's per-expert walk: the blockIdx.z fill is active)
547    add_executable(lat_q3k_onegroup_test src/prefill/lat_q3k_onegroup_test.cpp)
548    target_link_libraries(lat_q3k_onegroup_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
549    target_include_directories(lat_q3k_onegroup_test PRIVATE include)
550    # the v5 kernel with the q3k decode, against the frozen chain (memcmp, two shapes)
551    add_executable(lat_q3k_fusedv5_test src/prefill/lat_q3k_fusedv5_test.cpp)
552    target_link_libraries(lat_q3k_fusedv5_test PRIVATE strata_mmq strata_kernels strata_kernels_cpu ggml-base CUDA::cudart)
553    target_include_directories(lat_q3k_fusedv5_test PRIVATE include)
554    # the latent A stage's per-layer row tables (host only, no CUDA)
555    add_executable(lat_tables_test src/prefill/lat_tables_test.cpp)
556    target_include_directories(lat_tables_test PRIVATE include)
557    target_link_libraries(lat_mmq_bench PRIVATE strata_mmq ggml-base CUDA::cudart)
558    add_executable(mmq_expert_bench src/prefill/mmq_expert_bench.cpp)
559    target_link_libraries(mmq_expert_bench PRIVATE strata_mmq ggml-base CUDA::cudart)
560    endif()
561endmacro()
562
563# the CUDA local-memory spill check of the kernel libraries (cuobjdump)
564macro(strata_cuda_spill_guard)
565if(STRATA_ENABLE_CUDA AND STRATA_BUILD_TESTS AND NOT STRATA_ENABLE_HIP AND NOT STRATA_HIP_GFX906)
566  find_package(Python3 QUIET COMPONENTS Interpreter)
567  if(Python3_Interpreter_FOUND)
568    set(_strata_spill_libs)
569    foreach(_t strata_kernels strata_core strata_engine strata_prefill strata_qwen35 strata_mmq strata_pf_own strata_mmq_w4a4 strata_kq)
570      if(TARGET ${_t})
571        list(APPEND _strata_spill_libs $<TARGET_FILE:${_t}>)
572      endif()
573    endforeach()
574    add_test(NAME cuda_spill_guard COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/tools/check_cuda_spills.py
575             --cuobjdump ${CUDAToolkit_BIN_DIR}/cuobjdump${CMAKE_EXECUTABLE_SUFFIX} ${_strata_spill_libs})
576    set_tests_properties(cuda_spill_guard PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 600)
577    if(NOT WIN32)   # its stand-in demanglers are sh scripts
578      add_test(NAME check_cuda_spills_unit COMMAND ${Python3_EXECUTABLE} -m unittest tools.test_check_cuda_spills
579               WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
580    endif()
581  else()
582    message(STATUS "strata: no python3 - the cuda_spill_guard test is NOT registered")
583  endif()
584endif()
585endmacro()