okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
01529aaa8
main
1// src/kernels/cuda/s2_gemv_quads.cu - S2 with the code load amortised over four elements. 2// 3// THE HYPOTHESIS THIS TESTS. L4 measured 66 G weights/s = 0.55 weights/cycle/SM, and at an estimated ~13 4// instructions per weight that is ~7.2 instructions/cycle/SM against an SM's 4/cycle issue ceiling. Compute 5// (0.4% of FP32 peak) and bandwidth (2%) are excluded by measurement and the dependency chain was excluded by 6// experiment in L4, so instruction ISSUE is what remains. If that is right, cutting instructions per weight 7// must move the rate; if the rate does not move, the hypothesis is wrong and that is worth knowing too. 8// 9// WHERE THE INSTRUCTIONS GO. For S2 four consecutive elements share ONE byte (element i at byte i/4, bits 10// (i%4)*2), and four halves of x are eight contiguous bytes. The generic kernel walks with a thread stride 11// and loads one byte and one half PER ELEMENT; a thread that takes a contiguous quad instead loads one byte 12// and one 64-bit word for all four. Memory instructions per element drop from 2 to 0.5, and the address 13// arithmetic is computed once rather than four times. 14// 15// S2 IS SPECIALISED DELIBERATELY: it is 31.64 GiB of the 38 GiB pack, so it is the kernel that matters. The 16// generic `s_gemv_split` still serves S4 and S8. 17#include "strata/kernels/s_gemv.hpp" 18 19#include <cuda_fp16.h> 20#include <cuda_runtime.h> 21 22#include <cstdio> 23#include <cstdlib> 24 25namespace strata::kernels { 26namespace { 27 28// QK = 64 elements per group; a quad of 4 elements is 1/16 of a group, so 29// group = (quad * 4) >> 6 = quad >> 4 30constexpr int QK_S2 = 64; 31constexpr int CODES_PER_BYTE_S2 = 4; 32 33__global__ void s2_gemv_quads_kernel(const uint16_t* __restrict__ x, 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; // exactly 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]; // ONE load for four codes 48 const float d = s[q >> 4]; // (q*4) >> 6, the group index as a shift 49 // ONE 64-bit load for four halves. `x` is 256-byte aligned and four halves are eight bytes, so 50 // this is an aligned uint2 - if it were not, the misaligned access would fault rather than be slow. 51 const uint2 xw = *reinterpret_cast<const uint2*>(x + q * 4); 52 const __half2 h01 = *reinterpret_cast<const __half2*>(&xw.x); 53 const __half2 h23 = *reinterpret_cast<const __half2*>(&xw.y); 54 // the four codes, each carrying the -1 bias in the INTEGER domain; the scale is applied once per 55 // element here rather than once per group, which is the same expression the generic kernel uses 56 const float w0 = (float) ((int) (byte & 3) - 1) * d; 57 const float w1 = (float) ((int) ((byte >> 2) & 3) - 1) * d; 58 const float w2 = (float) ((int) ((byte >> 4) & 3) - 1) * d; 59 const float w3 = (float) ((int) ((byte >> 6) & 3) - 1) * d; 60 a0 += w0 * __low2float(h01); 61 a1 += w1 * __high2float(h01); 62 a2 += w2 * __low2float(h23); 63 a3 += w3 * __high2float(h23); 64 } 65 partial[tid] = (a0 + a1) + (a2 + a3); 66 __syncthreads(); 67 for (int step = threads_per_row / 2; step > 0; step >>= 1) { 68 if (tid < step) partial[tid] += partial[tid + step]; 69 __syncthreads(); 70 } 71 if (tid == 0) y[o] = partial[0]; 72} 73 74} // namespace 75 76void s2_gemv_quads(const uint16_t* x, const uint8_t* codes, const float* scales, float* y, int64_t n_in, 77 int64_t n_out, int threads_per_row) { 78 if (n_in <= 0 || n_out <= 0) return; 79 if (n_in % 4 != 0) { 80 std::fprintf(stderr, "s2_gemv_quads: n_in %lld is not a multiple of 4\n", (long long) n_in); 81 std::exit(1); 82 } 83 const size_t smem = (size_t) threads_per_row * sizeof(float); 84 s2_gemv_quads_kernel<<<(unsigned) n_out, threads_per_row, smem>>>(x, codes, scales, y, n_in, n_out, 85 threads_per_row); 86 const cudaError_t e = cudaDeviceSynchronize(); 87 if (e != cudaSuccess) { 88 std::fprintf(stderr, "s2_gemv_quads: %s\n", cudaGetErrorString(e)); 89 std::exit(1); 90 } 91} 92 93} // namespace strata::kernels