Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_d_deep.comp
gemv_d_deep.comp80 lines · 3.5 KiBRaw
| 1 | #version 450 |
| 2 | #extension GL_EXT_control_flow_attributes : require |
| 3 | //! VARIANT D — DEEP PREFETCH, LIGHT ALU. Variant A with 16 loads in flight instead of one. |
| 4 | //! |
| 5 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 6 | //! `analysis/gemv_probe/gemv_d_deep.comp`. See `gemv_a_control.comp` for why this copy |
| 7 | //! exists and when to delete it. |
| 8 | //! |
| 9 | //! The GLSL analogue of ml_drift's `__wave Type weights_cache[16]` + |
| 10 | //! `ucl::WaveLoad(weights_cache, ptr, offset, 16)`: a fixed-size local array filled by a |
| 11 | //! constant-trip-count loop, so glslc unrolls it and issues all sixteen loads before the |
| 12 | //! first is consumed. `DEPTH` is 16 to match the smaller of the two sizes recovered from |
| 13 | //! `libLiteRtGpuAccelerator.so`. |
| 14 | //! |
| 15 | //! `D - A` is the value of memory-level parallelism at this access pattern, with the ALU |
| 16 | //! held at one XOR per 16 bytes. If D is materially above A, prefetch depth is a real lever |
| 17 | //! (H1). If D and A are the same, the control was already saturating the memory system with |
| 18 | //! one load in flight and depth is not the differentiator. |
| 19 | //! |
| 20 | //! # The addresses are identical to variant A |
| 21 | //! |
| 22 | //! Lane `l` still visits `l, l+stride, l+2*stride, ...`. Only the batching changes: sixteen |
| 23 | //! are issued together rather than one at a time. So any difference is prefetch depth and |
| 24 | //! not a different access pattern. |
| 25 | //! |
| 26 | //! # Register pressure is the confound, and it is real |
| 27 | //! |
| 28 | //! `uvec4 cache[16]` is 64 vector registers per lane. On a Valhall part that will cut the |
| 29 | //! number of resident warps, so D trades occupancy for loads-in-flight and the measurement |
| 30 | //! is of the **net** of the two, not of prefetch depth alone. A D that is merely equal to A |
| 31 | //! may be depth helping and occupancy hurting in equal measure. Noted rather than tuned |
| 32 | //! around: a smaller DEPTH is the obvious follow-up if D is interesting. |
| 33 | |
| 34 | layout(local_size_x = 64) in; |
| 35 | |
| 36 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 data[]; }; |
| 37 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 38 | |
| 39 | layout(push_constant) uniform Push { |
| 40 | uint quads; |
| 41 | }; |
| 42 | |
| 43 | // A literal `#define` rather than a `const uint`, so the array size and both trip counts are |
| 44 | // textually constant and glslc has no excuse not to unroll. If variant D reads identically to |
| 45 | // A, disassemble with `spirv-dis` and confirm the sixteen loads were actually batched before |
| 46 | // concluding anything about prefetch depth. |
| 47 | #define DEPTH 16 |
| 48 | |
| 49 | void main() { |
| 50 | uint lane = gl_GlobalInvocationID.x; |
| 51 | uint stride = gl_NumWorkGroups.x * 64u; |
| 52 | uvec4 acc = uvec4(0u); |
| 53 | |
| 54 | // Whole batches first, with no per-load bounds test — a branch between the loads would |
| 55 | // be exactly the thing that stops them being issued together. |
| 56 | uint iters = quads / stride; |
| 57 | uint batches = iters / uint(DEPTH); |
| 58 | uint at = lane; |
| 59 | for (uint b = 0u; b < batches; b++) { |
| 60 | uvec4 cache[DEPTH]; |
| 61 | [[unroll]] for (uint d = 0u; d < uint(DEPTH); d++) { |
| 62 | cache[d] = data[at + d * stride]; |
| 63 | } |
| 64 | [[unroll]] for (uint d = 0u; d < uint(DEPTH); d++) { |
| 65 | acc ^= cache[d]; |
| 66 | } |
| 67 | at += uint(DEPTH) * stride; |
| 68 | } |
| 69 | // Tail, one at a time. At 256 MB and 2,048 workgroups this is empty — 16,777,216 quads |
| 70 | // over a stride of 131,072 is 128 iterations, exactly eight batches — but the loop is |
| 71 | // kept so the shader is correct at any size. |
| 72 | for (; at < quads; at += stride) { |
| 73 | acc ^= data[at]; |
| 74 | } |
| 75 | |
| 76 | uint folded = acc.x ^ acc.y ^ acc.z ^ acc.w; |
| 77 | if (folded == 0xFFFFFFFFu) { |
| 78 | sink[0] = folded; |
| 79 | } |
| 80 | } |