Commit 6574440
committed
perf(metal): deep-prompt global-layer SDPA rides a steel GEMM composition
The multiQ vector kernel re-reads the whole K/V stream once per query row.
At depth that traffic outgrows the SLC and the measured per-key cost ramps
0.032µs → 0.065µs (the #345 knee); even its floor runs the prompt n² at a
small fraction of the machine's matmul rate — mlx-lm's gemma4 prompt
attention (the same GEMM chain below, headDim 256 exceeding MLX's fused
steel_attention) prefilled 63K tokens 11× faster.
Past sdpaPromptGEMMMinKV (4096) a global layer's chunk SDPA now runs as
three dispatches per GQA group: S = Q @ Kᵀ (steel nt, batched over the
group's q-heads on grid.z with scalar batch strides, B stride 0 sharing
the group's K), an in-place causal row softmax (new
lthn_softmax_causal_rows_bf16 — per-row valid-prefix cap identical to the
multiQ kernel's, masked tail written zero for the following GEMM), and
O = P @ V (steel nn, batched). K/V are read once per kv head instead of
once per query row. Sliding layers and short prompts keep their existing
kernels; the S slabs double-buffer across kv heads and track their OWN
capacities (the #343 lesson).
Token-identity tier — S stores bf16 between the GEMMs, the same boundary
the fold's qmm and ≥32-row steel projections already trade at; pinned by
the per-row cosine closeness test vs the multiQ kernel.
Receipts (gemma-4-e2b-4bit, ctx 131072, temp 0, quiet GPU):
63K prefill 95.8s → 29.4s (3.3×) · needle HIT · byte-deterministic
97K prefill 276.0s → 54.7s (5.0×) · needle HIT · decode after 87.9 tok/s
(= the #344 receipt, decode untouched)
per-key cost flat 0.0107µs at every depth — the SLC-decay ramp is gone
26B-A4B 6.6K: 8.7s → 7.2s, needle HIT (quant/MoE arch rides the same lane)
engine/metal suite 1439 green · module suite 9057 green
Co-Authored-By: Virgil <virgil@lethean.io>1 parent 0007b21 commit 6574440
5 files changed
Lines changed: 482 additions & 14 deletions
File tree
- go/engine/metal
- kernels
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
85 | 92 | | |
86 | 93 | | |
87 | 94 | | |
| |||
139 | 146 | | |
140 | 147 | | |
141 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
142 | 166 | | |
143 | 167 | | |
144 | 168 | | |
| |||
890 | 914 | | |
891 | 915 | | |
892 | 916 | | |
| 917 | + | |
| 918 | + | |
| 919 | + | |
| 920 | + | |
| 921 | + | |
| 922 | + | |
| 923 | + | |
| 924 | + | |
| 925 | + | |
893 | 926 | | |
894 | 927 | | |
895 | 928 | | |
| |||
980 | 1013 | | |
981 | 1014 | | |
982 | 1015 | | |
| 1016 | + | |
| 1017 | + | |
| 1018 | + | |
| 1019 | + | |
| 1020 | + | |
| 1021 | + | |
| 1022 | + | |
983 | 1023 | | |
984 | 1024 | | |
985 | 1025 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
55 | 55 | | |
56 | 56 | | |
57 | 57 | | |
| |||
72 | 72 | | |
73 | 73 | | |
74 | 74 | | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
75 | 82 | | |
76 | 83 | | |
77 | 84 | | |
78 | 85 | | |
79 | 86 | | |
80 | | - | |
| 87 | + | |
81 | 88 | | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
85 | 92 | | |
86 | 93 | | |
87 | 94 | | |
88 | | - | |
89 | | - | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
90 | 101 | | |
91 | 102 | | |
92 | 103 | | |
| |||
116 | 127 | | |
117 | 128 | | |
118 | 129 | | |
119 | | - | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
120 | 151 | | |
121 | 152 | | |
122 | 153 | | |
123 | 154 | | |
124 | 155 | | |
125 | 156 | | |
126 | | - | |
127 | | - | |
| 157 | + | |
| 158 | + | |
128 | 159 | | |
129 | 160 | | |
130 | 161 | | |
131 | 162 | | |
132 | 163 | | |
133 | 164 | | |
134 | 165 | | |
135 | | - | |
136 | | - | |
| 166 | + | |
| 167 | + | |
137 | 168 | | |
138 | | - | |
| 169 | + | |
| 170 | + | |
139 | 171 | | |
140 | 172 | | |
141 | 173 | | |
142 | | - | |
143 | | - | |
144 | | - | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
145 | 177 | | |
146 | 178 | | |
147 | 179 | | |
148 | 180 | | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
149 | 184 | | |
150 | | - | |
| 185 | + | |
151 | 186 | | |
152 | 187 | | |
153 | 188 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
0 commit comments