Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / softmax.comp
softmax.comp109 lines · 4.1 KiBRaw
| 1 | #version 450 |
| 2 | //! Softmax over the **last** axis, one invocation per row. |
| 3 | //! |
| 4 | //! Two uses, both on an attention score map in PP-OCRv5 recognition. |
| 5 | //! |
| 6 | //! # Why the last axis, and why that needs no indexing |
| 7 | //! |
| 8 | //! The scores are `[heads, T, T]` with the key index innermost, so a row — one query's |
| 9 | //! distribution over every key — is a contiguous run of `out_w` elements. The row a |
| 10 | //! given invocation owns is therefore just `row * out_w`, and `p.count` is the number of |
| 11 | //! rows (`out_c * out_h`) rather than the element count. No `unpack` and no stride. |
| 12 | //! |
| 13 | //! That is only true because `nets::Kind::AttnScores` chose to emit the key index last. |
| 14 | //! A softmax over any other axis of this runtime's tensors would be a different shader, |
| 15 | //! and there is no use for one. |
| 16 | //! |
| 17 | //! # The maximum is subtracted, and it is not optional |
| 18 | //! |
| 19 | //! `exp` here is fp32, which overflows to infinity a little past 88. Scores are scaled |
| 20 | //! by `1 / sqrt(head_dim)` before they arrive so they are usually well inside that, but |
| 21 | //! "usually" is doing no work in a shader: one saturated activation upstream turns the |
| 22 | //! whole row into `inf / inf` and every probability into a NaN. Subtracting the row |
| 23 | //! maximum first makes the largest term exactly `exp(0)`, which also means the |
| 24 | //! denominator is at least 1 and the division cannot blow up. |
| 25 | //! |
| 26 | //! The result is unchanged in exact arithmetic — `exp(x - m) / sum(exp(x - m))` is the |
| 27 | //! same function — so this costs one extra pass over a row of `T` and buys the property |
| 28 | //! that the output is always a distribution. |
| 29 | //! |
| 30 | //! # Three passes over the row |
| 31 | //! |
| 32 | //! Maximum, then sum, then store, re-evaluating `exp` in the third rather than keeping |
| 33 | //! the row in registers: `T` is around 80 for a line of text, which is far too large for |
| 34 | //! a fixed-size local array and too small for the bandwidth to matter. |
| 35 | //! |
| 36 | //! # Row mode |
| 37 | //! |
| 38 | //! With `p.kw == 1` one workgroup owns one row and its lanes split the keys, for the few |
| 39 | //! long rows a decode step has (whisper's cross-attention is 1,500 keys). See `reduce.glsl`. |
| 40 | //! |
| 41 | //! # Key bound |
| 42 | //! |
| 43 | //! A non-zero `p.kh` normalises only the leading `p.kh` keys of each row and stores zeros past |
| 44 | //! them, for a sequence padded to a multiple of eight positions (Gemma 4's vision tower): the |
| 45 | //! padded keys then add exact zeros in `attn_apply`. |
| 46 | |
| 47 | #extension GL_KHR_shader_subgroup_basic : require |
| 48 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 49 | #include "common.glsl" |
| 50 | #include "reduce.glsl" |
| 51 | |
| 52 | void row_mode() { |
| 53 | uint row = reduce_row(); |
| 54 | if (row >= p.count) { |
| 55 | return; |
| 56 | } |
| 57 | uint lane = gl_LocalInvocationID.x; |
| 58 | uint lanes = gl_WorkGroupSize.x; |
| 59 | uint at = p.in0 + row * p.out_w; |
| 60 | uint keys = p.kh == 0u ? p.out_w : p.kh; |
| 61 | float peak = -65504.0; |
| 62 | for (uint i = lane; i < keys; i += lanes) { |
| 63 | peak = max(peak, float(arena[at + i])); |
| 64 | } |
| 65 | peak = workgroup_max(peak); |
| 66 | float total = 0.0; |
| 67 | for (uint i = lane; i < keys; i += lanes) { |
| 68 | total += exp(float(arena[at + i]) - peak); |
| 69 | } |
| 70 | float inverse = 1.0 / workgroup_sum(total); |
| 71 | uint out_row = p.out_at + row * p.out_w; |
| 72 | for (uint i = lane; i < p.out_w; i += lanes) { |
| 73 | arena[out_row + i] = i < keys ? float16_t(exp(float(arena[at + i]) - peak) * inverse) : float16_t(0.0); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void main() { |
| 78 | if (reduce_row_mode()) { |
| 79 | row_mode(); |
| 80 | return; |
| 81 | } |
| 82 | uint row = global_index(); |
| 83 | if (row >= p.count) { |
| 84 | return; |
| 85 | } |
| 86 | uint at = p.in0 + row * p.out_w; |
| 87 | uint keys = p.kh == 0u ? p.out_w : p.kh; |
| 88 | |
| 89 | // The most negative fp16 rather than -inf, matching `maxpool.comp`: every row here |
| 90 | // is non-empty, so the seed is never the answer. |
| 91 | float peak = -65504.0; |
| 92 | for (uint i = 0u; i < keys; i++) { |
| 93 | peak = max(peak, float(arena[at + i])); |
| 94 | } |
| 95 | |
| 96 | float total = 0.0; |
| 97 | for (uint i = 0u; i < keys; i++) { |
| 98 | total += exp(float(arena[at + i]) - peak); |
| 99 | } |
| 100 | float inverse = 1.0 / total; |
| 101 | |
| 102 | uint out_row = p.out_at + row * p.out_w; |
| 103 | for (uint i = 0u; i < keys; i++) { |
| 104 | arena[out_row + i] = float16_t(exp(float(arena[at + i]) - peak) * inverse); |
| 105 | } |
| 106 | for (uint i = keys; i < p.out_w; i++) { |
| 107 | arena[out_row + i] = float16_t(0.0); |
| 108 | } |
| 109 | } |