okami.mom/strata

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

Niko1221Merge f-own-1a (451f3e8a6): own-stack phase 1A and cleanup 5.4; the ggml/MMQ sources the engines build are vendored in third_party/strata-ggml (frozen at the pin, manifest and check), default engine builds need no llama.cpp download, the TQ overlay is plain files, CMakeLists.txt is split into cmake/backends/*; every engine unchanged (CUDA/HIP gfx1201/gfx1151/SYCL device code identical, SYCL 22/22 checksums, Metal 104/105 objects with only __TIME__ differing, CUDA tokens 26/26); release scripts still pass -DSTRATA_GGML_DIR (owner go 11-08)508e6999a

main
10.9 KiB168 linesraw
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)
21  set(STRATA_TQ_OVERLAY_OK OFF PARENT_SCOPE)
22  set(_cu "${LLAMA}/ggml/src/ggml-cuda")
23  set(_inc "${LLAMA}/ggml/include")
24  set(_tq "${CMAKE_SOURCE_DIR}/third_party/strata-ggml/ggml/src/ggml-cuda-tq")
25  foreach(_f "${_inc}/ggml.h" "${_cu}/mmq.cuh" "${_cu}/mmq-load-tiles.cuh" "${_cu}/common.cuh")
26    if(NOT EXISTS "${_f}")
27      message(STATUS "trellis MMQ overlay: ${_f} is missing: off")
28      return()
29    endif()
30  endforeach()
31
32  # ---- helpers: read a file with LF line ends; insert text before / after the single occurrence of an anchor ----------
33  macro(_tq_read VAR PATH)
34    file(READ "${PATH}" ${VAR})
35    string(REPLACE "\r\n" "\n" ${VAR} "${${VAR}}")
36    set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${PATH}")
37  endmacro()
38  macro(_tq_edit VAR WHERE ANCHOR TEXT)   # WHERE = BEFORE | AFTER; the anchor must occur exactly once
39    string(FIND "${${VAR}}" "${ANCHOR}" _p)
40    if(_p LESS 0)
41      set(_tq_fail "anchor not found: ${ANCHOR}")
42    else()
43      string(LENGTH "${ANCHOR}" _alen)
44      math(EXPR _after "${_p} + ${_alen}")
45      string(SUBSTRING "${${VAR}}" ${_after} -1 _tail)
46      string(FIND "${_tail}" "${ANCHOR}" _p2)
47      if(NOT _p2 LESS 0)
48        set(_tq_fail "anchor is not unique: ${ANCHOR}")
49      else()
50        string(SUBSTRING "${${VAR}}" 0 ${_p} _head)
51        if("${WHERE}" STREQUAL "AFTER")
52          set(${VAR} "${_head}${ANCHOR}${TEXT}${_tail}")
53        else()
54          set(${VAR} "${_head}${TEXT}${ANCHOR}${_tail}")
55        endif()
56      endif()
57    endif()
58  endmacro()
59  set(_tq_fail "")
60
61  file(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 -------
65  file(GLOB _all_cu "${_cu}/*.cuh" "${_cu}/*.h")
66  foreach(_f IN LISTS _all_cu)
67    get_filename_component(_n "${_f}" NAME)
68    configure_file("${_f}" "${OUT}/${_n}" COPYONLY)
69  endforeach()
70  configure_file("${_cu}/quantize.cu" "${OUT}/quantize.cu" COPYONLY)   # compiled from here: it includes mmq.cuh
71  if(IS_DIRECTORY "${_cu}/vendors")
72    file(COPY "${_cu}/vendors" DESTINATION "${OUT}")
73  endif()
74  file(GLOB _all_inc "${_inc}/*.h")
75  foreach(_f IN LISTS _all_inc)
76    get_filename_component(_n "${_f}" NAME)
77    configure_file("${_f}" "${OUT}/include/${_n}" COPYONLY)
78  endforeach()
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)
84        GGML_TYPE_TQK6    = 145,
85        GGML_TYPE_TQK7    = 146,
86        GGML_TYPE_TQP5    = 147, // Gyro-L pair trellis (the same fork's 147-149)
87        GGML_TYPE_TQP6    = 148,
88        GGML_TYPE_TQP7    = 149,
89")
90  file(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.
98  set(_ps "")        # ", prec_src1" in the generated callbacks
99  set(_pa "")        # ", GGML_PREC_Q8" in the loader's calls (the value upstream's own IQ2_XS loader passes)
100  string(FIND "${_m}" "ggml_cuda_mmq_get_nthreads(ggml_type type, int J, bool fallback, ggml_prec prec_src1)" _sig_new)
101  string(FIND "${_m}" "ggml_cuda_mmq_get_nthreads(ggml_type type, int J, bool fallback)" _sig_old)
102  if(NOT _sig_new LESS 0)
103    set(_ps ", prec_src1")
104    set(_pa ", GGML_PREC_Q8")
105  elseif(_sig_old LESS 0)
106    set(_tq_fail "the MMQ helper signatures of this ggml-cuda are not the ones the overlay knows (ggml_cuda_mmq_get_nthreads)")
107  endif()
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")
118  file(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")
122  file(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
124  file(READ "${_tq}/tq.cuh" _tqh)
125  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_tq}/tq.cuh")
126  string(REPLACE "#include \"../../../../ggml/ggml-common-tq.h\"" "#include \"ggml-common-tq.h\"" _tqh "${_tqh}")
127  string(REPLACE "#include \"../../../../../src/kernels/cuda/tq_lut_i8.cuh\"" "#include \"tq_lut_i8.cuh\"" _tqh "${_tqh}")
128  file(WRITE "${OUT}/tq.cuh" "${_tqh}")
129  file(READ "${_tq}/tq-mmq-tiles.cuh" _tiles)
130  set_property(DIRECTORY APPEND PROPERTY CMAKE_CONFIGURE_DEPENDS "${_tq}/tq-mmq-tiles.cuh")
131  foreach(_fn get_nthreads get_I get_sram_stride)
132    string(REPLACE "ggml_cuda_mmq_${_fn}(type, J, fallback)" "ggml_cuda_mmq_${_fn}(type, J, fallback${_pa})" _tiles "${_tiles}")
133  endforeach()
134  file(WRITE "${OUT}/tq-mmq-tiles.cuh" "${_tiles}")
135
136  # ---- mmq-config-*.cuh: every IQ2_XS CASE line gets one copy per trellis type ---------------------------------------
137  file(GLOB _cfgs "${_cu}/mmq-config-*.cuh")
138  foreach(_c ${_cfgs})
139    get_filename_component(_cn "${_c}" NAME)
140    _tq_read(_t "${_c}")
141    string(REPLACE ";" "<SEMI>" _t "${_t}")   # the CASE lines hold semicolons; a CMake list would split them
142    string(REGEX MATCHALL "[^\n]*CASE\\(GGML_TYPE_IQ2_XS,[^\n]*\n" _lines "${_t}")
143    list(REMOVE_DUPLICATES _lines)
144    foreach(_ln IN LISTS _lines)
145      set(_add "")
146      foreach(_ty TQ2_T TQK6 TQK7 TQP5 TQP6 TQP7)
147        string(REPLACE "GGML_TYPE_IQ2_XS," "GGML_TYPE_${_ty}," _x "${_ln}")
148        string(APPEND _add "${_x}")
149      endforeach()
150      _tq_edit(_t AFTER "${_ln}" "${_add}")
151    endforeach()
152    string(REPLACE "<SEMI>" ";" _t "${_t}")
153    file(WRITE "${OUT}/${_cn}" "${_t}")
154  endforeach()
155
156  # ---- template instances -------------------------------------------------------------------------------------------
157  foreach(_ty tq2_t tqk6 tqk7 tqp5 tqp6 tqp7)
158    string(TOUPPER "${_ty}" _TY)
159    file(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")
161  endforeach()
162
163  if(_tq_fail)
164    message(STATUS "trellis MMQ overlay off: ${_tq_fail}")
165    return()
166  endif()
167  set(STRATA_TQ_OVERLAY_OK ON PARENT_SCOPE)
168endfunction()