okami.mom/strata

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

waveringanaRestore the DeepSeek V4.1 engine and CPU tests removed upstream8ac8ce32e

main
8.8 KiB210 linesraw
1// Strata. Part of ds4_iq_hip.inc (included from it): the weights straight into the matrix cores' registers.
2// One lane of the B operand of a 16x16x16 step holds 8 consecutive k of one weight row: exactly one 8-value group of a 32-value
3// sub-block of an i-quant block (the grid entry, its signs).  grp_dec decodes that group alone: 8 int8 codes (two words) and the
4// scale (weight = scale * code), so a wave needs no shared memory for the weights and no barrier: every wave decodes its own
5// columns once and uses them for every row of the tile.  kb: the 32-value sub-block of the row, p: its group (0..3).
6// The decode is in two halves so that the loads of a k-step can be issued one k-step ahead of its use: grp_load fetches the
7// group raw bytes into registers (Raw), grp_dec turns them into codes and scale (the codebook lookups, the signs).
8__device__ __forceinline__ float hf(uint32_t h) { return __half2float(__ushort_as_half((uint16_t) h)); }
9#include "ds4_iq_hip_s.inc"
10
11constexpr int kKC = 64;   // K a step: two sub-blocks
12// BM rows (MT tiles of 16) x BN = NW 16 NTW weight rows per block; NW waves, each NTW column tiles of 16 and every row tile
13template <int T, bool GU, int BM, int NW, int NTW>
14__global__ void __launch_bounds__(NW * 32) set_kernel_r(const SetArgs a, int dbg) {
15    constexpr int NM = GU ? 2 : 1, MT = BM / 16, BN = NW * 16 * NTW, NTH = NW * 32;
16    static_assert(BM % 16 == 0, "row tiles");
17    __shared__ uint32_t tab[Ty<T>::TAB > 0 ? Ty<T>::TAB : 1];
18    __shared__ int srow[BM];
19    __shared__ __align__(16) _Float16 xs[2][BM][kStH];
20    const int e = blockIdx.z;
21    const int lo = a.bounds[e] - a.row0, hi_r = a.bounds[e + 1] - a.row0;
22    const int r0 = lo + blockIdx.x * BM;
23    if (r0 >= hi_r || hi_r - lo <= a.skip_le) return;   // (uniform)
24    load_tab<T>(tab);
25    const int nr = min(BM, hi_r - r0);
26    const int c0 = blockIdx.y * BN;
27    const uint8_t* blob = a.wbase + (size_t) e * a.stride;
28    const int K = GU ? a.n_embd : a.n_ff;
29    const size_t rb = row_bytes_t<T>(K);
30    const int tid = (int) threadIdx.x, warp = tid >> 5, lane = tid & 31, l16 = lane & 15, hi = lane >> 4;
31    for (int i = tid; i < BM; i += NTH) srow[i] = GU ? a.idx[r0 + min(i, nr - 1)] : r0 + min(i, nr - 1);
32    __syncthreads();
33    const int nst = K / kKC;
34    // the weight rows of this lane column tiles
35    const uint8_t* wrow[NM][NTW];
36#pragma unroll
37    for (int m = 0; m < NM; ++m)
38#pragma unroll
39        for (int j = 0; j < NTW; ++j)
40            wrow[m][j] = blob + a.tr[GU ? m : 2] + (size_t) ((c0 + warp * (16 * NTW) + j * 16 + l16)) * rb;
41
42    constexpr int NX = GU ? (BM * 16 + NTH - 1) / NTH : (BM * 8 + NTH - 1) / NTH;
43    struct XR {
44        float4 f[GU ? NX : 1];
45        uint4 u[GU ? 1 : NX];
46    };
47    auto fetchx = [&](int s, XR& R) {
48#pragma unroll
49        for (int xi = 0; xi < NX; ++xi) {
50            const int c = tid + xi * NTH;
51            if constexpr (GU) {
52                if (c < BM * 16) {
53                    const int row = c >> 4, ch = c & 15;
54                    R.f[xi] = *(const float4*) (a.xf + (size_t) srow[row] * a.ldx + (size_t) s * kKC + ch * 4);
55                }
56            } else {
57                if (c < BM * 8) {
58                    const int row = c >> 3, ch = c & 7;
59                    R.u[xi] = *(const uint4*) (a.xh + (size_t) srow[row] * a.ldx + (size_t) s * kKC + ch * 8);
60                }
61            }
62        }
63    };
64    auto storex = [&](const XR& R, int b) {
65#pragma unroll
66        for (int xi = 0; xi < NX; ++xi) {
67            const int c = tid + xi * NTH;
68            if constexpr (GU) {
69                if (c < BM * 16) {
70                    const int row = c >> 4, ch = c & 15;
71                    const float4 v = R.f[xi];
72                    const hv2 p = {(_Float16) v.x, (_Float16) v.y}, q = {(_Float16) v.z, (_Float16) v.w};
73                    *(uint2*) &xs[b][row][ch * 4] = make_uint2(as_u32(p), as_u32(q));
74                }
75            } else {
76                if (c < BM * 8) {
77                    const int row = c >> 3, ch = c & 7;
78                    *(uint4*) &xs[b][row][ch * 8] = R.u[xi];
79                }
80            }
81        }
82    };
83
84    fv8 acc[NM][MT][NTW];
85#pragma unroll
86    for (int m = 0; m < NM; ++m)
87#pragma unroll
88        for (int i = 0; i < MT; ++i)
89#pragma unroll
90            for (int j = 0; j < NTW; ++j) acc[m][i][j] = fv8{0, 0, 0, 0, 0, 0, 0, 0};
91
92    {
93        XR R0;
94        fetchx(0, R0);
95        storex(R0, 0);
96    }
97    // the weights of a step (64 values) are loaded one step ahead: two sets of registers, the step loop unrolled by two
98    SRaw sr0[NM][NTW], sr1[NM][NTW];
99    auto load_stage = [&](SRaw (&dst)[NM][NTW], int s) {
100#pragma unroll
101        for (int m = 0; m < NM; ++m)
102#pragma unroll
103            for (int j = 0; j < NTW; ++j) stage_load<T>(wrow[m][j], s, hi, dst[m][j]);
104    };
105    auto do_stage = [&](int s, SRaw (&cur)[NM][NTW], SRaw (&nxt)[NM][NTW]) {
106        const int b = s & 1;
107        XR R;
108        if (s + 1 < nst) {
109            fetchx(s + 1, R);
110            load_stage(nxt, s + 1);
111        }
112#if defined(DS4_HIPK_G11)
113        hv16 bf[NM][NTW];   // (gfx11: all 16 k of the step in a lane: its own 8 and the other half-wave's)
114#else
115        hv8 bf[NM][NTW];
116#endif
117        auto step = [&](auto tt) {
118            constexpr int t = decltype(tt)::value;
119            constexpr int sub = t >> 1, ks = t & 1;
120#pragma unroll
121            for (int m = 0; m < NM; ++m)
122#pragma unroll
123                for (int j = 0; j < NTW; ++j) {
124                    int c0_, c1_;
125                    float sc;
126                    step_dec<T, t>(cur[m][j], s, hi, tab, c0_, c1_, sc);
127                    const hv2 h = {(_Float16) sc, (_Float16) sc};
128                    const uint2 u0 = codes4(c0_, h), u1 = codes4(c1_, h);
129#if defined(DS4_HIPK_G11)
130                    const uint32_t own[4] = {u0.x, u0.y, u1.x, u1.y};
131                    uint32_t both[8];
132#pragma unroll
133                    for (int w = 0; w < 4; ++w) {
134                        const uint32_t other = __shfl_xor_sync(0xffffffffu, own[w], 16);
135                        both[w] = hi ? other : own[w];
136                        both[4 + w] = hi ? own[w] : other;
137                    }
138                    bf[m][j] = __builtin_bit_cast(hv16, both);
139#else
140                    bf[m][j] = __builtin_bit_cast(hv8, make_uint4(u0.x, u0.y, u1.x, u1.y));
141#endif
142                }
143#if defined(DS4_HIPK_G11)
144            const int kcol = sub * 32 + ks * 16;
145#else
146            const int kcol = sub * 32 + ks * 16 + hi * 8;
147#endif
148#pragma unroll
149            for (int i = 0; i < MT; ++i) {
150                if (i * 16 < nr) {   // (a tile past the rows: skipped, uniform)
151#if defined(DS4_HIPK_G11)
152                    const hv16 af = *(const hv16*) &xs[b][i * 16 + l16][kcol];
153#pragma unroll
154                    for (int m = 0; m < NM; ++m)
155#pragma unroll
156                        for (int j = 0; j < NTW; ++j) acc[m][i][j] = wmma16g11(af, bf[m][j], acc[m][i][j]);
157#else
158                    const hv8 af = *(const hv8*) &xs[b][i * 16 + l16][kcol];
159#pragma unroll
160                    for (int m = 0; m < NM; ++m)
161#pragma unroll
162                        for (int j = 0; j < NTW; ++j) acc[m][i][j] = wmma16(af, bf[m][j], acc[m][i][j]);
163#endif
164                }
165            }
166        };
167        step(std::integral_constant<int, 0>{});
168        step(std::integral_constant<int, 1>{});
169        step(std::integral_constant<int, 2>{});
170        step(std::integral_constant<int, 3>{});
171        if (s + 1 < nst) storex(R, b ^ 1);
172        __syncthreads();
173    };
174    load_stage(sr0, 0);
175    __syncthreads();
176    for (int s = 0; s < nst; s += 2) {   // (nst is a multiple of 4)
177        do_stage(s, sr0, sr1);
178        do_stage(s + 1, sr1, sr0);
179    }
180#pragma unroll
181    for (int i = 0; i < MT; ++i)
182#pragma unroll
183        for (int q = 0; q < 8; ++q) {
184#if defined(DS4_HIPK_G11)
185            const int row = i * 16 + 2 * q + hi;
186#else
187            const int row = i * 16 + 8 * hi + q;
188#endif
189            if (row >= nr) continue;
190            const size_t lr = (size_t) (r0 + row);
191#pragma unroll
192            for (int j = 0; j < NTW; ++j) {
193                const int col = c0 + warp * (16 * NTW) + j * 16 + l16;
194                if (GU) a.h[lr * a.ldo + col] = __half_as_ushort(__float2half_rn(swiglu(acc[0][i][j][q], acc[1][i][j][q], a.limit)));
195                else a.y[lr * a.ldo + col] = acc[0][i][j][q];
196            }
197        }
198}
199
200template <int T, bool GU, int BM, int NW, int NTW>
201inline bool runr(const SetArgs& a, cudaStream_t s) {
202    const int N = GU ? a.n_ff : a.n_embd;
203    constexpr int BN = NW * 16 * NTW;
204    if (N % BN != 0) return false;
205    const dim3 grid((unsigned) ((a.max_rows + BM - 1) / BM), (unsigned) (N / BN), (unsigned) a.n_exp);
206    static const int dbg = std::getenv("HIPK_DBG") ? std::atoi(std::getenv("HIPK_DBG")) : 0;
207    set_kernel_r<T, GU, BM, NW, NTW><<<grid, NW * 32, 0, s>>>(a, dbg);
208    launches().fetch_add(1, std::memory_order_relaxed);
209    return cudaPeekAtLastError() == cudaSuccess;
210}