okami.mom/strata

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

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

main
3.2 KiB73 linesraw
1// src/kernels/cuda/dequant_s2.cu - P2.S2's first naive kernel.
2//
3// S2 is Q2_0's canonical form and 31.64 GiB of the artifact is Q2_0, so this is the single hottest decode in
4// the engine.  The canonical plane layout is `docs/pack-format.md`'s: codes packed 4 per byte LSB-first
5// (element i at byte i/4, bits (i%4)*2 - Q2_0's own convention, kept for every form) and one FP32 scale per
6// group (64 elements for S2).  The decode is `(code + code_bias) * scale` with `code_bias = -1`, the
7// subtraction inside the INTEGER domain, which is what makes it bit-exact (docs/pack-format.md §3.1).
8//
9// Naive on purpose: one thread per block, no vectors, no shared memory, no unrolling.  Phase 3 changes this
10// file and the parity test in src/kernels/dequant_s2_parity.cpp is what says whether a change is still right.
11#define DPCT_PROFILING_ENABLED
12#include <sycl/sycl.hpp>
13#include "strata/kprof.hpp"
14#include <dpct/dpct.hpp>
15#include "strata/kernels/dequant_s2.hpp"
16
17#include <cstdio>
18#include <cstdlib>
19
20namespace strata::kernels {
21namespace {
22
23constexpr int QK = 64;             // S2 group = Q2_0 block = 64 elements
24constexpr int CODES_PER_BYTE = 4;
25
26__dpct_inline__ void dequant_s2_kernel(const uint8_t *__restrict__ codes,
27                                       const float *__restrict__ scales,
28                                       float *__restrict__ out,
29                                       long long n_blocks) {
30    auto item_ct1 = sycl::ext::oneapi::this_work_item::get_nd_item<3>();
31    const long long b =
32        (long long)item_ct1.get_group(2) * item_ct1.get_local_range(2) +
33        item_ct1.get_local_id(2);
34    if (b >= n_blocks) return;
35    const float d = scales[b];
36    const uint8_t* c = codes + b * (QK / CODES_PER_BYTE);
37    float* y = out + b * QK;
38    for (int j = 0; j < QK; ++j) {
39        const int code = (c[j / CODES_PER_BYTE] >> ((j % CODES_PER_BYTE) * 2)) & 0x03;
40        y[j] = (float)(code - 1) * d;      // the -1 is applied to the CODE, then ONE multiply
41    }
42}
43
44}  // namespace
45
46void dequant_s2(const uint8_t *codes, const float *scales, float *out,
47                int64_t n_blocks) try {
48    if (n_blocks <= 0) return;
49    const int threads = 256;
50    const long long blocks = (n_blocks + threads - 1) / threads;
51    {
52        auto exp_props = sycl::ext::oneapi::experimental::properties{
53            sycl::ext::oneapi::experimental::use_root_sync};
54
55        strata::kprof::kp(&dpct::get_in_order_queue(), __FILE__, __func__, __LINE__)
56            ->template parallel_for<dpct_kernel_name<class dequant_s2_kernel_bdb833>>(
57                sycl::nd_range<3>(sycl::range(1, 1, (unsigned)blocks) *
58                                      sycl::range(1, 1, threads),
59                                  sycl::range(1, 1, threads)),
60                exp_props, [=](sycl::nd_item<3> item_ct1) {
61                    dequant_s2_kernel(codes, scales, out, n_blocks);
62                });
63    }
64    const dpct::err0 e =
65        DPCT_CHECK_ERROR(dpct::get_current_device().queues_wait_and_throw());
66}
67catch (sycl::exception const &exc) {
68  std::cerr << exc.what() << "Exception caught at file:" << __FILE__
69            << ", line:" << __LINE__ << std::endl;
70  std::exit(1);
71}
72
73}  // namespace strata::kernels