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
8.5 KiB168 linesraw
1// Strata addition (third_party/strata-ggml/UPSTREAM.md): the trellis expert types (TQ2_T, TQK6, TQK7, TQP5-7) for ggml-cuda's MMQ tile loader.
2// Derived from agentionai/llama.cpp ggml/src/ggml-cuda/tq.cuh (MIT, the ggml authors; Laurent Zuijdwijk): only what the
3// MMQ path needs (no mat-vec kernels), reading Strata's copies of the codebook tables (third_party/ggml/ggml-common-tq.h,
4// src/kernels/cuda/tq_lut_i8.cuh) instead of the fork's ggml-common.h.
5//
6// A 128-weight block is 32 trellis steps of 4 weights.  Step t has a 16-bit state s (the codebook index uses its low
7// TQ_STATE_BITS bits) and its weights are d * lut[x >> 21].{lo,hi}, d * lut[(x >> 10) & 2047].{lo,hi},
8// x = (s & TQ_STATE_MASK) * 0x9e3779b1.  A block is split into 8 lanes of 4 consecutive steps (16 weights).
9#pragma once
10
11#include "common.cuh"
12#include "../../../../ggml/ggml-common-tq.h"            // Strata's two header copies, by path from this folder
13#include "../../../../../src/kernels/cuda/tq_lut_i8.cuh"
14
15#define TQ_LUT_POINTS   2048
16#define TQ_LANES        8   // lanes per 128-weight block
17#define TQ_STEPS_LANE   4   // trellis steps per lane
18#define TQ_PAIRS_LANE   8   // codebook pairs per lane (the TQP layout: 2 weights per step)
19
20#define QR_TQ 4
21#define QI_TQ (QK_TQK/QR_TQ)
22static_assert(QK_TQ2_T == QK_TQK, "trellis block sizes differ");
23
24template<>
25struct ggml_cuda_type_traits<GGML_TYPE_TQ2_T> {
26    static constexpr int qk = QK_TQ2_T;
27    static constexpr int qr = QR_TQ;
28    static constexpr int qi = QI_TQ;
29    static constexpr int bs = sizeof(block_tq2_t);
30};
31
32template<>
33struct ggml_cuda_type_traits<GGML_TYPE_TQK6> {
34    static constexpr int qk = QK_TQK;
35    static constexpr int qr = QR_TQ;
36    static constexpr int qi = QI_TQ;
37    static constexpr int bs = sizeof(block_tqk6);
38};
39
40template<>
41struct ggml_cuda_type_traits<GGML_TYPE_TQK7> {
42    static constexpr int qk = QK_TQK;
43    static constexpr int qr = QR_TQ;
44    static constexpr int qi = QI_TQ;
45    static constexpr int bs = sizeof(block_tqk7);
46};
47
48// Gyro-L pair trellis (types 147-149): the same lane packing (16 values = 4 ints of 4 int8), 64 steps of
49// 2 weights per 128-value block.
50template<> struct ggml_cuda_type_traits<GGML_TYPE_TQP5> {
51    static constexpr int qk = QK_TQP; static constexpr int qr = QR_TQ; static constexpr int qi = QI_TQ;
52    static constexpr int bs = sizeof(block_tqp5); };
53template<> struct ggml_cuda_type_traits<GGML_TYPE_TQP6> {
54    static constexpr int qk = QK_TQP; static constexpr int qr = QR_TQ; static constexpr int qi = QI_TQ;
55    static constexpr int bs = sizeof(block_tqp6); };
56template<> struct ggml_cuda_type_traits<GGML_TYPE_TQP7> {
57    static constexpr int qk = QK_TQP; static constexpr int qr = QR_TQ; static constexpr int qi = QI_TQ;
58    static constexpr int bs = sizeof(block_tqp7); };
59
60static constexpr __host__ __device__ bool ggml_cuda_type_is_tqp(ggml_type type) {
61    return type == GGML_TYPE_TQP5 || type == GGML_TYPE_TQP6 || type == GGML_TYPE_TQP7;
62}
63
64static constexpr __host__ __device__ bool ggml_cuda_type_is_tq(ggml_type type) {
65    return type == GGML_TYPE_TQ2_T || type == GGML_TYPE_TQK6 || type == GGML_TYPE_TQK7 || ggml_cuda_type_is_tqp(type);
66}
67
68template <ggml_type type> struct tq_block_info;
69template <> struct tq_block_info<GGML_TYPE_TQ2_T> { typedef block_tq2_t block_t; static constexpr int K = 8; };
70template <> struct tq_block_info<GGML_TYPE_TQK6>  { typedef block_tqk6  block_t; static constexpr int K = 6; };
71template <> struct tq_block_info<GGML_TYPE_TQK7>  { typedef block_tqk7  block_t; static constexpr int K = 7; };
72template <> struct tq_block_info<GGML_TYPE_TQP5>  { typedef block_tqp5  block_t; static constexpr int K = 5; };
73template <> struct tq_block_info<GGML_TYPE_TQP6>  { typedef block_tqp6  block_t; static constexpr int K = 6; };
74template <> struct tq_block_info<GGML_TYPE_TQP7>  { typedef block_tqp7  block_t; static constexpr int K = 7; };
75static_assert(QK_TQP == QK_TQK, "trellis block sizes differ");
76
77// States of steps 4l..4l+3 of one block. qs is the block's qs array, 2-byte aligned
78// (every trellis block has an even size and qs sits right after the fp16 scale).
79template <ggml_type type>
80static __device__ __forceinline__ void tq_states4(const uint8_t * qs, const int l, uint32_t s[TQ_STEPS_LANE]) {
81    const uint16_t * qs16 = (const uint16_t *) qs;
82    if constexpr (type == GGML_TYPE_TQ2_T) {
83        // s_t = qs[(t + 31) % 32] << 8 | qs[t]
84        const uint32_t w01  = qs16[2*l + 0];
85        const uint32_t w23  = qs16[2*l + 1];
86        const uint32_t prev = qs[(4*l + 31) & 31];
87        const uint32_t b0 = w01 & 0xFF, b1 = w01 >> 8, b2 = w23 & 0xFF, b3 = w23 >> 8;
88        s[0] = (prev << 8) | b0;
89        s[1] = (b0   << 8) | b1;
90        s[2] = (b1   << 8) | b2;
91        s[3] = (b2   << 8) | b3;
92    } else {
93        // Step t's state is the 16 stream bits from bit (31 - t)*K (circular over 32*K bits, i.e. 2*K u16 words).
94        // Steps 4l..4l+3 all lie in the four words from wb = off3 >> 4, off3 = (28 - 4l)*K.
95        constexpr int K      = tq_block_info<type>::K;
96        constexpr int nwords = 2*K;
97        const int off3 = (28 - 4*l)*K;
98        const int wb   = off3 >> 4;
99        const int r    = off3 & 15;
100        const int wi1  = wb + 1 >= nwords ? wb + 1 - nwords : wb + 1;
101        const int wi2  = wb + 2 >= nwords ? wb + 2 - nwords : wb + 2;
102        const int wi3  = wb + 3 >= nwords ? wb + 3 - nwords : wb + 3;
103        const uint64_t v = (uint64_t) qs16[wb] | ((uint64_t) qs16[wi1] << 16) |
104                           ((uint64_t) qs16[wi2] << 32) | ((uint64_t) qs16[wi3] << 48);
105#pragma unroll
106        for (int i = 0; i < TQ_STEPS_LANE; ++i) {
107            s[i] = (uint32_t) (v >> (r + (3 - i)*K)) & 0xFFFFu;
108        }
109    }
110}
111
112// TQP: the 15-bit states of steps 8l..8l+7 (s[j] = step 8l + j).  Step t's window starts at stream bit
113// (63 - t)*K, so the lane's steps lie in the bits from S = (56 - 8l)*K: step 8l + j at S + (7 - j)*K.
114// S = 8*(7 - l)*K, so r = S & 15 is 0 or 8 and r + 7*K + 15 <= 72: five u16 words (wrapped modulo the
115// 4*K words of the stream) cover it, the fifth only when r + 7*K + 15 > 64 (never for K = 5).
116template <ggml_type type>
117static __device__ __forceinline__ void tqp_states8(const uint8_t * qs, const int l, uint32_t s[TQ_PAIRS_LANE]) {
118    constexpr int K      = tq_block_info<type>::K;
119    constexpr int nwords = 4*K;
120    const uint16_t * qs16 = (const uint16_t *) qs;
121    const int S   = (56 - 8*l)*K;
122    const int wb  = S >> 4;
123    const int r   = S & 15;
124    const int wi1 = wb + 1 >= nwords ? wb + 1 - nwords : wb + 1;
125    const int wi2 = wb + 2 >= nwords ? wb + 2 - nwords : wb + 2;
126    const int wi3 = wb + 3 >= nwords ? wb + 3 - nwords : wb + 3;
127    uint64_t v = (uint64_t) qs16[wb] | ((uint64_t) qs16[wi1] << 16) |
128                 ((uint64_t) qs16[wi2] << 32) | ((uint64_t) qs16[wi3] << 48);
129    v >>= r;
130    if constexpr (8 + 7*K + TQ_STATE_BITS > 64) {
131        const int wi4 = wb + 4 >= nwords ? wb + 4 - nwords : wb + 4;
132        v |= r ? (uint64_t) qs16[wi4] << (64 - r) : 0;
133    }
134#pragma unroll
135    for (int j = 0; j < TQ_PAIRS_LANE; ++j) {
136        s[j] = (uint32_t) (v >> ((7 - j)*K)) & TQ_STATE_MASK;
137    }
138}
139
140// MMQ (batched) path: lane l of block x as 16 int8 values (4 packed ints, weight order) with one float scale per
141// lane, i.e. weights ~= scale * q.  The int8 codebook (one global scale) is already int8: no decode to float.
142template <ggml_type type>
143static __device__ __forceinline__ float tq_lane_to_q8(const void * x, const int l, int q[TQ_STEPS_LANE]) {
144    typedef typename tq_block_info<type>::block_t block_t;
145    const block_t * b = (const block_t *) x;
146
147    if constexpr (ggml_cuda_type_is_tqp(type)) {
148        // 8 pairs of 2 weights, in weight order: two pairs per int, as the TQ path packs two steps
149        uint32_t s[TQ_PAIRS_LANE];
150        tqp_states8<type>(b->qs, l, s);
151#pragma unroll
152        for (int i = 0; i < TQ_STEPS_LANE; ++i) {
153            const uint32_t p0 = tq_lut_i8[((s[2 * i] & TQ_STATE_MASK) * 0x9e3779b1u) >> 21];
154            const uint32_t p1 = tq_lut_i8[((s[2 * i + 1] & TQ_STATE_MASK) * 0x9e3779b1u) >> 21];
155            q[i] = (int) (p0 | (p1 << 16));
156        }
157    } else {
158        uint32_t s[TQ_STEPS_LANE];
159        tq_states4<type>(b->qs, l, s);
160#pragma unroll
161        for (int i = 0; i < TQ_STEPS_LANE; ++i) {
162            const uint32_t xh = (s[i] & TQ_STATE_MASK) * 0x9e3779b1u;
163            q[i] = (int) ((uint32_t) tq_lut_i8[xh >> 21] | ((uint32_t) tq_lut_i8[(xh >> 10) & 2047u] << 16));
164        }
165    }
166    // Strata's block structs hold the fp16 scale as uint16_t (the fork's as ggml_half): reinterpret, do not convert
167    return __half2float(__ushort_as_half(b->d)) * TQ_LUT_I8_SCALE;
168}