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

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

cache_write.comp54 lines · 2.8 KiBRaw
1#version 450
2//! Append one position to a KV cache, at the row the step supplies.
3//!
4//! # Why an op rather than a copy
5//!
6//! `Op::Copy` is a `vkCmdCopyBuffer` with its offsets baked into the recording, and the row a
7//! decode step writes moves by one every token. Recording it is therefore exactly the thing that
8//! forced a re-record per token. This is the same copy with the destination row read from
9//! `step_params.prefix` instead, which is the whole difference between a plan that is recorded
10//! once and one that is recorded per token.
11//!
12//! # Why the projection does not just write here directly
13//!
14//! It could, in principle: the K and V projections produce this row, so a convolution that knew
15//! about the cache could store into it. That would put a dynamic destination into
16//! `conv_point_int8.comp` and every other projection shader, for one caller. Keeping it a
17//! separate op costs one `d_model` copy per layer per step - 1024 fp16 elements for NLLB, against
18//! the 1024x1024 matrix multiply that produced them - and leaves every projection shader alone.
19//!
20//! # Push fields
21//!
22//! `p.in0` is the source row, `d_model` contiguous elements. `p.out_at` is the base of the cache,
23//! which is `[max_positions, 1, d_model]`. `p.in_c` is `d_model`, the distance between rows.
24//! `p.in_h` is `max_positions`, read only to refuse a write past the end. `p.count` is `d_model`.
25//!
26//! `out_c`/`out_h`/`out_w` are left as the cache's true dimensions rather than repurposed for the
27//! bound, because their product is what `assert_no_aliasing` treats as the region this op writes.
28
29#include "common.glsl"
30
31void main() {
32 uint index = global_index();
33 if (index >= p.count) {
34 return;
35 }
36 // A cache that has run out of room drops the position rather than corrupting whatever the
37 // arena put after it. The caller is expected to stop before this; a shader cannot report it,
38 // so the safe failure is the silent one rather than an out-of-bounds store.
39 // `p.group` is the positions being written: 0 or 1 is the decode step's single row, more is
40 // a prefill's whole prompt.
41 //
42 // The two need different addressing, and this is the one place the layouts disagree. A cache
43 // is position-major - position `t`, channel `c` at `t * width + c`. A prefill's K and V are
44 // `[width, 1, T]`, so they are **channel**-major: `c * T + t`. For one position the two
45 // coincide and a reshape is free; for more it is a transpose, and doing it here costs one
46 // divide rather than a whole extra op and a second pass over the arena.
47 uint positions = max(p.group, 1u);
48 uint channel = index / positions;
49 uint position = index % positions;
50 if (step_params.prefix + position >= p.in_h) {
51 return;
52 }
53 arena[p.out_at + (step_params.prefix + position) * p.in_c + channel] = arena[p.in0 + index];
54}