okami.mom/strata

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

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

main
7.0 KiB138 linesraw
1// Strata. gfx12 (RDNA4, R9700 = gfx1201) WMMA form of the trellis expert GEMV of the dsv41 CUDA lab (tools/dsv41_lab:
2// tqk_gemv3.cuh, wmma_emu.hpp = the specification, hip/tqk_gemv_wmma.hip.cpp = its first HIP form).  Layout "TQW" (see
3// wmma_emu.hpp::repack_tqw): a wave = T tiles of 16 rows, lane l = r + 16 kg owns row r and 16 consecutive steps of every
4// 128-weight block; activations are the B operand (NB <= 16 columns), split x = hi + lo in two fp16 for ~2^-22 accuracy.
5// CODE 0: the table codebook (K5/K6/K7 of the shipped files: two lookups into an 8 KB LDS table of half2 per step);
6// CODE 1: the computed "3single" code (no table; the new K4/K5 types if they take it).
7#pragma once
8#include <hip/hip_runtime.h>
9#include <cstdint>
10
11namespace strata::ds4::tqw {
12
13typedef _Float16 half8_t __attribute__((ext_vector_type(8)));
14typedef float float8_t __attribute__((ext_vector_type(8)));
15
16constexpr uint32_t kMask = 0x7fffu;
17
18struct Job {
19    const uint32_t* words;     // TQW words  [tile][P][K][32 lanes]
20    const uint16_t* scales;    // [tile][P][16 rows][2 blocks] fp16
21    const half8_t* xb;         // prepared B fragments (prep_b)
22    float* y;                  // [NB][ldy]
23    int rows, in, ldy;
24};
25
26__device__ __forceinline__ void step_words(const uint32_t* lut, uint32_t s, uint32_t& w0, uint32_t& w1, int code) {
27    if (code == 1) {
28        w0 = ((s * 89226354u + 64248484u) & 0x8FFF8FFFu) ^ 0x3B603B60u;
29        w1 = ((s * 0x9E3779B1u + 0x7F4A7C15u) & 0x8FFF8FFFu) ^ 0x3B603B60u;
30    } else {
31        const uint32_t x = s * 0x9e3779b1u;
32        w0 = lut[x >> 21];
33        w1 = lut[(x >> 10) & 2047u];
34    }
35}
36
37// activations -> B fragments.  out[(((blk * 8 + m) * 2 + kg) * nb + n) * 2 + {0 = hi, 1 = lo}] = 8 halves of x[n][blk * 128 + 4 * (16 kg + 2 m) + j]
38__global__ void prep_b(const float* x, int ldx, int nb, int in, half8_t* out) {
39    const int nblk = in / 128;
40    const size_t gid = (size_t) blockIdx.x * blockDim.x + threadIdx.x, n_all = (size_t) nblk * 8 * 2 * nb;
41    if (gid >= n_all) return;
42    const int n = (int) (gid % nb), kg = (int) ((gid / nb) % 2), m = (int) ((gid / (2 * nb)) % 8), blk = (int) (gid / (16 * nb));
43    const float* p = x + (size_t) n * ldx + blk * 128 + 4 * (16 * kg + 2 * m);
44    half8_t hi, lo;
45#pragma unroll
46    for (int j = 0; j < 8; ++j) { hi[j] = (_Float16) p[j]; lo[j] = (_Float16) (p[j] - (float) hi[j]); }
47    out[gid * 2] = hi;
48    out[gid * 2 + 1] = lo;
49}
50
51template <int K, int NB, int T, int EXACT, int CODE>
52__global__ void __launch_bounds__(128) gemv(const Job* jobs, const uint32_t* __restrict__ lut_g) {
53    __shared__ uint32_t lut[CODE == 0 ? 2048 : 1];
54    if (CODE == 0) {
55        for (int i = threadIdx.x; i < 2048; i += blockDim.x) lut[i] = lut_g[i];
56        __syncthreads();
57    }
58    const Job job = jobs[blockIdx.y];
59    const int wave = threadIdx.x >> 5, lane = threadIdx.x & 31, r = lane & 15, kg = lane >> 4;
60    const int tile0 = (blockIdx.x * (blockDim.x >> 5) + wave) * T;
61    if (tile0 * 16 >= job.rows) return;
62    const int nblk = job.in / 128, NP = nblk / 2;
63    float tot[T][8];
64#pragma unroll
65    for (int t = 0; t < T; ++t)
66#pragma unroll
67        for (int i = 0; i < 8; ++i) tot[t][i] = 0.f;
68    for (int P = 0; P < NP; ++P) {
69        uint32_t rec[T][K];
70#pragma unroll
71        for (int t = 0; t < T; ++t)
72#pragma unroll
73            for (int j = 0; j < K; ++j) rec[t][j] = __builtin_nontemporal_load(job.words + (((size_t) (tile0 + t) * NP + P) * K + j) * 32 + lane);
74#pragma unroll
75        for (int bk = 0; bk < 2; ++bk) {
76            half8_t bh[8], bl[8];
77            if (r < NB) {
78#pragma unroll
79                for (int m = 0; m < 8; ++m) {
80                    const half8_t* p = job.xb + (((((size_t) (2 * P + bk) * 8 + m) * 2 + kg) * NB + r) * 2);
81                    bh[m] = p[0];
82                    if (EXACT) bl[m] = p[1];
83                }
84            } else {
85#pragma unroll
86                for (int m = 0; m < 8; ++m) { bh[m] = half8_t{0, 0, 0, 0, 0, 0, 0, 0}; bl[m] = bh[m]; }
87            }
88#pragma unroll
89            for (int t = 0; t < T; ++t) {
90                // chunk bk of the record: bits [16 K bk, 16 K bk + 16 K); window 0 = steps u 0..7 (+ 15-K bits of the partner chunk),
91                // window 1 = steps u 8..15
92                constexpr int NW = K;   // (words of a record)
93                auto bits = [&](int pos, int n) -> uint64_t {   // n <= 56 bits at record bit pos (pos is a compile-time constant here)
94                    const int a = pos >> 5, sh = pos & 31;
95                    const uint32_t w0 = rec[t][a], w1 = a + 1 < NW ? rec[t][a + 1 < NW ? a + 1 : 0] : 0u, w2 = a + 2 < NW ? rec[t][a + 2 < NW ? a + 2 : 0] : 0u;
96                    // (w2:w1:w0) >> sh, low 64 bits
97                    const uint64_t out = (sh == 0) ? (((uint64_t) w1 << 32) | w0) : ((((uint64_t) w1 << 32) | w0) >> sh) | ((uint64_t) w2 << (64 - sh));
98                    return out & ((1ull << n) - 1ull);
99                };
100                const int b0 = 16 * K * bk;
101                const uint64_t lo8 = bits(b0, 8 * K), hi8 = bits(b0 + 8 * K, 8 * K);
102                const uint32_t mine = (uint32_t) (lo8 & 0xffffffffu);   // the bits the PARTNER needs: this lane's chunk bits [0, 15 - K)
103                const uint32_t pc = __shfl_xor((int) mine, 16);
104                const uint64_t win0 = hi8 | ((uint64_t) (pc & ((1u << (15 - K)) - 1u)) << (8 * K));
105                const uint64_t win1 = lo8 | ((hi8 & ((1ull << (15 - K)) - 1ull)) << (8 * K));
106                float8_t acc = {0, 0, 0, 0, 0, 0, 0, 0};
107#pragma unroll
108                for (int m = 0; m < 8; ++m) {
109                    half8_t a;
110#pragma unroll
111                    for (int s2 = 0; s2 < 2; ++s2) {
112                        const int u = 2 * m + s2;
113                        const uint32_t st = (uint32_t) (u < 8 ? win0 >> ((7 - u) * K) : win1 >> ((15 - u) * K)) & kMask;
114                        uint32_t w0, w1;
115                        step_words(lut, st, w0, w1, CODE);
116                        a[4 * s2 + 0] = __builtin_bit_cast(_Float16, (uint16_t) (w0 & 0xffffu));
117                        a[4 * s2 + 1] = __builtin_bit_cast(_Float16, (uint16_t) (w0 >> 16));
118                        a[4 * s2 + 2] = __builtin_bit_cast(_Float16, (uint16_t) (w1 & 0xffffu));
119                        a[4 * s2 + 3] = __builtin_bit_cast(_Float16, (uint16_t) (w1 >> 16));
120                    }
121                    acc = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, bh[m], acc);
122                    if (EXACT) acc = __builtin_amdgcn_wmma_f32_16x16x16_f16_w32_gfx12(a, bl[m], acc);
123                }
124                // scales of rows 8 kg .. 8 kg + 7 of this block: 8 consecutive (row, bk) pairs of 2 -> stride 2
125                const uint16_t* sp = job.scales + (((size_t) (tile0 + t) * NP + P) * 16 + 8 * kg) * 2 + bk;
126#pragma unroll
127                for (int i = 0; i < 8; ++i) tot[t][i] += (float) __builtin_bit_cast(_Float16, sp[2 * i]) * acc[i];
128            }
129        }
130    }
131#pragma unroll
132    for (int t = 0; t < T; ++t)
133        if (r < NB)
134#pragma unroll
135            for (int i = 0; i < 8; ++i) job.y[(size_t) r * job.ldy + (tile0 + t) * 16 + 8 * kg + i] = tot[t][i];
136}
137
138}  // namespace strata::ds4::tqw