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

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

softmax_causal.comp71 lines · 3.2 KiBRaw
1#version 450
2//! `softmax.comp` with the row truncated at the diagonal: a **causal** attention softmax.
3//!
4//! One use, TinyCLIP's text tower. Its three layers attend over the whole 77-token
5//! sequence at once and each query may only see itself and the keys before it, which the
6//! export spells as adding a `-inf` upper triangle to the score map before normalising.
7//!
8//! # Why a second shader rather than a flag on `softmax.comp`
9//!
10//! A `Push` field would be free to add and expensive to be wrong about: `softmax.comp` is
11//! shared by PP-OCRv5 recognition, both Supertonic encoders and SMaLL-100, so a field the
12//! bound is read from breaks four shipping nets at once if it is ever left unset. This
13//! file duplicates twenty lines instead.
14//!
15//! The other alternative was no shader at all — run the text tower one token at a time
16//! through `attn_scores_cached`, where one query needs no mask because the cache *is* the
17//! prefix. That is 77 plan rebuilds per query, each a `device_wait_idle`, for a tower that
18//! is three layers deep.
19//!
20//! # The bound is `query + 1`, and off by one in either direction is wrong
21//!
22//! The scores are `[heads, T, T]`, key innermost, so a row is contiguous and the row index
23//! is `head * out_h + query`. Query `q` may read keys `0 ..= q`, so the row is `q + 1`
24//! long. At `q = 0` that is one key and the distribution is exactly `[1, 0, ...]`; a bound
25//! of `q` would leave an empty row and divide by zero, and a bound of `q + 2` would let
26//! every position read one token of the future — a fluent, subtly wrong embedding.
27//!
28//! The masked tail is written as zero rather than left alone. `attn_apply.comp` multiplies
29//! the whole row by V, so a stale value there from a previous layer's score map would
30//! contribute; and the arena is reused, so "previous" is not even predictable.
31
32#include "common.glsl"
33
34void main() {
35 uint row = global_index();
36 if (row >= p.count) {
37 return;
38 }
39 // Rows are laid out head-major, `out_h` queries per head, so this is the query index
40 // whatever the head count is.
41 uint query = row % p.out_h;
42 uint keys = min(query + 1u, p.out_w);
43 // A sliding layer also drops everything more than `p.kh - 1` behind the query. Zero is no
44 // window, which is the ordinary causal mask and every net that predates batched prefill.
45 uint first = (p.kh != 0u && query + 1u > p.kh) ? query + 1u - p.kh : 0u;
46 uint at = p.in0 + row * p.out_w;
47
48 // The most negative fp16 rather than -inf, as `softmax.comp` does. `keys` is at least
49 // one, so the seed is never the answer.
50 float peak = -65504.0;
51 for (uint i = first; i < keys; i++) {
52 peak = max(peak, float(arena[at + i]));
53 }
54
55 float total = 0.0;
56 for (uint i = first; i < keys; i++) {
57 total += exp(float(arena[at + i]) - peak);
58 }
59 float inverse = 1.0 / total;
60
61 uint out_row = p.out_at + row * p.out_w;
62 for (uint i = 0u; i < first; i++) {
63 arena[out_row + i] = float16_t(0.0);
64 }
65 for (uint i = first; i < keys; i++) {
66 arena[out_row + i] = float16_t(exp(float(arena[at + i]) - peak) * inverse);
67 }
68 for (uint i = keys; i < p.out_w; i++) {
69 arena[out_row + i] = float16_t(0.0);
70 }
71}