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

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

rotary.comp68 lines · 3.5 KiBRaw
1#version 450
2//! Rotary position embedding, the half-split form: `out = x * cos + rotate_half(x) * sin`.
3//!
4//! Eight uses, on the query and the key of each of the four text cross-attentions in
5//! Supertonic's flow-matching sampler.
6//!
7//! # The split is halves, not pairs
8//!
9//! There are two conventions and they are not interchangeable. This one splits a head's
10//! `head_dim` channels down the middle and pairs channel `j` with channel `j + half`:
11//!
12//! out[j] = x[j] * cos[j] - x[j + half] * sin[j]
13//! out[j + half] = x[j + half] * cos[j] + x[j] * sin[j]
14//!
15//! The other convention pairs adjacent channels `(2j, 2j + 1)`. Both rotate the same 2-planes
16//! by the same angles, so both produce output of exactly the right shape and magnitude, and
17//! attention scores computed with one and weights trained for the other are simply wrong. The
18//! export's `Slice`s are `[0, 32)` and `[32, 64)` of a 64-wide head, so it is halves.
19//!
20//! # The angles come from the host
21//!
22//! `p.in1` is a `[2 * half, 1, W]` table: `cos` in the first `half` channels and `sin` in the
23//! rest. It is a function of the position and the frequency alone, so there is nothing to train
24//! and nothing to keep in the weights file — and in this export it is not even a function of the
25//! absolute position. The angle is `(position / length) * theta[j]`, so a query at the middle of
26//! a 60-frame latent and a key at the middle of a 20-character text get the *same* angle. That
27//! is what lets an 8-head cross-attention align two sequences of different lengths, and it means
28//! the table has to be rebuilt whenever either length changes. See `post::supertonic`.
29//!
30//! One table serves every head: `p.in_c` is `head_dim` and the frequency index is
31//! `(c % head_dim) % half`.
32
33#include "common.glsl"
34
35void main() {
36 uint index = global_index();
37 if (index >= p.count) {
38 return;
39 }
40 // The tensor is `[C, 1, W]`, so this is (channel, 0, position).
41 uint channel, unused_row, position;
42 unpack(index, channel, unused_row, position);
43 uint head_dim = p.in_c;
44 // Independent rotary blocks inside one head. 1 is ordinary RoPE and reduces every line below
45 // to what it was; 2 is the vision tower's row-and-column pair. See `Push::rope_axes`.
46 uint axes = max(p.rope_axes, 1u);
47 uint block = head_dim / axes;
48 uint half_dim = block / 2u;
49 uint within = channel % head_dim;
50 // Which block of the head this channel is in, and where inside it.
51 uint sub = within / block;
52 uint local = within % block;
53 uint frequency = local % half_dim;
54 // The partner is `half_dim` away **within the block**, so a rotation never crosses from the
55 // row half into the column half - which would mix two positions into one pair.
56 bool lower = local < half_dim;
57 uint partner = lower ? channel + half_dim : channel - half_dim;
58 // Each block reads its own slice of the angle table: cosines then sines, per block.
59 uint angle_base = sub * block;
60 float angle_cos = float(arena[p.in1 + (angle_base + frequency) * p.out_w + position]);
61 float angle_sin =
62 float(arena[p.in1 + (angle_base + half_dim + frequency) * p.out_w + position]);
63 float self = float(arena[p.in0 + channel * p.out_w + position]);
64 float other = float(arena[p.in0 + partner * p.out_w + position]);
65 // The lower half subtracts its partner's sine term; the upper half adds its own.
66 float rotated = lower ? -other * angle_sin : other * angle_sin;
67 arena[p.out_at + index] = float16_t(self * angle_cos + rotated);
68}