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

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

gemv_b_unpack.comp49 lines · 2.0 KiBRaw
1#version 450
2//! VARIANT B — CONTROL + INT4 UNPACK. Shallow prefetch, unpack ALU, no activation stream.
3//!
4//! TEMPORARY probe shader for task 9. Source of truth is
5//! `analysis/gemv_probe/gemv_b_unpack.comp`. See `gemv_a_control.comp` for why this copy
6//! exists and when to delete it.
7//!
8//! Differs from variant A in exactly one respect: each loaded `uvec4` is unpacked into its
9//! 32 signed nibbles and each is accumulated. Same load width, same access pattern, same
10//! workgroup size, same one-load-in-flight structure.
11//!
12//! `A - B` is therefore the cost of the unpack alone: `bitfieldExtract` + `int -> float` +
13//! an add, 32 times per 16 bytes. It deliberately does NOT include the activation fetch or
14//! the multiply — variant C adds those — so that the two halves of the 160-ops-per-16-bytes
15//! figure can be attributed separately.
16//!
17//! `bitfieldExtract` on a **signed** int sign-extends, matching `int4_at` in
18//! `shaders/common.glsl:216-219`. The accumulator is float, matching `conv_vec_int4.comp`,
19//! which accumulates in fp32 even though the operands are 4-bit and fp16.
20
21layout(local_size_x = 64) in;
22
23layout(set = 0, binding = 0) readonly buffer Source { uvec4 data[]; };
24layout(set = 0, binding = 1) buffer Sink { uint sink[]; };
25
26layout(push_constant) uniform Push {
27 uint quads;
28};
29
30void main() {
31 uint lane = gl_GlobalInvocationID.x;
32 uint stride = gl_NumWorkGroups.x * 64u;
33 float total = 0.0;
34 for (uint at = lane; at < quads; at += stride) {
35 uvec4 packed = data[at];
36 // Exactly the unpack loop of `conv_vec_int4.comp:110-115`, minus the activation.
37 for (uint word = 0u; word < 4u; word++) {
38 uint bits = packed[word];
39 for (uint n = 0u; n < 8u; n++) {
40 int code = bitfieldExtract(int(bits), int(n << 2u), 4);
41 total += float(code);
42 }
43 }
44 }
45 // As variant A: a condition that cannot fire, so the loop is live but the store is not.
46 if (floatBitsToUint(total) == 0xFFFFFFFFu) {
47 sink[0] = 1u;
48 }
49}