okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
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#include "strata/kernels/dequant_s2.hpp" 12 13#include <cuda_runtime.h> 14 15#include <cstdio> 16#include <cstdlib> 17 18namespace strata::kernels { 19namespace { 20 21constexpr int QK = 64; // S2 group = Q2_0 block = 64 elements 22constexpr int CODES_PER_BYTE = 4; 23 24__global__ void dequant_s2_kernel(const uint8_t* __restrict__ codes, const float* __restrict__ scales, 25 float* __restrict__ out, long long n_blocks) { 26 const long long b = (long long) blockIdx.x * blockDim.x + threadIdx.x; 27 if (b >= n_blocks) return; 28 const float d = scales[b]; 29 const uint8_t* c = codes + b * (QK / CODES_PER_BYTE); 30 float* y = out + b * QK; 31 for (int j = 0; j < QK; ++j) { 32 const int code = (c[j / CODES_PER_BYTE] >> ((j % CODES_PER_BYTE) * 2)) & 0x03; 33 y[j] = (float)(code - 1) * d; // the -1 is applied to the CODE, then ONE multiply 34 } 35} 36 37} // namespace 38 39void dequant_s2(const uint8_t* codes, const float* scales, float* out, int64_t n_blocks) { 40 if (n_blocks <= 0) return; 41 const int threads = 256; 42 const long long blocks = (n_blocks + threads - 1) / threads; 43 dequant_s2_kernel<<<(unsigned) blocks, threads>>>(codes, scales, out, n_blocks); 44 const cudaError_t e = cudaDeviceSynchronize(); 45 if (e != cudaSuccess) { 46 std::fprintf(stderr, "dequant_s2: %s\n", cudaGetErrorString(e)); 47 std::exit(1); 48 } 49} 50 51} // namespace strata::kernels