Modern Apps git

← Modern-Apps.git

ml: 16-tap wide loads in the int8 decode GEMV

Commit
a5ad79f7ea87ae8f474e32f052b1ae98e6dafdd7
Author
vayun-mathur <[email protected]>
When
2026-10-25 20:21 UTC (1 month ago)
Parents
4c092bf

Diff

library/ml/src/main/rust/shaders/common.glsl
diff --git a/library/ml/src/main/rust/shaders/common.glsl b/library/ml/src/main/rust/shaders/common.glslindex c755ff15d5..fe6ac929a4 100644--- a/library/ml/src/main/rust/shaders/common.glsl+++ b/library/ml/src/main/rust/shaders/common.glsl@@ -260,6 +260,13 @@ int int8_at(uint base, uint index) {     return bitfieldExtract(int(word), int((index & 3u) << 3u), 8); } +/// The four signed bytes of one 32-bit word, low byte first, as floats.+vec4 unpack_int8x4(uint word) {+    int w = int(word);+    return vec4(bitfieldExtract(w, 0, 8), bitfieldExtract(w, 8, 8), bitfieldExtract(w, 16, 8),+                bitfieldExtract(w, 24, 8));+}+ /// Taps one Q2_K superblock covers. Mirrors `weights::Q2K_BLOCK`. #define Q2K_BLOCK 256u 
library/ml/src/main/rust/shaders/conv_vec_int8.comp
diff --git a/library/ml/src/main/rust/shaders/conv_vec_int8.comp b/library/ml/src/main/rust/shaders/conv_vec_int8.compindex a1989314de..e23be0bdb7 100644--- a/library/ml/src/main/rust/shaders/conv_vec_int8.comp+++ b/library/ml/src/main/rust/shaders/conv_vec_int8.comp@@ -52,12 +52,26 @@ //! accumulation, the scale applied to the finished accumulator, then the bias, then the activation. //! `p.weight` is a **32-bit word** offset and `p.act_weight` the scale as an fp16 element index, //! which is why `Act::PRelu` cannot ride along.+//!+//! # Wide loads+//!+//! When `in_c % 16 == 0` and the weight and activation bases are 16-byte aligned - every+//! projection and head here - each lane reads sixteen taps per row with one `uvec4` load and the+//! matching sixteen activations with four `f16vec4` loads, reused across the rows. The byte-per-lane+//! loop below it issued one load per tap per row; on a Pixel 8 it streamed NLLB's decode weights at+//! 15 GB/s, 85% of a step.  // See the tail of `main`, and `conv_vec_int4.comp` which does the same. #extension GL_KHR_shader_subgroup_basic : require #extension GL_KHR_shader_subgroup_arithmetic : require #include "common.glsl" +/// The arena as quads of fp16, for the wide activation loads. Read-only, and never a range this+/// dispatch writes.+layout(std430, binding = 0) readonly buffer ArenaQuad {+    f16vec4 arena_quad[];+};+ /// Output channels per workgroup. /// /// Eight rather than one because a workgroup per output channel would spend 64 lanes on a single@@ -95,10 +109,31 @@ void main() {     }     // Rows past the output are accumulated anyway and discarded at the store. Skipping them would     // branch inside the hot loop for at most seven channels of the last workgroup.-    for (uint k = lane; k < p.in_c; k += 64u) {-        float x = float(arena[p.in0 + k]);+    bool wide = (p.in_c & 15u) == 0u && (p.weight & 3u) == 0u && (p.in0 & 3u) == 0u;+    if (wide) {+        uint row16[ROWS];         for (uint r = 0u; r < ROWS; r++) {-            acc[r] += float(int8_at(p.weight, (channel_base + r) * p.in_c + k)) * x;+            uint channel = min(channel_base + r, p.out_c - 1u);+            row16[r] = (p.weight + ((channel * p.in_c) >> 2u)) >> 2u;+        }+        uint quad0 = p.in0 >> 2u;+        for (uint k = lane * 16u; k < p.in_c; k += 1024u) {+            vec4 x0 = vec4(arena_quad[quad0 + (k >> 2u)]);+            vec4 x1 = vec4(arena_quad[quad0 + (k >> 2u) + 1u]);+            vec4 x2 = vec4(arena_quad[quad0 + (k >> 2u) + 2u]);+            vec4 x3 = vec4(arena_quad[quad0 + (k >> 2u) + 3u]);+            for (uint r = 0u; r < ROWS; r++) {+                uvec4 w = weights128[row16[r] + (k >> 4u)];+                acc[r] += dot(unpack_int8x4(w.x), x0) + dot(unpack_int8x4(w.y), x1)+                        + dot(unpack_int8x4(w.z), x2) + dot(unpack_int8x4(w.w), x3);+            }+        }+    } else {+        for (uint k = lane; k < p.in_c; k += 64u) {+            float x = float(arena[p.in0 + k]);+            for (uint r = 0u; r < ROWS; r++) {+                acc[r] += float(int8_at(p.weight, (channel_base + r) * p.in_c + k)) * x;+            }         }     }     // Reduce within each subgroup in registers, then across the handful a workgroup holds.
library/ml/src/main/rust/src/vulkan/parity_part4.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part4.rs b/library/ml/src/main/rust/src/vulkan/parity_part4.rsindex 6a1e68a019..9aee569717 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part4.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part4.rs@@ -1,3 +1,35 @@+#[test]+#[ignore = "needs a Vulkan device"]+fn a_wide_gemv_agrees_with_the_reference() {+    // `conv_vec_int8.comp`'s sixteen-tap path over several strides per lane (2,080 inputs is two+    // passes of 1,024 and a partial third) and an odd channel count, so the last workgroup's second+    // row reads a clamped row and must not be stored.+    let (out_channels, in_channels) = (37u32, 2080u32);+    let kernel: Vec<i8> = (0..(out_channels * in_channels) as i32)+        .map(|i| ((i * 29) % 253 - 126) as i8)+        .collect();+    let scales: Vec<f32> = (0..out_channels).map(|c| 0.000_976_562_5 * (1.0 + (c % 5) as f32)).collect();+    let blob = write_mixed(+        graph::SUPERTONIC_VE,+        &[+            Fixture::I8(vec![out_channels, in_channels, 1, 1], kernel),+            Fixture::F16(vec![out_channels], scales),+            Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.8)),+        ],+    );+    let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+    let input = spread(in_channels as usize, 0.6);+    let mut builder = Builder::new(&weights);+    let first = builder.input(Shape::new(in_channels, 1, 1));+    let last = builder.conv_int8(first, 0, out_channels, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::None);+    let plan = builder.finish(&[last]).expect("the gemv plan builds");+    assert!(plan.ops.iter().any(|op| matches!(+        op,+        crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvVecInt8, .. }+    )));+    compare("a wide int8 gemv", plan, weights.data().to_vec(), &[&input]);+}+ #[test] #[ignore = "needs a Vulkan device"] fn the_gemv_and_tiled_int8_shaders_agree_with_each_other() {