Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_j_split8x8.comp
gemv_j_split8x8.comp144 lines · 6.4 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT J — 8x8 SPLIT WORKGROUP, 6 K-ITERATIONS. litert-analyst's design, and the dose point |
| 3 | //! that makes G -> I -> J a curve rather than two isolated numbers. |
| 4 | //! |
| 5 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 6 | //! `analysis/gemv_probe/gemv_j_split8x8.comp`. See `gemv_a_control.comp` for why this copy |
| 7 | //! exists and when to delete it. |
| 8 | //! |
| 9 | //! # Why this exists |
| 10 | //! |
| 11 | //! diff-analyst and litert-analyst independently flagged the same risk with variant I: **three |
| 12 | //! iterations may be too few to produce an unambiguous signal**, partly because only two of the |
| 13 | //! three have a successor to overlap with. A single point at 3 can be read as "the effect is |
| 14 | //! small" when it is really "the dose was small". |
| 15 | //! |
| 16 | //! Adding J gives three points on the same axis with no new harness: |
| 17 | //! |
| 18 | //! | | iterations a lane | lanes | `ROWS` | predicted (litert-analyst) | |
| 19 | //! | :--- | ---: | ---: | ---: | ---: | |
| 20 | //! | **G** (production shape) | **1** | 48 of 64 | 2 | 2.77 GB/s — measured | |
| 21 | //! | **I** (16-lane) | **3** | 16 of 16 | 2 | ~6.2 GB/s | |
| 22 | //! | **J** (this) | **6** | 64 of 64 | 8 | higher again | |
| 23 | //! |
| 24 | //! **A monotonic rise across 1 -> 3 -> 6 is the evidence.** Any single point is not. |
| 25 | //! |
| 26 | //! # The design — don't put all 64 lanes on K |
| 27 | //! |
| 28 | //! My "you cannot K-loop 64 lanes over 48 blocks" was the right constraint but the wrong |
| 29 | //! conclusion: shrinking the workgroup (variant I) is only one way out, and it costs 4x the |
| 30 | //! threads. Splitting the workgroup on **two** indices keeps all 64: |
| 31 | //! |
| 32 | //! k_lane = lane & 7 (0..7) -> each covers 6 of the 48 blocks |
| 33 | //! row_group = lane >> 3 (0..7) -> each owns a different output channel |
| 34 | //! |
| 35 | //! 8 x 8 = 64 lanes, every one busy, every one running a 6-iteration K-loop. Unlike `ROWS 8` |
| 36 | //! (variant H) the extra rows do not cost a lane its K-work; unlike variant I no threads are |
| 37 | //! given up. |
| 38 | //! |
| 39 | //! Coverage check at `in_c = 1536`, 48 blocks: `base` = `k_lane*32`, `+256`, ... `+1280`, so |
| 40 | //! lane `k` takes blocks `k, k+8, k+16, k+24, k+32, k+40` — 6 blocks x 8 lanes = 48 exactly once. |
| 41 | //! |
| 42 | //! # The reduction, and the bonus that is NOT taken here |
| 43 | //! |
| 44 | //! litert-analyst noted that 8 contiguous lanes sit inside one 16-wide Mali subgroup, so the |
| 45 | //! cross-lane sum could be a subgroup shuffle with no shared memory and no `barrier()`. That is |
| 46 | //! true and it is the right production choice — but it needs `subgroupClusteredAdd(v, 8)` and |
| 47 | //! `GL_KHR_shader_subgroup_clustered`. |
| 48 | //! |
| 49 | //! **That support bit has now been measured and it IS present.** `subgroupSupportedOperations` |
| 50 | //! is 1791 (0x6FF) on the Pixel 8 / Mali-G715, which sets CLUSTERED (0x40) along with BASIC, |
| 51 | //! VOTE, ARITHMETIC, BALLOT, SHUFFLE, SHUFFLE_RELATIVE and QUAD, plus ROTATE and |
| 52 | //! ROTATE_CLUSTERED. `subgroupSupportedStages` is 48, i.e. FRAGMENT | COMPUTE. |
| 53 | //! |
| 54 | //! It is still not used here, deliberately: J's job is to measure the K-depth, and swapping the |
| 55 | //! reduction at the same time would confound the two. A plain `subgroupAdd` would be outright |
| 56 | //! wrong — it sums the whole 16-wide subgroup, which spans *two* row_groups. |
| 57 | //! |
| 58 | //! So this uses shared memory, which is correct everywhere and keeps one `barrier()` — the same |
| 59 | //! one the production kernel already pays. **If J wins, re-test with `subgroupClusteredAdd`; |
| 60 | //! that is a further saving on top, not part of what J measures.** |
| 61 | //! |
| 62 | //! # Confound, stated |
| 63 | //! |
| 64 | //! `ROWS` rises with the K-depth (8 here vs 2 in G and I) because `row_group` count is |
| 65 | //! `64 / k_lanes`. They are **structurally coupled** — at a fixed 64 lanes you cannot deepen the |
| 66 | //! K-loop without widening the rows. That coupling is inherent to the production design too, so |
| 67 | //! measuring the package is the honest thing; it just means J is not a clean single-axis probe. |
| 68 | //! H (`ROWS 8`, 1 iteration) is the control that separates them: if H is flat and J rises, the |
| 69 | //! K-depth is doing the work, not the rows. |
| 70 | //! |
| 71 | //! # Dispatch |
| 72 | //! |
| 73 | //! One workgroup per **eight** output channels: `count = out / 8` = 8,192, same as H. Bytes per |
| 74 | //! workgroup are 4x G's, and there are 4x fewer workgroups, so the total swept is identical. |
| 75 | |
| 76 | #extension GL_EXT_shader_16bit_storage : require |
| 77 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 78 | #extension GL_KHR_shader_subgroup_basic : require |
| 79 | #extension GL_KHR_shader_subgroup_arithmetic : require |
| 80 | |
| 81 | layout(local_size_x = 64) in; |
| 82 | |
| 83 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; |
| 84 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 85 | layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; }; |
| 86 | |
| 87 | layout(push_constant) uniform Push { |
| 88 | uint quads; |
| 89 | uint in_c; |
| 90 | uint count; |
| 91 | }; |
| 92 | |
| 93 | /// Lanes cooperating on one output channel. 48 blocks / 8 = 6 iterations at `in_c` 1536. |
| 94 | #define K_LANES 8u |
| 95 | /// Output channels a workgroup owns. `64 / K_LANES`. |
| 96 | #define ROWS 8u |
| 97 | #define I4_BLOCK 32u |
| 98 | #define ACT_MASK 4095u |
| 99 | |
| 100 | /// `[row_group][k_lane]`, for the 8-way reduction that cannot be a plain `subgroupAdd`. |
| 101 | shared float partial[ROWS][K_LANES]; |
| 102 | |
| 103 | void main() { |
| 104 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 105 | if (tile >= count) { |
| 106 | return; |
| 107 | } |
| 108 | uint lane = gl_LocalInvocationID.x; |
| 109 | uint k_lane = lane & (K_LANES - 1u); |
| 110 | uint row_group = lane / K_LANES; |
| 111 | uint channel = tile * ROWS + row_group; |
| 112 | uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 113 | |
| 114 | float acc = 0.0; |
| 115 | // Six iterations at in_c 1536, against G's one. This is the whole variant. |
| 116 | for (uint base = k_lane * I4_BLOCK; base < in_c; base += K_LANES * I4_BLOCK) { |
| 117 | float scale = float(act[(channel * blocks + base / I4_BLOCK) & ACT_MASK]); |
| 118 | uvec4 packed = weights128[(channel * in_c + base) / 32u]; |
| 119 | float total = 0.0; |
| 120 | for (uint word = 0u; word < 4u; word++) { |
| 121 | uint bits = packed[word]; |
| 122 | for (uint n = 0u; n < 8u; n++) { |
| 123 | int code = bitfieldExtract(int(bits), int(n << 2u), 4); |
| 124 | total += float(code) * float(act[(base + word * 8u + n) & ACT_MASK]); |
| 125 | } |
| 126 | } |
| 127 | acc += total * scale; |
| 128 | } |
| 129 | |
| 130 | // Shared memory, not `subgroupAdd`: a 16-wide subgroup spans two row_groups, so a plain |
| 131 | // subgroup sum would add two output channels together. See the header. |
| 132 | partial[row_group][k_lane] = acc; |
| 133 | barrier(); |
| 134 | if (lane >= ROWS) { |
| 135 | return; |
| 136 | } |
| 137 | float total = 0.0; |
| 138 | for (uint i = 0u; i < K_LANES; i++) { |
| 139 | total += partial[lane][i]; |
| 140 | } |
| 141 | if (floatBitsToUint(total) == 0xFFFFFFFFu) { |
| 142 | sink[0] = 1u; |
| 143 | } |
| 144 | } |