okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
41a63c550
main
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_HIPAND NOT STRATA_ENABLE_CUDAAND NOT WIN32AND NOT APPLE) 22set ( _fnx_hip ON ) 23endif () 24if ( NOT _fnx_hipAND ( NOT STRATA_ENABLE_CUDAOR WIN32OR APPLE)) 25message ( STATUS "STRATA_BUILD_SPLIT: needs a Linux CUDA or HIP build, skipped" ) 26return () 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" ) 32message ( WARNING "STRATA_BUILD_SPLIT: third_party/strata-ggml/strata-quant-common.h is missing; strata-split skipped" ) 33return () 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 42set ( STRATA_SPLIT_HIP_ARCHS "" CACHE STRING "code objects of strata-split-N (gfx1201;gfx1200;gfx11-generic), empty: CMAKE_HIP_ARCHITECTURES" ) 43if ( STRATA_SPLIT_HIP_ARCHS) 44set ( _fnx_hip_archs ${ STRATA_SPLIT_HIP_ARCHS }) 45else () 46set ( _fnx_hip_archs ${ STRATA_HIP_ARCH_LIST }) 47endif () 48set ( _fnx_hip_ok gfx1100 gfx1101 gfx1102 gfx1103 gfx11-generic gfx1200 gfx1201 ) 49set ( _fnx_hip_bad "" ) 50foreach ( _a${ _fnx_hip_archs }) 51if ( NOT _aIN_LIST _fnx_hip_ok) 52list ( APPEND _fnx_hip_bad ${ _a }) 53endif () 54endforeach () 55if ( NOT _fnx_hip_archsOR _fnx_hip_bad) 56message ( WARNING "STRATA_BUILD_SPLIT (HIP): the split engine builds for ${ _fnx_hip_ok } only (got ${ _fnx_hip_archs } ), skipped" ) 57return () 58endif () 59set ( 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) 61string ( REGEX MATCH "[0-9]+" STRATA_SPLIT_ARCH " ${ CMAKE_CUDA_ARCHITECTURES } " ) 62if ( NOT STRATA_SPLIT_ARCH) 63set ( STRATA_SPLIT_ARCH 86 ) 64endif () 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) 72list ( 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) 75set ( _fnx_lang HIP ) 76else () 77set ( _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) 82set_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 RANGE0 8) 87set ( _f ${ CMAKE_CURRENT_BINARY_DIR } /kernels_p ${ _p } .cu ) 88file ( GENERATE OUTPUT ${ _f } CONTENT "#define FNX_KPART ${ _p } \n#include \" ${ _sp } /src/kernels.cu\"\n" ) 89if ( _fnx_hip) 90set_source_files_properties ( $ { _f } PROPERTIES LANGUAGE HIP GENERATED TRUE ) 91endif () 92list ( 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_kernelsAND TARGET strata_core) 100add_library ( strata_split_df2 STATIC ${ CMAKE_SOURCE_DIR } /src/core/dflash2.cpp ${ _sp } /src/df2_bridge.cpp ) 101target_compile_definitions ( strata_split_df2 PRIVATE STRATA_DF2_NO_BIND=1 ) 102target_include_directories ( strata_split_df2 PRIVATE ${ _sp } /src ) 103target_link_libraries ( strata_split_df2 PUBLIC strata_kernels strata_core ${ _strata_gpu_runtime_target }) 104set ( _sp_df2 ON ) 105endif () 106 107foreach ( _n${ STRATA_SPLIT_CARDS }) 108if ( NOT _nMATCHES "^[234] $ " ) 109message ( FATAL_ERROR "STRATA_SPLIT_CARDS: 2, 3 or 4, not ${ _n } " ) 110endif () 111set ( _t strata-split- ${ _n }) 112add_executable ( $ { _t } ${ _sp_engine } ${ _sp_kparts }) 113if ( _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 115target_include_directories ( $ { _t } PRIVATE ${ _sp } /src/hip ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 116target_compile_definitions ( $ { _t } PRIVATE FNX_NCARD= ${ _n } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 FNX_HOSTRING=1 FNX_HIP=1 117FNX_DIR= " ${ _sp } " ) 118target_compile_options ( $ { _t } PRIVATE " $ < $ <COMPILE_LANGUAGE:HIP>:SHELL:-include ${ _sp } /src/hip/fnx_hip.h>" ) 119target_link_libraries ( $ { _t } PRIVATE strata_hip_runtime ${ STRATA_HIP_BLAS_TARGETS }) 120set_target_properties ( $ { _t } PROPERTIES CXX_STANDARD 17 HIP_STANDARD 17 HIP_STANDARD_REQUIRED ON HIP_ARCHITECTURES " ${ _fnx_hip_archs } " ) 121find_package ( Threads REQUIRED ) 122target_link_libraries ( $ { _t } PRIVATE Threads::Threads ) 123if ( _sp_df2) 124target_compile_definitions ( $ { _t } PRIVATE FNX_DF2=1 ) 125target_link_libraries ( $ { _t } PRIVATE strata_split_df2 ) 126endif () 127continue () 128endif () 129target_include_directories ( $ { _t } PRIVATE ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 130target_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 } 131FNX_DIR= " ${ _sp } " ) 132target_compile_options ( $ { _t } PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 133set_target_properties ( $ { _t } PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON 134CXX_STANDARD 17 ) 135find_package ( Threads REQUIRED ) 136find_package ( CUDAToolkit REQUIRED ) 137target_link_libraries ( $ { _t } PRIVATE CUDA::cublas CUDA::cublasLt CUDA::cuda_driver Threads::Threads ) 138if ( _sp_df2) 139target_compile_definitions ( $ { _t } PRIVATE FNX_DF2=1 ) 140target_link_libraries ( $ { _t } PRIVATE strata_split_df2 ) 141endif () 142# libcudart shared: every runtime call the binary can make is then visible to `nm -D --undefined-only` (the no-peer test reads it) 143set_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) 151list ( GET STRATA_SPLIT_CARDS 0 _nst ) 152set ( _t strata-split-selftest ) 153add_executable ( $ { _t } EXCLUDE_FROM_ALL ${ _sp } /tools/selftest_main.cpp ${ _sp } /src/selftest.cu ${ _sp } /src/moe_mma.cu ${ _sp_kparts }) 154set_source_files_properties ( $ { _sp } /tools/selftest_main.cpp ${ _sp } /src/selftest.cu ${ _sp } /src/moe_mma.cu PROPERTIES LANGUAGE HIP ) 155target_include_directories ( $ { _t } PRIVATE ${ _sp } /src/hip ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 156target_compile_definitions ( $ { _t } PRIVATE FNX_NCARD= ${ _nst } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 FNX_HOSTRING=1 FNX_HIP=1 157FNX_DIR= " ${ _sp } " ) 158target_compile_options ( $ { _t } PRIVATE " $ < $ <COMPILE_LANGUAGE:HIP>:SHELL:-include ${ _sp } /src/hip/fnx_hip.h>" ) 159target_link_libraries ( $ { _t } PRIVATE strata_hip_runtime ${ STRATA_HIP_BLAS_TARGETS }) 160set_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) 165list ( GET STRATA_SPLIT_CARDS 0 _n0 ) 166foreach ( _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) 167add_executable ( split_ ${ _tt } ${ _sp } /tools/ ${ _tt } .cpp ) 168target_include_directories ( split_ ${ _tt } PRIVATE ${ _sp } /src ${ _sp } /tools ) 169target_compile_definitions ( split_ ${ _tt } PRIVATE FNX_NCARD= ${ _n0 } FNX_27B=0 ) 170target_compile_features ( split_ ${ _tt } PRIVATE cxx_std_17 ) 171set_target_properties ( split_ ${ _tt } PROPERTIES CUDA_ARCHITECTURES OFF ) 172find_package ( Threads REQUIRED ) 173target_link_libraries ( split_ ${ _tt } PRIVATE Threads::Threads ${ CMAKE_DL_LIBS }) 174add_test ( NAME split_ ${ _tt } COMMAND split_ ${ _tt }) 175endforeach () 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) 177add_executable ( split_convert_dump ${ _sp } /tools/convert_dump.cpp ${ _sp } /src/gguf.cpp ) 178target_include_directories ( split_convert_dump PRIVATE ${ _sp } /src ${ _sp } /tools ) 179target_compile_definitions ( split_convert_dump PRIVATE FNX_NCARD= ${ _n0 } FNX_27B=0 ) 180target_compile_features ( split_convert_dump PRIVATE cxx_std_17 ) 181set_target_properties ( split_convert_dump PROPERTIES CUDA_ARCHITECTURES OFF ) 182target_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 184add_executable ( split_moe_tile_test ${ _sp } /tools/moe_tile_test.cu ${ _sp } /src/gguf.cpp ) 185target_include_directories ( split_moe_tile_test PRIVATE ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 186target_compile_definitions ( split_moe_tile_test PRIVATE FNX_NCARD= ${ _n0 } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 ) 187target_compile_options ( split_moe_tile_test PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 188set_target_properties ( split_moe_tile_test PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CXX_STANDARD 17 ) 189target_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) 192if ( NOT _fnx_hipAND _strata_ggml_rootAND EXISTS " ${ _strata_ggml_root } /ggml/src/ggml-cuda/vecdotq.cuh" ) 193add_executable ( split_qdot_test ${ _sp } /tools/qdot_test.cu ) 194target_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 }) 196target_compile_definitions ( split_qdot_test PRIVATE FNX_NCARD= ${ _n0 } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 ) 197target_compile_options ( split_qdot_test PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 198set_target_properties ( split_qdot_test PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CXX_STANDARD 17 ) 199endif () 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. 202if ( NOT _fnx_hip) 203add_executable ( split_qdot_golden ${ _sp } /tools/qdot_golden.cu ) 204target_include_directories ( split_qdot_golden PRIVATE ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 205target_compile_definitions ( split_qdot_golden PRIVATE FNX_NCARD= ${ _n0 } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 ) 206target_compile_options ( split_qdot_golden PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 207set_target_properties ( split_qdot_golden PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CXX_STANDARD 17 ) 208add_test ( NAME split_qdot_golden COMMAND split_qdot_golden ${ _sp } /tools/qdot_golden.txt ) 209set_tests_properties ( split_qdot_golden PROPERTIES SKIP_RETURN_CODE 77 TIMEOUT 300 ) 210endif () 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) 212if ( NOT _fnx_hip) 213add_executable ( split_gdn_tc_test ${ _sp } /tools/gdn_tc_test.cu ) 214target_include_directories ( split_gdn_tc_test PRIVATE ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 215target_compile_definitions ( split_gdn_tc_test PRIVATE FNX_NCARD= ${ _n0 } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 ) 216target_compile_options ( split_gdn_tc_test PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 217set_target_properties ( split_gdn_tc_test PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CXX_STANDARD 17 ) 218endif () 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 220set ( _sp_run_src ${ _sp_engine }) 221list ( REMOVE_ITEM _sp_run_src ${ _sp } /tools/sfnx.cpp ) 222add_executable ( split_fnx_run ${ _sp_run_src } ${ _sp } /tools/fnx_run.cpp ${ _sp_kparts }) 223set_source_files_properties ( $ { _sp } /tools/fnx_run.cpp PROPERTIES LANGUAGE CUDA ) 224target_include_directories ( split_fnx_run PRIVATE ${ _sp } /src ${ _sp } /tools ${ _sp_quant }) 225target_compile_definitions ( split_fnx_run PRIVATE FNX_NCARD= ${ _n0 } FNX_ARCH= ${ STRATA_SPLIT_ARCH } FNX_27B=0 FNX_HOSTRING=1 FNX_DIR= " ${ _sp } " ) 226target_compile_options ( split_fnx_run PRIVATE $ < $ <COMPILE_LANGUAGE:CUDA>:--use_fast_math --expt-relaxed-constexpr -Wno-deprecated-gpu-targets> ) 227set_target_properties ( split_fnx_run PROPERTIES CUDA_ARCHITECTURES " ${ STRATA_SPLIT_ARCH } -real" CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON CXX_STANDARD 17 ) 228find_package ( CUDAToolkit REQUIRED ) 229target_link_libraries ( split_fnx_run PRIVATE CUDA::cublas CUDA::cublasLt CUDA::cuda_driver Threads::Threads ) 230set_target_properties ( split_fnx_run PROPERTIES CUDA_RUNTIME_LIBRARY Shared ) 231endif ()