Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_g_shape_full.comp
gemv_g_shape_full.comp117 lines · 5.2 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT G — REAL KERNEL SHAPE, FULL DEQUANT. `conv_vec_int4.comp` reproduced. |
| 3 | //! |
| 4 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 5 | //! `analysis/gemv_probe/gemv_g_shape_full.comp`. See `gemv_a_control.comp` for why this copy |
| 6 | //! exists and when to delete it. |
| 7 | //! |
| 8 | //! The second anchor. This is the real kernel's structure *and* its arithmetic: the same |
| 9 | //! tile mapping, the same one-iteration loop at `in_c = 1536`, the same 16 idle lanes, the |
| 10 | //! same per-block scale fetch, the same 32 scalar activation loads per `uvec4`, the same |
| 11 | //! `subgroupAdd` and shared-memory reduction. |
| 12 | //! |
| 13 | //! **G must land on the 3.4-4.0 GB/s already measured for `[65536, 1536]`.** If it does not, |
| 14 | //! F and G are not reproducing the real kernel and neither is evidence of anything. A is the |
| 15 | //! anchor at the ceiling; G is the anchor at the floor. |
| 16 | //! |
| 17 | //! # The one deliberate deviation — NOW NARROWED, because it was distorting the result |
| 18 | //! |
| 19 | //! The **scale** table still comes from the shared fp16 buffer, masked into range. That is one |
| 20 | //! `AND` per row per trip (2 per trip against 64 activation loads) and is negligible. |
| 21 | //! |
| 22 | //! The **activation** loads were also masked, and that was not negligible. `& ACT_MASK` on every |
| 23 | //! access is **32 extra ALU ops per `uvec4`** on top of ~160 — a ~20% instruction increase |
| 24 | //! injected into the exact loop this variant exists to measure. The real kernel has no such mask |
| 25 | //! (`conv_vec_int4.comp:114` is a plain `arena[p.in0 + base + word * 8u + n]`). |
| 26 | //! |
| 27 | //! That deviation was originally judged harmless — "a mask is one ALU op and does not change the |
| 28 | //! instruction mix meaningfully" — which was reasonable while we believed the kernel was |
| 29 | //! bandwidth-bound. Once `A→F = 1.00x` and `F→G = 10.8x` established that this loop is |
| 30 | //! ALU-bound, the assumption inverted: the mask lands precisely on the binding constraint. |
| 31 | //! Measured consequence: production decode runs 1.6-1.8x faster than this probe. |
| 32 | //! |
| 33 | //! So the activation buffer is now sized to `in_c` by the runner, exactly as the real kernel's |
| 34 | //! activation vector is (3 KB at `in_c` 1536, 24 KB at 12288), and the mask is gone. This both |
| 35 | //! removes the 32 ops and reproduces the real cache footprint instead of a fixed 8 KB. |
| 36 | |
| 37 | #extension GL_EXT_shader_16bit_storage : require |
| 38 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 39 | #extension GL_KHR_shader_subgroup_basic : require |
| 40 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 41 | |
| 42 | layout(local_size_x = 64) in; |
| 43 | |
| 44 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; |
| 45 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 46 | layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; }; |
| 47 | |
| 48 | layout(push_constant) uniform Push { |
| 49 | uint quads; |
| 50 | uint in_c; |
| 51 | uint count; |
| 52 | }; |
| 53 | |
| 54 | #define ROWS 2u |
| 55 | #define I4_BLOCK 32u |
| 56 | /// Masks the **scale** index only. The activation buffer is sized to `in_c` by the runner, so |
| 57 | /// activation loads need no mask — which is the point, see the header. |
| 58 | #define ACT_MASK 4095u |
| 59 | |
| 60 | shared float partial[ROWS][64]; |
| 61 | |
| 62 | void main() { |
| 63 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 64 | if (tile >= count) { |
| 65 | return; |
| 66 | } |
| 67 | uint channel_base = tile * ROWS; |
| 68 | uint lane = gl_LocalInvocationID.x; |
| 69 | uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 70 | float acc[ROWS]; |
| 71 | for (uint r = 0u; r < ROWS; r++) { |
| 72 | acc[r] = 0.0; |
| 73 | } |
| 74 | for (uint base = lane * I4_BLOCK; base < in_c; base += 64u * I4_BLOCK) { |
| 75 | uint block = base / I4_BLOCK; |
| 76 | // The per-block scale fetch, one per row, as `conv_vec_int4.comp:77-80`. |
| 77 | float scale[ROWS]; |
| 78 | for (uint r = 0u; r < ROWS; r++) { |
| 79 | scale[r] = float(act[((channel_base + r) * blocks + block) & ACT_MASK]); |
| 80 | } |
| 81 | for (uint r = 0u; r < ROWS; r++) { |
| 82 | uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u]; |
| 83 | float total = 0.0; |
| 84 | // `conv_vec_int4.comp:110-116`, verbatim: 32 nibbles, 32 scalar fp16 activation |
| 85 | // loads, 32 FMAs. Note the activation index does not depend on `r`, exactly as in |
| 86 | // the real kernel - so if the compiler does not hoist it, both variants pay the |
| 87 | // same doubled load count and the comparison stays honest. |
| 88 | for (uint word = 0u; word < 4u; word++) { |
| 89 | uint bits = packed[word]; |
| 90 | for (uint n = 0u; n < 8u; n++) { |
| 91 | int code = bitfieldExtract(int(bits), int(n << 2u), 4); |
| 92 | // Unmasked, matching `conv_vec_int4.comp:114`. The runner guarantees the |
| 93 | // buffer holds at least `in_c` elements. |
| 94 | total += float(code) * float(act[base + word * 8u + n]); |
| 95 | } |
| 96 | } |
| 97 | acc[r] += total * scale[r]; |
| 98 | } |
| 99 | } |
| 100 | for (uint r = 0u; r < ROWS; r++) { |
| 101 | float summed = subgroupAdd(acc[r]); |
| 102 | if (subgroupElect()) { |
| 103 | partial[r][gl_SubgroupID] = summed; |
| 104 | } |
| 105 | } |
| 106 | barrier(); |
| 107 | if (lane >= ROWS) { |
| 108 | return; |
| 109 | } |
| 110 | float total = 0.0; |
| 111 | for (uint i = 0u; i < gl_NumSubgroups; i++) { |
| 112 | total += partial[lane][i]; |
| 113 | } |
| 114 | if (floatBitsToUint(total) == 0xFFFFFFFFu) { |
| 115 | sink[0] = 1u; |
| 116 | } |
| 117 | } |