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

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

conv_point_int4_dot.comp114 lines · 5.0 KiBRaw
1#version 450
2//! The int4 GEMM on the hardware int8 dot product (`VK_KHR_shader_integer_dot_product`).
3//!
4//! Reads the activations `quantize_blocks.comp` staged at element `p.kw`: int8 per position per
5//! 32-channel block in `[block][half][position]` 16-byte cells, and an f32 scale table. A weight
6//! word's 8 two's-complement nibbles become two packed byte quads, `w << 4 & 0xF0F0F0F0` and
7//! `w & 0xF0F0F0F0`: each byte is its nibble times 16, so no offset correction is needed (the
8//! staged scale carries the 1/16). Each quad meets its activation quad in one signed
9//! `OpSDotAccSatKHR` (the Mali-G715 accelerates signed but not mixed-signedness); a block's int32
10//! sum never nears saturation (|sum| <= 32 * 127 * 128). Then `acc += isum * (ascale * wscale)`.
11//!
12//! 2 channels x 8 positions per lane over 32 x 32 tiles; a lane's positions are 4 apart, so the
13//! 4 lanes sharing a row pair read 4 consecutive positions' cells. Each row's block is unpacked
14//! once and reused by 8 positions: the kernel is ALU-bound, and the unpack is the overhead that
15//! shrinks with more positions. On a Pixel 8 (200-token Gemma prefill) ConvPointInt4Dot took
16//! 2050 ms with 4 x 4 consecutive positions, `[block][position]` cells and an add per
17//! `OpSDotKHR`; 1500 ms with this layout at 4 x 4; 885 ms with the accumulating dot; 760 ms at
18//! 2 x 8. 1 x 16 took 1080 ms (activation loads), and the 8 scales as two vec4 loads 855 ms.
19//!
20//! The tiling is also the `in1` 0 path of `conv_point_int4.comp`, so the emit's dispatch serves
21//! that path when the device lacks the extension: `Shaders::for_kind` then hands out the fp32
22//! pipeline with this push unchanged.
23//! Requires `in_c % 32 == 0`, `p.weight % 4 == 0`, `p.kw % 8 == 0`.
24#extension GL_EXT_spirv_intrinsics : require
25#include "common.glsl"
26spirv_instruction(extensions = ["SPV_KHR_integer_dot_product"], capabilities = [6019, 6018], id = 4453)
27int sdot_acc(uint a, uint b, int acc, spirv_literal int format);
28layout(std430, binding = 0) readonly buffer ArenaOct {
29 uvec4 arena_oct[];
30};
31layout(std430, binding = 0) readonly buffer ArenaWord {
32 uint arena_word[];
33};
34#define TILE 32u
35#define ROWS 2u
36#define COLS 8u
37#define ACROSS (TILE / COLS)
38#define HIGH 0xF0F0F0F0u
39void main() {
40 uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;
41 if (tile >= p.count) {
42 return;
43 }
44 uint positions = p.out_h * p.out_w;
45 uint tiles_across = (positions + TILE - 1u) / TILE;
46 uint lid = gl_LocalInvocationID.x;
47 uint channel_base = (tile / tiles_across) * TILE + (lid / ACROSS) * ROWS;
48 uint position_base = (tile % tiles_across) * TILE + (lid % ACROSS);
49 if (position_base >= positions) {
50 return;
51 }
52 uint blocks = p.in_c >> 5u;
53 uint rows[ROWS];
54 for (uint i = 0u; i < ROWS; i++) {
55 rows[i] = min(channel_base + i, p.out_c - 1u);
56 }
57 uint cols[COLS];
58 for (uint j = 0u; j < COLS; j++) {
59 cols[j] = min(position_base + ACROSS * j, positions - 1u);
60 }
61 float acc[ROWS][COLS];
62 for (uint i = 0u; i < ROWS; i++) {
63 for (uint j = 0u; j < COLS; j++) {
64 acc[i][j] = 0.0;
65 }
66 }
67 uint data = p.kw >> 3u;
68 uint meta = (p.kw + positions * p.in_c / 2u) >> 1u;
69 for (uint block = 0u; block < blocks; block++) {
70 uint wq[ROWS][8];
71 float ws[ROWS];
72 for (uint i = 0u; i < ROWS; i++) {
73 uvec4 w = weights128[(p.weight >> 2u) + rows[i] * blocks + block];
74 for (uint k = 0u; k < 4u; k++) {
75 wq[i][2u * k] = (w[k] << 4u) & HIGH;
76 wq[i][2u * k + 1u] = w[k] & HIGH;
77 }
78 ws[i] = int4_scale(rows[i], block, blocks);
79 }
80 uint at = data + block * 2u * positions;
81 uint scales = meta + block * positions;
82 for (uint j = 0u; j < COLS; j++) {
83 uvec4 x0 = arena_oct[at + cols[j]];
84 uvec4 x1 = arena_oct[at + positions + cols[j]];
85 float a = uintBitsToFloat(arena_word[scales + cols[j]]);
86 for (uint i = 0u; i < ROWS; i++) {
87 int s = sdot_acc(x0.x, wq[i][0], 0, 0);
88 s = sdot_acc(x0.y, wq[i][1], s, 0);
89 s = sdot_acc(x0.z, wq[i][2], s, 0);
90 s = sdot_acc(x0.w, wq[i][3], s, 0);
91 s = sdot_acc(x1.x, wq[i][4], s, 0);
92 s = sdot_acc(x1.y, wq[i][5], s, 0);
93 s = sdot_acc(x1.z, wq[i][6], s, 0);
94 s = sdot_acc(x1.w, wq[i][7], s, 0);
95 acc[i][j] += float(s) * (a * ws[i]);
96 }
97 }
98 }
99 for (uint i = 0u; i < ROWS; i++) {
100 uint channel = channel_base + i;
101 if (channel >= p.out_c) {
102 continue;
103 }
104 float offset = float(weights[p.bias + channel]);
105 for (uint j = 0u; j < COLS; j++) {
106 uint position = position_base + ACROSS * j;
107 if (position < positions) {
108 uint index = channel * positions + position;
109 arena[p.out_at + index] =
110 float16_t(quant_store(fused_store(acc[i][j] + offset, p.act, channel, index)));
111 }
112 }
113 }
114}