diff --git a/engine/src/ling32/metal/ling32_core.metal b/engine/src/ling32/metal/ling32_core.metal index a8cf81db7c..e5177f641b 100644 --- a/engine/src/ling32/metal/ling32_core.metal +++ b/engine/src/ling32/metal/ling32_core.metal @@ -591,6 +591,33 @@ kernel void mla_qabs(device const float* q [[buffer(0)]], device const ushort* w for (int d = 0; d < L32_NOPE; ++d) acc += qs[d] * bf16f_(wk[((ulong)h * L32_NOPE + d) * L32_KVL + k]); qabs[(ulong)th * L32_KVL + k] = acc; } +// the same for decode / verify rows (N <= 4): one simdgroup per (row x head, tile of 128 latent dims), 4 consecutive latent dims per lane (one 8-byte load per weight row), the weight loads of 8 rows in flight; +// every output dimension keeps its own sequential sum over the 128 nope dims: bit-identical. grid (N*H, KVL/128), 32 threads +kernel void mla_qabs_d(device const float* q [[buffer(0)]], device const ushort* wk [[buffer(1)]], device float* qabs [[buffer(2)]], + device const int* d_pos0 [[buffer(3)]], constant int& absmin [[buffer(4)]], constant int& Nq [[buffer(5)]], + uint2 gid [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) { + if (*d_pos0 + Nq <= absmin) return; + const int th = int(gid.x), h = th % L32_H; + threadgroup float qs[L32_NOPE]; + for (int d = int(tid); d < L32_NOPE; d += 32) qs[d] = q[(ulong)th * (L32_NOPE + L32_ROPE) + d]; + threadgroup_barrier(mem_flags::mem_threadgroup); + const int k = int(gid.y) * 128 + int(tid) * 4; + float4 acc = float4(0.0f); + for (int d0 = 0; d0 < L32_NOPE; d0 += 8) { + ushort4 w[8]; +#pragma unroll + for (int u = 0; u < 8; ++u) w[u] = *(device const ushort4*)(wk + ((ulong)h * L32_NOPE + d0 + u) * L32_KVL + k); +#pragma unroll + for (int u = 0; u < 8; ++u) { + const float qv = qs[d0 + u]; + acc.x += qv * bf16f_(w[u].x); + acc.y += qv * bf16f_(w[u].y); + acc.z += qv * bf16f_(w[u].z); + acc.w += qv * bf16f_(w[u].w); + } + } + *(device float4*)(qabs + (ulong)th * L32_KVL + k) = acc; +} // combine the splits: ctx[th][k] = sum_s acc_s[k] e^(m_s - M) / sum_s l_s e^(m_s - M). grid N*H, 256 threads kernel void mla_attn_combine(device const float* part [[buffer(0)]], device float* ctx [[buffer(1)]], constant int& S [[buffer(2)]], device const int* d_pos0 [[buffer(3)]], constant int& reg_lo [[buffer(4)]], constant int& reg_hi [[buffer(5)]], constant int& kc [[buffer(6)]], uint th [[threadgroup_position_in_grid]], uint tid [[thread_index_in_threadgroup]]) { diff --git a/engine/src/ling32/metal/ling32_metal_main.cpp b/engine/src/ling32/metal/ling32_metal_main.cpp index cc5995f91c..607f018c2d 100644 --- a/engine/src/ling32/metal/ling32_metal_main.cpp +++ b/engine/src/ling32/metal/ling32_metal_main.cpp @@ -604,7 +604,9 @@ struct Engine { NB(t, N); t.add("mla_prep_q", {B(R(q)), B(R(rope_tab)), B(posr.b ? posr : R(pos))}, N, H, 1, 32); NB(t, N); - t.add("mla_qabs", {B(R(q)), B(ar(M.wk_b)), B(R(qabs)), B(posref), I(absmin), I(N)}, N * H, c.kv_lora / 128, 1, 128); + static const bool qabs_d = !std::getenv("L32_QABS_D") || std::atoi(std::getenv("L32_QABS_D")) != 0; // decode / verify rows: one simdgroup per tile, 8-byte weight loads (bit-identical); L32_QABS_D=0: the plain kernel + if (qabs_d && N <= 4) t.add("mla_qabs_d", {B(R(q)), B(ar(M.wk_b)), B(R(qabs)), B(posref), I(absmin), I(N)}, N * H, c.kv_lora / 128, 1, 32); + else t.add("mla_qabs", {B(R(q)), B(ar(M.wk_b)), B(R(qabs)), B(posref), I(absmin), I(N)}, N * H, c.kv_lora / 128, 1, 128); if (N <= 16 && mla_kc > 0) { // decode / verify: split-K over the keys (32 heads alone would leave the GPU idle) const float asc = 1.0f / std::sqrt(float(c.nope + c.rope)); const bool use_mmah = mla_mmah && H % 8 == 0 && H % 4 == 0 && max_ctx > mla_thr; // long contexts only: the latency of one split block (about 160 us) loses to part4 below about 8K diff --git a/engine/src/ling32/metal/prof_layer.sh b/engine/src/ling32/metal/prof_layer.sh new file mode 100644 index 0000000000..86c0128bba --- /dev/null +++ b/engine/src/ling32/metal/prof_layer.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# prof_layer.sh: the per-dispatch profile of one KDA layer and one MLA layer of the decode step (N = 1, each dispatch alone in its own command buffer) at ctx 131072 after a 512-token prompt. Run through bg_run3.sh on m1002 (about 1 minute). +S=$HOME/strata-mac/ling32 +B=$S/bin/ling32-metal +M=$S/models/Ling-3.2-Air-SQ-96.strata +$B --model $M --ctx 131072 --chunk 4096 --bench-prompt 512 --gen 2 --reps 1 --profile 2>&1 | grep -E "^\[layer [35]|^ [a-z_0-9]+ +[0-9.]+ us|^ sum |^prefill [0-9]" | head -60 | cut -c1-110