okami.mom/strata

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

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
25.0 KiB500 linesraw
1// src/kernels/s_gemv_q8k_parity.cpp - the Q8_K activation path of `s_gemv`.
2//
3// THE POINT OF THIS KERNEL IS A CONTRACT, so the test is built around the contract rather than around the
4// arithmetic:
5//
6//   1. the kernel against a host reference that dequantizes the SAME Q8_K bytes and the SAME S-form planes,
7//      in double - so what is under test is the indexing, not the format;
8//   2. the Q8_K activation is produced by the ACTUAL `quantize_q8_K`, which is byte-exact against ggml
9//      (round 198).  Building the activation by hand here would make this a test of two transcriptions;
10//   3. **THE Q8_K-vs-FP16 GAP IS MEASURED**, because that is the number the whole activation-contract
11//      decision rests on and it has only ever been measured on Q2_0 weights (0.6175%) and on BF16 ones
12//      (0.141-0.156%).  A K-quant weight is a third case and it is the one the dense projections use.
13#define DPCT_PROFILING_ENABLED
14#include <sycl/sycl.hpp>
15#include <dpct/dpct.hpp>
16#include "strata/kernels/f16_bits.hpp"
17#include "strata/kernels/quantize_act.hpp"
18#include "strata/kernels/s_gemv.hpp"
19
20#include <cmath>
21#include <cstdio>
22#include <cstdlib>
23#include <cstring>
24#include <random>
25#include <vector>
26
27namespace {
28
29using strata::kernels::SForm;
30using strata::kernels::Codebook;
31
32void check(dpct::err0 e, const char *what) {
33}
34
35/// The host decode, transcribed from `docs/pack-format.md`'s rule: `value = cb[code] * scale + offset`, with
36/// the integer bias applied INSIDE the integer domain before the one multiply.
37const signed char kIq4Nl[16] = {-127, -104, -83, -65, -49, -35, -22, -10, 1, 13, 25, 38, 53, 69, 89, 113};
38
39double weight_at(const std::vector<uint8_t>& codes, const std::vector<float>& scales,
40                 const std::vector<float>& offset, const SForm& f, long long n_in, long long o, long long i) {
41    const int per_byte = 8 / f.code_bits;
42    const long long codes_per_row = n_in / per_byte;
43    const long long n_groups = n_in / f.group_elems;
44    const uint8_t* c = codes.data() + o * codes_per_row;
45    const int code = (c[i / per_byte] >> ((int) (i % per_byte) * f.code_bits)) & ((1 << f.code_bits) - 1);
46    const double v = (f.codebook == Codebook::Iq4Nl) ? (double) kIq4Nl[code & 0x0F]
47                                                     : (double) (code + f.code_bias);
48    const long long g = i / f.group_elems;
49    double w = v * (double) scales[(size_t) (o * n_groups + g)];
50    if (f.has_offset) w += (double) offset[(size_t) (o * n_groups + g)];
51    return w;
52}
53
54/// Dequantize a block_q8_K buffer on the host: 292 bytes per 256 elements.
55double q8k_at(const std::vector<uint8_t>& x, long long i) {
56    const uint8_t* blk = x.data() + (size_t) (i / 256) * 292;
57    float d;
58    std::memcpy(&d, blk, 4);
59    const int8_t q = ((const int8_t*) (blk + 4))[i % 256];
60    return (double) d * (double) q;
61}
62
63struct Case {
64    const char* what;
65    int bits;
66    int bias;
67    int group;
68    Codebook cb;
69    bool has_offset;
70};
71
72/// THE RAGGED LAST BLOCK.  The split kernels give each warp one row and a block eight warps, so with
73/// `n_out % 8 != 0` the last block holds warps that have no row.  Those warps used to return BEFORE the
74/// codebook barrier - undefined behaviour that every real shape (a multiple of 8) avoided.  Here `n_out` is
75/// deliberately ragged, every row is checked against the host reference over the same bytes, and a guard
76/// band past `n_out` must come back untouched: a warp without a row must neither hang the block nor write.
77/// (`compute-sanitizer --tool synccheck` on this binary is the direct check of the barrier itself.)
78int ragged_rows(const Case& cs, long long n_out) {
79    const long long n_in = 512;
80    const long long guard = 64;
81    std::mt19937 rng(77 + (unsigned) n_out);
82    std::normal_distribution<float> g(0.0f, 1.0f);
83    std::vector<float> xf((size_t) n_in);
84    for (auto& v : xf) v = g(rng);
85    SForm f;
86    f.code_bits = cs.bits;
87    f.code_bias = cs.bias;
88    f.group_elems = cs.group;
89    f.codebook = cs.cb;
90    f.has_offset = cs.has_offset;
91    const int per_byte = 8 / cs.bits;
92    std::vector<uint8_t> codes((size_t) (n_out * n_in / per_byte));
93    for (auto& v : codes) v = (uint8_t) (rng() & 0xFF);
94    const long long n_groups = n_in / cs.group;
95    std::vector<float> scales((size_t) (n_out * n_groups)), offs((size_t) (n_out * n_groups));
96    for (auto& v : scales) v = 0.01f + 0.001f * (float) (rng() % 20);
97    for (auto& v : offs) v = 0.2f * g(rng);
98
99    float* d_xf = nullptr;
100    uint8_t *d_xk = nullptr, *d_x0 = nullptr, *d_codes = nullptr;
101    float *d_scales = nullptr, *d_offs = nullptr, *d_y = nullptr;
102    std::vector<uint8_t> xk((size_t) (n_in / 256) * 292), x0((size_t) (n_in / 32) * 34);
103    check(DPCT_CHECK_ERROR(d_xf = (float *)sycl::malloc_device(
104                               (size_t)n_in * 4, dpct::get_in_order_queue())),
105          "rxf");
106    check(DPCT_CHECK_ERROR(d_xk = (uint8_t *)sycl::malloc_device(
107                               xk.size(), dpct::get_in_order_queue())),
108          "rxk");
109    check(DPCT_CHECK_ERROR(d_x0 = (uint8_t *)sycl::malloc_device(
110                               x0.size(), dpct::get_in_order_queue())),
111          "rx0");
112    check(DPCT_CHECK_ERROR(d_codes = (uint8_t *)sycl::malloc_device(
113                               codes.size(), dpct::get_in_order_queue())),
114          "rcodes");
115    check(DPCT_CHECK_ERROR(d_scales = (float *)sycl::malloc_device(
116                               scales.size() * 4, dpct::get_in_order_queue())),
117          "rscales");
118    check(DPCT_CHECK_ERROR(d_offs = (float *)sycl::malloc_device(
119                               offs.size() * 4, dpct::get_in_order_queue())),
120          "roffs");
121    check(DPCT_CHECK_ERROR(
122              d_y = (float *)sycl::malloc_device((size_t)(n_out + guard) * 4,
123                                                 dpct::get_in_order_queue())),
124          "ry");
125    /*
126    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
127    the original code the source host memory is pageable memory. If the memory
128    is not pageable, call wait() on event return by memcpy API to ensure
129    synchronization behavior.
130    */
131    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(d_xf, xf.data(),
132                                                             (size_t)n_in * 4).wait()),
133          "rcxf");
134    /*
135    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
136    the original code the source host memory is pageable memory. If the memory
137    is not pageable, call wait() on event return by memcpy API to ensure
138    synchronization behavior.
139    */
140    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
141              d_codes, codes.data(), codes.size()).wait()),
142          "rcc");
143    /*
144    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
145    the original code the source host memory is pageable memory. If the memory
146    is not pageable, call wait() on event return by memcpy API to ensure
147    synchronization behavior.
148    */
149    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
150              d_scales, scales.data(), scales.size() * 4).wait()),
151          "rcs");
152    /*
153    DPCT1114: cudaMemcpy is migrated to asynchronization memcpy, assuming in
154    the original code the source host memory is pageable memory. If the memory
155    is not pageable, call wait() on event return by memcpy API to ensure
156    synchronization behavior.
157    */
158    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
159              d_offs, offs.data(), offs.size() * 4).wait()),
160          "rco");
161    strata::kernels::quantize_q8_K(d_xf, d_xk, n_in, nullptr);
162    strata::kernels::quantize_q8_0(d_xf, d_x0, n_in, nullptr);
163    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
164                               .memcpy(xk.data(), d_xk, xk.size())
165                               .wait()),
166          "rcxk");
167    check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
168                               .memcpy(x0.data(), d_x0, x0.size())
169                               .wait()),
170          "rcx0");
171
172    int bad = 0;
173    for (int kind = 0; kind < 2; ++kind) {           // 0: Q8_K activation, 1: Q8_0 activation
174        std::vector<float> want((size_t) n_out);
175        std::vector<double> mag((size_t) n_out);   // sum |w_i a_i| per row: a row whose sum cancels near 0
176        for (long long o = 0; o < n_out; ++o) {    // must not turn f32 rounding into a relative failure
177            double acc = 0, am = 0;
178            for (long long i = 0; i < n_in; ++i) {
179                double a;
180                if (kind == 0) {
181                    a = q8k_at(xk, i);
182                } else {
183                    const uint8_t* blk = x0.data() + (size_t) (i / 32) * 34;
184                    uint16_t dbits;
185                    std::memcpy(&dbits, blk, 2);
186                    a = (double) strata::kernels::f32_from_f16(dbits) * (double) ((const int8_t*) (blk + 2))[i % 32];
187                }
188                const double wa = weight_at(codes, scales, offs, f, n_in, o, i) * a;
189                acc += wa;
190                am += std::fabs(wa);
191            }
192            want[(size_t) o] = (float) acc;
193            mag[(size_t) o] = am;
194        }
195        // the guard band holds a NaN pattern; any write there shows up as a changed bit
196        std::vector<uint32_t> sentinel((size_t) (n_out + guard), 0x7FC0DEADu);
197        /*
198        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
199        assuming in the original code the source host memory is pageable memory.
200        If the memory is not pageable, call wait() on event return by memcpy API
201        to ensure synchronization behavior.
202        */
203        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
204                  d_y, sentinel.data(), sentinel.size() * 4).wait()),
205              "rsent");
206        if (kind == 0)
207            strata::kernels::s_gemv_q8k_split(d_xk, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in,
208                                              n_out, f, nullptr);
209        else
210            strata::kernels::s_gemv_q8_0_split(d_x0, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in,
211                                               n_out, f, nullptr);
212        check(DPCT_CHECK_ERROR(
213                  dpct::get_current_device().queues_wait_and_throw()),
214              "ragged split");
215        std::vector<uint32_t> got_bits(sentinel.size());
216        check(DPCT_CHECK_ERROR(
217                  (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
218                      .memcpy(got_bits.data(), d_y, got_bits.size() * 4)
219                      .wait()),
220              "rcy");
221        double m = 0, d = 0;
222        for (long long o = 0; o < n_out; ++o) {
223            float v;
224            std::memcpy(&v, &got_bits[(size_t) o], 4);
225            m += mag[(size_t) o];
226            d += std::fabs((double) want[(size_t) o] - (double) v);
227        }
228        long long touched = 0;
229        for (long long o = n_out; o < n_out + guard; ++o) touched += got_bits[(size_t) o] != 0x7FC0DEADu;
230        const double rel = d / (m > 1e-30 ? m : 1e-30);
231        const bool ok = rel <= 1e-5 && touched == 0;
232        std::printf("  %-28s %s n_out %3lld: rel %.3e, guard words written %lld  %s\n", cs.what,
233                    kind == 0 ? "Q8_K" : "Q8_0", n_out, rel, touched, ok ? "ok" : "*** WRONG ***");
234        if (!ok) ++bad;
235    }
236    sycl::free(d_xf, dpct::get_in_order_queue());
237        sycl::free(d_xk, dpct::get_in_order_queue());
238        sycl::free(d_x0, dpct::get_in_order_queue());
239        sycl::free(d_codes, dpct::get_in_order_queue());
240        sycl::free(d_scales, dpct::get_in_order_queue());
241        sycl::free(d_offs, dpct::get_in_order_queue());
242    sycl::free(d_y, dpct::get_in_order_queue());
243    return bad;
244}
245
246}  // namespace
247
248int main(int argc, char** argv) {
249    bool selftest = false;
250    for (int i = 1; i < argc; ++i) {
251        if (std::string(argv[i]) == "--selftest") selftest = true;
252        else { std::fprintf(stderr, "usage: s_gemv_q8k_parity [--selftest]\n"); return 2; }
253    }
254    int bad = 0;
255
256    // The forms the DENSE weights actually use, taken from the manifest rather than invented:
257    //   attn_qkv.weight   IQ4_XS  S4  group 32, no offset   (the GDN mixer's qkv)
258    //   attn_gate.weight  Q4_K    S4  group 32, offset      (the GDN mixer's z)
259    //   ssm_out.weight    Q5_K    S8  group 32, offset      (the GDN mixer's output)
260    const Case cases[] = {
261        {"IQ4_XS  S4 g32 no-offset", 4, -8, 32, Codebook::Affine, false},
262        {"Q4_K    S4 g32 offset", 4, 0, 32, Codebook::Affine, true},
263        {"Q5_K    S8 g32 offset", 8, 0, 32, Codebook::Affine, true},
264    };
265
266    for (const Case& cs : cases) {
267        const long long n_in = 512, n_out = 64;   // 2 Q8_K blocks wide, small enough for a host reference
268        std::mt19937 rng(2024);
269        std::normal_distribution<float> g(0.0f, 1.0f);
270        std::vector<float> xf((size_t) n_in);
271        for (auto& v : xf) v = g(rng);
272
273        SForm f;
274        f.code_bits = cs.bits;
275        f.code_bias = cs.bias;
276        f.group_elems = cs.group;
277        f.codebook = cs.cb;
278        f.has_offset = cs.has_offset;
279
280        const int per_byte = 8 / cs.bits;
281        std::vector<uint8_t> codes((size_t) (n_out * n_in / per_byte));
282        for (auto& v : codes) v = (uint8_t) (rng() & 0xFF);
283        const long long n_groups = n_in / cs.group;
284        std::vector<float> scales((size_t) (n_out * n_groups)), offs((size_t) (n_out * n_groups));
285        for (auto& v : scales) v = 0.01f + 0.001f * (float) (rng() % 20);
286        for (auto& v : offs) v = 0.2f * g(rng);
287
288        // ---- the activation, from the REAL quantizer
289        std::vector<uint8_t> xq8k((size_t) (n_in / 256) * 292);
290        float* d_xf = nullptr;
291        uint8_t* d_xq = nullptr;
292        check(
293            DPCT_CHECK_ERROR(d_xf = (float *)sycl::malloc_device(
294                                 (size_t)n_in * 4, dpct::get_in_order_queue())),
295            "xf");
296        check(DPCT_CHECK_ERROR(d_xq = (uint8_t *)sycl::malloc_device(
297                                   xq8k.size(), dpct::get_in_order_queue())),
298              "xq");
299        /*
300        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
301        assuming in the original code the source host memory is pageable memory.
302        If the memory is not pageable, call wait() on event return by memcpy API
303        to ensure synchronization behavior.
304        */
305        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
306                  d_xf, xf.data(), (size_t)n_in * 4).wait()),
307              "cxf");
308        strata::kernels::quantize_q8_K(d_xf, d_xq, n_in, nullptr);
309        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
310                                   .memcpy(xq8k.data(), d_xq, xq8k.size())
311                                   .wait()),
312              "cxq");
313
314        // ---- host reference in double, over the same bytes
315        std::vector<float> want((size_t) n_out, 0.0f);
316        for (long long o = 0; o < n_out; ++o) {
317            double acc = 0;
318            for (long long i = 0; i < n_in; ++i)
319                acc += weight_at(codes, scales, offs, f, n_in, o, i) * q8k_at(xq8k, i);
320            want[(size_t) o] = (float) acc;
321        }
322
323        // ---- device
324        uint8_t* d_codes = nullptr;
325        float *d_scales = nullptr, *d_offs = nullptr, *d_y = nullptr;
326        check(DPCT_CHECK_ERROR(d_codes = (uint8_t *)sycl::malloc_device(
327                                   codes.size(), dpct::get_in_order_queue())),
328              "codes");
329        check(DPCT_CHECK_ERROR(
330                  d_scales = (float *)sycl::malloc_device(
331                      scales.size() * 4, dpct::get_in_order_queue())),
332              "scales");
333        check(
334            DPCT_CHECK_ERROR(d_offs = (float *)sycl::malloc_device(
335                                 offs.size() * 4, dpct::get_in_order_queue())),
336            "offs");
337        check(DPCT_CHECK_ERROR(
338                  d_y = (float *)sycl::malloc_device(
339                      (size_t)n_out * 4, dpct::get_in_order_queue())),
340              "y");
341        /*
342        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
343        assuming in the original code the source host memory is pageable memory.
344        If the memory is not pageable, call wait() on event return by memcpy API
345        to ensure synchronization behavior.
346        */
347        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
348                  d_codes, codes.data(), codes.size()).wait()),
349              "cc");
350        /*
351        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
352        assuming in the original code the source host memory is pageable memory.
353        If the memory is not pageable, call wait() on event return by memcpy API
354        to ensure synchronization behavior.
355        */
356        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
357                  d_scales, scales.data(), scales.size() * 4).wait()),
358              "cs");
359        /*
360        DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
361        assuming in the original code the source host memory is pageable memory.
362        If the memory is not pageable, call wait() on event return by memcpy API
363        to ensure synchronization behavior.
364        */
365        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
366                  d_offs, offs.data(), offs.size() * 4).wait()),
367              "co");
368
369        std::vector<float> naive((size_t) n_out), warp((size_t) n_out);
370        strata::kernels::s_gemv_q8k(d_xq, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in, n_out,
371                                    f, nullptr);
372        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
373                                   .memcpy(naive.data(), d_y, (size_t)n_out * 4)
374                                   .wait()),
375              "cy1");
376        strata::kernels::s_gemv_q8k_split(d_xq, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in,
377                                          n_out, f, nullptr);
378        check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
379                                   .memcpy(warp.data(), d_y, (size_t)n_out * 4)
380                                   .wait()),
381              "cy2");
382
383        double mag = 0, d1 = 0, d2 = 0;
384        for (long long o = 0; o < n_out; ++o) {
385            mag += std::fabs((double) want[(size_t) o]);
386            d1 += std::fabs((double) want[(size_t) o] - (double) naive[(size_t) o]);
387            d2 += std::fabs((double) want[(size_t) o] - (double) warp[(size_t) o]);
388        }
389        std::printf("  %-28s naive %.3e   warp %.3e\n", cs.what, d1 / mag, d2 / mag);
390        // The kernel accumulates in f32, the reference in f64, over 512 terms with values of similar size.
391        // 1e-5 is the arithmetic's floor, not a structural allowance.
392        if (!(d1 / mag <= 1e-5 && d2 / mag <= 1e-5)) { std::printf("    *** over 1e-5 ***\n"); ++bad; }
393
394        // ---- 2b. THE Q8_0 ACTIVATION, on the SAME weight planes.  `s_gemv_q8_0_split` exists because the
395        // legacy block formats want Q8_0 and `ffn_down_shexp` cannot use Q8_K at all (n_in = 640 is not a
396        // multiple of 256).  It shares every line with the Q8_K kernel except the activation loader, so what
397        // this checks is the LOADER - the 34-byte block, the fp16 `d`, and the element index inside it.
398        {
399            std::vector<uint8_t> xq0((size_t) (n_in / 32) * 34);
400            uint8_t* d_x0 = nullptr;
401            check(DPCT_CHECK_ERROR(d_x0 = (uint8_t *)sycl::malloc_device(
402                                       xq0.size(), dpct::get_in_order_queue())),
403                  "x0");
404            strata::kernels::quantize_q8_0(d_xf, d_x0, n_in, nullptr);
405            check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
406                                       .memcpy(xq0.data(), d_x0, xq0.size())
407                                       .wait()),
408                  "cx0");
409
410            // the host reference over the SAME Q8_0 bytes, with the same weight decode
411            std::vector<float> want0((size_t) n_out, 0.0f);
412            for (long long o = 0; o < n_out; ++o) {
413                double acc = 0;
414                for (long long i = 0; i < n_in; ++i) {
415                    const uint8_t* blk = xq0.data() + (size_t) (i / 32) * 34;
416                    uint16_t dbits;
417                    std::memcpy(&dbits, blk, 2);
418                    const double d = (double) strata::kernels::f32_from_f16(dbits);
419                    const int8_t q = ((const int8_t*) (blk + 2))[i % 32];
420                    acc += weight_at(codes, scales, offs, f, n_in, o, i) * d * (double) q;
421                }
422                want0[(size_t) o] = (float) acc;
423            }
424            strata::kernels::s_gemv_q8_0_split(d_x0, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y,
425                                               n_in, n_out, f, nullptr);
426            std::vector<float> got0((size_t) n_out);
427            check(DPCT_CHECK_ERROR(
428                      (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
429                          .memcpy(got0.data(), d_y, (size_t)n_out * 4)
430                          .wait()),
431                  "cy0");
432            double m0 = 0, dd0 = 0;
433            for (long long o = 0; o < n_out; ++o) {
434                m0 += std::fabs((double) want0[(size_t) o]);
435                dd0 += std::fabs((double) want0[(size_t) o] - (double) got0[(size_t) o]);
436            }
437            std::printf("  %-28s Q8_0 act %.3e\n", cs.what, dd0 / (m0 > 1e-30 ? m0 : 1e-30));
438            if (!(dd0 / (m0 > 1e-30 ? m0 : 1e-30) <= 1e-5)) {
439                std::printf("    *** the Q8_0 activation loader is WRONG ***\n");
440                ++bad;
441            }
442            check(
443                DPCT_CHECK_ERROR(sycl::free(d_x0, dpct::get_in_order_queue())),
444                "fx0");
445        }
446
447        // ---- 3. THE Q8_K vs FP16 GAP, on a K-quant weight - the case the contract decision needs
448        {
449            std::vector<uint16_t> x16((size_t) n_in);
450            for (long long i = 0; i < n_in; ++i) x16[(size_t) i] = strata::kernels::f16_from_f32(xf[(size_t) i]);
451            uint16_t* d_x16 = nullptr;
452            check(DPCT_CHECK_ERROR(
453                      d_x16 = (uint16_t *)sycl::malloc_device(
454                          (size_t)n_in * 2, dpct::get_in_order_queue())),
455                  "x16");
456            /*
457            DPCT1114: cudaMemcpy is migrated to asynchronization memcpy,
458            assuming in the original code the source host memory is pageable
459            memory. If the memory is not pageable, call wait() on event return
460            by memcpy API to ensure synchronization behavior.
461            */
462            check(DPCT_CHECK_ERROR((dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue()).memcpy(
463                      d_x16, x16.data(), (size_t)n_in * 2).wait()),
464                  "cx16");
465            std::vector<float> fp16out((size_t) n_out);
466            strata::kernels::s_gemv(d_x16, d_codes, d_scales, cs.has_offset ? d_offs : nullptr, d_y, n_in, n_out,
467                                    f);
468            check(DPCT_CHECK_ERROR(
469                      (dpct::get_current_device().queues_wait_and_throw(), dpct::get_in_order_queue())
470                          .memcpy(fp16out.data(), d_y, (size_t)n_out * 4)
471                          .wait()),
472                  "cy3");
473            double dd = 0;
474            for (long long o = 0; o < n_out; ++o)
475                dd += std::fabs((double) naive[(size_t) o] - (double) fp16out[(size_t) o]);
476            const double rel = dd / mag;
477            const bool visible = rel > 1e-3;
478            std::printf("      %-24s %s (%.4f%% apart, tolerance 0.1%%)\n", "Q8_K vs fp16 activation",
479                        visible ? "yes" : "*** NO ***", rel * 100);
480            if (!visible) ++bad;
481            sycl::free(d_x16, dpct::get_in_order_queue());
482        }
483        sycl::free(d_xf, dpct::get_in_order_queue());
484            sycl::free(d_xq, dpct::get_in_order_queue());
485            sycl::free(d_codes, dpct::get_in_order_queue());
486            sycl::free(d_scales, dpct::get_in_order_queue());
487            sycl::free(d_offs, dpct::get_in_order_queue());
488            sycl::free(d_y, dpct::get_in_order_queue());
489    }
490
491    // ---- 4. output widths that are NOT a multiple of the eight rows per block
492    std::printf("\n");
493    for (const Case& cs : cases)
494        for (long long n_out : {1LL, 7LL, 9LL, 61LL}) bad += ragged_rows(cs, n_out);
495
496    std::printf("\ns_gemv_q8k: %d failures\n", bad);
497    if (bad) return 1;
498    if (selftest) std::printf("s_gemv_q8k_parity OK\n");
499    return 0;
500}