okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3a76d5eaf
main
1// metal/kernels/dense_mm.metal - dense projections for a PROMPT chunk (after quant/units/mma.metal): 2// act_f16 X[t][k] -> half (the matrix units' operand) 3// dq_f16<F> a quantized matrix -> half rows (ref_quant's values rounded to fp16), once per chunk 4// gemm_f16 Y[t][n] = Xh[t] . Wh[n] (fp32) by MetalPerformancePrimitives matmul2d, TM x TN tiles, K dynamic; 5// rows of Y past n_out / t past n_tok are not written (the tensors carry the extents) 6#include <MetalPerformancePrimitives/MetalPerformancePrimitives.h> 7using namespace mpp::tensor_ops; 8 9kernel void act_f16(device const float* x [[buffer(0)]], device half* xh [[buffer(1)]], constant uint& n [[buffer(2)]], 10 uint gid [[thread_position_in_grid]]) { 11 if (gid < n) xh[gid] = half(x[gid]); 12} 13 14struct DqArgs { 15 uint n_in; // values per row 16 uint n_out; // rows 17 uint row_bytes; 18 uint perm; // 0: row r -> r; p > 0: row r = c * (n_out / p) + d -> d * p + c (the HC up matrix with the p 19 // streams of an output interleaved, for gemm_hc_up's epilogue) 20}; 21 22// one thread per unit: grid (units per row / 64, n_out), 64 threads 23template <int F> 24kernel void dq_f16(device const uchar* W [[buffer(0)]], device half* Wh [[buffer(1)]], constant DqArgs& a [[buffer(2)]], 25 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) { 26 constexpr int UNIT = fmt<F>::UNIT, H = UNIT / 2; 27 threadgroup uint tab[fmt<F>::TAB]; 28 load_tab<F>(tab, tid, 64); 29 threadgroup_barrier(mem_flags::mem_threadgroup); 30 const int u = int(tg.x) * 64 + tid, r = int(tg.y); 31 if (u * UNIT >= int(a.n_in)) return; 32 // the unit's chunk origin: a 128-wide chunk holds whole units of every format 33 const int k0 = (u * UNIT) / 128 * 128; 34 float w[UNIT]; 35 int p0, p1; 36 DW<F>::run(W + ulong(r) * a.row_bytes, u, k0, w, p0, p1, tab); 37 const int q = a.perm ? int(a.n_out / a.perm) : 1; 38 const int ro = a.perm ? (r % q) * int(a.perm) + r / q : r; 39 device half* o = Wh + ulong(ro) * a.n_in + k0; 40 for (int j = 0; j < H; ++j) { 41 o[p0 + j] = half(w[j]); 42 o[p1 + j] = half(w[H + j]); 43 } 44} 45 46struct GemmArgs { int M, N, K; int pad; }; // Y is M rows of N floats 47 48template <int TM, int TN, int SG> 49kernel void gemm_f16(device half* A [[buffer(0)]], device half* B [[buffer(1)]], device float* Y [[buffer(2)]], 50 constant GemmArgs& a [[buffer(3)]], uint3 tg [[threadgroup_position_in_grid]]) { 51 tensor<device half, dextents<int32_t, 2>, tensor_inline> tA(A, dextents<int32_t, 2>(a.K, a.M)); 52 tensor<device half, dextents<int32_t, 2>, tensor_inline> tB(B, dextents<int32_t, 2>(a.K, a.N)); 53 tensor<device float, dextents<int32_t, 2>, tensor_inline> tY(Y, dextents<int32_t, 2>(a.N, a.M)); 54 constexpr auto desc = matmul2d_descriptor(TM, TN, dynamic_length_v<int>, false, true, false); 55 matmul2d<desc, execution_simdgroups<SG>> op; 56 auto mA = tA.slice(0, int(tg.y) * TM); 57 auto mB = tB.slice(0, int(tg.x) * TN); 58 auto mY = tY.slice(int(tg.x) * TN, int(tg.y) * TM); 59 op.run(mA, mB, mY); 60} 61 62#define DQ(F) template [[host_name("dq_f16_" #F)]] kernel decltype(dq_f16<F>) dq_f16<F>; 63#define DQ_(F) DQ(F) 64DQ_(T_F32) DQ_(T_BF16) DQ_(T_Q8_0) DQ_(T_Q4_0) DQ_(T_Q5_0) DQ_(T_IQ4_NL) DQ_(T_IQ4_XS) DQ_(T_Q2_0) DQ_(T_IQ3_S) DQ_(T_Q3_K) 65DQ_(T_Q4_K) DQ_(T_Q5_K) DQ_(T_Q6_K) 66template [[host_name("gemm_f16_64_64_4")]] kernel decltype(gemm_f16<64, 64, 4>) gemm_f16<64, 64, 4>; 67template [[host_name("gemm_f16_128_64_4")]] kernel decltype(gemm_f16<128, 64, 4>) gemm_f16<128, 64, 4>; 68template [[host_name("gemm_f16_64_128_4")]] kernel decltype(gemm_f16<64, 128, 4>) gemm_f16<64, 128, 4>; 69template [[host_name("gemm_f16_64_32_4")]] kernel decltype(gemm_f16<64, 32, 4>) gemm_f16<64, 32, 4>; 70template [[host_name("gemm_f16_128_128_4")]] kernel decltype(gemm_f16<128, 128, 4>) gemm_f16<128, 128, 4>; 71template [[host_name("gemm_f16_128_128_8")]] kernel decltype(gemm_f16<128, 128, 8>) gemm_f16<128, 128, 8>; 72template [[host_name("gemm_f16_128_64_8")]] kernel decltype(gemm_f16<128, 64, 8>) gemm_f16<128, 64, 8>; 73template [[host_name("gemm_f16_64_64_8")]] kernel decltype(gemm_f16<64, 64, 8>) gemm_f16<64, 64, 8>; 74template [[host_name("gemm_f16_256_64_8")]] kernel decltype(gemm_f16<256, 64, 8>) gemm_f16<256, 64, 8>; 75template [[host_name("gemm_f16_64_256_8")]] kernel decltype(gemm_f16<64, 256, 8>) gemm_f16<64, 256, 8>; 76template [[host_name("gemm_f16_128_32_4")]] kernel decltype(gemm_f16<128, 32, 4>) gemm_f16<128, 32, 4>; 77template [[host_name("gemm_f16_32_128_4")]] kernel decltype(gemm_f16<32, 128, 4>) gemm_f16<32, 128, 4>;