okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
508e6999a
main
1# cmake/strata_ggml_tq_overlay.cmake - MMQ prompt kernels for the Gyro trellis types (TQ2_T, TQK6, TQK7) on an UPSTREAM llama.cpp. 2# 3# ONLY FOR -DSTRATA_GGML_DIR=<llama.cpp checkout> BUILDS (the override kept for two releases). The default build uses the ggml vendored in 4# third_party/strata-ggml, which holds this script's output as plain files in ggml/src/ggml-cuda-tq (third_party/strata-ggml/UPSTREAM.md); 5# this script is not run for it. 6# 7# The agentionai llama.cpp fork has these types in its ggml (ggml.h enum, ggml-cuda/tq.cuh, a tile loader, MMQ configs and 8# routing, three template instances). Upstream does not. This script writes, at configure time, a small OVERLAY of the 9# pinned ggml-cuda headers into the build tree: copies of ggml.h, mmq.cuh, mmq-load-tiles.cuh and the mmq-config-*.cuh with 10# the trellis types added the way IQ2_XS is handled (same Q8_0_16 tile layout, same dot kernels, same configs - the fork's 11# own numbers for them are copies of IQ2_XS's), plus tq.cuh / tq-mmq-tiles.cuh from third_party/strata-ggml/ggml/src/ggml-cuda-tq and the template 12# instances. Only the strata_mmq target sees the overlay (it comes first on its include path); every other type's 13# instances are compiled from the untouched pinned files, and the additions are additive (identity gate: Q2_0, IQ3_XXS). 14# 15# Every edit is anchored on pinned text; if an anchor is missing (another llama.cpp), the script says so and the trellis 16# MMQ stays off (STRATA_TQ_OVERLAY_OK=OFF): the engine then reads trellis prompts through the FP16 path, as before. 17# 18# strata_ggml_tq_overlay(<llama.cpp dir> <out dir>) -> STRATA_TQ_OVERLAY_OK (parent scope) 19 20function ( strata_ggml_tq_overlay LLAMA OUT ) 21set ( STRATA_TQ_OVERLAY_OK OFF PARENT_SCOPE ) 22set ( _cu " ${ LLAMA } /ggml/src/ggml-cuda" ) 23set ( _inc " ${ LLAMA } /ggml/include" ) 24set ( _tq " ${ CMAKE_SOURCE_DIR } /third_party/strata-ggml/ggml/src/ggml-cuda-tq" ) 25foreach ( _f" ${ _inc } /ggml.h" " ${ _cu } /mmq.cuh" " ${ _cu } /mmq-load-tiles.cuh" " ${ _cu } /common.cuh" ) 26if ( NOT EXISTS " ${ _f } " ) 27message ( STATUS "trellis MMQ overlay: ${ _f } is missing: off" ) 28return () 29endif () 30endforeach () 31 32# ---- helpers: read a file with LF line ends; insert text before / after the single occurrence of an anchor ---------- 33macro ( _tq_read VAR PATH ) 34file ( READ " ${ PATH } " ${ VAR }) 35string ( REPLACE "\r\n" "\n" ${ VAR } " ${ $ { VAR }} " ) 36set_property ( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS " ${ PATH } " ) 37endmacro () 38macro ( _tq_edit VAR WHERE ANCHOR TEXT ) # WHERE = BEFORE | AFTER; the anchor must occur exactly once 39string ( FIND " ${ $ { VAR }} " " ${ ANCHOR } " _p ) 40if ( _pLESS 0 ) 41set ( _tq_fail "anchor not found: ${ ANCHOR } " ) 42else () 43string ( LENGTH " ${ ANCHOR } " _alen ) 44math ( EXPR _after " ${ _p } + ${ _alen } " ) 45string ( SUBSTRING " ${ $ { VAR }} " ${ _after } -1 _tail ) 46string ( FIND " ${ _tail } " " ${ ANCHOR } " _p2 ) 47if ( NOT _p2LESS 0 ) 48set ( _tq_fail "anchor is not unique: ${ ANCHOR } " ) 49else () 50string ( SUBSTRING " ${ $ { VAR }} " 0 ${ _p } _head ) 51if ( " ${ WHERE } " STREQUAL "AFTER" ) 52set ( $ { VAR } " ${ _head }${ ANCHOR }${ TEXT }${ _tail } " ) 53else () 54set ( $ { VAR } " ${ _head }${ TEXT }${ ANCHOR }${ _tail } " ) 55endif () 56endif () 57endif () 58endmacro () 59set ( _tq_fail "" ) 60 61file ( MAKE_DIRECTORY " ${ OUT } /include" " ${ OUT } /template-instances" ) 62 63# ---- the whole header set of ggml-cuda and ggml/include is copied: quote includes resolve next to the including file, 64# so a header left in the pinned directory would pull the pinned mmq.cuh / ggml.h back in beside the patched ones ------- 65file ( GLOB _all_cu " ${ _cu } /*.cuh" " ${ _cu } /*.h" ) 66foreach ( _f IN LISTS _all_cu) 67get_filename_component ( _n " ${ _f } " NAME ) 68configure_file ( " ${ _f } " " ${ OUT } / ${ _n } " COPYONLY ) 69endforeach () 70configure_file ( " ${ _cu } /quantize.cu" " ${ OUT } /quantize.cu" COPYONLY ) # compiled from here: it includes mmq.cuh 71if ( IS_DIRECTORY " ${ _cu } /vendors" ) 72file ( COPY " ${ _cu } /vendors" DESTINATION " ${ OUT } " ) 73endif () 74file ( GLOB _all_inc " ${ _inc } /*.h" ) 75foreach ( _f IN LISTS _all_inc) 76get_filename_component ( _n " ${ _f } " NAME ) 77configure_file ( " ${ _f } " " ${ OUT } /include/ ${ _n } " COPYONLY ) 78endforeach () 79 80# ---- ggml.h: the three enumerators (values above GGML_TYPE_COUNT: ggml's own tables never see them) --------------------- 81_tq_read ( _h " ${ _inc } /ggml.h" ) 82_tq_edit ( _h BEFORE " GGML_TYPE_COUNT = 43," 83" GGML_TYPE_TQ2_T = 144, // Agention trellis types (Strata overlay; the fork's ids) 84GGML_TYPE_TQK6 = 145, 85GGML_TYPE_TQK7 = 146, 86GGML_TYPE_TQP5 = 147, // Gyro-L pair trellis (the same fork's 147-149) 87GGML_TYPE_TQP6 = 148, 88GGML_TYPE_TQP7 = 149, 89" ) 90file ( WRITE " ${ OUT } /include/ggml.h" " ${ _h } " ) 91 92# ---- mmq.cuh: routing of the trellis types as IQ2_XS ---------------------------------------------------------------- 93_tq_read ( _m " ${ _cu } /mmq.cuh" ) 94# #3217: llama.cpp from b11xxx on gives the MMQ helpers a ggml_prec argument (template <..., ggml_prec prec_src1> for the write-back and 95# MMA dot callbacks, a fourth parameter for ggml_cuda_mmq_get_nthreads / get_I / get_sram_stride). The pinned commit has neither. The 96# generated routing and the loader follow whichever signature mmq.cuh has; one that is neither turns the overlay off at configure time 97# (the engine then reads trellis prompts through the FP16 path) instead of failing later inside nvcc. 98set ( _ps "" ) # ", prec_src1" in the generated callbacks 99set ( _pa "" ) # ", GGML_PREC_Q8" in the loader's calls (the value upstream's own IQ2_XS loader passes) 100string ( FIND " ${ _m } " "ggml_cuda_mmq_get_nthreads(ggml_type type, int J, bool fallback, ggml_prec prec_src1)" _sig_new ) 101string ( FIND " ${ _m } " "ggml_cuda_mmq_get_nthreads(ggml_type type, int J, bool fallback)" _sig_old ) 102if ( NOT _sig_newLESS 0 ) 103set ( _ps ", prec_src1" ) 104set ( _pa ", GGML_PREC_Q8" ) 105elseif ( _sig_oldLESS 0 ) 106set ( _tq_fail "the MMQ helper signatures of this ggml-cuda are not the ones the overlay knows (ggml_cuda_mmq_get_nthreads)" ) 107endif () 108_tq_edit ( _m AFTER " case GGML_TYPE_IQ2_XXS:\n case GGML_TYPE_IQ2_XS:\n" 109" case GGML_TYPE_TQ2_T:\n case GGML_TYPE_TQK6:\n case GGML_TYPE_TQK7:\n case GGML_TYPE_TQP5:\n case GGML_TYPE_TQP6:\n case GGML_TYPE_TQP7:\n" ) 110_tq_edit ( _m AFTER " case GGML_TYPE_IQ2_XS: return MMQ_DP4A_TXS_Q8_0_16;\n" 111" case GGML_TYPE_TQ2_T: return MMQ_DP4A_TXS_Q8_0_16;\n case GGML_TYPE_TQK6: return MMQ_DP4A_TXS_Q8_0_16;\n case GGML_TYPE_TQK7: return MMQ_DP4A_TXS_Q8_0_16;\n case GGML_TYPE_TQP5: return MMQ_DP4A_TXS_Q8_0_16;\n case GGML_TYPE_TQP6: return MMQ_DP4A_TXS_Q8_0_16;\n case GGML_TYPE_TQP7: return MMQ_DP4A_TXS_Q8_0_16;\n" ) 112_tq_edit ( _m BEFORE " case GGML_TYPE_IQ3_XXS:\n return ggml_cuda_mmq_util_funcs(\n VDR_IQ3_XXS_Q8_1_MMQ," 113" case GGML_TYPE_TQ2_T:\n case GGML_TYPE_TQK6:\n case GGML_TYPE_TQK7:\n case GGML_TYPE_TQP5:\n case GGML_TYPE_TQP6:\n case GGML_TYPE_TQP7:\n return ggml_cuda_mmq_util_funcs(\n VDR_IQ2_XS_Q8_1_MMQ,\n ggml_cuda_mmq_load_tiles_tq<type, J, fallback>,\n ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_dp4a<type, J, fallback>,\n ggml_cuda_mmq_write_back_dp4a<type, J, fallback ${ _ps } >);\n" ) 114_tq_edit ( _m BEFORE " case GGML_TYPE_IQ3_XXS:\n return ggml_cuda_mmq_util_funcs(\n -1,\n ggml_cuda_mmq_load_tiles_iq3_xxs<type, J, fallback>," 115" case GGML_TYPE_TQ2_T:\n case GGML_TYPE_TQK6:\n case GGML_TYPE_TQK7:\n case GGML_TYPE_TQP5:\n case GGML_TYPE_TQP6:\n case GGML_TYPE_TQP7:\n return ggml_cuda_mmq_util_funcs(\n -1,\n ggml_cuda_mmq_load_tiles_tq<type, J, fallback>,\n ggml_cuda_mmq_vec_dot_q8_0_16_q8_1_mma<type, J, fallback ${ _ps } >,\n ggml_cuda_mmq_write_back_mma<type, J, fallback ${ _ps } >);\n" ) 116_tq_edit ( _m AFTER "extern DECL_MMQ_CASE(GGML_TYPE_IQ4_XS);\n" 117"extern DECL_MMQ_CASE(GGML_TYPE_TQ2_T);\nextern DECL_MMQ_CASE(GGML_TYPE_TQK6);\nextern DECL_MMQ_CASE(GGML_TYPE_TQK7);\nextern DECL_MMQ_CASE(GGML_TYPE_TQP5);\nextern DECL_MMQ_CASE(GGML_TYPE_TQP6);\nextern DECL_MMQ_CASE(GGML_TYPE_TQP7);\n" ) 118file ( WRITE " ${ OUT } /mmq.cuh" " ${ _m } " ) 119 120# ---- mmq-load-tiles.cuh: the loader, at the end ------------------------------------------------------------------- 121_tq_read ( _l " ${ _cu } /mmq-load-tiles.cuh" ) 122file ( WRITE " ${ OUT } /mmq-load-tiles.cuh" " ${ _l } \n#include \"tq-mmq-tiles.cuh\"\n" ) 123# the vendored tq.cuh names its two Strata headers by path from its own folder; here they sit beside it (the caller copies them): plain names 124file ( READ " ${ _tq } /tq.cuh" _tqh ) 125set_property ( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS " ${ _tq } /tq.cuh" ) 126string ( REPLACE "#include \"../../../../ggml/ggml-common-tq.h\"" "#include \"ggml-common-tq.h\"" _tqh " ${ _tqh } " ) 127string ( REPLACE "#include \"../../../../../src/kernels/cuda/tq_lut_i8.cuh\"" "#include \"tq_lut_i8.cuh\"" _tqh " ${ _tqh } " ) 128file ( WRITE " ${ OUT } /tq.cuh" " ${ _tqh } " ) 129file ( READ " ${ _tq } /tq-mmq-tiles.cuh" _tiles ) 130set_property ( DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS " ${ _tq } /tq-mmq-tiles.cuh" ) 131foreach ( _fn get_nthreads get_I get_sram_stride) 132string ( REPLACE "ggml_cuda_mmq_ ${ _fn } (type, J, fallback)" "ggml_cuda_mmq_ ${ _fn } (type, J, fallback ${ _pa } )" _tiles " ${ _tiles } " ) 133endforeach () 134file ( WRITE " ${ OUT } /tq-mmq-tiles.cuh" " ${ _tiles } " ) 135 136# ---- mmq-config-*.cuh: every IQ2_XS CASE line gets one copy per trellis type --------------------------------------- 137file ( GLOB _cfgs " ${ _cu } /mmq-config-*.cuh" ) 138foreach ( _c${ _cfgs }) 139get_filename_component ( _cn " ${ _c } " NAME ) 140_tq_read ( _t " ${ _c } " ) 141string ( REPLACE ";" "<SEMI>" _t " ${ _t } " ) # the CASE lines hold semicolons; a CMake list would split them 142string ( REGEX MATCHALL "[^\n]*CASE\\(GGML_TYPE_IQ2_XS,[^\n]*\n" _lines " ${ _t } " ) 143list ( REMOVE_DUPLICATES _lines ) 144foreach ( _ln IN LISTS _lines) 145set ( _add "" ) 146foreach ( _ty TQ2_T TQK6 TQK7 TQP5 TQP6 TQP7) 147string ( REPLACE "GGML_TYPE_IQ2_XS," "GGML_TYPE_ ${ _ty } ," _x " ${ _ln } " ) 148string ( APPEND _add " ${ _x } " ) 149endforeach () 150_tq_edit ( _t AFTER " ${ _ln } " " ${ _add } " ) 151endforeach () 152string ( REPLACE "<SEMI>" ";" _t " ${ _t } " ) 153file ( WRITE " ${ OUT } / ${ _cn } " " ${ _t } " ) 154endforeach () 155 156# ---- template instances ------------------------------------------------------------------------------------------- 157foreach ( _ty tq2_t tqk6 tqk7 tqp5 tqp6 tqp7) 158string ( TOUPPER " ${ _ty } " _TY ) 159file ( WRITE " ${ OUT } /template-instances/mmq-instance- ${ _ty } .cu" 160"// Strata trellis overlay (cmake/strata_ggml_tq_overlay.cmake)\n\n#include \"../mmq.cuh\"\n\nDECL_MMQ_CASE(GGML_TYPE_ ${ _TY } );\n" ) 161endforeach () 162 163if ( _tq_fail) 164message ( STATUS "trellis MMQ overlay off: ${ _tq_fail } " ) 165return () 166endif () 167set ( STRATA_TQ_OVERLAY_OK ON PARENT_SCOPE ) 168endfunction ()