Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / argmax.comp
argmax.comp64 lines · 2.8 KiBRaw
| 1 | #version 450 |
| 2 | //! Greedy argmax over a logits row, first stage: one `(lo, hi)` index pair per workgroup. |
| 3 | //! |
| 4 | //! # Why two stages |
| 5 | //! |
| 6 | //! One 64-lane workgroup grid-striding over 262,144 logits leaves the GPU idle: 64 lanes |
| 7 | //! each scan 4,096 values (~1.6 ms of a 24 ms NLLB decode step on a Pixel 8). Here up to |
| 8 | //! 64 workgroups (see `argmax_groups` in `nets`) each keep their slice's first maximum, |
| 9 | //! and `argmax_fold.comp` re-reads the candidates and keeps the greatest. Same `[lo, hi]` |
| 10 | //! fp16 output contract, same lowest-index tie-break, decided on fp32 values throughout. |
| 11 | //! |
| 12 | //! # How |
| 13 | //! |
| 14 | //! `p.group` is the workgroup count; workgroup `g` owns |
| 15 | //! `[g * slice, min((g + 1) * slice, p.count))` with `slice = ceil(count / groups)`. |
| 16 | //! 64 lanes grid-stride within the slice, each keeping its local (value, index) maximum |
| 17 | //! with strict `>` (first index wins); a shared 64-entry reduction folds them with the |
| 18 | //! same tie-break lane 0 of the single-stage shader used (equal values compare indices). |
| 19 | //! Lane 0 stores the winner as `(lo = id % 2048, hi = id / 2048)` — both exactly |
| 20 | //! representable, like the final pair. The fold re-reads the row at these indices, so |
| 21 | //! the fp16 store here never decides a near-tie. `PER_LANE` must equal |
| 22 | //! `ARGMAX_PER_LANE` in `nets`. |
| 23 | #include "common.glsl" |
| 24 | #define PER_LANE 64u |
| 25 | shared uint best_idx[64]; |
| 26 | shared float best_val[64]; |
| 27 | void main() { |
| 28 | uint lane = gl_LocalInvocationID.x; |
| 29 | uint group = gl_WorkGroupID.x; |
| 30 | uint groups = max(p.group, 1u); |
| 31 | uint slice = (p.count + groups - 1u) / groups; |
| 32 | uint first = group * slice; |
| 33 | uint end = min(first + slice, p.count); |
| 34 | // Grid-stride within this workgroup's slice: with one workgroup this is one pass. |
| 35 | float mine_val = -65504.0; |
| 36 | uint mine_idx = 0u; |
| 37 | uint lanes = 64u; |
| 38 | for (uint i = first + lane; i < end; i += lanes) { |
| 39 | float v = float(arena[p.in0 + i]); |
| 40 | if (v > mine_val) { |
| 41 | mine_val = v; |
| 42 | mine_idx = i; |
| 43 | } |
| 44 | } |
| 45 | best_val[lane] = mine_val; |
| 46 | best_idx[lane] = mine_idx; |
| 47 | barrier(); |
| 48 | // Lane 0 folds the 64 lane-maxima. Each lane holds its own first index, but lanes |
| 49 | // interleave, so an equal value from a later lane can still be the earlier index - the |
| 50 | // tie compares indices, which is what keeps the FIRST index globally, matching the |
| 51 | // host `argmax` loop. |
| 52 | if (lane == 0u) { |
| 53 | float top_val = best_val[0]; |
| 54 | uint top_idx = best_idx[0]; |
| 55 | for (uint l = 1u; l < 64u; l++) { |
| 56 | if (best_val[l] > top_val || (best_val[l] == top_val && best_idx[l] < top_idx)) { |
| 57 | top_val = best_val[l]; |
| 58 | top_idx = best_idx[l]; |
| 59 | } |
| 60 | } |
| 61 | arena[p.out_at + group * 2u] = float16_t(float(top_idx % 2048u)); |
| 62 | arena[p.out_at + group * 2u + 1u] = float16_t(float(top_idx / 2048u)); |
| 63 | } |
| 64 | } |