Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / attn_apply_banded.comp
attn_apply_banded.comp69 lines · 3.2 KiBRaw
| 1 | #version 450 |
| 2 | //! `O[h][d][q] = sum_j S[h][q][j] * V[h][d][k]`, the value half of banded attention. |
| 3 | //! |
| 4 | //! `k = q - (band - 1) + j`, the same mapping `attn_scores_banded.comp` used to build the band. |
| 5 | //! Twelve uses, one per conformer layer of Gemma 4's audio tower. |
| 6 | //! |
| 7 | //! # What differs from `attn_apply.comp` |
| 8 | //! |
| 9 | //! Only the inner loop's bound and its indirection. `S` is `[heads, T, band]` rather than |
| 10 | //! `[heads, T, T]`, so a row is `band` wide and column `j` refers to key `k` rather than to |
| 11 | //! itself. `V` and the output are `[d_model, 1, T]`, exactly as before, so the head |
| 12 | //! concatenation that follows attention is still not an op: output channel `c` belongs to head |
| 13 | //! `c / head_dim` and is written where the concatenated result wants it. |
| 14 | //! |
| 15 | //! `p.out_w` is the sequence length, `p.kh` the band width. The row stride is the band and the |
| 16 | //! value stride is the sequence, and those being different numbers is the whole difference. |
| 17 | //! |
| 18 | //! # The guard, again |
| 19 | //! |
| 20 | //! `j + q + 1 >= band`, subtraction after. It **mirrors `band_key_in` in |
| 21 | //! `nets/gemma4_audio.rs`**, identically to `attn_scores_banded.comp`; the reasoning is written |
| 22 | //! out there and in that function's own docs, and the CPU arm calls `band_key_in(p.kh, ..)` |
| 23 | //! directly rather than restating it. |
| 24 | //! |
| 25 | //! A dead column is skipped rather than accumulated. That is exact rather than approximate: the |
| 26 | //! softmax gave it `exp(-65504 - peak) = 0`, so it would contribute nothing anyway. Skipping it |
| 27 | //! is not about the arithmetic - it is that `V[channel * T + k]` for a wrapped `k` reads live |
| 28 | //! arena belonging to another tensor. Multiplying that by a zero weight is fine; reading it at |
| 29 | //! all is what the guard prevents, and a wrapped read that happens to land on a denormal or a |
| 30 | //! NaN would poison the sum through `0 * NaN`. |
| 31 | //! |
| 32 | //! # Precision |
| 33 | //! |
| 34 | //! `S` is a distribution over the band, so every product is bounded by the corresponding `V` and |
| 35 | //! the sum of a convex combination cannot exceed `V`'s range. `float` accumulation is ample; the |
| 36 | //! fp16 store is the only rounding. |
| 37 | |
| 38 | #include "common.glsl" |
| 39 | |
| 40 | void main() { |
| 41 | uint index = global_index(); |
| 42 | if (index >= p.count) { |
| 43 | return; |
| 44 | } |
| 45 | // The output is `[d_model, 1, T]`, so this is (channel, 0, query). |
| 46 | uint channel, unused_row, query; |
| 47 | unpack(index, channel, unused_row, query); |
| 48 | uint keys = p.out_w; |
| 49 | uint band = p.kh; |
| 50 | uint head_dim = p.out_c / p.group; |
| 51 | // `S[head][query][*]`, contiguous across the band. |
| 52 | uint row = p.in0 + ((channel / head_dim) * keys + query) * band; |
| 53 | // `V[channel][*]`, contiguous across the sequence, because V's height is one. |
| 54 | uint values = p.in1 + channel * keys; |
| 55 | |
| 56 | float total = 0.0; |
| 57 | for (uint j = 0u; j < band; j++) { |
| 58 | // Mirrors `band_key_in` in `nets/gemma4_audio.rs`. Guard strictly before the subtraction, |
| 59 | // so nothing can wrap; see `attn_scores_banded.comp` for the rearrangements that are |
| 60 | // wrong. This gates the read of `V`, not the accumulation. |
| 61 | if (j + query + 1u >= band) { |
| 62 | uint key = query + j + 1u - band; |
| 63 | if (key < keys) { |
| 64 | total += float(arena[row + j]) * float(arena[values + key]); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | arena[p.out_at + index] = float16_t(total); |
| 69 | } |