okami.mom/strata

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

Niko1221Merge f-037-arc (5eaee81a4)86bed1c92

main
15.9 KiB331 linesraw
1// src/kernels/cuda/dequant_bf16.cu - see include/strata/kernels/dequant_bf16.hpp.
2//
3// Arithmetic transcribed from ggml/src/ggml-quants.c at the pinned llama.cpp (MIT License, Copyright (c) 2023-2026
4// The ggml authors): dequantize_row_q2_0/q4_0/q5_0/q8_0/q3_K/q4_K/q5_K/q6_K/iq4_nl/iq4_xs.
5#define DPCT_PROFILING_ENABLED
6#include <sycl/sycl.hpp>
7#include "strata/kprof.hpp"
8#include <dpct/dpct.hpp>
9#include "strata/sycl_queue.hpp"
10#include "strata/kernels/dequant_bf16.hpp"
11#include "strata/kernels/iq_kernels.hpp"
12
13#include <cstdio>
14#include <cstdlib>
15
16namespace strata::kernels {
17namespace {
18
19__dpct_inline__ float h2f(const uint8_t *p) {
20    return sycl::vec<sycl::half, 1>(sycl::bit_cast<sycl::half, unsigned short>(
21                                        (uint16_t)(p[0] | (p[1] << 8))))
22        .convert<float, sycl::rounding_mode::automatic>()[0];
23}
24__dpct_inline__ uint16_t f2bf(float f) {
25    uint32_t u = sycl::bit_cast<unsigned int>(f);
26    // a NaN (a NaN scale in the block) stays a quiet NaN, as in ggml_compute_fp32_to_bf16 and `bf16_from_f32`:
27    // the rounding add below would carry it into -0 or inf
28    if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t) ((u >> 16) | 64u);
29    u += 0x7fffu + ((u >> 16) & 1u);          // round to nearest even
30    return (uint16_t) (u >> 16);
31}
32__dpct_inline__ void put(uint16_t *o, int i, float v) { o[i] = f2bf(v); }
33struct H16 { uint16_t v; };
34__dpct_inline__ void put(H16 *o, int i, float v) {
35    o[i].v = sycl::bit_cast<unsigned short, sycl::half>(
36        sycl::vec<float, 1>(v)
37            .convert<sycl::half, sycl::rounding_mode::rte>()[0]);
38}
39__dpct_inline__ void put(float *o, int i, float v) { o[i] = v; }
40
41const int8_t kv_iq4nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10,
42                             1,    13,   25,  38,  53,  69,  89,  113};
43
44__dpct_inline__ void scale_min_k4(int j, const uint8_t *q, int &d, int &m) {
45    if (j < 4) { d = q[j] & 63; m = q[j + 4] & 63; }
46    else { d = (q[j + 4] & 0xF) | ((q[j - 4] >> 6) << 4); m = (q[j + 4] >> 4) | ((q[j] >> 6) << 4); }
47}
48
49// One 32-element group `g` (row-major over the whole slice); `out` points at that group's 32 outputs.
50template <int TYPE, typename T>
51/*
52DPCT1110: The total declared local variable size in device function group32
53exceeds 128 bytes and may cause high register pressure. Consult with your
54hardware vendor to find the total register size available and adjust the code,
55or use smaller sub-group size to avoid high register pressure.
56*/
57__dpct_inline__ void group32(const uint8_t *row_blocks, int gi_in_row, T *out) {
58    if constexpr (TYPE == 42) {                                   // Q2_0: 64 per block of 18 B
59        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 2) * 18;
60        const float d = h2f(b);
61        const int e0 = (gi_in_row % 2) * 32;
62#pragma unroll
63        for (int j = 0; j < 32; ++j) {
64            const int e = e0 + j;
65            const int q = (b[2 + e / 4] >> ((e % 4) * 2)) & 3;
66            put(out, j, (float) (q - 1) * d);
67        }
68    } else if constexpr (TYPE == 2) {                              // Q4_0
69        const uint8_t* b = row_blocks + (size_t) gi_in_row * 18;
70        const float d = h2f(b);
71#pragma unroll
72        for (int j = 0; j < 16; ++j) {
73            put(out, j, (float) ((b[2 + j] & 0x0F) - 8) * d);
74            put(out, j + 16, (float) ((b[2 + j] >> 4) - 8) * d);
75        }
76    } else if constexpr (TYPE == 6) {                              // Q5_0
77        const uint8_t* b = row_blocks + (size_t) gi_in_row * 22;
78        const float d = h2f(b);
79        const uint32_t qh = (uint32_t) b[2] | ((uint32_t) b[3] << 8) | ((uint32_t) b[4] << 16) | ((uint32_t) b[5] << 24);
80        for (int j = 0; j < 16; ++j) {
81            const int xh0 = ((qh >> j) << 4) & 0x10;
82            const int xh1 = (qh >> (j + 12)) & 0x10;
83            put(out, j, (float) (((b[6 + j] & 0x0F) | xh0) - 16) * d);
84            put(out, j + 16, (float) (((b[6 + j] >> 4) | xh1) - 16) * d);
85        }
86    } else if constexpr (TYPE == 7) {                              // Q5_1
87        const uint8_t* b = row_blocks + (size_t) gi_in_row * 24;
88        const float d = h2f(b), m = h2f(b + 2);
89        const uint32_t qh = (uint32_t) b[4] | ((uint32_t) b[5] << 8) |
90                            ((uint32_t) b[6] << 16) | ((uint32_t) b[7] << 24);
91        for (int j = 0; j < 16; ++j) {
92            const int xh0 = ((qh >> j) & 1) << 4;
93            const int xh1 = ((qh >> (j + 16)) & 1) << 4;
94            put(out, j, (float) ((b[8 + j] & 15) | xh0) * d + m);
95            put(out, j + 16, (float) ((b[8 + j] >> 4) | xh1) * d + m);
96        }
97    } else if constexpr (TYPE == 8) {                              // Q8_0
98        const uint8_t* b = row_blocks + (size_t) gi_in_row * 34;
99        const float d = h2f(b);
100#pragma unroll
101        for (int j = 0; j < 32; ++j) put(out, j, (float)(int8_t)b[2 + j] * d);
102    } else if constexpr (TYPE == 20) {                             // IQ4_NL
103        const uint8_t* b = row_blocks + (size_t) gi_in_row * 18;
104        const float d = h2f(b);
105#pragma unroll
106        for (int j = 0; j < 16; ++j) {
107            put(out, j, d * (float) kv_iq4nl[b[2 + j] & 0xf]);
108            put(out, j + 16, d * (float) kv_iq4nl[b[2 + j] >> 4]);
109        }
110    } else if constexpr (TYPE == 11) {                             // Q3_K: hmask[32] qs[64] scales[12] d
111        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 110;
112        const int gi = gi_in_row % 8, n = gi / 4, jj = gi % 4;
113        const uint8_t* hm = b;
114        const uint8_t* q = b + 32 + n * 32;
115        const uint8_t* sc = b + 96;
116        const float d_all = h2f(b + 108);
117        uint32_t aux[4];
118        memcpy(aux, sc, 12);
119        const uint32_t kmask1 = 0x03030303u, kmask2 = 0x0f0f0f0fu, tmp = aux[2];
120        aux[2] = ((aux[0] >> 4) & kmask2) | (((tmp >> 4) & kmask1) << 4);
121        aux[3] = ((aux[1] >> 4) & kmask2) | (((tmp >> 6) & kmask1) << 4);
122        aux[0] = (aux[0] & kmask2) | (((tmp >> 0) & kmask1) << 4);
123        aux[1] = (aux[1] & kmask2) | (((tmp >> 2) & kmask1) << 4);
124        const int8_t* scales = reinterpret_cast<const int8_t*>(aux);
125        const int shift = 2 * jj;
126        const uint8_t m = (uint8_t) (1u << (n * 4 + jj));
127        for (int t = 0; t < 32; ++t) {
128            const int is = n * 8 + jj * 2 + (t >= 16 ? 1 : 0);
129            const float dl = d_all * (float) (scales[is] - 32);
130            put(out, t, dl * (float) ((int) ((q[t] >> shift) & 3) - ((hm[t] & m) ? 0 : 4)));
131        }
132    } else if constexpr (TYPE == 12) {                             // Q4_K: d dmin scales[12] qs[128]
133        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 144;
134        const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;
135        const float d = h2f(b), dmin = h2f(b + 2);
136        int sc, m;
137        scale_min_k4(gi, b + 4, sc, m);
138        const float d1 = d * (float) sc, m1 = dmin * (float) m;
139        const uint8_t* q = b + 16 + 32 * j64;
140#pragma unroll
141        for (int l = 0; l < 32; ++l)
142            put(out, l, d1 * (float)(hi ? (q[l] >> 4) : (q[l] & 0xF)) - m1);
143    } else if constexpr (TYPE == 13) {                             // Q5_K: d dmin scales[12] qh[32] qs[128]
144        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 176;
145        const int gi = gi_in_row % 8, j64 = gi / 2, hi = gi % 2;
146        const float d = h2f(b), dmin = h2f(b + 2);
147        int sc, m;
148        scale_min_k4(gi, b + 4, sc, m);
149        const float d1 = d * (float) sc, m1 = dmin * (float) m;
150        const uint8_t* qh = b + 16;
151        const uint8_t* ql = b + 48 + 32 * j64;
152        const uint8_t u = (uint8_t) (1u << (2 * j64 + hi));
153        for (int l = 0; l < 32; ++l) {
154            const int nib = hi ? (ql[l] >> 4) : (ql[l] & 0xF);
155            put(out, l, d1 * (float) (nib + ((qh[l] & u) ? 16 : 0)) - m1);
156        }
157    } else if constexpr (TYPE == 14) {                             // Q6_K: ql[128] qh[64] scales[16] d
158        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 210;
159        const int gi = gi_in_row % 8, n = gi / 4, qu = gi % 4;
160        const uint8_t* ql = b + 64 * n;
161        const uint8_t* qh = b + 128 + 32 * n;
162        const int8_t* sc = reinterpret_cast<const int8_t*>(b + 192) + 8 * n;
163        const float d = h2f(b + 208);
164        for (int l = 0; l < 32; ++l) {
165            const int is = l / 16;
166            int q;
167            if (qu == 0) q = (ql[l] & 0xF) | (((qh[l] >> 0) & 3) << 4);
168            else if (qu == 1) q = (ql[l + 32] & 0xF) | (((qh[l] >> 2) & 3) << 4);
169            else if (qu == 2) q = (ql[l] >> 4) | (((qh[l] >> 4) & 3) << 4);
170            else q = (ql[l + 32] >> 4) | (((qh[l] >> 6) & 3) << 4);
171            put(out, l, d * (float) sc[is + 2 * qu] * (float) (q - 32));
172        }
173    } else if constexpr (TYPE == 23) {                             // IQ4_XS: d scales_h scales_l[4] qs[128]
174        const uint8_t* b = row_blocks + (size_t) (gi_in_row / 8) * 136;
175        const int ib = gi_in_row % 8;
176        const float d = h2f(b);
177        const uint16_t scales_h = (uint16_t) (b[2] | (b[3] << 8));
178        const int ls = ((b[4 + ib / 2] >> (4 * (ib % 2))) & 0xf) | (((scales_h >> (2 * ib)) & 3) << 4);
179        const float dl = d * (float) (ls - 32);
180        const uint8_t* qs = b + 8 + 16 * ib;
181#pragma unroll
182        for (int j = 0; j < 16; ++j) {
183            put(out, j, dl * (float) kv_iq4nl[qs[j] & 0xf]);
184            put(out, j + 16, dl * (float) kv_iq4nl[qs[j] >> 4]);
185        }
186    }
187}
188
189template <int TYPE, typename T>
190__dpct_inline__ void dequant_kernel(const uint8_t *__restrict__ blocks,
191                                    int64_t row_bytes, int64_t row0,
192                                    int64_t rows, int64_t groups_per_row,
193                                    int64_t ld, T *__restrict__ out) {
194    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
195    const int64_t g =
196        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
197        item_ct1.get_local_id(2);
198    if (g >= rows * groups_per_row) return;
199    const int64_t r = g / groups_per_row, gi = g % groups_per_row;
200    group32<TYPE>(blocks + (row0 + r) * row_bytes, (int) gi, out + r * ld + gi * 32);
201}
202
203bool geometry(int type, int& block_elems, int& block_bytes) {
204    switch (type) {
205    case 2: block_elems = 32; block_bytes = 18; return true;
206    case 6: block_elems = 32; block_bytes = 22; return true;
207    case 7: block_elems = 32; block_bytes = 24; return true;
208    case 8: block_elems = 32; block_bytes = 34; return true;
209    case 20: block_elems = 32; block_bytes = 18; return true;
210    case 11: block_elems = 256; block_bytes = 110; return true;
211    case 12: block_elems = 256; block_bytes = 144; return true;
212    case 13: block_elems = 256; block_bytes = 176; return true;
213    case 14: block_elems = 256; block_bytes = 210; return true;
214    case 23: block_elems = 256; block_bytes = 136; return true;
215    case 42: block_elems = 64; block_bytes = 18; return true;
216    default: return false;
217    }
218}
219
220template <typename T>
221void launch(int type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, T* out, void* stream,
222            int64_t ld = 0) {
223    int be = 0, bb = 0;
224    if (!geometry(type, be, bb) || cols % be != 0 || rows <= 0) {
225        std::fprintf(stderr, "dequant: unsupported type %d or shape %lld x %lld\n", type, (long long) rows,
226                     (long long) cols);
227        std::exit(1);
228    }
229    const int64_t row_bytes = cols / be * bb, gpr = cols / 32, total = rows * gpr;
230    if (ld <= 0) ld = cols;
231    const unsigned grid = (unsigned) ((total + 255) / 256);
232    const uint8_t* p = (const uint8_t*) blocks;
233    dpct::queue_ptr st = strata::q_of(stream);
234#define STRATA_DQ(TY)                                                          \
235    {                                                                          \
236        auto exp_props = sycl::ext::oneapi::experimental::properties{          \
237            sycl::ext::oneapi::experimental::use_root_sync};                   \
238                                                                               \
239        strata::kprof::kp(st, __FILE__, __func__, __LINE__)->submit([&](sycl::handler &cgh) {                                   \
240            auto p_ct0 = p;                                                    \
241            auto row_bytes_ct1 = row_bytes;                                    \
242            auto row0_ct2 = row0;                                              \
243            auto rows_ct3 = rows;                                              \
244            auto gpr_ct4 = gpr;                                                \
245            auto ld_ct5 = ld;                                                  \
246            auto out_ct6 = out;                                                \
247                                                                               \
248            cgh.parallel_for<dpct_kernel_name<class dequant_kernel_ec9311,     \
249                                              dpct_kernel_scalar<TY>, T>>(     \
250                sycl::nd_range<3>(sycl::range(1, 1, grid) *                    \
251                                      sycl::range(1, 1, 256),                  \
252                                  sycl::range(1, 1, 256)),                     \
253                exp_props, [=](sycl::nd_item<3> item_ct1) {                    \
254                    dequant_kernel<TY, T>(p_ct0, row_bytes_ct1, row0_ct2,      \
255                                          rows_ct3, gpr_ct4, ld_ct5, out_ct6); \
256                });                                                            \
257        });                                                                    \
258    } break
259    switch (type) {
260    case 2: STRATA_DQ(2);
261    case 6: STRATA_DQ(6);
262    case 7: STRATA_DQ(7);
263    case 8: STRATA_DQ(8);
264    case 11: STRATA_DQ(11);
265    case 12: STRATA_DQ(12);
266    case 13: STRATA_DQ(13);
267    case 14: STRATA_DQ(14);
268    case 20: STRATA_DQ(20);
269    case 23: STRATA_DQ(23);
270    case 42: STRATA_DQ(42);
271    }
272#undef STRATA_DQ
273    /*
274    DPCT1010: SYCL uses exceptions to report errors and does not use the
275    error codes. The cudaGetLastError function call was replaced with 0. You
276    need to rewrite this code.
277    */
278    const dpct::err0 e = 0;
279    /*
280    DPCT1009: SYCL reports errors using exceptions and does not use error
281    codes. Please replace the "get_error_string_dummy(...)" with a real
282    error-handling function.
283    */
284}
285
286}  // namespace
287
288bool dequant_bf16_supported(int ggml_type) noexcept {
289    int a, b;
290    return geometry(ggml_type, a, b);
291}
292
293namespace {
294// plan v0.3 P6: the i-quant formats (llama.cpp's dequantizers, iq_kernels.cu)
295bool iq_only(int t) { return t == 16 || t == 17 || t == 18 || t == 21 || t == 22 || t == 29; }
296}  // namespace
297
298void dequant_bf16(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, uint16_t* out,
299                  void* stream) {
300    launch<uint16_t>(ggml_type, blocks, row0, rows, cols, out, stream);
301}
302
303void dequant_f16(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, uint16_t* out,
304                 void* stream) {
305    if (iq_only(ggml_type)) {
306        iq_dequant_f16(ggml_type, (const uint8_t*) blocks + (size_t) row0 * iq_row_bytes(ggml_type, cols), rows * cols,
307                       out, stream);
308        return;
309    }
310    launch<H16>(ggml_type, blocks, row0, rows, cols, reinterpret_cast<H16*>(out), stream);
311}
312
313bool dequant_f16_ld(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, int64_t ld,
314                    uint16_t* out, void* stream) {
315    int be = 0, bb = 0;
316    if (iq_only(ggml_type) || !geometry(ggml_type, be, bb) || cols % be != 0 || rows <= 0 || ld < cols || ld % 8 != 0)
317        return false;
318    launch<H16>(ggml_type, blocks, row0, rows, cols, reinterpret_cast<H16*>(out), stream, ld);
319    return true;
320}
321
322void dequant_f32(int ggml_type, const void* blocks, int64_t row0, int64_t rows, int64_t cols, float* out, void* stream) {
323    if (iq_only(ggml_type)) {
324        iq_dequant_f32(ggml_type, (const uint8_t*) blocks + (size_t) row0 * iq_row_bytes(ggml_type, cols), rows * cols,
325                       out, stream);
326        return;
327    }
328    launch<float>(ggml_type, blocks, row0, rows, cols, out, stream);
329}
330
331}  // namespace strata::kernels