Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_k_rwarena.comp
gemv_k_rwarena.comp124 lines · 4.7 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT K — VARIANT G WITH A READ-WRITE ARENA. Tests whether aliasing blocks load fusion. |
| 3 | //! |
| 4 | //! TEMPORARY probe shader. Source of truth is this file; see `gemv_a_control.comp` for why a |
| 5 | //! copy lives in `shaders/` and when to delete it. |
| 6 | //! |
| 7 | //! # Why |
| 8 | //! |
| 9 | //! Removing `& ACT_MASK` from variant G took it from 2.64 to 18.5 GB/s — 7x — because the mask |
| 10 | //! made `base + word*8 + n` no longer *provably consecutive*, so the compiler could not fuse 8 |
| 11 | //! scalar `float16_t` loads into one 16-byte vector load. The cost was never the `AND`; it was |
| 12 | //! the fusion it prevented. |
| 13 | //! |
| 14 | //! That fixed probe is now **4x FASTER than production** (18.5 vs 4.61 GB/s end-to-end). So the |
| 15 | //! real kernel has something the probe does not, and the obvious place to look is the same |
| 16 | //! class: something that blocks the same fusion. |
| 17 | //! |
| 18 | //! **`common.glsl:38` declares the arena READ-WRITE:** |
| 19 | //! |
| 20 | //! ```glsl |
| 21 | //! layout(std430, binding = 0) buffer Arena { float16_t arena[]; }; // no `readonly` |
| 22 | //! ``` |
| 23 | //! |
| 24 | //! and `conv_vec_int4.comp:179` stores into it: |
| 25 | //! |
| 26 | //! ```glsl |
| 27 | //! arena[p.out_at + channel] = float16_t(activate(total + offset, p.act, channel)); |
| 28 | //! ``` |
| 29 | //! |
| 30 | //! The activation loads at `:114` read **the same buffer** the shader writes. The compiler |
| 31 | //! cannot prove the store does not alias the loads, so it may not fuse them — and may be forced |
| 32 | //! to reload across the store. Every probe variant so far declared its activation buffer |
| 33 | //! `readonly`, which hands the compiler a guarantee production does not have. |
| 34 | //! |
| 35 | //! # What K changes, and only this |
| 36 | //! |
| 37 | //! Byte-for-byte variant G, except: |
| 38 | //! * `Acts` loses `readonly`, matching `Arena` |
| 39 | //! * the result is stored back into that buffer, as `conv_vec_int4.comp:179` does |
| 40 | //! |
| 41 | //! The store targets `[in_c, in_c + 64)`, past the activation range, so it cannot corrupt the |
| 42 | //! inputs — but the compiler cannot know that, which is exactly the point. |
| 43 | //! |
| 44 | //! # Reading it |
| 45 | //! |
| 46 | //! * `K ≈ G` (~18.5) -> aliasing is not the blocker; the production gap is elsewhere. |
| 47 | //! * `K << G`, near production's ~4.6 -> **the read-write arena is blocking load fusion in the |
| 48 | //! shipped kernel**, and the fix is a `readonly` alias, a `restrict` qualifier, or a separate |
| 49 | //! binding for the input slice. That would be a large, cheap, real win. |
| 50 | |
| 51 | #extension GL_EXT_shader_16bit_storage : require |
| 52 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 53 | #extension GL_KHR_shader_subgroup_basic : require |
| 54 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 55 | |
| 56 | layout(local_size_x = 64) in; |
| 57 | |
| 58 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; |
| 59 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 60 | /// **Not** `readonly` — this is the whole variant. Matches `common.glsl:38`. |
| 61 | layout(set = 0, binding = 2) buffer Acts { float16_t act[]; }; |
| 62 | |
| 63 | layout(push_constant) uniform Push { |
| 64 | uint quads; |
| 65 | uint in_c; |
| 66 | uint count; |
| 67 | }; |
| 68 | |
| 69 | #define ROWS 2u |
| 70 | #define I4_BLOCK 32u |
| 71 | /// Scale-table masking only, as in variant G. |
| 72 | #define ACT_MASK 4095u |
| 73 | |
| 74 | shared float partial[ROWS][64]; |
| 75 | |
| 76 | void main() { |
| 77 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 78 | if (tile >= count) { |
| 79 | return; |
| 80 | } |
| 81 | uint channel_base = tile * ROWS; |
| 82 | uint lane = gl_LocalInvocationID.x; |
| 83 | uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 84 | float acc[ROWS]; |
| 85 | for (uint r = 0u; r < ROWS; r++) { |
| 86 | acc[r] = 0.0; |
| 87 | } |
| 88 | for (uint base = lane * I4_BLOCK; base < in_c; base += 64u * I4_BLOCK) { |
| 89 | uint block = base / I4_BLOCK; |
| 90 | float scale[ROWS]; |
| 91 | for (uint r = 0u; r < ROWS; r++) { |
| 92 | scale[r] = float(act[((channel_base + r) * blocks + block) & ACT_MASK]); |
| 93 | } |
| 94 | for (uint r = 0u; r < ROWS; r++) { |
| 95 | uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u]; |
| 96 | float total = 0.0; |
| 97 | for (uint word = 0u; word < 4u; word++) { |
| 98 | uint bits = packed[word]; |
| 99 | for (uint n = 0u; n < 8u; n++) { |
| 100 | int code = bitfieldExtract(int(bits), int(n << 2u), 4); |
| 101 | total += float(code) * float(act[base + word * 8u + n]); |
| 102 | } |
| 103 | } |
| 104 | acc[r] += total * scale[r]; |
| 105 | } |
| 106 | } |
| 107 | for (uint r = 0u; r < ROWS; r++) { |
| 108 | float summed = subgroupAdd(acc[r]); |
| 109 | if (subgroupElect()) { |
| 110 | partial[r][gl_SubgroupID] = summed; |
| 111 | } |
| 112 | } |
| 113 | barrier(); |
| 114 | if (lane >= ROWS) { |
| 115 | return; |
| 116 | } |
| 117 | float total = 0.0; |
| 118 | for (uint i = 0u; i < gl_NumSubgroups; i++) { |
| 119 | total += partial[lane][i]; |
| 120 | } |
| 121 | // The store into the same buffer the activations came from, as the real kernel does. Past |
| 122 | // the activation range so the inputs stay intact - but the compiler cannot prove that. |
| 123 | act[in_c + lane] = float16_t(total); |
| 124 | } |