Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / reduce.glsl
reduce.glsl81 lines · 3.8 KiBRaw
| 1 | // Workgroup-wide reductions for the row-parallel mode of the reducing shaders. |
| 2 | // |
| 3 | // Included after `common.glsl` by `layernorm.comp`, `rmsnorm.comp`, `softmax.comp` and |
| 4 | // `softmax_prefix.comp`, each of which must enable `GL_KHR_shader_subgroup_basic` and |
| 5 | // `GL_KHR_shader_subgroup_arithmetic` before its first include - `#extension` has to precede |
| 6 | // every declaration. |
| 7 | // |
| 8 | // # Why a row mode exists |
| 9 | // |
| 10 | // Those shaders were written for one invocation per row, each walking the whole row on its own. |
| 11 | // That is right when there are thousands of rows, and it leaves the GPU nearly idle when there |
| 12 | // are a handful: a decode step normalises **one** position of 1,024 channels, so a single thread |
| 13 | // does 3,072 dependent loads while every other lane on the device waits. On a Pixel 8 that made |
| 14 | // NLLB's 37 layer norms 47% of a decode step at 790 us each, and Gemma's 241 RMS norms 200 ms of |
| 15 | // a 409 ms token. |
| 16 | // |
| 17 | // With `p.kw == 1` a whole 64-lane workgroup owns one row instead: lanes stride the row, reduce |
| 18 | // in registers with a subgroup op, then across the workgroup's subgroups through `reduce_partial`. |
| 19 | // `p.kw` is otherwise unused by these shaders, `p.count` stays the row count, and the work per row |
| 20 | // is unchanged - so the reference interpreter, `Kind::arena_reads` and the barrier schedule need |
| 21 | // no change. `Builder` picks the mode; see `ROW_REDUCE_MAX_ROWS` in `nets`. |
| 22 | // |
| 23 | // Every call is reached by the whole workgroup: the row index is per workgroup, so an early |
| 24 | // `return` on it is uniform, and `p.kw` is a push constant. |
| 25 | |
| 26 | // Sixteen slots: a 64-lane workgroup has at most 16 subgroups whenever subgroups have at least |
| 27 | // 4 lanes (every real device), and the loops below take more in batches of 16, summing in the |
| 28 | // same order, so the result never depends on it. Not 64: on Mali the driver's per-dispatch |
| 29 | // recording cost grows with shared bytes x workgroups, and 64 slots made Supertonic's row-mode |
| 30 | // layer norms cost up to ~1 ms each to record at a new sentence length. |
| 31 | #define REDUCE_SLOTS 16u |
| 32 | shared float reduce_partial[REDUCE_SLOTS]; |
| 33 | |
| 34 | // The row this workgroup owns in row mode, across the same 2-D grid `global_index` flattens. |
| 35 | uint reduce_row() { |
| 36 | return gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 37 | } |
| 38 | |
| 39 | // Whether this dispatch runs one workgroup per row. |
| 40 | bool reduce_row_mode() { |
| 41 | return p.kw == 1u; |
| 42 | } |
| 43 | |
| 44 | // The sum of `value` over every lane of the workgroup, returned to every lane. |
| 45 | float workgroup_sum(float value) { |
| 46 | float summed = subgroupAdd(value); |
| 47 | float total = 0.0; |
| 48 | for (uint first = 0u; first < gl_NumSubgroups; first += REDUCE_SLOTS) { |
| 49 | if (subgroupElect() && gl_SubgroupID >= first && gl_SubgroupID < first + REDUCE_SLOTS) { |
| 50 | reduce_partial[gl_SubgroupID - first] = summed; |
| 51 | } |
| 52 | barrier(); |
| 53 | uint count = min(REDUCE_SLOTS, gl_NumSubgroups - first); |
| 54 | for (uint i = 0u; i < count; i++) { |
| 55 | total += reduce_partial[i]; |
| 56 | } |
| 57 | // The next batch or reduction reuses the slots, so nobody may write them until everyone |
| 58 | // has read. |
| 59 | barrier(); |
| 60 | } |
| 61 | return total; |
| 62 | } |
| 63 | |
| 64 | // The maximum of `value` over every lane of the workgroup, returned to every lane. |
| 65 | float workgroup_max(float value) { |
| 66 | float top = subgroupMax(value); |
| 67 | float peak = 0.0; |
| 68 | for (uint first = 0u; first < gl_NumSubgroups; first += REDUCE_SLOTS) { |
| 69 | if (subgroupElect() && gl_SubgroupID >= first && gl_SubgroupID < first + REDUCE_SLOTS) { |
| 70 | reduce_partial[gl_SubgroupID - first] = top; |
| 71 | } |
| 72 | barrier(); |
| 73 | peak = first == 0u ? reduce_partial[0] : max(peak, reduce_partial[0]); |
| 74 | uint count = min(REDUCE_SLOTS, gl_NumSubgroups - first); |
| 75 | for (uint i = 1u; i < count; i++) { |
| 76 | peak = max(peak, reduce_partial[i]); |
| 77 | } |
| 78 | barrier(); |
| 79 | } |
| 80 | return peak; |
| 81 | } |