Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / library / ml / src / main / rust / shaders / gemv_i_kloop16.comp

gemv_i_kloop16.comp139 lines · 5.7 KiBRaw
1#version 450
2//! VARIANT I — 16-LANE WORKGROUP, REAL K-ITERATIONS. Tests the axis variant C actually validated.
3//!
4//! TEMPORARY probe shader for task 9. Source of truth is
5//! `analysis/gemv_probe/gemv_i_kloop16.comp`. See `gemv_a_control.comp` for why this copy
6//! exists and when to delete it.
7//!
8//! # Why this exists
9//!
10//! litert-analyst is right that variant C validated **K-iterations**, not `ROWS`. C is
11//! grid-stride with ~128 iterations a lane: iteration N's dequant chain hides behind
12//! iteration N+1's memory latency. `ROWS` is a different axis — independent output rows
13//! *within* one iteration — and variant H tests that. **Neither H nor this had been measured
14//! when `ROWS 8` shipped to production.**
15//!
16//! # The structural problem this variant works around
17//!
18//! At `in_c = 1536` and `I4_BLOCK = 32` there are exactly **48 blocks**. With a 64-lane
19//! workgroup there is barely one block per lane, which is why the loop runs once and why
20//! lanes 48..63 idle. **You cannot K-loop 64 lanes over 48 blocks.** So on the shapes that
21//! carry 71% of the bytes, the K-axis is unavailable at the current workgroup size — which
22//! is the honest reason `ROWS` looked like the only lever, and why the recommendation went
23//! out as "a `#define`".
24//!
25//! Shrinking the workgroup restores it. At **16 lanes** the stride is `16 * 32 = 512` taps,
26//! so `in_c = 1536` gives each lane **three** sequential blocks — a real K-loop, with every
27//! lane busy:
28//!
29//! lane 0: blocks 0, 16, 32
30//! lane 1: blocks 1, 17, 33 ... lane 15: blocks 15, 31, 47
31//!
32//! Three iterations is not C's 128, but it is the difference between "no next iteration" and
33//! "a next iteration exists", which is the whole of what the 7.02x measured.
34//!
35//! # Read against G and H
36//!
37//! * `I >> G` and `H ~= G` -> **K-iterations are the mechanism, `ROWS` is not.** The
38//! production `ROWS 8` change is not the fix and should be reconsidered; the fix is a
39//! workgroup-size and lane-mapping change.
40//! * `H >> G` and `I ~= G` -> `ROWS` is the mechanism after all and the shipped change is right.
41//! * both above `G` -> two independent sources of ILP; prefer whichever costs less. `ROWS`
42//! costs registers (more live accumulators, lower occupancy); K-looping costs none, because
43//! the same accumulator is reused across iterations.
44//! * neither above `G` -> the exposure is not ILP at all, and register pressure or the
45//! `barrier()` is the next suspect.
46//!
47//! # Confounds, stated rather than hidden
48//!
49//! This changes three things at once — workgroup size 64 -> 16, K-iterations 1 -> 3, and
50//! subgroups per workgroup 4 -> 1 (so the cross-subgroup `barrier()` becomes trivial on a
51//! 16-wide Mali subgroup). **They are not separable**: at `in_c = 1536` you cannot obtain
52//! K-iterations without reducing the lane count, because there are only 48 blocks to go
53//! round. So this measures "the K-loop route" as a package, which is what it would be in
54//! production too.
55//!
56//! `ROWS` stays at 2 so the only difference from G is that package.
57//!
58//! # Dispatch
59//!
60//! Unchanged from G: one workgroup per `ROWS = 2` output channels, `count = out / 2` =
61//! 32,768. Each workgroup is 16 threads rather than 64, so total invocations drop 4x — still
62//! 32,768 subgroups against 448 warp slots, 73x oversubscribed.
63
64#extension GL_EXT_shader_16bit_storage : require
65#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
66#extension GL_KHR_shader_subgroup_basic : require
67#extension GL_KHR_shader_subgroup_arithmetic : require
68
69/// 16, not 64. This is the variant.
70layout(local_size_x = 16) in;
71
72layout(set = 0, binding = 0) readonly buffer Source { uvec4 weights128[]; };
73layout(set = 0, binding = 1) buffer Sink { uint sink[]; };
74layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; };
75
76layout(push_constant) uniform Push {
77 uint quads;
78 uint in_c;
79 uint count;
80};
81
82#define ROWS 2u
83#define I4_BLOCK 32u
84#define LANES 16u
85#define ACT_MASK 4095u
86
87shared float partial[ROWS][LANES];
88
89void main() {
90 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
91 if (tile >= count) {
92 return;
93 }
94 uint channel_base = tile * ROWS;
95 uint lane = gl_LocalInvocationID.x;
96 uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK;
97 float acc[ROWS];
98 for (uint r = 0u; r < ROWS; r++) {
99 acc[r] = 0.0;
100 }
101 // The only change from G: the stride is `LANES * I4_BLOCK` = 512 rather than 2048, so at
102 // in_c 1536 this runs THREE times instead of once, and every lane participates.
103 for (uint base = lane * I4_BLOCK; base < in_c; base += LANES * I4_BLOCK) {
104 uint block = base / I4_BLOCK;
105 float scale[ROWS];
106 for (uint r = 0u; r < ROWS; r++) {
107 scale[r] = float(act[((channel_base + r) * blocks + block) & ACT_MASK]);
108 }
109 for (uint r = 0u; r < ROWS; r++) {
110 uvec4 packed = weights128[((channel_base + r) * in_c + base) / 32u];
111 float total = 0.0;
112 for (uint word = 0u; word < 4u; word++) {
113 uint bits = packed[word];
114 for (uint n = 0u; n < 8u; n++) {
115 int code = bitfieldExtract(int(bits), int(n << 2u), 4);
116 total += float(code) * float(act[(base + word * 8u + n) & ACT_MASK]);
117 }
118 }
119 acc[r] += total * scale[r];
120 }
121 }
122 for (uint r = 0u; r < ROWS; r++) {
123 float summed = subgroupAdd(acc[r]);
124 if (subgroupElect()) {
125 partial[r][gl_SubgroupID] = summed;
126 }
127 }
128 barrier();
129 if (lane >= ROWS) {
130 return;
131 }
132 float total = 0.0;
133 for (uint i = 0u; i < gl_NumSubgroups; i++) {
134 total += partial[lane][i];
135 }
136 if (floatBitsToUint(total) == 0xFFFFFFFFu) {
137 sink[0] = 1u;
138 }
139}