Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_f_shape.comp
gemv_f_shape.comp109 lines · 4.6 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT F — REAL KERNEL SHAPE, TRIVIAL ALU. The variant that tests lead's finding. |
| 3 | //! |
| 4 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 5 | //! `analysis/gemv_probe/gemv_f_shape.comp`. See `gemv_a_control.comp` for why this copy |
| 6 | //! exists and when to delete it. |
| 7 | //! |
| 8 | //! # Why variants A-E do not test the thing that matters |
| 9 | //! |
| 10 | //! Every one of them grid-strides over 256 MB, so a lane runs ~128 iterations and has a |
| 11 | //! continuous stream of work. `conv_vec_int4.comp` does not. At `p.in_c = 1536` and |
| 12 | //! `I4_BLOCK = 32` its loop is |
| 13 | //! |
| 14 | //! for (uint base = lane * 32u; base < 1536u; base += 2048u) |
| 15 | //! |
| 16 | //! so `base + 2048 >= 1536` on the first step and **the body runs exactly once**, while |
| 17 | //! `lane * 32 < 1536` means **only lanes 0..47 enter it at all** — 16 of 64 idle. A wave's |
| 18 | //! whole life is: issue two `uvec4` loads, stall the full memory latency, do 64 MACs, hit |
| 19 | //! `barrier()`, exit. There is no second iteration to overlap the first's latency with. |
| 20 | //! |
| 21 | //! That is not a small detail. `in_c = 1536` covers q, k, v, the fused gate-up, the four |
| 22 | //! logits splits and the per-layer projection — **about 902 MB of the 1,297 MB a decode |
| 23 | //! step reads, 70% of the bytes.** (`o_proj` at 2048 also runs once but uses all 64 lanes; |
| 24 | //! only `down` at 6144/12288 gets 3 and 6 iterations.) |
| 25 | //! |
| 26 | //! So A-E measure "dequant ALU inside a stream", which is a question nobody is asking. |
| 27 | //! F and G measure the shape the real kernel actually has. |
| 28 | //! |
| 29 | //! # What F is |
| 30 | //! |
| 31 | //! `conv_vec_int4.comp`'s exact structure — same tile mapping, same `lane * I4_BLOCK` start, |
| 32 | //! same 2048 stride, same two `uvec4` loads per lane, same `subgroupAdd`, same `barrier`, |
| 33 | //! same shared reduction — with the 160-op dequant replaced by four ops. Everything about |
| 34 | //! the *shape* is preserved and only the arithmetic is removed. |
| 35 | //! |
| 36 | //! Read against G (same shape, full arithmetic) and against A (full stream, trivial ALU): |
| 37 | //! |
| 38 | //! * `F ~= G ~= 3.4` and `A ~= 20` -> the **shape** is the whole story. ALU is irrelevant, |
| 39 | //! and the fix is to give each lane a stream: more taps per lane, fewer lanes idle, or a |
| 40 | //! loop that actually iterates. |
| 41 | //! * `F ~= 20`, `G ~= 3.4` -> the **ALU** is the whole story after all. |
| 42 | //! * `F ~= 8`, `G ~= 3.4` -> both contribute, and their sizes say in what ratio. |
| 43 | //! |
| 44 | //! G should land on the ~3.4-4.0 GB/s already measured for `[65536, 1536]`. If it does not, |
| 45 | //! F and G are not reproducing the real kernel and neither number means anything — G is the |
| 46 | //! second anchor, exactly as A is the first. |
| 47 | |
| 48 | #extension GL_KHR_shader_subgroup_basic : require |
| 49 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 50 | |
| 51 | layout(local_size_x = 64) in; |
| 52 | |
| 53 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; |
| 54 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 55 | |
| 56 | layout(push_constant) uniform Push { |
| 57 | uint quads; |
| 58 | /// Taps per output channel. 1536 reproduces the shape 70% of the model's bytes have. |
| 59 | uint in_c; |
| 60 | /// Output tiles, i.e. `out_channels / ROWS`. One workgroup each, as `p.count`. |
| 61 | uint count; |
| 62 | }; |
| 63 | |
| 64 | #define ROWS 2u |
| 65 | #define I4_BLOCK 32u |
| 66 | |
| 67 | shared float partial[ROWS][64]; |
| 68 | |
| 69 | void main() { |
| 70 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 71 | if (tile >= count) { |
| 72 | return; |
| 73 | } |
| 74 | uint channel_base = tile * ROWS; |
| 75 | uint lane = gl_LocalInvocationID.x; |
| 76 | float acc[ROWS]; |
| 77 | for (uint r = 0u; r < ROWS; r++) { |
| 78 | acc[r] = 0.0; |
| 79 | } |
| 80 | // Byte-for-byte the loop bounds of `conv_vec_int4.comp:75`. At in_c 1536 this runs once |
| 81 | // and lanes 48..63 skip it entirely - which is the point of the variant, not a bug. |
| 82 | for (uint base = lane * I4_BLOCK; base < in_c; base += 64u * I4_BLOCK) { |
| 83 | for (uint r = 0u; r < ROWS; r++) { |
| 84 | uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u]; |
| 85 | // Four ops where the real kernel does ~160. The load, the address arithmetic and |
| 86 | // the loop structure are identical; only the consumption differs. |
| 87 | acc[r] += float(packed.x ^ packed.y ^ packed.z ^ packed.w); |
| 88 | } |
| 89 | } |
| 90 | // The reduction tail, unchanged from `conv_vec_int4.comp:159-176`, so the barrier and the |
| 91 | // cross-subgroup sync are paid here too. |
| 92 | for (uint r = 0u; r < ROWS; r++) { |
| 93 | float summed = subgroupAdd(acc[r]); |
| 94 | if (subgroupElect()) { |
| 95 | partial[r][gl_SubgroupID] = summed; |
| 96 | } |
| 97 | } |
| 98 | barrier(); |
| 99 | if (lane >= ROWS) { |
| 100 | return; |
| 101 | } |
| 102 | float total = 0.0; |
| 103 | for (uint i = 0u; i < gl_NumSubgroups; i++) { |
| 104 | total += partial[lane][i]; |
| 105 | } |
| 106 | if (floatBitsToUint(total) == 0xFFFFFFFFu) { |
| 107 | sink[0] = 1u; |
| 108 | } |
| 109 | } |