okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3a76d5eaf
main
1// metal/kernels/gdn_tprobe.metal - go / no-go probe for a tensor-op (MPP matmul2d) chunk-state kernel: the five products of one GDN 2// chunk step (W S, Q S, M delta, K^T delta' accumulated into S) on fp16 operands, S kept in fp32 threadgroup memory, 64 chunk steps 3// a threadgroup, grid h_v * 4 (the shape of gdn_cstate2). No numerics: timing only. 4#include <MetalPerformancePrimitives/MetalPerformancePrimitives.h> 5using namespace metal; 6using namespace mpp::tensor_ops; 7 8constant int PROBE [[function_constant(0)]]; 9constant int probe = is_function_constant_defined(PROBE) ? PROBE : 0; // 1 = skip the products (SIMT parts only) 10 11kernel void gdn_tprobe(device float* out [[buffer(0)]], device half* Wh [[buffer(1)]], device half* Qh [[buffer(2)]], 12 device half* Kh [[buffer(3)]], device half* Mh [[buffer(4)]], constant int& NC [[buffer(5)]], 13 uint3 tg [[threadgroup_position_in_grid]], ushort tid [[thread_index_in_threadgroup]]) { 14 threadgroup half Sh[128 * 32]; 15 threadgroup half Dh[32 * 32]; 16 threadgroup float X[32 * 32], Y[32 * 32], Z[32 * 32]; 17 tensor<threadgroup half, dextents<int32_t, 2>, tensor_inline> tSh(Sh, dextents<int32_t, 2>(32, 128)); 18 tensor<threadgroup half, dextents<int32_t, 2>, tensor_inline> tDh(Dh, dextents<int32_t, 2>(32, 32)); 19 tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline> tX(X, dextents<int32_t, 2>(32, 32)); 20 tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline> tY(Y, dextents<int32_t, 2>(32, 32)); 21 tensor<threadgroup float, dextents<int32_t, 2>, tensor_inline> tZ(Z, dextents<int32_t, 2>(32, 32)); 22 constexpr auto d_ws = matmul2d_descriptor(32, 32, 128, false, false, false); // [32 x 128] . [128 x 32] 23 constexpr auto d_md = matmul2d_descriptor(32, 32, 32, false, false, false); // [32 x 32] . [32 x 32] 24 constexpr auto d_up = matmul2d_descriptor(128, 32, 32, true, false, false, matmul2d_descriptor::mode::multiply_accumulate); // K^T delta' 25 matmul2d<d_ws, execution_simdgroups<4>> op_ws; 26 matmul2d<d_md, execution_simdgroups<4>> op_md; 27 matmul2d<d_up, execution_simdgroups<4>> op_up; 28 const ulong hb = ulong(tg.x) / 4; 29 using KT = tensor<device half, dextents<int32_t, 2>, tensor_inline>; 30 using DT = tensor<threadgroup half, dextents<int32_t, 2>, tensor_inline>; 31 auto cS = op_up.get_destination_cooperative_tensor<KT, DT, float>(); 32#pragma clang loop unroll(full) 33 for (uint16_t i = 0; i < cS.get_capacity(); ++i) 34 if (cS.is_valid_element(i)) cS[i] = 0.001f * float(i & 15); 35 for (int c = 0; c < NC; ++c) { 36 const ulong cb = (hb * NC + c); 37#pragma clang loop unroll(full) 38 for (uint16_t i = 0; i < cS.get_capacity(); ++i) 39 if (cS.is_valid_element(i)) { 40 auto ids = cS.get_multidimensional_index(i); 41 Sh[ids[1] * 32 + ids[0]] = half(cS[i]); 42 } 43 threadgroup_barrier(mem_flags::mem_threadgroup); 44 if (probe != 1) { 45 tensor<device half, dextents<int32_t, 2>, tensor_inline> tW(Wh + cb * 32 * 128, dextents<int32_t, 2>(128, 32)); 46 tensor<device half, dextents<int32_t, 2>, tensor_inline> tQ(Qh + cb * 32 * 128, dextents<int32_t, 2>(128, 32)); 47 op_ws.run(tW, tSh, tX); 48 op_ws.run(tQ, tSh, tY); 49 } 50 threadgroup_barrier(mem_flags::mem_threadgroup); 51 for (int i = tid; i < 32 * 32; i += 128) Dh[i] = half(0.01f * X[i]); 52 threadgroup_barrier(mem_flags::mem_threadgroup); 53 if (probe != 1) { 54 tensor<device half, dextents<int32_t, 2>, tensor_inline> tM(Mh + cb * 32 * 32, dextents<int32_t, 2>(32, 32)); 55 op_md.run(tM, tDh, tZ); 56 } 57 threadgroup_barrier(mem_flags::mem_threadgroup); 58 for (int i = tid; i < 32 * 32; i += 128) Dh[i] = half(Z[i] + 0.01f * Y[i]); 59#pragma clang loop unroll(full) 60 for (uint16_t i = 0; i < cS.get_capacity(); ++i) 61 if (cS.is_valid_element(i)) cS[i] *= 0.99f; 62 threadgroup_barrier(mem_flags::mem_threadgroup); 63 if (probe != 1) { 64 tensor<device half, dextents<int32_t, 2>, tensor_inline> tK(Kh + cb * 32 * 128, dextents<int32_t, 2>(128, 32)); 65 op_up.run(tK, tDh, cS); 66 } 67 threadgroup_barrier(mem_flags::mem_threadgroup); 68 } 69#pragma clang loop unroll(full) 70 for (uint16_t i = 0; i < cS.get_capacity(); ++i) 71 if (cS.is_valid_element(i)) { 72 auto ids = cS.get_multidimensional_index(i); 73 out[ulong(tg.x) * 4096 + ids[1] * 32 + ids[0]] = cS[i]; 74 } 75}