okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6ca60e9a0
main
1// src/kernels/cuda/nib_gu_proto.cuh - STRATA_NIB_GU_PROTO: a staging variant of nib_gu_kernel (STRATA_EXPERT_NIBBLE's 2// 4-bit gate/up), included by iq_kernels.cu inside its anonymous namespace after nib_gu_kernel. 3// 4// The kernel keeps the S skeleton's per-row arithmetic by calling nib_row_dot_s itself (16 lanes a row, lane t's items 5// k = t, t+32, t+64 into s and t+16, t+48 into s16, NibLoad<TG>::load then Split<TG>::apply on the staged block_q8_1, 6// __fadd_rn(s, s16), xor 8, 4, 2, 1). Only the staging and the block -> (group, rows) mapping change: 7// - a block takes RB row blocks (16 rows each) of one group: grid (80 / RB, gy). The group's entries are staged 8// once and the RB row blocks run one after another on them; 9// - the staging reads the entries' q8_1 rows (2,880 B = 180 x 16 B each, 16-aligned when xq is) as 16-byte loads, 10// every load issued before the first store, so only the stores sit between the two barriers. The S skeleton's 11// word loop (nib_pass_s) has a serial load -> store chain there that stalls all eight warps of the block. 12// The outputs are written without nib_pass_s's dynamically indexed s[t] (a select on t), the same values. 13// Id 31 = RB 2, the one STRATA_NIB_GU_PROTO=1 selects: one GP100 die (PH402), T=1, gate/up alone, IQ2_S / IQ3_XXS / 14// IQ3_S x1.25-1.27 against the S / P skeleton, every gate and up row bitwise equal (iq_nibble_parity (7), 15// native_expert_bench --nibble --vs 31). 16#pragma once 17 18// The n (1..GRP_NC) entries from e into s_x (block_q8_1 rows of 80). Block-uniform (e, n): every thread reaches both 19// barriers. 16-byte loads, all in flight before the first store. 20__device__ __forceinline__ void nibx_stage(block_q8_1* s_x, const int32_t* __restrict__ ent_tok, 21 const block_q8_1* __restrict__ xq, int e, int n) { 22 constexpr int kV = (GRP_NC * 180 + 255) / 256; // 3 23 const int nv = n * 180; 24 uint4 v[kV]; 25#pragma unroll 26 for (int j = 0; j < kV; ++j) { 27 const int i = threadIdx.x + 256 * j; 28 if (i < nv) { 29 const int c = i >= 540 ? 3 : i >= 360 ? 2 : i >= 180 ? 1 : 0; 30 v[j] = ((const uint4*) (xq + (size_t) ent_tok[e + c] * 80))[i - 180 * c]; 31 } 32 } 33 __syncthreads(); 34#pragma unroll 35 for (int j = 0; j < kV; ++j) { 36 const int i = threadIdx.x + 256 * j; 37 if (i < nv) ((uint4*) s_x)[i] = v[j]; 38 } 39 __syncthreads(); 40} 41 42// nib_pass_s's compute on the staged entries (nib_row_dot_s, the production function), stored without the dynamically 43// indexed s[t] 44template<int TG> 45__device__ __forceinline__ void nibx_run(const uint8_t* slot, int row, const block_q8_1* s_x, int e, int n, int t, 46 float* dst, int r) { 47 const uint4* crow = (const uint4*) (slot + nibk::kCodesOff) + (size_t) row * nibk::kItems; 48 const uint8_t* srow = slot + nibk::kScOff + (size_t) row * nibk::kItems; 49 const __half* drow = (const __half*) (slot + nibk::kDOff) + (size_t) row * nibk::kSuper; 50 if (n == 1) { 51 const int o[1] = { 0 }; float s[1]; 52 nib_row_dot_s<TG, 1>(crow, srow, drow, s_x, o, t, s); 53 if (t == 0) dst[(size_t) e * nibk::kNFf + r] = s[0]; 54 } else if (n == 2) { 55 const int o[2] = { 0, 80 }; float s[2]; 56 nib_row_dot_s<TG, 2>(crow, srow, drow, s_x, o, t, s); 57 if (t < 2) dst[(size_t) (e + t) * nibk::kNFf + r] = t == 0 ? s[0] : s[1]; 58 } else if (n == 3) { 59 const int o[3] = { 0, 80, 160 }; float s[3]; 60 nib_row_dot_s<TG, 3>(crow, srow, drow, s_x, o, t, s); 61 if (t < 3) dst[(size_t) (e + t) * nibk::kNFf + r] = t == 0 ? s[0] : t == 1 ? s[1] : s[2]; 62 } else { 63 const int o[4] = { 0, 80, 160, 240 }; float s[4]; 64 nib_row_dot_s<TG, 4>(crow, srow, drow, s_x, o, t, s); 65 if (t < 4) dst[(size_t) (e + t) * nibk::kNFf + r] = t == 0 ? s[0] : t == 1 ? s[1] : t == 2 ? s[2] : s[3]; 66 } 67} 68 69template<int TG, int RB> 70__global__ void __launch_bounds__(256) nibx_gu_kernel(const unsigned long long* __restrict__ grp_ptr, 71 const int32_t* __restrict__ grp_start, 72 const int32_t* __restrict__ n_groups, 73 const int32_t* __restrict__ ent_tok, 74 const block_q8_1* __restrict__ xq, float* __restrict__ gate, 75 float* __restrict__ up) { 76 static_assert(80 % RB == 0, "row blocks per block divide 80"); 77 const int ng = *n_groups; 78 if (blockIdx.y >= (unsigned) ng) return; 79 STRATA_SHARED_ALIGN16_U32(s_buf, kNibSWords); 80 block_q8_1* s_x = (block_q8_1*) s_buf; 81 const int subwarp = threadIdx.x >> 4, t = threadIdx.x & 15; 82 const int rb0 = blockIdx.x * RB; 83 for (int g = blockIdx.y; g < ng; g += gridDim.y) { 84 const uint8_t* slot = (const uint8_t*) grp_ptr[g]; 85 const int e0 = grp_start[g], e1 = grp_start[g + 1]; 86 const int nt = e1 - e0; 87 if (nt <= 0) continue; // an empty group: nothing to write (block-uniform, no barrier skipped unevenly) 88 if (nt <= GRP_NC) { 89 nibx_stage(s_x, ent_tok, xq, e0, nt); 90#pragma unroll 1 91 for (int j = 0; j < RB; ++j) { 92 const int row = (rb0 + j) * 16 + subwarp; 93 const bool is_up = row >= nibk::kNFf; 94 nibx_run<TG>(slot, row, s_x, e0, nt, t, is_up ? up : gate, is_up ? row - (int) nibk::kNFf : row); 95 } 96 } else { // more entries than one pass holds: per row block, the passes as nib_pass_s runs them 97#pragma unroll 1 98 for (int j = 0; j < RB; ++j) { 99 const int row = (rb0 + j) * 16 + subwarp; 100 const bool is_up = row >= nibk::kNFf; 101 for (int e = e0; e < e1; e += GRP_NC) { 102 const int n = min(GRP_NC, e1 - e); 103 nibx_stage(s_x, ent_tok, xq, e, n); 104 nibx_run<TG>(slot, row, s_x, e, n, t, is_up ? up : gate, is_up ? row - (int) nibk::kNFf : row); 105 } 106 } 107 } 108 } 109} 110 111int g_nibx_occ = 0; // the last attribute query's resident blocks per SM 112 113template<int TG, int RB> 114void nibx_launch_one(dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, const int32_t* grp_start, 115 const int32_t* n_groups, const int32_t* ent_tok, const block_q8_1* X, float* gate, float* up, 116 cudaFuncAttributes* attr) { 117 if (attr) { 118 cudaFuncGetAttributes(attr, nibx_gu_kernel<TG, RB>); 119 cudaOccupancyMaxActiveBlocksPerMultiprocessor(&g_nibx_occ, nibx_gu_kernel<TG, RB>, 256, 0); 120 return; 121 } 122 nibx_gu_kernel<TG, RB><<<dim3(80 / RB, grid.y), 256, 0, s>>>(grp_ptr, grp_start, n_groups, ent_tok, X, gate, up); 123} 124 125// false: not the gu-proto id (31) or not a converted format. attr != nullptr: only the kernel's attributes, nothing 126// launched. 127bool nibx_launch(int gu_type, int id, dim3 grid, cudaStream_t s, const unsigned long long* grp_ptr, 128 const int32_t* grp_start, const int32_t* n_groups, const int32_t* ent_tok, const block_q8_1* X, 129 float* gate, float* up, cudaFuncAttributes* attr = nullptr) { 130 if (id != 31) return false; 131 switch (gu_type) { 132 case 22: nibx_launch_one<22, 2>(grid, s, grp_ptr, grp_start, n_groups, ent_tok, X, gate, up, attr); return true; 133 case 18: nibx_launch_one<18, 2>(grid, s, grp_ptr, grp_start, n_groups, ent_tok, X, gate, up, attr); return true; 134 case 21: nibx_launch_one<21, 2>(grid, s, grp_ptr, grp_start, n_groups, ent_tok, X, gate, up, attr); return true; 135 default: return false; 136 } 137}