Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / attn_apply_cached.comp

attn_apply_cached.comp71 lines · 3.4 KiBRaw
1#version 450
2//! `O[d] = sum_j S[h][j] * Vcache[j][d]`, attention output for **one** query.
3//!
4//! The counterpart of `attn_scores_cached.comp`, and it exists for the same reason: V is a
5//! position-major `[T, 1, d_model]` cache rather than the `[d_model, 1, T]` every projection
6//! writes, so appending a position is one contiguous copy instead of `d_model` scattered ones. See
7//! that shader for why the three shared attention shaders are left untouched.
8//!
9//! # Back into the layout the next projection reads
10//!
11//! The output is `[d_model, 1, 1]` — channel-major, one position — which is exactly what
12//! `conv_point_int8.comp` wants next. So the cache layout is confined to the two operands that are
13//! caches, and nothing else in the decoder changes shape. As in `attn_apply.comp`, the head
14//! concatenation is not an op: output channel `c` belongs to head `c / head_dim` and is written in
15//! place, which *is* the concatenated result.
16//!
17//! V needs no head arithmetic — channel `c` of V is channel `c` of the output — so the head index
18//! only picks which of `S`'s rows to read.
19//!
20//! # This is the strided side of the layout
21//!
22//! `Vcache[j][d]` for fixed `d` walks `p.in_c` apart, where `attn_apply.comp`'s read is contiguous.
23//! That is the price of the cheap append, and it is paid once per output channel per key rather
24//! than per weight tap; the projections either side of it dominate.
25//!
26//! # Precision
27//!
28//! `S` is a distribution, so every product is bounded by the corresponding `V` and the sum of a
29//! convex combination cannot exceed `V`'s range. `float` accumulation is ample and the fp16 store
30//! is the only rounding, as in `attn_apply.comp`.
31//!
32//! # Push fields
33//!
34//! `p.in0` is `S` at `[heads, 1, key_stride]`, `p.in1` the cache, `p.in_c` and `p.out_c` are
35//! `d_model`, `p.in_w` the key stride and `p.group` the head count.
36//!
37//! When `p.dyn_keys` is set, `p.in_w` remains the distance between rows of `S` — the maximum the
38//! plan was built for — while the number of keys summed over is `step_params.prefix + 1`. The two
39//! must not be conflated: using the attended count as the row stride would read head `h`'s row
40//! from the wrong offset the moment the step is shorter than the maximum.
41
42#include "common.glsl"
43
44void main() {
45 uint index = global_index();
46 if (index >= p.count) {
47 return;
48 }
49 // The output is `[d_model, 1, queries]`. Adjacent lanes take adjacent channels of one query,
50 // so the probability row is a broadcast and the V reads are contiguous; at one query this is
51 // `channel = index`.
52 uint queries = max(p.dil_h, 1u);
53 uint channel = index % p.out_c;
54 uint q = index / p.out_c;
55 uint stride = p.in_w;
56 uint head_dim = p.out_c / p.group;
57 uint head = channel / head_dim;
58 // `S[head][q][*]`, contiguous in the key index.
59 uint row = p.in0 + (head * queries + q) * stride;
60 // V's channel for this query head, which under multi-query attention is shared.
61 uint value = p.in1 + kv_head_of(head) * head_dim + (channel % head_dim);
62 uint vstride = kv_stride(head_dim);
63
64 float total = 0.0;
65 for (uint j = attn_first_q(q); j <= attn_last_q(stride, q); j++) {
66 total += float(arena[row + j]) * float(arena[value + j * vstride]);
67 }
68 // A folded int8 round-trip: the attention mixture's `Quantize` (live's
69 // `Mm` round-trip at `MM_SCALE`) stores here instead of its own dispatch.
70 arena[p.out_at + channel * queries + q] = float16_t(quant_store(total));
71}