Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_h_rows8.comp
gemv_h_rows8.comp137 lines · 5.7 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT H — VARIANT G WITH `ROWS 8`. The fix, as a measurement. |
| 3 | //! |
| 4 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 5 | //! `analysis/gemv_probe/gemv_h_rows8.comp`. See `gemv_a_control.comp` for why this copy |
| 6 | //! exists and when to delete it. |
| 7 | //! |
| 8 | //! # Why this exists |
| 9 | //! |
| 10 | //! Task 9 measured an interaction, not a main effect: |
| 11 | //! |
| 12 | //! | | trivial ALU | full dequant | cost of ALU | |
| 13 | //! | :--- | ---: | ---: | ---: | |
| 14 | //! | stream (~128 iterations a lane) | A 19.70 | C 19.45 | **1.01x — free** | |
| 15 | //! | one-shot (real gemv shape) | F 16.74 | G 2.77 | **6.04x** | |
| 16 | //! |
| 17 | //! The identical arithmetic is **free** in a stream and costs **6x** in a one-shot wave. So |
| 18 | //! the fix is not to remove the arithmetic — C proves it is affordable — it is to give each |
| 19 | //! lane a stream to hide it in. |
| 20 | //! |
| 21 | //! # The cheapest way to create a stream is already in the shader |
| 22 | //! |
| 23 | //! `conv_vec_int4.comp` loops over `ROWS` output channels, each an independent |
| 24 | //! (load, unpack, accumulate) pair. At `ROWS 2` a lane does two of them and then exits. At |
| 25 | //! `ROWS 8` it does eight — eight independent loads whose addresses are all known up front, |
| 26 | //! and eight ALU bursts that can overlap with each other's loads. That is the same structure |
| 27 | //! the streaming variants get from their grid-stride loop, obtained without touching the |
| 28 | //! numerics, the weight format or the arena layout. |
| 29 | //! |
| 30 | //! **This variant is G with one `#define` changed.** Everything else — the tile mapping, the |
| 31 | //! `lane * I4_BLOCK` start, the 2048 stride, the per-block scale fetch, the 32 scalar |
| 32 | //! activation loads, the `subgroupAdd` and the shared reduction — is byte-for-byte G. |
| 33 | //! |
| 34 | //! # Reading it |
| 35 | //! |
| 36 | //! * `H` well above `G` -> the interaction is real and `ROWS` is the lever. The production |
| 37 | //! fix is a `#define` plus a `count` change in `Builder::conv_int4`, and it needs no |
| 38 | //! converter change, no format change and no numerics change. |
| 39 | //! * `H ~= G` -> more independent work per lane does **not** create the overlap, and the |
| 40 | //! one-shot penalty is something else — most likely register pressure capping resident |
| 41 | //! waves, in which case `ROWS 8` makes it worse and the next probe is an occupancy one. |
| 42 | //! |
| 43 | //! # The reason `ROWS` was lowered to 2, and why it may not apply |
| 44 | //! |
| 45 | //! `conv_vec_int4.comp:40-47` dropped it from 8 because *"a 1,536-wide projection launched |
| 46 | //! 192 workgroups - about 12k threads, barely over a Mali G715's capacity"*. That reasoning |
| 47 | //! is about `out = 1536`. The shapes that dominate the byte budget are far wider — the logits |
| 48 | //! head is `out = 65536`, which at `ROWS 8` still launches 8,192 workgroups, 32,768 subgroups, |
| 49 | //! against 448 warp slots. **73x oversubscribed.** So the occupancy argument that justified |
| 50 | //! `ROWS 2` does not obviously bind at the shapes that matter, and this measures whether it |
| 51 | //! does. |
| 52 | //! |
| 53 | //! Likely outcome is that `ROWS` wants to be a function of `out`, not a constant. |
| 54 | //! |
| 55 | //! # Dispatch |
| 56 | //! |
| 57 | //! One workgroup per **eight** output channels, so the runner must pass `count = out / 8` |
| 58 | //! (`GEMV_OUT / 8` = 8,192 workgroups) rather than `out / 2`. The runner already forwards its |
| 59 | //! `groups` argument as `count`, so this is one row in the variants array. |
| 60 | |
| 61 | #extension GL_EXT_shader_16bit_storage : require |
| 62 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 63 | #extension GL_KHR_shader_subgroup_basic : require |
| 64 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 65 | |
| 66 | layout(local_size_x = 64) in; |
| 67 | |
| 68 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; |
| 69 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 70 | layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; }; |
| 71 | |
| 72 | layout(push_constant) uniform Push { |
| 73 | uint quads; |
| 74 | uint in_c; |
| 75 | uint count; |
| 76 | }; |
| 77 | |
| 78 | /// The only difference from `gemv_g_shape_full.comp`. |
| 79 | #define ROWS 8u |
| 80 | #define I4_BLOCK 32u |
| 81 | /// Scale-table masking only. The **activation** loads are unmasked — see |
| 82 | /// `gemv_g_shape_full.comp` for why the mask had to go. |
| 83 | #define ACT_MASK 4095u |
| 84 | |
| 85 | shared float partial[ROWS][64]; |
| 86 | |
| 87 | void main() { |
| 88 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 89 | if (tile >= count) { |
| 90 | return; |
| 91 | } |
| 92 | uint channel_base = tile * ROWS; |
| 93 | uint lane = gl_LocalInvocationID.x; |
| 94 | uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 95 | float acc[ROWS]; |
| 96 | for (uint r = 0u; r < ROWS; r++) { |
| 97 | acc[r] = 0.0; |
| 98 | } |
| 99 | for (uint base = lane * I4_BLOCK; base < in_c; base += 64u * I4_BLOCK) { |
| 100 | uint block = base / I4_BLOCK; |
| 101 | float scale[ROWS]; |
| 102 | for (uint r = 0u; r < ROWS; r++) { |
| 103 | scale[r] = float(act[((channel_base + r) * blocks + block) & ACT_MASK]); |
| 104 | } |
| 105 | // Eight independent (load, unpack, accumulate) pairs instead of two. The addresses |
| 106 | // are all known before the first load returns, so these can overlap. |
| 107 | for (uint r = 0u; r < ROWS; r++) { |
| 108 | uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u]; |
| 109 | float total = 0.0; |
| 110 | for (uint word = 0u; word < 4u; word++) { |
| 111 | uint bits = packed[word]; |
| 112 | for (uint n = 0u; n < 8u; n++) { |
| 113 | int code = bitfieldExtract(int(bits), int(n << 2u), 4); |
| 114 | total += float(code) * float(act[(base + word * 8u + n) & ACT_MASK]); |
| 115 | } |
| 116 | } |
| 117 | acc[r] += total * scale[r]; |
| 118 | } |
| 119 | } |
| 120 | for (uint r = 0u; r < ROWS; r++) { |
| 121 | float summed = subgroupAdd(acc[r]); |
| 122 | if (subgroupElect()) { |
| 123 | partial[r][gl_SubgroupID] = summed; |
| 124 | } |
| 125 | } |
| 126 | barrier(); |
| 127 | if (lane >= ROWS) { |
| 128 | return; |
| 129 | } |
| 130 | float total = 0.0; |
| 131 | for (uint i = 0u; i < gl_NumSubgroups; i++) { |
| 132 | total += partial[lane][i]; |
| 133 | } |
| 134 | if (floatBitsToUint(total) == 0xFFFFFFFFu) { |
| 135 | sink[0] = 1u; |
| 136 | } |
| 137 | } |