okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0116b0049
main
1// metal/kernels/sample.metal - greedy sampling over the LM head's logits: the largest logit, ties to the lowest 2// token id (NaN never wins). Two stages: argmax_part (grid (parts, n_tok), 256 threads) writes one (value, id) 3// pair per threadgroup; argmax_final (grid (n_tok), 256 threads) reduces them in order. 4 5struct ArgArgs { 6 uint n_vocab; 7 uint parts; 8}; 9 10static inline void better(thread float& bv, thread int& bi, float v, int i) { 11 if (v > bv || (v == bv && i < bi)) { bv = v; bi = i; } 12} 13 14static inline void tg_argmax(thread float& bv, thread int& bi, threadgroup float* sv, threadgroup int* si, ushort sg, 15 ushort lane) { 16 for (ushort m = 16; m > 0; m >>= 1) { 17 const float ov = simd_shuffle_xor(bv, m); 18 const int oi = simd_shuffle_xor(bi, m); 19 better(bv, bi, ov, oi); 20 } 21 if (lane == 0) { sv[sg] = bv; si[sg] = bi; } 22 threadgroup_barrier(mem_flags::mem_threadgroup); 23 bv = -INFINITY; 24 bi = 0x7fffffff; 25 for (int k = 0; k < 8; ++k) better(bv, bi, sv[k], si[k]); 26} 27 28kernel void argmax_part(device const float* logits [[buffer(0)]], device float* pv [[buffer(1)]], 29 device int* pi [[buffer(2)]], constant ArgArgs& a [[buffer(3)]], 30 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 31 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 32 threadgroup float sv[8]; 33 threadgroup int si[8]; 34 const uint t = tg.y, per = (a.n_vocab + a.parts - 1) / a.parts; 35 const uint b0 = tg.x * per, b1 = min(b0 + per, a.n_vocab); 36 device const float* l = logits + t * a.n_vocab; 37 float bv = -INFINITY; 38 int bi = 0x7fffffff; 39 for (uint i = b0 + tid; i < b1; i += 256) { 40 const float v = l[i]; 41 if (!isnan(v)) better(bv, bi, v, int(i)); 42 } 43 tg_argmax(bv, bi, sv, si, sg, lane); 44 if (tid == 0) { pv[t * a.parts + tg.x] = bv; pi[t * a.parts + tg.x] = bi; } 45} 46 47kernel void argmax_final(device const float* pv [[buffer(0)]], device const int* pi [[buffer(1)]], 48 device int* out [[buffer(2)]], constant ArgArgs& a [[buffer(3)]], 49 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 50 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 51 threadgroup float sv[8]; 52 threadgroup int si[8]; 53 const uint t = tg.x; 54 float bv = -INFINITY; 55 int bi = 0x7fffffff; 56 for (uint k = tid; k < a.parts; k += 256) better(bv, bi, pv[t * a.parts + k], pi[t * a.parts + k]); 57 tg_argmax(bv, bi, sv, si, sg, lane); 58 if (tid == 0) out[t] = bi; 59} 60 61// --------------------------------------------------------------------------------------------------------------- 62// The sampler chain (llama.cpp's order, as src/kernels/cuda/sampler.cu `sampler_kernel`, the reference): 63// logit bias -> penalties -> top_k -> top_p -> min_p -> temperature -> one Philox draw keyed (seed, position) 64// Two stages. samp_part: grid (parts, rows), 256 threads: a part's slice of the vocabulary (<= 4096 logits) goes to threadgroup 65// memory with the bias and the penalties applied, and k rounds of a block argmax (ties to the lowest id; NaN and -inf never win) 66// leave that part's k best. samp_pick: grid (rows): the parts' k lists are merged by k more rounds (the same order), then one 67// thread runs the chain over the k kept candidates and writes the token. Metal has no double: the sums and the cumulative 68// probabilities are float (the CUDA reference uses double); the draw is the same Philox 4x32-10 value as CUDA's for equal 69// (seed, counter). A row's counter is the position of the token it samples, so the text does not depend on how it was cut 70// into windows. greedy (flag 2): the first candidate of the penalised / biased logits. 71 72constant uint SF_BIAS = 1u, SF_GREEDY = 2u, SF_PEN = 4u; 73 74struct SampArgs { 75 uint n_vocab, parts, k, hist_len, pen_n, min_keep, flags; 76 float top_p, min_p, inv_t, rep, freq, pres; 77 uint seed_lo, seed_hi, ctr_lo, ctr_hi; 78}; 79 80static inline float philox_u(ulong seed, ulong ctr) { 81 uint c0 = uint(ctr), c1 = uint(ctr >> 32), c2 = uint(seed), c3 = uint(seed >> 32); 82 for (uint i = 0; i < 10; ++i) { 83 const uint hi0 = mulhi(0x9E3779B9u, c0), hi1 = mulhi(0xBB67AE85u, c2); 84 const uint lo0 = 0x9E3779B9u * c0, lo1 = 0xBB67AE85u * c2; 85 const uint n0 = hi1 ^ c1 ^ i, n1 = lo1, n2 = hi0 ^ c3, n3 = lo0; 86 c0 = n0; c1 = n1; c2 = n2; c3 = n3; 87 } 88 return float(c0 >> 8) * (1.0f / 16777216.0f); 89} 90 91kernel void samp_part(device const float* logits [[buffer(0)]], device const float* bias [[buffer(1)]], device const int* hist [[buffer(2)]], 92 device float* cv [[buffer(3)]], device int* ci [[buffer(4)]], constant SampArgs& a [[buffer(5)]], 93 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]], 94 ushort sg [[simdgroup_index_in_threadgroup]], ushort lane [[thread_index_in_simdgroup]]) { 95 threadgroup float sv[4096]; 96 threadgroup float rv[8]; 97 threadgroup int ri[8]; 98 const uint row = tg.y, part = tg.x; 99 const uint per = (a.n_vocab + a.parts - 1) / a.parts; 100 const uint b0 = part * per, b1 = min(b0 + per, a.n_vocab); 101 const uint cnt = b1 > b0 ? b1 - b0 : 0; 102 device const float* l = logits + row * a.n_vocab; 103 device const int* h = hist + row * a.hist_len + (a.hist_len - a.pen_n); // the penalty window is the row's last pen_n tokens 104 for (uint i = tid; i < 4096; i += 256) { 105 float s = -INFINITY; 106 if (i < cnt) { 107 const uint v = b0 + i; 108 s = l[v]; 109 if (a.flags & SF_BIAS) s += bias[v]; 110 if (a.flags & SF_PEN) { 111 int c = 0; 112 for (uint j = 0; j < a.pen_n; ++j) c += (h[j] == int(v)) ? 1 : 0; 113 if (c > 0) { 114 if (s <= 0.0f) s *= a.rep; else s /= a.rep; 115 s -= float(c) * a.freq + a.pres; 116 } 117 } 118 if (isnan(s)) s = -INFINITY; 119 } 120 sv[i] = s; 121 } 122 threadgroup_barrier(mem_flags::mem_threadgroup); 123 device float* ov = cv + (row * a.parts + part) * a.k; 124 device int* oi = ci + (row * a.parts + part) * a.k; 125 for (uint r = 0; r < a.k; ++r) { 126 float bv = -INFINITY; 127 int bi = 0x7fffffff; 128 for (uint i = tid; i < cnt; i += 256) { 129 const float s = sv[i]; 130 if (s > bv) { bv = s; bi = int(i); } 131 } 132 tg_argmax(bv, bi, rv, ri, sg, lane); 133 if (tid == 0) { 134 ov[r] = bv; 135 oi[r] = bi < int(cnt) ? int(b0) + bi : 0x7fffffff; 136 if (bi < int(cnt)) sv[bi] = -INFINITY; 137 } 138 threadgroup_barrier(mem_flags::mem_threadgroup); 139 } 140} 141 142kernel void samp_pick(device const float* cv [[buffer(0)]], device const int* ci [[buffer(1)]], device int* out [[buffer(2)]], 143 constant SampArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]], 144 ushort tid [[thread_index_in_threadgroup]], ushort sg [[simdgroup_index_in_threadgroup]], 145 ushort lane [[thread_index_in_simdgroup]]) { 146 threadgroup float cvs[4096]; 147 threadgroup float rv[8]; 148 threadgroup int ri[8]; 149 threadgroup int sel_id[64]; 150 threadgroup float sel_lg[64]; 151 const uint row = tg.x, n = a.parts * a.k; 152 device const int* cid = ci + row * n; // the candidates' ids stay in device memory (the threadgroup holds the values only) 153 for (uint i = tid; i < n; i += 256) cvs[i] = cv[row * n + i]; 154 threadgroup_barrier(mem_flags::mem_threadgroup); 155 for (uint r = 0; r < a.k; ++r) { 156 // 1. the largest value left 157 float bv = -INFINITY; 158 int bi = 0x7fffffff; 159 for (uint i = tid; i < n; i += 256) if (cvs[i] > bv) { bv = cvs[i]; bi = int(i); } 160 tg_argmax(bv, bi, rv, ri, sg, lane); 161 // 2. among the candidates holding it, the lowest id (the selection order: value descending, ties to the lowest id) 162 int mn = 0x7fffffff; 163 if (bv > -INFINITY) 164 for (uint i = tid; i < n; i += 256) if (cvs[i] == bv) mn = min(mn, cid[i]); 165 mn = simd_min(mn); 166 threadgroup_barrier(mem_flags::mem_threadgroup); 167 if (lane == 0) ri[sg] = mn; 168 threadgroup_barrier(mem_flags::mem_threadgroup); 169 for (int k = 0; k < 8; ++k) mn = min(mn, ri[k]); 170 if (bv > -INFINITY) 171 for (uint i = tid; i < n; i += 256) if (cvs[i] == bv && cid[i] == mn) cvs[i] = -INFINITY; 172 if (tid == 0) { sel_id[r] = (bv > -INFINITY && mn < int(a.n_vocab)) ? mn : 0; sel_lg[r] = bv; } 173 threadgroup_barrier(mem_flags::mem_threadgroup); 174 } 175 if (tid != 0) return; 176 const uint k = a.k; 177 int pick = sel_id[0]; 178 if (!(a.flags & SF_GREEDY)) { 179 uint n_keep = k; 180 float mx = sel_lg[0]; 181 for (uint i = 1; i < k; ++i) mx = max(mx, sel_lg[i]); 182 if (a.top_p < 1.0f) { 183 float sum = 0.0f; 184 for (uint i = 0; i < k; ++i) sum += exp(sel_lg[i] - mx); 185 float cum = 0.0f; 186 uint cut = k; 187 for (uint i = 0; i < k; ++i) { 188 cum += exp(sel_lg[i] - mx) / sum; 189 if (cum >= a.top_p) { cut = i + 1; break; } 190 } 191 if (cut < a.min_keep) cut = a.min_keep < k ? a.min_keep : k; 192 n_keep = cut; 193 } 194 if (a.min_p > 0.0f) { 195 const float thresh = sel_lg[0] + log(a.min_p); 196 for (uint i = 0; i < n_keep; ++i) 197 if (sel_lg[i] < thresh) { n_keep = i; break; } 198 } 199 float smx = sel_lg[0] * a.inv_t; 200 for (uint i = 1; i < n_keep; ++i) smx = max(smx, sel_lg[i] * a.inv_t); 201 float sum = 0.0f; 202 for (uint i = 0; i < n_keep; ++i) sum += exp(sel_lg[i] * a.inv_t - smx); 203 pick = sel_id[n_keep - 1]; 204 const float u = philox_u(ulong(a.seed_lo) | (ulong(a.seed_hi) << 32), (ulong(a.ctr_lo) | (ulong(a.ctr_hi) << 32)) + ulong(row)); 205 float cum = 0.0f; 206 for (uint i = 0; i < n_keep; ++i) { 207 cum += exp(sel_lg[i] * a.inv_t - smx) / sum; 208 if (u < cum) { pick = sel_id[i]; break; } 209 } 210 } 211 out[row] = pick; 212}