okami.mom/strata

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

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

main
3.4 KiB86 linesraw
1// src/kernels/cuda/s2_gemv.cu - P2.S2: the S2 GEMV, one thread per output row.
2//
3// Naive per the phase rule: dequantize on the fly, FP32 accumulation inside the row, no shared memory, no
4// vector loads, no __ldg hints.  Phase 3 changes this file; the parity test is what says whether a change is
5// still right.
6#define DPCT_PROFILING_ENABLED
7#include <sycl/sycl.hpp>
8#include "strata/kprof.hpp"
9#include <dpct/dpct.hpp>
10#include "strata/kernels/s2_gemv.hpp"
11
12#include <cstdio>
13#include <cstdlib>
14
15namespace strata::kernels {
16namespace {
17
18constexpr int QK = 64;
19
20__dpct_inline__ void s2_gemv_kernel(const uint16_t *__restrict__ x,
21                                    const uint8_t *__restrict__ codes,
22                                    const float *__restrict__ scales,
23                                    float *__restrict__ y, long long n_in,
24                                    long long n_out) {
25    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
26    const long long o =
27        (long long)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
28        item_ct1.get_local_id(2);
29    if (o >= n_out) return;
30
31    const long long nb = n_in / QK;
32    const uint8_t* c = codes + o * nb * (QK / 4);
33    const float* s = scales + o * nb;
34
35    float acc = 0.0f;
36    for (long long b = 0; b < nb; ++b) {
37        const float d = s[b];
38        const uint8_t* cb = c + b * (QK / 4);
39        const uint16_t* xb = x + b * QK;
40        for (int j = 0; j < QK; ++j) {
41            // (code - 1) in the INTEGER domain, then the group scale, then the activation; the CPU reference
42            // below is written in the same order so the two differ only by floating-point contraction.
43            const int code = (cb[j >> 2] >> ((j & 3) * 2)) & 0x03;
44            acc += (float)(code - 1) * d *
45                   sycl::vec<sycl::half, 1>(
46                       sycl::bit_cast<sycl::half, unsigned short>(xb[j]))
47                       .convert<float, sycl::rounding_mode::automatic>()[0];
48        }
49    }
50    y[o] = acc;
51}
52
53}  // namespace
54
55void s2_gemv(const uint16_t *x, const uint8_t *codes, const float *scales,
56             float *y, int64_t n_in, int64_t n_out) try {
57    if (n_in <= 0 || n_out <= 0) return;
58    if (n_in % QK != 0) {
59        std::fprintf(stderr, "s2_gemv: n_in %lld is not a multiple of %d\n", (long long) n_in, QK);
60        std::exit(1);
61    }
62    const int threads = 128;
63    const long long blocks = (n_out + threads - 1) / threads;
64    {
65        auto exp_props = sycl::ext::oneapi::experimental::properties{
66            sycl::ext::oneapi::experimental::use_root_sync};
67
68        strata::kprof::kp(&dpct::get_in_order_queue(), __FILE__, __func__, __LINE__)
69            ->template parallel_for<dpct_kernel_name<class s2_gemv_kernel_163eb8>>(
70                sycl::nd_range<3>(sycl::range(1, 1, (unsigned)blocks) *
71                                      sycl::range(1, 1, threads),
72                                  sycl::range(1, 1, threads)),
73                exp_props, [=](sycl::nd_item<3> item_ct1) {
74                    s2_gemv_kernel(x, codes, scales, y, n_in, n_out);
75                });
76    }
77    const dpct::err0 e =
78        DPCT_CHECK_ERROR(dpct::get_current_device().queues_wait_and_throw());
79}
80catch (sycl::exception const &exc) {
81  std::cerr << exc.what() << "Exception caught at file:" << __FILE__
82            << ", line:" << __LINE__ << std::endl;
83  std::exit(1);
84}
85
86}  // namespace strata::kernels