okami.mom/strata

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

waveringanaedge0-strata: add the engine for Edge0 145B and Edge0 120Bfd19246dc

main
3.2 KiB76 linesraw
1// src/kernels/cpu/kq_scalar.cpp - scalar fallbacks for the edge0 latent CPU lane on non-x86 hosts
2// (Windows ARM64, DGX Spark).  The AVX-2 versions live in kq_avx2.cpp and are chosen at run time by
3// cpu_avx2_ok(); on a host without them these run instead - same numbers, one row/token at a time.
4// The row math mirrors pool.cpp's static lat_*_row functions (the reference for the AVX2 kernels).
5#include "strata/kernels/cpu/kq_avx2.hpp"
6#include "strata/kernels/f16_bits.hpp"
7
8#include <cmath>
9
10namespace strata::kernels::cpu {
11
12void lat_quant_q8_1_qs_avx2(const float* x, const float* d_ptr, int8_t* q) {
13    // The AVX2 version's scalar contract (probe_qs.cpp): divide by d (not 1/d), round
14    // ties-away-from-zero like roundf, clamp to [-127, 127].
15    const float d = *d_ptr;
16    for (int j = 0; j < 32; ++j) {
17        const float v = x[j] / d;
18        int r = (int) std::lroundf(v);   // roundf: half away from zero
19        if (r > 127) r = 127;
20        if (r < -127) r = -127;
21        q[j] = (int8_t) r;
22    }
23}
24
25void lat_q8_0_q8_1_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,
26                        float* const* out, int r0, int r1) {
27    const int nb = n / 32;
28    for (int r = r0; r < r1; ++r) {
29        const uint8_t* row = w + (size_t) r * row_bytes;
30        for (int t = 0; t < nt; ++t) {
31            const uint8_t* xq = (const uint8_t*) act[t];
32            float acc = 0.0f;
33            for (int b = 0; b < nb; ++b) {
34                const uint8_t* wb = row + (size_t) b * 34;
35                const uint8_t* xb = xq + (size_t) b * 36;
36                const float dw = f32_from_f16((uint16_t)(wb[0] | (wb[1] << 8)));
37                const float dx = f32_from_f16((uint16_t)(xb[0] | (xb[1] << 8)));
38                const int8_t* ws = (const int8_t*)(wb + 2);
39                const int8_t* xs = (const int8_t*)(xb + 4);
40                int s = 0;
41                for (int j = 0; j < 32; ++j) s += (int) xs[j] * (int) ws[j];
42                acc += dw * dx * (float) s;
43            }
44            out[t][r] = acc;
45        }
46    }
47}
48
49void lat_q4_0_q8_1_rows(const uint8_t* w, size_t row_bytes, int n, const void* const* act, int nt,
50                        float* const* out, int r0, int r1) {
51    const int nb = n / 32;
52    for (int r = r0; r < r1; ++r) {
53        const uint8_t* row = w + (size_t) r * row_bytes;
54        for (int t = 0; t < nt; ++t) {
55            const uint8_t* xq = (const uint8_t*) act[t];
56            float acc = 0.0f;
57            for (int b = 0; b < nb; ++b) {
58                const uint8_t* wb = row + (size_t) b * 18;
59                const uint8_t* xb = xq + (size_t) b * 36;
60                const float dw = f32_from_f16((uint16_t)(wb[0] | (wb[1] << 8)));
61                const float dx = f32_from_f16((uint16_t)(xb[0] | (xb[1] << 8)));
62                const int8_t* xs = (const int8_t*)(xb + 4);
63                int s = 0;
64                for (int j = 0; j < 16; ++j) {
65                    const int byte_ = wb[2 + j];
66                    s += (int) xs[j] * ((int)(byte_ & 0xF) - 8);
67                    s += (int) xs[j + 16] * ((int)(byte_ >> 4) - 8);
68                }
69                acc += dw * dx * (float) s;
70            }
71            out[t][r] = acc;
72        }
73    }
74}
75
76}  // namespace strata::kernels::cpu