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

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

topk.comp88 lines · 3.9 KiBRaw
1#version 450
2//! The 64 largest logits of a row, highest first, ties by lower index - in two stages like
3//! `argmax.comp` / `argmax_fold.comp`, selected by the same 64 rounds in both.
4//!
5//! Round `r` takes the best `(value, index)` strictly below round `r - 1`'s winner in the order
6//! "greater value, then lower index", so equal values are taken in index order and nothing is
7//! taken twice without marking anything. Each lane scans its elements for its own best
8//! qualifying key and a 64-entry shared reduction picks the round's winner.
9//!
10//! `p.kh == 0`, stage 1: workgroup `g` of `p.group` owns `[g * slice, min((g + 1) * slice,
11//! p.count))` of the row at `p.in0` and writes its top 64 ids to `p.out_at + (g * 64 + r) * 2`
12//! as `(lo = id % 2048, hi = id / 2048)`, both exact in fp16. Missing ranks (a slice under 64)
13//! are `(2047, 2047)`, an id past any row.
14//!
15//! `p.kh == 1`, stage 2: one workgroup reads the `p.in_c` candidate ids at `p.in0`, re-reads each
16//! one's logit from the row at `p.in1` (`p.in_w` long), and writes rank `r` as `(value, lo, hi)`
17//! at `p.out_at + r * 3`. The values are the row's own fp16 values, so nothing is rounded twice.
18#include "common.glsl"
19#define K 64u
20#define NONE 0xFFFFFFFFu
21shared float best_val[64];
22shared uint best_idx[64];
23
24bool better(float av, uint ai, float bv, uint bi) {
25 return bi == NONE || (ai != NONE && (av > bv || (av == bv && ai < bi)));
26}
27
28void main() {
29 uint lane = gl_LocalInvocationID.x;
30 bool fold = p.kh != 0u;
31 uint group = gl_WorkGroupID.x;
32 uint groups = max(p.group, 1u);
33 uint slice = (p.count + groups - 1u) / groups;
34 uint first = fold ? 0u : group * slice;
35 uint end = fold ? p.in_c : min(first + slice, p.count);
36 float prev_val = 0.0;
37 uint prev_idx = NONE;
38 bool done = false;
39 for (uint r = 0u; r < K; r++) {
40 float mine_val = 0.0;
41 uint mine_idx = NONE;
42 for (uint e = first + lane; !done && e < end; e += 64u) {
43 uint idx = e;
44 if (fold) {
45 idx = uint(float(arena[p.in0 + e * 2u])) + 2048u * uint(float(arena[p.in0 + e * 2u + 1u]));
46 if (idx >= p.in_w) {
47 continue;
48 }
49 }
50 float v = float(arena[(fold ? p.in1 : p.in0) + idx]);
51 // Strictly after the previous winner in the order, so each key is taken once.
52 bool after = prev_idx == NONE || v < prev_val || (v == prev_val && idx > prev_idx);
53 if (after && better(v, idx, mine_val, mine_idx)) {
54 mine_val = v;
55 mine_idx = idx;
56 }
57 }
58 best_val[lane] = mine_val;
59 best_idx[lane] = mine_idx;
60 barrier();
61 for (uint stride = 32u; stride > 0u; stride >>= 1u) {
62 if (lane < stride && better(best_val[lane + stride], best_idx[lane + stride], best_val[lane], best_idx[lane])) {
63 best_val[lane] = best_val[lane + stride];
64 best_idx[lane] = best_idx[lane + stride];
65 }
66 barrier();
67 }
68 float win_val = best_val[0];
69 uint win_idx = best_idx[0];
70 barrier();
71 if (lane == 0u) {
72 uint lo = win_idx == NONE ? 2047u : win_idx % 2048u;
73 uint hi = win_idx == NONE ? 2047u : win_idx / 2048u;
74 if (fold) {
75 arena[p.out_at + r * 3u] = float16_t(win_idx == NONE ? -65504.0 : win_val);
76 arena[p.out_at + r * 3u + 1u] = float16_t(float(lo));
77 arena[p.out_at + r * 3u + 2u] = float16_t(float(hi));
78 } else {
79 arena[p.out_at + (group * K + r) * 2u] = float16_t(float(lo));
80 arena[p.out_at + (group * K + r) * 2u + 1u] = float16_t(float(hi));
81 }
82 }
83 // Uniform across the workgroup: every lane read the same winner.
84 done = win_idx == NONE;
85 prev_val = win_val;
86 prev_idx = win_idx;
87 }
88}