Modern Apps git

← Modern-Apps.git

ml: int4 GEMM per-word weights + 16-byte activation loads, prefill GEMM -27%

Commit
40def1605661fc8eb64fad76844ccf965c068fb3
Author
vayun-mathur <[email protected]>
When
2026-10-26 05:23 UTC (1 month ago)
Parents
d792df9

Diff

library/ml/src/main/rust/shaders/conv_point_int4.comp
diff --git a/library/ml/src/main/rust/shaders/conv_point_int4.comp b/library/ml/src/main/rust/shaders/conv_point_int4.compindex da71e6bfd1..44d6cfab54 100644--- a/library/ml/src/main/rust/shaders/conv_point_int4.comp+++ b/library/ml/src/main/rust/shaders/conv_point_int4.comp@@ -23,11 +23,36 @@ layout(std430, binding = 0) readonly buffer ArenaQuad { #define TILE 32u #define BLOCK 4u -/// `p.in1 == 1`: 4 channels x 8 positions per lane over a 32 x 64 tile, chosen by the emit only+/// The same arena as 16-byte groups of eight fp16, for `wide()` when the eight positions a lane+/// owns are one aligned group.+layout(std430, binding = 0) readonly buffer ArenaOct {+    uvec4 arena_oct[];+};++void store8(uint channel, uint position_base, uint positions, vec4 lo, vec4 hi) {+    if (channel >= p.out_c) {+        return;+    }+    float offset = float(weights[p.bias + channel]);+    for (uint j = 0u; j < 8u; j++) {+        uint position = position_base + j;+        if (position >= positions) {+            continue;+        }+        float acc = j < 4u ? lo[j] : hi[j - 4u];+        uint index = channel * positions + position;+        arena[p.out_at + index] = float16_t(quant_store(fused_store(acc + offset, p.act, channel, index)));+    }+}++/// `p.in1 == 1` or `4`: 4 channels x 8 positions per lane over a 32 x 64 tile, chosen by the emit only /// when `in_c % 32`, `p.weight % 4`, `positions % 4` and `p.in0 % 4` are all zero. Each nibble /// unpack feeds 8 FMAs instead of 4, and the block scale is folded into the tap so the lane keeps /// 32 accumulators and no per-block partials (scale per tap, not per block sum: rounding moves-/// within the int4 fixtures' tolerance).+/// within the int4 fixtures' tolerance). Weights come one word (8 taps) per row at a time rather+/// than a whole block up front: fewer live registers, 13% faster on a Pixel 8. `in1` 4 (positions+/// and `p.in0` % 8 == 0) reads the eight activations as one 16-byte load instead of two 8-byte+/// ones: ConvPointInt4 of a 200-token Gemma prefill 3.06 -> 2.25 s with both. void wide() {     uint positions = p.out_h * p.out_w;     uint tiles_across = (positions + 63u) / 64u;@@ -43,10 +68,8 @@ void wide() {     }     uint blocks = p.in_c >> 5u;     uint rows[BLOCK];-    uint rows32[BLOCK];     for (uint i = 0u; i < BLOCK; i++) {         rows[i] = min(channel_base + i, p.out_c - 1u);-        rows32[i] = (p.weight >> 2u) + rows[i] * blocks;     }     vec4 lo[BLOCK];     vec4 hi[BLOCK];@@ -58,43 +81,93 @@ void wide() {     uint quad = (p.in0 + position_base) >> 2u;     bool second = position_base + 4u < positions;     for (uint block = 0u; block < blocks; block++) {-        uvec4 w[BLOCK];         float scale[BLOCK];         for (uint i = 0u; i < BLOCK; i++) {-            w[i] = weights128[rows32[i] + block];             scale[i] = int4_scale(rows[i], block, blocks);         }-        uint k = block * I4_BLOCK;-        for (uint t = 0u; t < I4_BLOCK; t++) {-            uint at = quad + (k + t) * quads;-            vec4 x0 = vec4(arena_quad[at]);-            vec4 x1 = second ? vec4(arena_quad[at + 1u]) : vec4(0.0);+        for (uint word = 0u; word < 4u; word++) {+            uint w[BLOCK];             for (uint i = 0u; i < BLOCK; i++) {-                float tap = float(bitfieldExtract(int(w[i][t >> 3u]), int((t & 7u) << 2u), 4)) * scale[i];-                lo[i] += tap * x0;-                hi[i] += tap * x1;+                w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word];+            }+            uint k = block * I4_BLOCK + word * 8u;+            for (uint t = 0u; t < 8u; t++) {+                uint at = quad + (k + t) * quads;+                vec4 x0 = vec4(arena_quad[at]);+                vec4 x1 = second ? vec4(arena_quad[at + 1u]) : vec4(0.0);+                for (uint i = 0u; i < BLOCK; i++) {+                    float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i];+                    lo[i] += tap * x0;+                    hi[i] += tap * x1;+                }             }         }     }     for (uint i = 0u; i < BLOCK; i++) {-        uint channel = channel_base + i;-        if (channel >= p.out_c) {-            continue;+        store8(channel_base + i, position_base, positions, lo[i], hi[i]);+    }+}++/// `wide()` with the 16-byte activation load (`in1` 4).+void wide_oct() {+    uint positions = p.out_h * p.out_w;+    uint tiles_across = (positions + 63u) / 64u;+    uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x;+    if (tile >= p.count) {+        return;+    }+    uint lid = gl_LocalInvocationID.x;+    uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;+    uint position_base = (tile % tiles_across) * 64u + (lid & 7u) * 8u;+    if (position_base >= positions) {+        return;+    }+    uint blocks = p.in_c >> 5u;+    uint rows[BLOCK];+    for (uint i = 0u; i < BLOCK; i++) {+        rows[i] = min(channel_base + i, p.out_c - 1u);+    }+    vec4 lo[BLOCK];+    vec4 hi[BLOCK];+    for (uint i = 0u; i < BLOCK; i++) {+        lo[i] = vec4(0.0);+        hi[i] = vec4(0.0);+    }+    uint octs = positions >> 3u;+    uint oct = (p.in0 + position_base) >> 3u;+    for (uint block = 0u; block < blocks; block++) {+        float scale[BLOCK];+        for (uint i = 0u; i < BLOCK; i++) {+            scale[i] = int4_scale(rows[i], block, blocks);         }-        float offset = float(weights[p.bias + channel]);-        for (uint j = 0u; j < 8u; j++) {-            uint position = position_base + j;-            if (position >= positions) {-                continue;+        for (uint word = 0u; word < 4u; word++) {+            uint w[BLOCK];+            for (uint i = 0u; i < BLOCK; i++) {+                w[i] = weights32[p.weight + (rows[i] * blocks + block) * 4u + word];+            }+            uint k = block * I4_BLOCK + word * 8u;+            for (uint t = 0u; t < 8u; t++) {+                uvec4 u = arena_oct[oct + (k + t) * octs];+                vec4 x0 = vec4(unpackHalf2x16(u.x), unpackHalf2x16(u.y));+                vec4 x1 = vec4(unpackHalf2x16(u.z), unpackHalf2x16(u.w));+                for (uint i = 0u; i < BLOCK; i++) {+                    float tap = float(bitfieldExtract(int(w[i]), int(t << 2u), 4)) * scale[i];+                    lo[i] += tap * x0;+                    hi[i] += tap * x1;+                }             }-            float acc = j < 4u ? lo[i][j] : hi[i][j - 4u];-            uint index = channel * positions + position;-            arena[p.out_at + index] = float16_t(quant_store(fused_store(acc + offset, p.act, channel, index)));         }     }+    for (uint i = 0u; i < BLOCK; i++) {+        store8(channel_base + i, position_base, positions, lo[i], hi[i]);+    } }  void main() {+    if (p.in1 == 4u) {+        wide_oct();+        return;+    }     if (p.in1 == 1u) {         wide();         return;
library/ml/src/main/rust/src/nets/mod_part9.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part9.rs b/library/ml/src/main/rust/src/nets/mod_part9.rsindex a4b27542ee..5272033332 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -110,6 +110,9 @@ impl<'a> Builder<'a> {                     && *weight % 4 == 0                     && positions % 4 == 0                     && input_at % 4 == 0;+                // Its 16-byte activation load (`in1` = 4, `wide_oct()`) when each lane's eight positions+                // are one aligned group.+                let int4_oct = int4_wide && positions % 8 == 0 && input_at % 8 == 0;                 // A grouped single-position 1x1 (maia's bias expansion): `grouped_rows` in                 // conv_int8.comp, `in1` = 2, wide loads of each group's taps.                 let grouped_vec = kind == Kind::ConvInt8@@ -152,6 +155,8 @@ impl<'a> Builder<'a> {                         in0: input_at,                         in1: if kind == Kind::ConvPointQ2K {                             q2k_split+                        } else if int4_oct {+                            4                         } else if row_gemm || grouped_vec {                             2                         } else if widened {
library/ml/src/main/rust/src/vulkan/parity_part7.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part7.rs b/library/ml/src/main/rust/src/vulkan/parity_part7.rsindex 9ab1bfe9da..33149455ae 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part7.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part7.rs@@ -451,6 +451,7 @@ fn the_wide_int4_gemm_agrees_with_the_reference_at_gemma_prefill_shapes() {         ("a wide 1536 -> 12288 gemm at 200 positions", 12288, 1536, 200, Act::None),         ("a wide ragged gemm at 36 positions", 1000, 2048, 36, Act::Gelu),         ("a wide gemm at 68 positions", 96, 1536, 68, Act::None),+        ("a wide 16-byte-load gemm at 72 positions", 1000, 1536, 72, Act::Gelu),     ] {         int4_gemm_fixture(label, out, inp, positions, act);     }@@ -471,8 +472,8 @@ fn the_wide_int4_gemm_agrees_with_the_reference_at_gemma_prefill_shapes() {     assert!(         plan.ops.iter().any(|op| matches!(             op,-            crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt4, push, .. } if push.in1 == 1+            crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt4, push, .. } if push.in1 == 4         )),-        "an aligned int4 GEMM must take the wide path",+        "an 8-aligned int4 GEMM must take the wide 16-byte-load path",     ); }