ml: int4 GEMM 4x8 per lane, gemma4 prefill GPU 3.93 -> 3.28 s on Pixel
- Commit
dda41b850565be1df6168561d86bc5119f230810- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:29 UTC (1 month ago)
- Parents
008db1e
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 4db46f8448..da71e6bfd1 100644--- a/library/ml/src/main/rust/shaders/conv_point_int4.comp+++ b/library/ml/src/main/rust/shaders/conv_point_int4.comp@@ -23,7 +23,82 @@ 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+/// 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).+void wide() {+ 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];+ 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];+ for (uint i = 0u; i < BLOCK; i++) {+ lo[i] = vec4(0.0);+ hi[i] = vec4(0.0);+ }+ uint quads = positions >> 2u;+ 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 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;+ }+ }+ }+ for (uint i = 0u; i < BLOCK; i++) {+ uint channel = channel_base + i;+ if (channel >= p.out_c) {+ continue;+ }+ 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[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)));+ }+ }+}+ void main() {+ if (p.in1 == 1u) {+ wide();+ return;+ } uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return;
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 a6afe2d94e..9ab1bfe9da 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part7.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part7.rs@@ -439,3 +439,40 @@ fn device_top_k_equals_a_host_sort() { let short: Vec<f32> = (0..40).map(|i| ((i * 13) % 7) as f32 - 3.0).collect(); top_k_fixture("a 40-entry row", &short); }++#[test]+#[ignore = "needs a Vulkan device"]+fn the_wide_int4_gemm_agrees_with_the_reference_at_gemma_prefill_shapes() {+ // `in1` = 1 (4 x 8 per lane, 32 x 64 tiles) is chosen for whole blocks and quad-aligned+ // columns. The 12288-wide MLP projection at 200 tokens; 36 positions leave a partial 64-tile+ // and lanes whose second quad is past the end; 68 spill one quad into a second tile; 1000+ // channels leave a partial row tile.+ for (label, out, inp, positions, act) in [+ ("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),+ ] {+ int4_gemm_fixture(label, out, inp, positions, act);+ }+ let blocks = 1536u32.div_ceil(32);+ let blob = write_mixed(+ graph::SUPERTONIC_VE,+ &[+ Fixture::I4(vec![64, 1536, 1, 1], vec![1; 64 * 1536]),+ Fixture::F16(vec![64, blocks], vec![0.01; (64 * blocks) as usize]),+ Fixture::F16(vec![64], vec![0.0; 64]),+ ],+ );+ let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the int4 fixture parses");+ let mut builder = Builder::new(&weights);+ let first = builder.input(Shape::new(1536, 1, 200));+ let last = builder.conv_int4(first, 0, 64, Act::None);+ let plan = builder.finish(&[last]).expect("the plan builds");+ assert!(+ plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt4, push, .. } if push.in1 == 1+ )),+ "an aligned int4 GEMM must take the wide path",+ );+}