okami.mom/strata

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

Niko1221Strata: Qwen3.8-Flash-Next on a 12-24 GB NVIDIA GPU + 64 GB RAM, one-click install01529aaa8

main
4.6 KiB96 linesraw
1// src/kernels/cuda/s2_gemv_q8.cu - the S2 GEMV that consumes Q8_0 ACTIVATIONS, as ggml does.
2//
3// THE GAP THIS CLOSES (round 186).  `ggml_mul_mat` converts src1 - the ACTIVATION - to the weight's
4// `vec_dot_type` before the dot product, and for Q2_0 that is Q8_0.  The CUDA kernels built in rounds 166-177
5// take FP16 activations instead, while the CPU expert path (`bench/micro/cpu_s2.cpp`) already quantizes to int8
6// and uses `vpdpbusd`.  Left that way the two paths compute different numbers for different experts of the SAME
7// token, and the measured size of the difference is the activation contract itself: 0.9498% relative, against
8// P2.S2's 1e-3 tolerance for FP16 paths.
9//
10// This is the CORRECTNESS version, not the fast one.  It dequantizes each activation (`xq * dx`) inside the
11// loop and does the same float arithmetic as `s2_gemv_quads`; the speed win available here is `__dp4a`, which
12// turns four int8 MACs into one instruction, and that belongs to Phase 3 once the numerics are settled.  The
13// phase's rule is the simplest correct kernel first, and a dp4a version whose numerics are wrong would be
14// indistinguishable from a fast one.
15//
16// Layout: activations are (n_in/32) ggml Q8_0 blocks, 34 bytes each - `fp16 d` then `int8 qs[32]` - produced by
17// `strata::kernels::quantize_q8_0`.  A quad of four elements lies inside one 32-element block, so the block
18// scale is constant across it and is loaded once.
19#include "strata/kernels/s2_gemv_q8.hpp"
20
21#include <cuda_fp16.h>
22#include <cuda_runtime.h>
23
24#include <cstdio>
25#include <cstdlib>
26
27namespace strata::kernels {
28namespace {
29
30constexpr int QK_S2 = 64;
31constexpr int QK8_0 = 32;
32
33__global__ void s2_gemv_q8_kernel(const uint8_t* __restrict__ act, const uint8_t* __restrict__ codes,
34                                  const float* __restrict__ scales, float* __restrict__ y, long long n_in,
35                                  long long n_out, int threads_per_row) {
36    extern __shared__ float partial[];
37    const long long o = blockIdx.x;
38    if (o >= n_out) return;
39    const int tid = threadIdx.x;
40
41    const long long n_quads = n_in / 4;
42    const uint8_t* c = codes + o * n_quads;             // one code byte per quad
43    const float* s = scales + o * (n_in / QK_S2);
44
45    float a0 = 0.0f, a1 = 0.0f, a2 = 0.0f, a3 = 0.0f;
46    for (long long q = tid; q < n_quads; q += threads_per_row) {
47        const uint8_t byte = c[q];
48        const float d = s[q >> 4];                      // (q*4) >> 6, the S2 group index as a shift
49        // the activation quad: four int8 in one 32-element block, so one block scale
50        const long long ablk = (q * 4) / QK8_0;
51        const uint8_t* blk = act + ablk * 34;
52        const uint16_t dbits = (uint16_t) (blk[0] | (blk[1] << 8));
53        const float dx = __half2float(__ushort_as_half(dbits));
54        const int8_t* xq = reinterpret_cast<const int8_t*>(blk + 2);
55        const int off = (int) ((q * 4) % QK8_0);
56
57        const float w0 = (float) ((int) (byte & 3) - 1) * d;
58        const float w1 = (float) ((int) ((byte >> 2) & 3) - 1) * d;
59        const float w2 = (float) ((int) ((byte >> 4) & 3) - 1) * d;
60        const float w3 = (float) ((int) ((byte >> 6) & 3) - 1) * d;
61        a0 += w0 * ((float) xq[off + 0] * dx);
62        a1 += w1 * ((float) xq[off + 1] * dx);
63        a2 += w2 * ((float) xq[off + 2] * dx);
64        a3 += w3 * ((float) xq[off + 3] * dx);
65    }
66    partial[tid] = (a0 + a1) + (a2 + a3);
67    __syncthreads();
68    for (int step = threads_per_row / 2; step > 0; step >>= 1) {
69        if (tid < step) partial[tid] += partial[tid + step];
70        __syncthreads();
71    }
72    if (tid == 0) y[o] = partial[0];
73}
74
75}  // namespace
76
77void s2_gemv_q8(const uint8_t* act, const uint8_t* codes, const float* scales, float* y, int64_t n_in,
78                int64_t n_out, int threads_per_row, void* stream) {
79    if (n_in <= 0 || n_out <= 0) return;
80    if (n_in % QK8_0 != 0 || n_in % QK_S2 != 0) {
81        std::fprintf(stderr, "s2_gemv_q8: n_in %lld must be a multiple of %d\n", (long long) n_in, QK_S2);
82        std::exit(1);
83    }
84    const size_t smem = (size_t) threads_per_row * sizeof(float);
85    s2_gemv_q8_kernel<<<(unsigned) n_out, threads_per_row, smem, (cudaStream_t) stream>>>(act, codes, scales, y,
86                                                                                         n_in, n_out,
87                                                                                         threads_per_row);
88    const cudaError_t e = cudaGetLastError();
89    if (e != cudaSuccess) {
90        std::fprintf(stderr, "s2_gemv_q8 launch: %s\n", cudaGetErrorString(e));
91        std::exit(1);
92    }
93    if (stream == nullptr) cudaDeviceSynchronize();
94}
95
96}  // namespace strata::kernels