okami.mom/strata

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

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

main
1.8 KiB42 linesraw
1// src/kernels/cuda/probe_q8gpu.cu - DESIGN-4: the GPU-side Q8_0 quantizer for the latent CPU
2// share's act rows (the D-stage output quantized on the device, written straight into mapped host
3// memory - the host's 7.2 ms/window serial act quantization becomes a pointer).
4//
5// Bit-exact against ggml's quantize_row_q8_0_ref by construction (probe_std.cu, 2M blocks x2,
6// zero diff): d = amax/127 (f16), q = roundf(x * (1/d)) - the MULTIPLICATION, not the division
7// (x/d vs x*(1/d) differs at half-points: 10 bytes per 2M blocks, all +-1).
8#include "strata/kernels/q8_1_finite.hpp"
9
10namespace strata::kernels {
11
12struct block_q8_0_g { __half d; int8_t qs[32]; };
13
14__global__ void quantize_q8_0_rows_kernel(const float* __restrict__ x, block_q8_0_g* __restrict__ y,
15                                          long long n) {
16    const long long i = (long long) blockIdx.x * blockDim.x + threadIdx.x;
17    if (i >= n) return;
18    const float xi = x[i];
19    float amax = fabsf(xi);
20    #pragma unroll
21    for (int o = 16; o > 0; o >>= 1)
22        amax = fmaxf(amax, __shfl_xor_sync(0xffffffffu, amax, o));
23    const float d = q8_1_finite(amax / 127.0f);
24    const long long ib = i / 32, iqs = i % 32;
25    float q = 0.0f;
26    if (amax != 0.0f) {
27        const float id = 1.0f / d;   // the CPU ref multiplies - replicate exactly (half-point parity)
28        q = roundf(xi * id);
29    }
30    q = q > 127.0f ? 127.0f : q < -127.0f ? -127.0f : q;
31    y[ib].qs[iqs] = (int8_t) q;
32    if (iqs == 0) y[ib].d = __float2half(d);
33}
34
35void quantize_q8_0_rows(const float* x, long long n_rows, long long n_cols, void* y, void* stream) {
36    const long long n = n_rows * n_cols;
37    if (n <= 0) return;
38    quantize_q8_0_rows_kernel<<<(unsigned) ((n + 255) / 256), 256, 0, (cudaStream_t) stream>>>(
39        x, (block_q8_0_g*) y, n);
40}
41
42}  // namespace strata::kernels