Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / ml / src / main / rust / shaders / gemv_tex_gemv.comp
gemv_tex_gemv.comp62 lines · 2.6 KiBRaw
| 1 | #version 450 |
| 2 | //! GEMV-shaped sweep through a texel-buffer view: the texture-path half of the |
| 3 | //! Phase-3 proof slice. |
| 4 | //! |
| 5 | //! # What this isolates that `probe_texel.comp` does not |
| 6 | //! |
| 7 | //! `probe_texel.comp` streams 256 MB with 1 XOR/16 B — a pure read-bandwidth |
| 8 | //! ceiling. This runs the REAL gemv access pattern from `gemv_g_shape_full` |
| 9 | //! (variant G: K=1, ROWS 2, 64 lanes, int4 unpack + activation fetch + FMA) |
| 10 | //! with the weight fetch switched from SSBO `weights128[]` to |
| 11 | //! `texelFetch(usamplerBuffer)`. Same launch, same arithmetic, same |
| 12 | //! activation stream — the ONLY difference is the weight-fetch path. |
| 13 | //! |
| 14 | //! G-vs-TEX is therefore the texture-path value *inside the real kernel*, |
| 15 | //! not inside a stream: if the kernel is shape-bound (bottleneck report says |
| 16 | //! it is on host), texture fetch changes nothing and Phase 3 is abandoned. |
| 17 | //! If TEX beats G on P8, the promotion path (image-backed weights) is real. |
| 18 | #extension GL_EXT_shader_16bit_storage : require |
| 19 | #extension GL_EXT_shader_explicit_arithmetic_types_float16 : require |
| 20 | layout(local_size_x = 64) in; |
| 21 | layout(set = 0, binding = 0) uniform usamplerBuffer weights_tex; |
| 22 | layout(set = 0, binding = 1) readonly buffer Acts { float16_t act[]; }; |
| 23 | layout(set = 0, binding = 2) buffer Sink { uint sink[]; }; |
| 24 | layout(push_constant) uniform Push { |
| 25 | uint quads; |
| 26 | uint in_c; |
| 27 | uint count; |
| 28 | }; |
| 29 | #define ROWS 2u |
| 30 | #define I4_BLOCK 32u |
| 31 | #define LANES 64u |
| 32 | void main() { |
| 33 | uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; |
| 34 | if (tile >= count) { |
| 35 | return; |
| 36 | } |
| 37 | uint channel_base = tile * ROWS; |
| 38 | uint lane = gl_LocalInvocationID.x; |
| 39 | uint blocks = (in_c + I4_BLOCK - 1u) / I4_BLOCK; |
| 40 | float acc[ROWS]; |
| 41 | for (uint r = 0u; r < ROWS; r++) { |
| 42 | acc[r] = 0.0; |
| 43 | } |
| 44 | // One K-iteration (matches G): each lane walks its share of blocks. |
| 45 | for (uint b = lane; b < blocks; b += LANES) { |
| 46 | // One uvec4 = 8 int4 weights = the same 32-tap block G reads, but via |
| 47 | // the texture unit instead of the SSBO load path. |
| 48 | uvec4 packed = texelFetch(weights_tex, int(b)); |
| 49 | for (uint r = 0u; r < ROWS; r++) { |
| 50 | uint word = (r == 0u) ? packed.x : packed.y; |
| 51 | float wsum = float(word & 15u) + float((word >> 4u) & 15u) |
| 52 | + float((word >> 8u) & 15u) + float((word >> 12u) & 15u) |
| 53 | + float((word >> 16u) & 15u) + float((word >> 20u) & 15u) |
| 54 | + float((word >> 24u) & 15u) + float(word >> 28u); |
| 55 | acc[r] += wsum * float(act[(channel_base + r) * in_c + b * 32u]); |
| 56 | } |
| 57 | } |
| 58 | uint folded = uint(acc[0] * 1000.0) ^ uint(acc[1] * 1000.0) ^ tile; |
| 59 | if (folded == 0xFFFFFFFFu) { |
| 60 | sink[0] = folded; |
| 61 | } |
| 62 | } |