okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6a74320ca
main
1// metal/kernels/mma.metal - a GEMV for windows of tokens on the simdgroup matrix units (after quant/units.metal). 2// 3// y[t][r] = W[r] . x[t] for NTOK (1..8) tokens: each simdgroup owns 8 rows; the K axis runs in chunks of 128. Per 4// chunk the simdgroup decodes its 8 x 128 weights - with ref_quant's arithmetic, so a tile element is exactly the 5// dequantized weight - into threadgroup memory, then 16 simdgroup_multiply_accumulate steps take A = the 8 x 8 weight 6// tile and B = the 8 (k) x 8 (token) slice of x, loaded transposed straight from device memory (x has 8 rows: the 7// tokens past the window are padding, and an output column depends only on its own token). 8// 9// Activations are the raw plane (no pre-scaling); n_in must be a multiple of 128. Every format's units sit inside 10// one 128-wide chunk: Q4_K/Q5_K units span 64 positions, Q6_K units 128, the others are contiguous. 11 12// ---- decoders to actual weights: w[UNIT] and the chunk offsets of its two halves (p0: w[0..H), p1: w[H..UNIT)) ---- 13template <int F> struct DW { 14 // generic: w = s * q (quant.metal's deq is ref_quant's order: the scale times the code), contiguous 15 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 16 threadgroup const uint* tab) { 17 constexpr int UNIT = fmt<F>::UNIT, UPB = fmt<F>::QK / UNIT; 18 float q[UNIT]; 19 const float s = deq<F>(row + (u / UPB) * fmt<F>::BB, u % UPB, q, tab); 20 for (int j = 0; j < UNIT; ++j) w[j] = q[j] * s; 21 p0 = u * UNIT - k0; 22 p1 = p0 + UNIT / 2; 23 } 24}; 25// Q2_0: (code - 1) * d 26template <> struct DW<T_Q2_0> { 27 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 28 threadgroup const uint*) { 29 device const uchar* b = row + (u >> 2) * 18; 30 device const ushort* p = (device const ushort*)(b + 2 + 4 * (u & 3)); 31 const ushort a = p[0], c = p[1]; 32 const float d = rdh(b); 33 for (int j = 0; j < 8; ++j) { 34 w[j] = float(int((a >> (2 * j)) & 3) - 1) * d; 35 w[8 + j] = float(int((c >> (2 * j)) & 3) - 1) * d; 36 } 37 p0 = 16 * u - k0; 38 p1 = p0 + 8; 39 } 40}; 41template <> struct DW<T_Q5_1> { 42 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 43 threadgroup const uint* tab) { 44 device const uchar* b = row + u * 24; 45 float q[32]; 46 const float d = deq<T_Q5_1>(b, 0, q, tab), m = rdh(b + 2); 47 for (int j = 0; j < 32; ++j) w[j] = q[j] * d + m; 48 p0 = 32 * u - k0; 49 p1 = p0 + 16; 50 } 51}; 52template <> struct DW<T_F32> { 53 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 54 threadgroup const uint*) { 55 device const float* p = (device const float*)(row + 64 * u); 56 for (int j = 0; j < 16; ++j) w[j] = p[j]; 57 p0 = 16 * u - k0; 58 p1 = p0 + 8; 59 } 60}; 61template <> struct DW<T_BF16> { 62 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 63 threadgroup const uint*) { 64 device const uint4* p = (device const uint4*)(row + 32 * u); 65 const uint4 a = p[0], c = p[1]; 66 const uint v[8] = {a.x, a.y, a.z, a.w, c.x, c.y, c.z, c.w}; 67 for (int k = 0; k < 8; ++k) { 68 w[2 * k] = as_type<float>(v[k] << 16); 69 w[2 * k + 1] = as_type<float>(v[k] & 0xFFFF0000u); 70 } 71 p0 = 16 * u - k0; 72 p1 = p0 + 8; 73 } 74}; 75// Q4_K / Q5_K: d1 * code - m1 (ref: d * sc, dmin * m), runs at 256b + 64c + 16m and + 32 76template <> struct DW<T_Q4_K> { 77 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 78 threadgroup const uint*) { 79 device const uchar* b = row + (u >> 3) * 144; 80 const int c = (u >> 1) & 3, m = u & 1; 81 const float d = rdh(b), dm = rdh(b + 2); 82 float s1, m1, s2, m2; 83 scale_min_k4(2 * c, b + 4, s1, m1); 84 scale_min_k4(2 * c + 1, b + 4, s2, m2); 85 const float d1 = d * s1, mm1 = dm * m1, d2 = d * s2, mm2 = dm * m2; 86 device const uchar* q = b + 16 + 32 * c + 16 * m; 87 for (int j = 0; j < 16; ++j) { 88 w[j] = d1 * float(q[j] & 15) - mm1; 89 w[16 + j] = d2 * float(q[j] >> 4) - mm2; 90 } 91 p0 = 256 * (u >> 3) + 64 * c + 16 * m - k0; 92 p1 = p0 + 32; 93 } 94}; 95template <> struct DW<T_Q5_K> { 96 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 97 threadgroup const uint*) { 98 device const uchar* b = row + (u >> 3) * 176; 99 const int c = (u >> 1) & 3, m = u & 1; 100 const float d = rdh(b), dm = rdh(b + 2); 101 float s1, m1, s2, m2; 102 scale_min_k4(2 * c, b + 4, s1, m1); 103 scale_min_k4(2 * c + 1, b + 4, s2, m2); 104 const float d1 = d * s1, mm1 = dm * m1, d2 = d * s2, mm2 = dm * m2; 105 device const uchar* ql = b + 48 + 32 * c + 16 * m; 106 device const uchar* qh = b + 16 + 16 * m; 107 const uchar u1 = uchar(1 << (2 * c)), u2 = uchar(2 << (2 * c)); 108 for (int j = 0; j < 16; ++j) { 109 w[j] = d1 * float((ql[j] & 15) + ((qh[j] & u1) ? 16 : 0)) - mm1; 110 w[16 + j] = d2 * float((ql[j] >> 4) + ((qh[j] & u2) ? 16 : 0)) - mm2; 111 } 112 p0 = 256 * (u >> 3) + 64 * c + 16 * m - k0; 113 p1 = p0 + 32; 114 } 115}; 116// Q6_K: d * sc * (code - 32), runs at 256b + 128n + 32p + 16m and + 64 117template <> struct DW<T_Q6_K> { 118 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 119 threadgroup const uint*) { 120 device const uchar* b = row + (u >> 3) * 210; 121 const int n = (u >> 2) & 1, m = (u >> 1) & 1, p = u & 1; 122 device const uchar* ql = b + 64 * n + 32 * p + 16 * m; 123 device const uchar* qh = b + 128 + 32 * n + 16 * m; 124 device const char* sc = (device const char*)(b + 192); 125 const float d = rdh(b + 208); 126 const float ds1 = d * float(sc[8 * n + m + 2 * p]), ds2 = d * float(sc[8 * n + m + 2 * p + 4]); 127 for (int j = 0; j < 16; ++j) { 128 const int c1 = int((ql[j] & 15) | (((qh[j] >> (2 * p)) & 3) << 4)) - 32; 129 const int c2 = int((ql[j] >> 4) | (((qh[j] >> (2 * p + 4)) & 3) << 4)) - 32; 130 w[j] = ds1 * float(c1); 131 w[16 + j] = ds2 * float(c2); 132 } 133 p0 = 256 * (u >> 3) + 128 * n + 32 * p + 16 * m - k0; 134 p1 = p0 + 64; 135 } 136}; 137// Q3_K: d_all * (sc - 32) * (code - (hbit ? 0 : 4)), contiguous 32 138template <> struct DW<T_Q3_K> { 139 static inline void run(device const uchar* row, int u, int k0, thread float* w, thread int& p0, thread int& p1, 140 threadgroup const uint*) { 141 device const uchar* b = row + (u >> 3) * 110; 142 const int n = (u >> 2) & 1, j = u & 3; 143 device const uchar* q = b + 32 + 32 * n; 144 device const uchar* hm = b; 145 device const uchar* s = b + 96; 146 const int i1 = 8 * n + 2 * j, i2 = i1 + 1; 147 const int l1 = (i1 < 8 ? s[i1] & 0xF : s[i1 - 8] >> 4) | (((s[8 + (i1 & 3)] >> (2 * (i1 >> 2))) & 3) << 4); 148 const int l2 = (i2 < 8 ? s[i2] & 0xF : s[i2 - 8] >> 4) | (((s[8 + (i2 & 3)] >> (2 * (i2 >> 2))) & 3) << 4); 149 const float d = rdh(b + 108); 150 const float dl1 = d * float(l1 - 32), dl2 = d * float(l2 - 32); 151 const uchar hb = uchar(1 << (4 * n + j)); 152 for (int l = 0; l < 16; ++l) { 153 w[l] = dl1 * float(int((q[l] >> (2 * j)) & 3) - ((hm[l] & hb) ? 0 : 4)); 154 w[16 + l] = dl2 * float(int((q[l + 16] >> (2 * j)) & 3) - ((hm[l + 16] & hb) ? 0 : 4)); 155 } 156 p0 = 32 * u - k0; 157 p1 = p0 + 16; 158 } 159}; 160 161struct MmaArgs { 162 uint n_in; 163 uint n_out; 164 uint row_bytes; 165 uint x_stride; // floats between the 8 token rows of x 166 uint y_stride; 167}; 168constant int MMA_NTOK [[function_constant(4)]]; 169 170template <int F, int SG> 171kernel void gemv_mma(device const uchar* W [[buffer(0)]], device const float* x [[buffer(1)]], 172 device float* y [[buffer(2)]], constant MmaArgs& a [[buffer(3)]], 173 uint3 tg [[threadgroup_position_in_grid]], ushort sg [[simdgroup_index_in_threadgroup]], 174 ushort lane [[thread_index_in_simdgroup]], ushort tid [[thread_index_in_threadgroup]]) { 175 constexpr int UNIT = fmt<F>::UNIT, UPC = 128 / UNIT, H = UNIT / 2; 176 threadgroup float wt[SG][8 * 128]; 177 threadgroup float ct[SG][64]; 178 threadgroup uint tab[fmt<F>::TAB]; 179 load_tab<F>(tab, tid, SG * 32); 180 threadgroup_barrier(mem_flags::mem_threadgroup); 181 const int row0 = (int(tg.x) * SG + sg) * 8; 182 simdgroup_float8x8 C = make_filled_simdgroup_matrix<float, 8, 8>(0.0f); 183 threadgroup float* w8 = wt[sg]; 184 for (int k0 = 0; k0 < int(a.n_in); k0 += 128) { 185 for (int it = lane; it < 8 * UPC; it += 32) { 186 const int r = it / UPC, uu = it - r * UPC; 187 const int rr = min(row0 + r, int(a.n_out) - 1); 188 float w[UNIT]; 189 int p0, p1; 190 DW<F>::run(W + ulong(rr) * a.row_bytes, k0 / UNIT + uu, k0, w, p0, p1, tab); 191 for (int j = 0; j < H; ++j) { 192 w8[r * 128 + p0 + j] = w[j]; 193 w8[r * 128 + p1 + j] = w[H + j]; 194 } 195 } 196 simdgroup_barrier(mem_flags::mem_threadgroup); 197 for (int k8 = 0; k8 < 128; k8 += 8) { 198 simdgroup_float8x8 A, B; 199 simdgroup_load(A, w8 + k8, 128); 200 simdgroup_load(B, x + k0 + k8, a.x_stride, ulong2(0, 0), true); 201 simdgroup_multiply_accumulate(C, A, B, C); 202 } 203 simdgroup_barrier(mem_flags::mem_threadgroup); 204 } 205 simdgroup_store(C, ct[sg], 8); 206 simdgroup_barrier(mem_flags::mem_threadgroup); 207 for (int e = lane; e < 64; e += 32) { 208 const int r = e >> 3, t = e & 7; 209 if (t < MMA_NTOK && row0 + r < int(a.n_out)) y[t * int(a.y_stride) + row0 + r] = ct[sg][e]; 210 } 211} 212 213#define MM(F, S) template [[host_name("gemv_mma_" #F "_" #S)]] kernel decltype(gemv_mma<F, S>) gemv_mma<F, S>; 214#define MM_SET(F) MM(F, 1) MM(F, 2) MM(F, 4) 215MM_SET(T_BF16) MM_SET(T_Q8_0) MM_SET(T_Q4_0) MM_SET(T_Q5_0) MM_SET(T_IQ4_NL) MM_SET(T_IQ4_XS) MM_SET(T_Q2_0) 216MM_SET(T_IQ3_S) MM_SET(T_Q3_K) MM_SET(T_Q4_K) MM_SET(T_Q5_K) MM_SET(T_Q6_K)