Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_c_full.comp
gemv_c_full.comp61 lines · 2.5 KiBRaw
| 1 | #version 450 |
| 2 | //! VARIANT C — FULL DEQUANT. Variant B plus the fp16 activation fetch and the multiply. |
| 3 | //! |
| 4 | //! TEMPORARY probe shader for task 9. Source of truth is |
| 5 | //! `analysis/gemv_probe/gemv_c_full.comp`. See `gemv_a_control.comp` for why this copy |
| 6 | //! exists and when to delete it. |
| 7 | //! |
| 8 | //! This is `conv_vec_int4.comp`'s inner loop reproduced exactly, over the control's load |
| 9 | //! structure. `C - B` is the cost of the activation stream: 32 scalar `float16_t` loads and |
| 10 | //! 32 multiplies per 16 bytes of weights. |
| 11 | //! |
| 12 | //! # Why the activation buffer is small and indexed this way |
| 13 | //! |
| 14 | //! In the real kernel the activation vector is `in_c` elements — 3 KB at `in_c` 1536 — and |
| 15 | //! every workgroup re-reads all of it (`conv_vec_int4.comp:44-47` accepts that cost |
| 16 | //! deliberately). It is therefore cache-resident, and a probe that streamed a large |
| 17 | //! activation buffer would measure DRAM traffic the real kernel does not pay. |
| 18 | //! |
| 19 | //! So the buffer is 4,096 `float16_t` (8 KB), and `abase` walks 128 distinct 32-element |
| 20 | //! blocks. That keeps the access lane-varying and contiguous-within-block, as in the real |
| 21 | //! kernel, while staying resident — which is the honest analogue. |
| 22 | //! |
| 23 | //! # Bytes accounting |
| 24 | //! |
| 25 | //! The reported GB/s counts only the 256 MB source sweep, never the activation reads. That |
| 26 | //! matches how the real gemv's 3.37 GB/s is computed (`probe_gemv` divides by weight bytes |
| 27 | //! only, `bridge.rs:3167`), so the numbers are directly comparable. |
| 28 | |
| 29 | #extension GL_EXT_shader_16bit_storage : require |
| 30 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 31 | |
| 32 | layout(local_size_x = 64) in; |
| 33 | |
| 34 | layout(set = 0, binding = 0) readonly buffer Source { uvec4 data[]; }; |
| 35 | layout(set = 0, binding = 1) buffer Sink { uint sink[]; }; |
| 36 | layout(set = 0, binding = 2) readonly buffer Acts { float16_t act[]; }; |
| 37 | |
| 38 | layout(push_constant) uniform Push { |
| 39 | uint quads; |
| 40 | }; |
| 41 | |
| 42 | void main() { |
| 43 | uint lane = gl_GlobalInvocationID.x; |
| 44 | uint stride = gl_NumWorkGroups.x * 64u; |
| 45 | float total = 0.0; |
| 46 | for (uint at = lane; at < quads; at += stride) { |
| 47 | uvec4 packed = data[at]; |
| 48 | uint abase = (at & 127u) * 32u; |
| 49 | // `conv_vec_int4.comp:110-115`, verbatim in structure. |
| 50 | for (uint word = 0u; word < 4u; word++) { |
| 51 | uint bits = packed[word]; |
| 52 | for (uint n = 0u; n < 8u; n++) { |
| 53 | int code = bitfieldExtract(int(bits), int(n << 2u), 4); |
| 54 | total += float(code) * float(act[abase + word * 8u + n]); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | if (floatBitsToUint(total) == 0xFFFFFFFFu) { |
| 59 | sink[0] = 1u; |
| 60 | } |
| 61 | } |