#version 450 //! VARIANT I — 16-LANE WORKGROUP, REAL K-ITERATIONS. Tests the axis variant C actually validated. //! //! TEMPORARY probe shader for task 9. Source of truth is //! `analysis/gemv_probe/gemv_i_kloop16.comp`. See `gemv_a_control.comp` for why this copy //! exists and when to delete it. //! //! # Why this exists //! //! litert-analyst is right that variant C validated **K-iterations**, not `ROWS`. C is //! grid-stride with ~128 iterations a lane: iteration N's dequant chain hides behind //! iteration N+1's memory latency. `ROWS` is a different axis — independent output rows //! *within* one iteration — and variant H tests that. **Neither H nor this had been measured //! when `ROWS 8` shipped to production.** //! //! # The structural problem this variant works around //! //! At `in_c = 1536` and `I4_BLOCK = 32` there are exactly **48 blocks**. With a 64-lane //! workgroup there is barely one block per lane, which is why the loop runs once and why //! lanes 48..63 idle. **You cannot K-loop 64 lanes over 48 blocks.** So on the shapes that //! carry 71% of the bytes, the K-axis is unavailable at the current workgroup size — which //! is the honest reason `ROWS` looked like the only lever, and why the recommendation went //! out as "a `#define`". //! //! Shrinking the workgroup restores it. At **16 lanes** the stride is `16 * 32 = 512` taps, //! so `in_c = 1536` gives each lane **three** sequential blocks — a real K-loop, with every //! lane busy: //! //! lane 0: blocks 0, 16, 32 //! lane 1: blocks 1, 17, 33 ... lane 15: blocks 15, 31, 47 //! //! Three iterations is not C's 128, but it is the difference between "no next iteration" and //! "a next iteration exists", which is the whole of what the 7.02x measured. //! //! # Read against G and H //! //! * `I >> G` and `H ~= G` -> **K-iterations are the mechanism, `ROWS` is not.** The //! production `ROWS 8` change is not the fix and should be reconsidered; the fix is a //! workgroup-size and lane-mapping change. //! * `H >> G` and `I ~= G` -> `ROWS` is the mechanism after all and the shipped change is right. //! * both above `G` -> two independent sources of ILP; prefer whichever costs less. `ROWS` //! costs registers (more live accumulators, lower occupancy); K-looping costs none, because //! the same accumulator is reused across iterations. //! * neither above `G` -> the exposure is not ILP at all, and register pressure or the //! `barrier()` is the next suspect. //! //! # Confounds, stated rather than hidden //! //! This changes three things at once — workgroup size 64 -> 16, K-iterations 1 -> 3, and //! subgroups per workgroup 4 -> 1 (so the cross-subgroup `barrier()` becomes trivial on a //! 16-wide Mali subgroup). **They are not separable**: at `in_c = 1536` you cannot obtain //! K-iterations without reducing the lane count, because there are only 48 blocks to go //! round. So this measures "the K-loop route" as a package, which is what it would be in //! production too. //! //! `ROWS` stays at 2 so the only difference from G is that package. //! //! # Dispatch //! //! Unchanged from G: one workgroup per `ROWS = 2` output channels, `count = out / 2` = //! 32,768. Each workgroup is 16 threads rather than 64, so total invocations drop 4x — still //! 32,768 subgroups against 448 warp slots, 73x oversubscribed. #extension GL_EXT_shader_16bit_storage : require #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require #extension GL_KHR_shader_subgroup_basic : require #extension GL_KHR_shader_subgroup_arithmetic : require /// 16, not 64. This is the variant. layout(local_size_x = 16) in; layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; }; layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; }; layout(push_constant) uniform Push { uint quads; uint in_c; uint count; }; #define ROWS 2u #define I4_BLOCK 32u #define LANES 16u #define ACT_MASK 4095u shared float partial[ROWS][LANES]; void main() { uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= count) { return; } uint channel_base = tile * ROWS; uint lane = gl_LocalInvocationID.x; uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; float acc[ROWS]; for (uint r = 0u; r < ROWS; r++) { acc[r] = 0.0; } // The only change from G: the stride is `LANES * I4_BLOCK` = 512 rather than 2048, so at // in_c 1536 this runs THREE times instead of once, and every lane participates. for (uint base = lane * I4_BLOCK; base < in_c; base += LANES * I4_BLOCK) { uint block = base / I4_BLOCK; float scale[ROWS]; for (uint r = 0u; r < ROWS; r++) { scale[r] = float(act[((channel_base + r) * blocks + block) & ACT_MASK]); } for (uint r = 0u; r < ROWS; r++) { uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u]; float total = 0.0; for (uint word = 0u; word < 4u; word++) { uint bits = packed[word]; for (uint n = 0u; n < 8u; n++) { int code = bitfieldExtract(int(bits), int(n << 2u), 4); total += float(code) * float(act[(base + word * 8u + n) & ACT_MASK]); } } acc[r] += total * scale[r]; } } for (uint r = 0u; r < ROWS; r++) { float summed = subgroupAdd(acc[r]); if (subgroupElect()) { partial[r][gl_SubgroupID] = summed; } } barrier(); if (lane >= ROWS) { return; } float total = 0.0; for (uint i = 0u; i < gl_NumSubgroups; i++) { total += partial[lane][i]; } if (floatBitsToUint(total) == 0xFFFFFFFFu) { sink[0] = 1u; } }