Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / softmax_prefix.comp
softmax_prefix.comp97 lines · 3.6 KiBRaw
| 1 | #version 450 |
| 2 | //! Softmax over the first `step_params.prefix + 1` entries of each row, not the whole row. |
| 3 | //! |
| 4 | //! # Why not a flag in `softmax.comp` |
| 5 | //! |
| 6 | //! `softmax.comp` is shared by PP-OCRv5 recognition, both Supertonic encoders, whisper, maia and |
| 7 | //! NLLB's encoder. It is the one shader a decode change most wants to touch and least should: a |
| 8 | //! branch added there is a branch every one of those nets executes on every row, and a mistake in |
| 9 | //! it is a mistake in all of them at once. This is a separate pipeline, reached only by |
| 10 | //! `Kind::SoftmaxPrefix`, so nothing that does not decode can be affected by it. |
| 11 | //! |
| 12 | //! # Row length versus row stride |
| 13 | //! |
| 14 | //! The plan is built once at a maximum context, so a score map row is `p.out_w` elements wide but |
| 15 | //! only the leading `step_params.prefix + 1` of them were written by |
| 16 | //! `attn_scores_cached.comp` this step. The rest are stale values from earlier steps — not zeros, |
| 17 | //! not `-inf`. Reading them would put finite garbage into both the maximum and the sum, so every |
| 18 | //! one of the three passes stops at the attended count while the row *offset* keeps using the |
| 19 | //! full stride. |
| 20 | //! |
| 21 | //! The output is written over the same leading span. Entries past it keep whatever they held, |
| 22 | //! which is safe precisely because `attn_apply_cached.comp` sums over the same bound. |
| 23 | //! |
| 24 | //! # Row mode |
| 25 | //! |
| 26 | //! With `p.kw == 1` one workgroup owns one row and its lanes split the attended keys. A decode |
| 27 | //! step has one row per head and up to the whole context per row. See `reduce.glsl`. |
| 28 | |
| 29 | #extension GL_KHR_shader_subgroup_basic : require |
| 30 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 31 | #include "common.glsl" |
| 32 | #include "reduce.glsl" |
| 33 | |
| 34 | void row_mode() { |
| 35 | uint row = reduce_row(); |
| 36 | if (row >= p.count) { |
| 37 | return; |
| 38 | } |
| 39 | uint lane = gl_LocalInvocationID.x; |
| 40 | uint lanes = gl_WorkGroupSize.x; |
| 41 | uint stride = p.out_w; |
| 42 | // Rows are `[heads, queries]`, so a row's query is its index modulo the query count. |
| 43 | uint q = row % max(p.dil_h, 1u); |
| 44 | uint first = attn_first_q(q); |
| 45 | uint last = attn_last_q(stride, q); |
| 46 | uint at = p.in0 + row * stride; |
| 47 | float peak = -65504.0; |
| 48 | for (uint i = first + lane; i <= last; i += lanes) { |
| 49 | peak = max(peak, float(arena[at + i])); |
| 50 | } |
| 51 | peak = workgroup_max(peak); |
| 52 | float total = 0.0; |
| 53 | for (uint i = first + lane; i <= last; i += lanes) { |
| 54 | total += exp(float(arena[at + i]) - peak); |
| 55 | } |
| 56 | float inverse = 1.0 / workgroup_sum(total); |
| 57 | uint out_row = p.out_at + row * stride; |
| 58 | for (uint i = first + lane; i <= last; i += lanes) { |
| 59 | arena[out_row + i] = float16_t(exp(float(arena[at + i]) - peak) * inverse); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | void main() { |
| 64 | if (reduce_row_mode()) { |
| 65 | row_mode(); |
| 66 | return; |
| 67 | } |
| 68 | uint row = global_index(); |
| 69 | if (row >= p.count) { |
| 70 | return; |
| 71 | } |
| 72 | uint stride = p.out_w; |
| 73 | uint q = row % max(p.dil_h, 1u); |
| 74 | uint first = attn_first_q(q); |
| 75 | uint last = attn_last_q(stride, q); |
| 76 | uint at = p.in0 + row * stride; |
| 77 | // The most negative |
| 78 | |
| 79 | // The most negative fp16 rather than -inf, matching `softmax.comp`: a decode step always |
| 80 | // attends over at least its own position, so the range is never empty and the seed is never |
| 81 | // the answer. |
| 82 | float peak = -65504.0; |
| 83 | for (uint i = first; i <= last; i++) { |
| 84 | peak = max(peak, float(arena[at + i])); |
| 85 | } |
| 86 | |
| 87 | float total = 0.0; |
| 88 | for (uint i = first; i <= last; i++) { |
| 89 | total += exp(float(arena[at + i]) - peak); |
| 90 | } |
| 91 | float inverse = 1.0 / total; |
| 92 | |
| 93 | uint out_row = p.out_at + row * stride; |
| 94 | for (uint i = first; i <= last; i++) { |
| 95 | arena[out_row + i] = float16_t(exp(float(arena[at + i]) - peak) * inverse); |
| 96 | } |
| 97 | } |