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

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

gemv_e_deep_full.comp93 lines · 3.8 KiBRaw
1#version 450
2#extension GL_EXT_control_flow_attributes : require
3//! VARIANT E — DEEP PREFETCH + FULL DEQUANT. Variant D's loads, variant C's arithmetic.
4//!
5//! TEMPORARY probe shader for task 9. Source of truth is
6//! `analysis/gemv_probe/gemv_e_deep_full.comp`. See `gemv_a_control.comp` for why this copy
7//! exists and when to delete it.
8//!
9//! The combination, and the variant that decides what to build. `E - C` is what deep
10//! prefetch is worth **once the ALU cost is present**, which is the only form of the
11//! question that matters: ml_drift's `WaveLoad` sits in a kernel that also dequantises, so
12//! a prefetch win measured against a no-ALU control would not transfer.
13//!
14//! Reading the 2x2:
15//!
16//! | | shallow | deep |
17//! | :--- | :--- | :--- |
18//! | light ALU | A | D |
19//! | full ALU | C | E |
20//!
21//! * `C << A` and `E ~= C` -> ALU-bound. H2. Cut dequant work; prefetch does not help.
22//! * `C << A` and `E >> C` -> ALU exposes latency the control did not. H1 and H2 together:
23//! the arithmetic lengthens the dependency chain and depth hides
24//! it. Prefetch is the cheaper fix and should go first.
25//! * `C ~= A` and `D >> A` -> not ALU at all; depth is the lever even without arithmetic.
26//! * `C ~= A` and `D ~= A` -> neither hypothesis. The 6x lives somewhere neither of us has
27//! looked, and the difference must be in what
28//! `conv_vec_int4.comp` does that none of these five do:
29//! per-block scale fetches, the `ROWS` loop, `subgroupAdd`, the
30//! shared-memory reduction, or the dispatch shape itself.
31//!
32//! That last row is a real possible outcome and it is why all five run rather than just C.
33
34#extension GL_EXT_shader_16bit_storage : require
35#extension GL_EXT_shader_explicit_arithmetic_types_float16 : require
36
37layout(local_size_x = 64) in;
38
39layout(set = 0, binding = 0) readonly buffer Source { uvec4 data[]; };
40layout(set = 0, binding = 1) buffer Sink { uint sink[]; };
41layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; };
42
43layout(push_constant) uniform Push {
44 uint quads;
45};
46
47// See `gemv_d_deep.comp` for why this is a literal `#define`.
48#define DEPTH 16
49
50void main() {
51 uint lane = gl_GlobalInvocationID.x;
52 uint stride = gl_NumWorkGroups.x * 64u;
53 float total = 0.0;
54
55 uint iters = quads / stride;
56 uint batches = iters / uint(DEPTH);
57 uint at = lane;
58 for (uint b = 0u; b < batches; b++) {
59 uvec4 cache[DEPTH];
60 [[unroll]] for (uint d = 0u; d < uint(DEPTH); d++) {
61 cache[d] = data[at + d * stride];
62 }
63 // Consume phase: the whole of variant C's arithmetic, but against registers that
64 // were filled sixteen loads ago rather than one.
65 [[unroll]] for (uint d = 0u; d < uint(DEPTH); d++) {
66 uvec4 packed = cache[d];
67 uint abase = ((at + d * stride) & 127u) * 32u;
68 [[unroll]] for (uint word = 0u; word < 4u; word++) {
69 uint bits = packed[word];
70 [[unroll]] for (uint n = 0u; n < 8u; n++) {
71 int code = bitfieldExtract(int(bits), int(n << 2u), 4);
72 total += float(code) * float(act[abase + word * 8u + n]);
73 }
74 }
75 }
76 at += uint(DEPTH) * stride;
77 }
78 for (; at < quads; at += stride) {
79 uvec4 packed = data[at];
80 uint abase = (at & 127u) * 32u;
81 [[unroll]] for (uint word = 0u; word < 4u; word++) {
82 uint bits = packed[word];
83 [[unroll]] for (uint n = 0u; n < 8u; n++) {
84 int code = bitfieldExtract(int(bits), int(n << 2u), 4);
85 total += float(code) * float(act[abase + word * 8u + n]);
86 }
87 }
88 }
89
90 if (floatBitsToUint(total) == 0xFFFFFFFFu) {
91 sink[0] = 1u;
92 }
93}