okami.mom/strata

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

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

main
14.0 KiB321 linesraw
1// src/prefill/moe_mmq.cu - see include/strata/prefill/moe_mmq.hpp.  llama.cpp's MMQ (ggml-cuda, MIT) is compiled
2// from the pinned llama.cpp checkout the build already takes ggml from; src/prefill/ggml_cuda_host.cu supplies the
3// few host symbols of ggml-cuda.cu it references.
4#define DPCT_PROFILING_ENABLED
5#include <sycl/sycl.hpp>
6#include "strata/kprof.hpp"
7#include <dpct/dpct.hpp>
8#include "strata/sycl_queue.hpp"
9#include "strata/prefill/moe_mmq.hpp"
10
11#include "common.cuh"
12#include "mmq.cuh"
13#include "quantize.cuh"
14
15#include <cstdio>
16#include <cstdlib>
17
18namespace strata::prefill::mmq {
19namespace {
20
21void ck(dpct::err0 e, const char *what) {
22}
23
24int64_t pad512(int64_t n) { return (n + 511) / 512 * 512; }
25
26__dpct_inline__ void copy16_kernel(const sycl::uint4 *__restrict__ a,
27                                   int64_t na,
28                                   const sycl::uint4 *__restrict__ b,
29                                   int64_t nb, sycl::uint4 *__restrict__ ab_dst,
30                                   const sycl::uint4 *__restrict__ c,
31                                   int64_t nc,
32                                   sycl::uint4 *__restrict__ c_dst) {
33    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
34    const int64_t i =
35        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
36        item_ct1.get_local_id(2);
37    if (i < na) ab_dst[i] = a[i];
38    else if (i < na + nb) ab_dst[i] = b[i - na];
39    else if (i < na + nb + nc) c_dst[i - na - nb] = c[i - na - nb];
40}
41__dpct_inline__ void copy1_kernel(const uint8_t *__restrict__ a, int64_t na,
42                                  const uint8_t *__restrict__ b, int64_t nb,
43                                  uint8_t *__restrict__ ab_dst,
44                                  const uint8_t *__restrict__ c, int64_t nc,
45                                  uint8_t *__restrict__ c_dst) {
46    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
47    const int64_t i =
48        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
49        item_ct1.get_local_id(2);
50    if (i < na) ab_dst[i] = a[i];
51    else if (i < na + nb) ab_dst[i] = b[i - na];
52    else if (i < na + nb + nc) c_dst[i - na - nb] = c[i - na - nb];
53}
54
55// Strata blob: gate/up codes [1280][640 B], down codes [2560][160 B], gate/up scales [1280][40] f16, down scales
56// [2560][10] f16 (the layout of prefill/kernels.cu's blob_dequant_kernel).  A GGUF Q2_0 block is {f16 d; 16 code
57// bytes} with the same 2-bit codes in the same order, so a block is a scale and a 16-byte run of codes.
58/*
59DPCT1110: The total declared local variable size in device function
60strata_q2_kernel exceeds 128 bytes and may cause high register pressure. Consult
61with your hardware vendor to find the total register size available and adjust
62the code, or use smaller sub-group size to avoid high register pressure.
63*/
64__dpct_inline__ void strata_q2_kernel(const uint8_t *__restrict__ blob,
65                                      uint16_t *__restrict__ gu,
66                                      uint16_t *__restrict__ dn) {
67    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
68    constexpr size_t O_D_CODES = (size_t)1280 * 640,
69                     O_GU_SC = O_D_CODES + (size_t)2560 * 160,
70                     O_D_SC = O_GU_SC + (size_t)1280 * 40 * 2;
71    const int64_t i =
72        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
73        item_ct1.get_local_id(2); // one GGUF block
74    const int64_t n_gu = 1280LL * 40, n_d = 2560LL * 10;
75    const uint8_t* codes;
76    const uint16_t* scale;
77    uint16_t* out;
78    if (i < n_gu) {
79        const int64_t row = i / 40, b = i % 40;
80        codes = blob + row * 640 + b * 16;
81        scale = (const uint16_t*) (blob + O_GU_SC) + row * 40 + b;
82        out = gu + i * 9;
83    } else if (i < n_gu + n_d) {
84        const int64_t j = i - n_gu, row = j / 10, b = j % 10;
85        codes = blob + O_D_CODES + row * 160 + b * 16;
86        scale = (const uint16_t*) (blob + O_D_SC) + row * 10 + b;
87        out = dn + j * 9;
88    } else {
89        return;
90    }
91    const sycl::uint4 q = *(const sycl::uint4 *)codes;
92    const uint16_t* qh = (const uint16_t*) &q;
93    out[0] = *scale;
94#pragma unroll
95    for (int k = 0; k < 8; ++k) out[1 + k] = qh[k];
96}
97
98__dpct_inline__ void swiglu_kernel(const float *__restrict__ gu,
99                                   float *__restrict__ h, int64_t rows,
100                                   int64_t n_ff, bool interleaved) {
101    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
102    const int64_t i =
103        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
104        item_ct1.get_local_id(2);
105    if (i >= rows * n_ff) return;
106    const int64_t r = i / n_ff, k = i % n_ff;
107    const float* row = gu + r * 2 * n_ff;
108    const float g = interleaved ? row[2 * k] : row[k], u = interleaved ? row[2 * k + 1] : row[n_ff + k];
109    h[i] = g / (1.0f + sycl::native::exp(-g)) * u;
110}
111
112__dpct_inline__ void iota_kernel(int32_t *dst, int64_t n) {
113    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
114    const int64_t i =
115        (int64_t)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
116        item_ct1.get_local_id(2);
117    if (i < n) dst[i] = (int32_t) i;
118}
119
120unsigned blocks(int64_t n) { return (unsigned) ((n + 255) / 256); }
121
122}  // namespace
123
124bool built() { return true; }
125
126bool supported(int t) {
127    switch ((ggml_type) t) {
128        case GGML_TYPE_Q2_0: case GGML_TYPE_IQ2_XXS: case GGML_TYPE_IQ2_XS: case GGML_TYPE_IQ2_S:
129        case GGML_TYPE_IQ3_XXS: case GGML_TYPE_IQ3_S: case GGML_TYPE_IQ4_NL: case GGML_TYPE_IQ4_XS:
130            return true;
131        default:
132            return false;
133    }
134}
135
136size_t matrix_bytes(int t, int64_t rows, int64_t cols) {
137    return (size_t) rows * (size_t) (cols / ggml_blck_size((ggml_type) t)) * ggml_type_size((ggml_type) t);
138}
139
140size_t q8_bytes(int64_t rows, int64_t cols) {
141    return (size_t) rows * (size_t) pad512(cols) * sizeof(block_q8_1_mmq) / (4 * QK8_1) + 128 * sizeof(block_q8_1_mmq);
142}
143
144void quantize(const float* x, const int32_t* ids, void* xq, int t, int64_t cols, int64_t ld, int64_t rows, void* stream) {
145    if (rows <= 0) return;
146    quantize_mmq_q8_1_cuda(x, ids, xq, (ggml_type) t, cols, ld, rows * ld, rows * ld, pad512(cols), rows, 1, 1,
147                           (cudaStream_t) stream);
148    /*
149    DPCT1010: SYCL uses exceptions to report errors and does not use the
150    error codes. The cudaGetLastError function call was replaced with 0. You
151    need to rewrite this code.
152    */
153    ck(0, "quantize");
154}
155
156Context::Context() {
157    int dev = 0;
158    dev = dpct::get_current_device_id();
159    ctx_ = new ggml_backend_cuda_context(dev);
160}
161Context::~Context() { delete (ggml_backend_cuda_context*) ctx_; }
162
163void Context::run(const Product& p, void* stream) {
164    if (p.n <= 0 || p.max_rows <= 0) return;
165    const ggml_type t = (ggml_type) p.type;
166    const int64_t qk = ggml_blck_size(t), bpr = p.w_cols / qk;
167    const mmq_args a = {(const char*) p.w, t, (const int*) p.xq, p.ids, p.bounds, p.dst, nullptr,
168                        p.w_cols, p.w_rows, p.total_rows, bpr, p.total_rows, p.ld_dst,
169                        p.n, p.n, (int64_t) (p.expert_bytes / ggml_type_size(t)), 0, 0,
170                        1, 1, 0, 0, 0,
171                        p.max_rows, p.max_rows};
172    auto& ctx = *(ggml_backend_cuda_context*) ctx_;
173    const dpct::queue_ptr s = strata::q_of(stream);
174    switch (t) {
175        case GGML_TYPE_Q2_0: mul_mat_q_case<GGML_TYPE_Q2_0>(ctx, a, s); break;
176        case GGML_TYPE_IQ2_XXS: mul_mat_q_case<GGML_TYPE_IQ2_XXS>(ctx, a, s); break;
177        case GGML_TYPE_IQ2_XS: mul_mat_q_case<GGML_TYPE_IQ2_XS>(ctx, a, s); break;
178        case GGML_TYPE_IQ2_S: mul_mat_q_case<GGML_TYPE_IQ2_S>(ctx, a, s); break;
179        case GGML_TYPE_IQ3_XXS: mul_mat_q_case<GGML_TYPE_IQ3_XXS>(ctx, a, s); break;
180        case GGML_TYPE_IQ3_S: mul_mat_q_case<GGML_TYPE_IQ3_S>(ctx, a, s); break;
181        case GGML_TYPE_IQ4_NL: mul_mat_q_case<GGML_TYPE_IQ4_NL>(ctx, a, s); break;
182        case GGML_TYPE_IQ4_XS: mul_mat_q_case<GGML_TYPE_IQ4_XS>(ctx, a, s); break;
183        default:
184            std::fprintf(stderr, "prefill mmq: type %d is not covered\n", (int) t);
185            std::exit(1);
186    }
187    /*
188    DPCT1010: SYCL uses exceptions to report errors and does not use the
189    error codes. The cudaGetLastError function call was replaced with 0. You
190    need to rewrite this code.
191    */
192    ck(0, "mul_mat_q");
193}
194
195void gather_native(const void* gate, const void* up, size_t gu_half_bytes, const void* down, size_t d_bytes,
196                   void* gu_dst, void* d_dst, void* stream) {
197    const dpct::queue_ptr s = strata::q_of(stream);
198    const bool a16 = ((uintptr_t) gate | (uintptr_t) up | (uintptr_t) down | (uintptr_t) gu_dst | (uintptr_t) d_dst |
199                      gu_half_bytes | d_bytes) % 16 == 0;
200    if (a16) {
201        const int64_t na = (int64_t) gu_half_bytes / 16, nc = (int64_t) d_bytes / 16;
202        {
203            auto exp_props = sycl::ext::oneapi::experimental::properties{
204                sycl::ext::oneapi::experimental::use_root_sync};
205
206            strata::kprof::kp(s, __FILE__, __func__, __LINE__)->template parallel_for<dpct_kernel_name<class copy16_kernel_b12127>>(
207                sycl::nd_range<3>(sycl::range(1, 1, blocks(2 * na + nc)) *
208                                      sycl::range(1, 1, 256),
209                                  sycl::range(1, 1, 256)),
210                exp_props, [=](sycl::nd_item<3> item_ct1) {
211                    copy16_kernel(
212                        (const sycl::uint4 *)gate, na, (const sycl::uint4 *)up,
213                        na, (sycl::uint4 *)gu_dst, (const sycl::uint4 *)down,
214                        nc, (sycl::uint4 *)d_dst);
215                });
216        }
217    } else {
218        const int64_t na = (int64_t) gu_half_bytes, nc = (int64_t) d_bytes;
219        {
220            auto exp_props = sycl::ext::oneapi::experimental::properties{
221                sycl::ext::oneapi::experimental::use_root_sync};
222
223            strata::kprof::kp(s, __FILE__, __func__, __LINE__)->template parallel_for<dpct_kernel_name<class copy1_kernel_c557f3>>(
224                sycl::nd_range<3>(sycl::range(1, 1, blocks(2 * na + nc)) *
225                                      sycl::range(1, 1, 256),
226                                  sycl::range(1, 1, 256)),
227                exp_props, [=](sycl::nd_item<3> item_ct1) {
228                    copy1_kernel((const uint8_t *)gate, na, (const uint8_t *)up,
229                                 na, (uint8_t *)gu_dst, (const uint8_t *)down,
230                                 nc, (uint8_t *)d_dst);
231                });
232        }
233    }
234    /*
235    DPCT1010: SYCL uses exceptions to report errors and does not use the
236    error codes. The cudaGetLastError function call was replaced with 0. You
237    need to rewrite this code.
238    */
239    ck(0, "gather_native");
240}
241
242void gather_strata_q2(const uint8_t* blob, void* gu_dst, void* d_dst, void* stream) {
243    {
244        auto exp_props = sycl::ext::oneapi::experimental::properties{
245            sycl::ext::oneapi::experimental::use_root_sync};
246
247        strata::kprof::kp(strata::q_of(stream), __FILE__, __func__, __LINE__)
248            ->template parallel_for<dpct_kernel_name<class strata_q2_kernel_793a65>>(
249                sycl::nd_range<3>(
250                    sycl::range(1, 1, blocks(1280LL * 40 + 2560LL * 10)) *
251                        sycl::range(1, 1, 256),
252                    sycl::range(1, 1, 256)),
253                exp_props, [=](sycl::nd_item<3> item_ct1) {
254                    strata_q2_kernel(blob, (uint16_t *)gu_dst,
255                                     (uint16_t *)d_dst);
256                });
257    }
258    /*
259    DPCT1010: SYCL uses exceptions to report errors and does not use the
260    error codes. The cudaGetLastError function call was replaced with 0. You
261    need to rewrite this code.
262    */
263    ck(0, "gather_strata_q2");
264}
265
266void swiglu(const float* gu, float* h, int64_t rows, int64_t n_ff, bool interleaved, void* stream) {
267    if (rows <= 0) return;
268    {
269        auto exp_props = sycl::ext::oneapi::experimental::properties{
270            sycl::ext::oneapi::experimental::use_root_sync};
271
272        strata::kprof::kp(strata::q_of(stream), __FILE__, __func__, __LINE__)
273            ->template parallel_for<dpct_kernel_name<class swiglu_kernel_3f7a6f>>(
274                sycl::nd_range<3>(sycl::range(1, 1, blocks(rows * n_ff)) *
275                                      sycl::range(1, 1, 256),
276                                  sycl::range(1, 1, 256)),
277                exp_props, [=](sycl::nd_item<3> item_ct1) {
278                    swiglu_kernel(gu, h, rows, n_ff, interleaved);
279                });
280    }
281    /*
282    DPCT1010: SYCL uses exceptions to report errors and does not use the
283    error codes. The cudaGetLastError function call was replaced with 0. You
284    need to rewrite this code.
285    */
286    ck(0, "swiglu");
287}
288
289void iota(int32_t* dst, int64_t n, void* stream) {
290    if (n <= 0) return;
291    {
292        auto exp_props = sycl::ext::oneapi::experimental::properties{
293            sycl::ext::oneapi::experimental::use_root_sync};
294
295        strata::kprof::kp(strata::q_of(stream), __FILE__, __func__, __LINE__)
296            ->template parallel_for<dpct_kernel_name<class iota_kernel_9245a0>>(
297                sycl::nd_range<3>(sycl::range(1, 1, blocks(n)) *
298                                      sycl::range(1, 1, 256),
299                                  sycl::range(1, 1, 256)),
300                exp_props, [=](sycl::nd_item<3> item_ct1) {
301                    iota_kernel(dst, n);
302                });
303    }
304    /*
305    DPCT1010: SYCL uses exceptions to report errors and does not use the
306    error codes. The cudaGetLastError function call was replaced with 0. You
307    need to rewrite this code.
308    */
309    ck(0, "iota");
310}
311
312// STRATA_MMQ_DIRECT (include/strata/prefill/moe_mmq.hpp): the pointer-table MMQ is CUDA only, so SYCL keeps the
313// gather - these say so, for a prefill.cpp migrated from a tree that calls them
314bool tab_built() { return false; }
315bool tab_fits(int, int64_t) { return false; }
316size_t overread_bytes(int, int64_t) { return 0; }
317int direct_mode() { return 0; }
318size_t direct_slot_guard() { return 0; }
319void prep_group(const GuardTab&, size_t, void*, void*, size_t, void*) {}
320
321}  // namespace strata::prefill::mmq