okami.mom/strata

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

Niko1221Merge f-own-4x c915680bd into f-own-int (round 15: opt-in Q8_0-down, Q8_0 gate/up and Q6_K fused prompt kernels, the leaner Q6_K loads, the split engine's golden dot test; not the trellis side branch)41a63c550

main
16.4 KiB231 linesraw
1# strata-split: the opt-in engine that splits EVERY layer of Qwen3.8-Flash-Next across 2-4 NVIDIA or AMD cards (sFNX, MIT,
2# Copyright (c) 2026 John Fink, https://github.com/johnfink8/sfnx; third_party/sfnx/NOTICE.md).  Its own executable, one per card
3# count and architecture, driven by serve/server.py through the same line protocol as `strata --serve`.  OFF by default
4# (-DSTRATA_BUILD_SPLIT=ON), Linux + CUDA (sm_86) or Linux + HIP (-DSTRATA_ENABLE_HIP=ON, gfx1201: R9700, RX 9070), and nothing else
5# in the tree depends on it. The HIP build compiles the same sources as HIP (FNX_HIP=1): gfx12 WMMA for the expert prompt kernel
6# and the attention tiles (src/hip/mma.h, moe_mma.cuh), the driver's stream memory operations through hipStreamWrite/WaitValue32
7# (src/hip/cuda.h), the rest through the tree's hip_compat shim plus src/hip/fnx_hip.h.
8#
9# No card ever writes into another card's memory: the buffers the cards exchange sums through live in page-locked host
10# memory (src/engine.cu, src/prefill.cu), so no peer access is enabled, and tests/split/test_split_no_peer.py fails the
11# build's tests if a peer API call appears in this directory.
12#
13#   STRATA_SPLIT_CARDS   card counts to build, e.g. "2;3" (default "2": 3 and 4 cards have not been run with the host exchange)
14#   STRATA_SPLIT_ARCH    the one compute capability the kernels are built for (default: the first number in CMAKE_CUDA_ARCHITECTURES, else 86);
15#                        the engine refuses a card of any other architecture
16set(STRATA_SPLIT_CARDS "2" CACHE STRING "card counts strata-split is built for (2, 3, 4)")
17set(STRATA_SPLIT_ARCH "" CACHE STRING "compute capability strata-split is built for (e.g. 86)")
18set(STRATA_SPLIT_PF_JOB "64" CACHE STRING "rows of one expert per prompt-path job: 64 (default) or 128")
19
20set(_fnx_hip OFF)
21if(STRATA_ENABLE_HIP AND NOT STRATA_ENABLE_CUDA AND NOT WIN32 AND NOT APPLE)
22  set(_fnx_hip ON)
23endif()
24if(NOT _fnx_hip AND (NOT STRATA_ENABLE_CUDA OR WIN32 OR APPLE))
25  message(STATUS "STRATA_BUILD_SPLIT: needs a Linux CUDA or HIP build, skipped")
26  return()
27endif()
28# the quant header (block layouts, codebook tables): the split's only file from third_party/strata-ggml; its dot products are its own
29# (src/qdot.cuh, ggml vecdotq.cuh's arithmetic), so no ggml-cuda header is on its include path
30get_filename_component(_sp_quant "${CMAKE_CURRENT_SOURCE_DIR}/../../third_party/strata-ggml" ABSOLUTE)
31if(NOT EXISTS "${_sp_quant}/strata-quant-common.h")
32  message(WARNING "STRATA_BUILD_SPLIT: third_party/strata-ggml/strata-quant-common.h is missing; strata-split skipped")
33  return()
34endif()
35if(_fnx_hip)
36  # one binary per card count with a code object for each listed architecture: gfx1201 (R9700, RX 9070; run), gfx1200 (RX 9060; same ISA, not run), and RDNA3
37  # gfx11-generic = gfx1100 gfx1101 gfx1102 gfx1103 in one code object (RX 7000 and the 780M iGPU; the gfx11 WMMA path, hip/mma.h FNX_WMMA_GFX11; the self-test passes on a 780M,
38  # no multi-card PC has run it). Only gfx1201 is automatic
39  # (strata_control/split_engine.py AUTO_SPLIT_AMD_ARCHS); on another card the engine runs src/selftest.cu before it loads the model
40  # STRATA_SPLIT_HIP_ARCHS names the split engine's code objects when they are not the tree's CMAKE_HIP_ARCHITECTURES (whose list the HIP backend checks for the main
41  # engine and does not know gfx11-generic); empty: the tree's list
42  set(STRATA_SPLIT_HIP_ARCHS "" CACHE STRING "code objects of strata-split-N (gfx1201;gfx1200;gfx11-generic), empty: CMAKE_HIP_ARCHITECTURES")
43  if(STRATA_SPLIT_HIP_ARCHS)
44    set(_fnx_hip_archs ${STRATA_SPLIT_HIP_ARCHS})
45  else()
46    set(_fnx_hip_archs ${STRATA_HIP_ARCH_LIST})
47  endif()
48  set(_fnx_hip_ok gfx1100 gfx1101 gfx1102 gfx1103 gfx11-generic gfx1200 gfx1201)
49  set(_fnx_hip_bad "")
50  foreach(_a ${_fnx_hip_archs})
51    if(NOT _a IN_LIST _fnx_hip_ok)
52      list(APPEND _fnx_hip_bad ${_a})
53    endif()
54  endforeach()
55  if(NOT _fnx_hip_archs OR _fnx_hip_bad)
56    message(WARNING "STRATA_BUILD_SPLIT (HIP): the split engine builds for ${_fnx_hip_ok} only (got ${_fnx_hip_archs}), skipped")
57    return()
58  endif()
59  set(STRATA_SPLIT_ARCH 1201)   # (FNX_ARCH is only compared with 80 and 70: any HIP value takes the WMMA paths)
60elseif(NOT STRATA_SPLIT_ARCH)
61  string(REGEX MATCH "[0-9]+" STRATA_SPLIT_ARCH "${CMAKE_CUDA_ARCHITECTURES}")
62  if(NOT STRATA_SPLIT_ARCH)
63    set(STRATA_SPLIT_ARCH 86)
64  endif()
65endif()
66
67set(_sp ${CMAKE_CURRENT_SOURCE_DIR})
68set(_sp_engine
69    ${_sp}/src/gguf.cpp ${_sp}/src/model.cpp ${_sp}/src/store.cpp ${_sp}/src/moe_mma.cu ${_sp}/src/engine.cu
70    ${_sp}/src/engine_common.cu ${_sp}/src/prefill.cu ${_sp}/src/session.cpp ${_sp}/src/batch.cpp ${_sp}/tools/sfnx.cpp)
71if(_fnx_hip)
72  list(APPEND _sp_engine ${_sp}/src/selftest.cu)   # the kernel self-test of a card Strata has not run the split on (hip/selftest_policy.hpp)
73endif()
74if(_fnx_hip)
75  set(_fnx_lang HIP)
76else()
77  set(_fnx_lang CUDA)
78endif()
79set_source_files_properties(${_sp}/src/gguf.cpp ${_sp}/src/model.cpp ${_sp}/src/store.cpp ${_sp}/src/session.cpp ${_sp}/src/batch.cpp
80                            ${_sp}/tools/sfnx.cpp PROPERTIES LANGUAGE ${_fnx_lang})
81if(_fnx_hip)
82  set_source_files_properties(${_sp}/src/moe_mma.cu ${_sp}/src/engine.cu ${_sp}/src/engine_common.cu ${_sp}/src/prefill.cu ${_sp}/src/selftest.cu PROPERTIES LANGUAGE HIP)
83endif()
84# kernels.cu is compiled in nine parts (FNX_KPART), each with its share of the launchers, so the build runs in parallel
85set(_sp_kparts "")
86foreach(_p RANGE 0 8)
87  set(_f ${CMAKE_CURRENT_BINARY_DIR}/kernels_p${_p}.cu)
88  file(GENERATE OUTPUT ${_f} CONTENT "#define FNX_KPART ${_p}\n#include \"${_sp}/src/kernels.cu\"\n")
89  if(_fnx_hip)
90    set_source_files_properties(${_f} PROPERTIES LANGUAGE HIP GENERATED TRUE)
91  endif()
92  list(APPEND _sp_kparts ${_f})
93endforeach()
94
95# DFlash2 on the split (src/split/src/df2_bridge.*): the main tree's drafter (src/core/dflash2.cpp, bind() compiled out: it copies head rows between cards with
96# the peer API, which this engine never calls) and its kernels, in a library of their own; the fnx sources see only df2_bridge.hpp. CUDA builds of the split
97# must be configured with -DCMAKE_CUDA_RUNTIME_LIBRARY=Shared (the engine links the shared runtime so the no-peer check can read its symbols)
98set(_sp_df2 OFF)
99if(TARGET strata_kernels AND TARGET strata_core)
100  add_library(strata_split_df2 STATIC ${CMAKE_SOURCE_DIR}/src/core/dflash2.cpp ${_sp}/src/df2_bridge.cpp)
101  target_compile_definitions(strata_split_df2 PRIVATE STRATA_DF2_NO_BIND=1)
102  target_include_directories(strata_split_df2 PRIVATE ${_sp}/src)
103  target_link_libraries(strata_split_df2 PUBLIC strata_kernels strata_core ${_strata_gpu_runtime_target})
104  set(_sp_df2 ON)
105endif()
106
107foreach(_n ${STRATA_SPLIT_CARDS})
108  if(NOT _n MATCHES "^[234]$")
109    message(FATAL_ERROR "STRATA_SPLIT_CARDS: 2, 3 or 4, not ${_n}")
110  endif()
111  set(_t strata-split-${_n})
112  add_executable(${_t} ${_sp_engine} ${_sp_kparts})
113  if(_fnx_hip)
114    # HIP: src/hip first (cuda.h, mma.h stand-ins), the force-included fnx_hip.h after the tree's cuda_runtime.h shim
115    target_include_directories(${_t} PRIVATE ${_sp}/src/hip ${_sp}/src ${_sp}/tools ${_sp_quant})
116    target_compile_definitions(${_t} PRIVATE FNX_NCARD=${_n} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0 FNX_HOSTRING=1 FNX_HIP=1
117                               FNX_DIR="${_sp}")
118    target_compile_options(${_t} PRIVATE "$<$<COMPILE_LANGUAGE:HIP>:SHELL:-include ${_sp}/src/hip/fnx_hip.h>")
119    target_link_libraries(${_t} PRIVATE strata_hip_runtime ${STRATA_HIP_BLAS_TARGETS})
120    set_target_properties(${_t} PROPERTIES CXX_STANDARD 17 HIP_STANDARD 17 HIP_STANDARD_REQUIRED ON HIP_ARCHITECTURES "${_fnx_hip_archs}")
121    find_package(Threads REQUIRED)
122    target_link_libraries(${_t} PRIVATE Threads::Threads)
123    if(_sp_df2)
124      target_compile_definitions(${_t} PRIVATE FNX_DF2=1)
125      target_link_libraries(${_t} PRIVATE strata_split_df2)
126    endif()
127    continue()
128  endif()
129  target_include_directories(${_t} PRIVATE ${_sp}/src ${_sp}/tools ${_sp_quant})
130  target_compile_definitions(${_t} PRIVATE FNX_NCARD=${_n} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0 FNX_HOSTRING=1 FNX_PF_JOB=${STRATA_SPLIT_PF_JOB}
131                             FNX_DIR="${_sp}")
132  target_compile_options(${_t} PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
133  set_target_properties(${_t} PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON
134                        CXX_STANDARD 17)
135  find_package(Threads REQUIRED)
136  find_package(CUDAToolkit REQUIRED)
137  target_link_libraries(${_t} PRIVATE CUDA::cublas CUDA::cublasLt CUDA::cuda_driver Threads::Threads)
138  if(_sp_df2)
139    target_compile_definitions(${_t} PRIVATE FNX_DF2=1)
140    target_link_libraries(${_t} PRIVATE strata_split_df2)
141  endif()
142  # libcudart shared: every runtime call the binary can make is then visible to `nm -D --undefined-only` (the no-peer test reads it)
143  set_target_properties(${_t} PROPERTIES CUDA_RUNTIME_LIBRARY Shared)
144endforeach()
145
146# the engine's kernel self-test on one card, with no model and no engine start (cleanup 4.4; tools/selftest_main.cpp).  HIP only, off by default
147# (EXCLUDE_FROM_ALL: built by name).  The engine runs the same src/selftest.cu before it loads a model on a card Strata has not run the split on;
148# this is the way to ask a card that question alone:
149#   cmake --build <dir> --target strata-split-selftest && <dir>/src/split/strata-split-selftest [device]     (exit 0: every test passed)
150if(_fnx_hip)
151  list(GET STRATA_SPLIT_CARDS 0 _nst)
152  set(_t strata-split-selftest)
153  add_executable(${_t} EXCLUDE_FROM_ALL ${_sp}/tools/selftest_main.cpp ${_sp}/src/selftest.cu ${_sp}/src/moe_mma.cu ${_sp_kparts})
154  set_source_files_properties(${_sp}/tools/selftest_main.cpp ${_sp}/src/selftest.cu ${_sp}/src/moe_mma.cu PROPERTIES LANGUAGE HIP)
155  target_include_directories(${_t} PRIVATE ${_sp}/src/hip ${_sp}/src ${_sp}/tools ${_sp_quant})
156  target_compile_definitions(${_t} PRIVATE FNX_NCARD=${_nst} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0 FNX_HOSTRING=1 FNX_HIP=1
157                             FNX_DIR="${_sp}")
158  target_compile_options(${_t} PRIVATE "$<$<COMPILE_LANGUAGE:HIP>:SHELL:-include ${_sp}/src/hip/fnx_hip.h>")
159  target_link_libraries(${_t} PRIVATE strata_hip_runtime ${STRATA_HIP_BLAS_TARGETS})
160  set_target_properties(${_t} PROPERTIES CXX_STANDARD 17 HIP_STANDARD 17 HIP_STANDARD_REQUIRED ON HIP_ARCHITECTURES "${_fnx_hip_archs}")
161endif()
162
163# the CPU unit tests sFNX ships (split rule, ring plan, card checks, batch plan, rope, input parser): plain C++, no GPU
164if(STRATA_BUILD_TESTS)
165  list(GET STRATA_SPLIT_CARDS 0 _n0)
166  foreach(_tt expert_residency_test batch_plan_test pf_ring_plan_test card_check_test rope_test sfnx_input_test peer_policy_test rccl_policy_test selftest_policy_test memlock_test)
167    add_executable(split_${_tt} ${_sp}/tools/${_tt}.cpp)
168    target_include_directories(split_${_tt} PRIVATE ${_sp}/src ${_sp}/tools)
169    target_compile_definitions(split_${_tt} PRIVATE FNX_NCARD=${_n0} FNX_27B=0)
170    target_compile_features(split_${_tt} PRIVATE cxx_std_17)
171    set_target_properties(split_${_tt} PROPERTIES CUDA_ARCHITECTURES OFF)
172    find_package(Threads REQUIRED)
173    target_link_libraries(split_${_tt} PRIVATE Threads::Threads ${CMAKE_DL_LIBS})
174    add_test(NAME split_${_tt} COMMAND split_${_tt})
175  endforeach()
176  # the load-time conversion's oracle tool (tests/split/convert_check.py compares its output with gguf-py on a real file; not a ctest)
177  add_executable(split_convert_dump ${_sp}/tools/convert_dump.cpp ${_sp}/src/gguf.cpp)
178  target_include_directories(split_convert_dump PRIVATE ${_sp}/src ${_sp}/tools)
179  target_compile_definitions(split_convert_dump PRIVATE FNX_NCARD=${_n0} FNX_27B=0)
180  target_compile_features(split_convert_dump PRIVATE cxx_std_17)
181  set_target_properties(split_convert_dump PROPERTIES CUDA_ARCHITECTURES OFF)
182  target_link_libraries(split_convert_dump PRIVATE Threads::Threads)
183  # the weight tiles of the prompt kernel run on a GPU against the file (tests/split/tile_check.py reads its output); not a ctest
184  add_executable(split_moe_tile_test ${_sp}/tools/moe_tile_test.cu ${_sp}/src/gguf.cpp)
185  target_include_directories(split_moe_tile_test PRIVATE ${_sp}/src ${_sp}/tools ${_sp_quant})
186  target_compile_definitions(split_moe_tile_test PRIVATE FNX_NCARD=${_n0} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0)
187  target_compile_options(split_moe_tile_test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
188  set_target_properties(split_moe_tile_test PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CXX_STANDARD 17)
189  target_link_libraries(split_moe_tile_test PRIVATE Threads::Threads)
190  # src/qdot.cuh (the split's decode dots) against ggml-cuda's vecdotq.cuh, bit for bit, on random blocks (not a ctest: needs a GPU). The one split
191  # target that includes ggml-cuda, as the reference; it goes when the vendored tree goes (phaseA-tests/par/RESULTS-OWN-4S.md, oracle note)
192  if(NOT _fnx_hip AND _strata_ggml_root AND EXISTS "${_strata_ggml_root}/ggml/src/ggml-cuda/vecdotq.cuh")
193    add_executable(split_qdot_test ${_sp}/tools/qdot_test.cu)
194    target_include_directories(split_qdot_test PRIVATE ${_sp}/src ${_strata_ggml_root}/ggml/src/ggml-cuda ${_strata_ggml_root}/ggml/src
195                               ${_strata_ggml_root}/ggml/include ${_sp_quant})
196    target_compile_definitions(split_qdot_test PRIVATE FNX_NCARD=${_n0} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0)
197    target_compile_options(split_qdot_test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
198    set_target_properties(split_qdot_test PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CXX_STANDARD 17)
199  endif()
200  # the same dots against the stored digests tools/qdot_golden.txt (written by split_qdot_test --write-golden), without ggml: the check that stays when the
201  # vendored tree goes.  A ctest that skips (77) without a GPU.  CUDA only for now: the HIP build gets it once its bits were compared on a card.
202  if(NOT _fnx_hip)
203    add_executable(split_qdot_golden ${_sp}/tools/qdot_golden.cu)
204    target_include_directories(split_qdot_golden PRIVATE ${_sp}/src ${_sp}/tools ${_sp_quant})
205    target_compile_definitions(split_qdot_golden PRIVATE FNX_NCARD=${_n0} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0)
206    target_compile_options(split_qdot_golden PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
207    set_target_properties(split_qdot_golden PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CXX_STANDARD 17)
208    add_test(NAME split_qdot_golden COMMAND split_qdot_golden ${_sp}/tools/qdot_golden.txt)
209    set_tests_properties(split_qdot_golden PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300)
210  endif()
211  # FNX_PFX_GDN_TC's tensor-core chunk scan against the sub-chunk kernels it replaces, one card, random inputs (not a ctest: needs a GPU)
212  if(NOT _fnx_hip)
213    add_executable(split_gdn_tc_test ${_sp}/tools/gdn_tc_test.cu)
214    target_include_directories(split_gdn_tc_test PRIVATE ${_sp}/src ${_sp}/tools ${_sp_quant})
215    target_compile_definitions(split_gdn_tc_test PRIVATE FNX_NCARD=${_n0} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0)
216    target_compile_options(split_gdn_tc_test PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
217    set_target_properties(split_gdn_tc_test PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CXX_STANDARD 17)
218  endif()
219  # fnx_run: sFNX's test driver (teacher-forced logits to an .lgr file, prompt/decode checks), the same engine objects with its own main
220  set(_sp_run_src ${_sp_engine})
221  list(REMOVE_ITEM _sp_run_src ${_sp}/tools/sfnx.cpp)
222  add_executable(split_fnx_run ${_sp_run_src} ${_sp}/tools/fnx_run.cpp ${_sp_kparts})
223  set_source_files_properties(${_sp}/tools/fnx_run.cpp PROPERTIES LANGUAGE CUDA)
224  target_include_directories(split_fnx_run PRIVATE ${_sp}/src ${_sp}/tools ${_sp_quant})
225  target_compile_definitions(split_fnx_run PRIVATE FNX_NCARD=${_n0} FNX_ARCH=${STRATA_SPLIT_ARCH} FNX_27B=0 FNX_HOSTRING=1 FNX_DIR="${_sp}")
226  target_compile_options(split_fnx_run PRIVATE $<$<COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets>)
227  set_target_properties(split_fnx_run PROPERTIES CUDA_ARCHITECTURES "${STRATA_SPLIT_ARCH}-real" CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON CXX_STANDARD 17)
228  find_package(CUDAToolkit REQUIRED)
229  target_link_libraries(split_fnx_run PRIVATE CUDA::cublas CUDA::cublasLt CUDA::cuda_driver Threads::Threads)
230  set_target_properties(split_fnx_run PROPERTIES CUDA_RUNTIME_LIBRARY Shared)
231endif()