ml: register-blocked int8 GEMM with wide loads for encoders/prefill
- Commit
a986916f3d8edaa48a3555296d1e754542dbee99- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-25 20:09 UTC (1 month ago)
- Parents
8eb8ee1
Diff
library/ml/src/main/rust/shaders/conv_point_int8.comp
diff --git a/library/ml/src/main/rust/shaders/conv_point_int8.comp b/library/ml/src/main/rust/shaders/conv_point_int8.compindex 8b9be95ff7..89388a8910 100644--- a/library/ml/src/main/rust/shaders/conv_point_int8.comp+++ b/library/ml/src/main/rust/shaders/conv_point_int8.comp@@ -1,5 +1,5 @@ #version 450-//! `conv_point.comp` with int8 weights, dequantised by one scale per output channel.+//! A `1 x 1` convolution with int8 weights over a sequence: an int8-by-fp16 matrix product. //! //! `out[o][p] = act(scale[o] * sum_i W[o][i] * in[i][p] + bias[o])`, `p` over positions. //!@@ -7,106 +7,178 @@ //! //! `conv_int8.comp` is shaped like `conv.comp`: one invocation per output element, each reading //! its own weight row from global memory with no reuse. That is survivable for a spatial kernel-//! and catastrophic for a wide `1 x 1` over a short sequence — the same 15.2 GFLOP/s against a-//! device peak of order 1000 that `conv_point.comp` exists to fix.+//! and catastrophic for a wide `1 x 1` over a sequence, which is every encoder projection and every+//! prefill: the weight row is re-read once per position. //!-//! Supertonic's sampler is 92% pointwise convolution by parameter count and runs `2 * STEPS`-//! times per utterance, so quantising it onto the untiled path would move almost all of its work-//! onto the slow one and take text-to-speech past real time. Quantising is a size win only if the-//! tiled shape has an int8 form, which is this.+//! # Register blocking, not shared-memory tiling //!-//! # What differs from `conv_point.comp`+//! Each invocation owns a 4 x 4 block - four output channels by four positions - and the 64 of a+//! workgroup tile a 32 x 32 block of the output, eight across and eight down. Every weight word read+//! feeds four positions and every activation read feeds four channels, which is the whole of the+//! reuse; it is held in registers rather than staged through shared memory. //!-//! Two lines, and nothing structural. The weight tile is staged from `int8_at` through the 32-bit-//! view of the weights buffer rather than read as fp16, and the finished accumulator is scaled-//! before the bias is added. Everything else — the tiling, the 2 x 2 register blocking, both-//! barriers — is that shader unchanged; see it for why the tiling is shaped the way it is.+//! The previous version staged 16 x 16 tiles of both operands through shared memory with two+//! barriers per 16 input channels. On Mali, shared memory is ordinary cached memory and a barrier+//! stalls the whole workgroup, so that bought no reuse the cache was not already giving and paid+//! for it 128 times per 1,024-wide projection: on a Pixel 8 it reached 9-16 GMAC/s, which made it+//! 98% of NLLB's encoder and a third of whisper's. //!-//! The scale is per **output channel**, which is the tile's row axis, so an invocation's two rows-//! carry two scales and each applies to a whole finished accumulator. A per-input-channel scale-//! could not be hoisted out of the inner loop this way.+//! # Four input channels per weight load //!-//! `p.weight` is a **32-bit word** offset, as in `conv_int8.comp`. `p.act_weight` holds the scale-//! tensor as an fp16 element index, which is why `Act::PRelu` cannot ride along.+//! Int8 weights pack four to a 32-bit word along the input axis, so one load from the word view+//! gives four taps of one output channel. That needs every row to start on a word, which is+//! `in_c % 4 == 0`; every projection in every net here satisfies it, and the rare one that does not+//! takes the element-at-a-time loop instead.+//!+//! # The scale+//!+//! Per **output channel**, so each of an invocation's four rows carries one scale applied to its+//! finished accumulators. `p.weight` is a **32-bit word** offset, as in `conv_int8.comp`;+//! `p.act_weight` holds the scale tensor as an fp16 element index, which is why `Act::PRelu`+//! cannot ride along.+//!+//! # Wide loads+//!+//! When `in_c % 16 == 0` and the position count is a multiple of four - every encoder and prefill+//! here - weights are read sixteen taps at a time through the `uvec4` view and activations four+//! positions at a time through a read-only `f16vec4` view of the arena. Scalar loads were the+//! limit: one 32-bit load per four MACs. The narrow paths below remain for anything else. #include "common.glsl" -/// Output channels and positions per workgroup, and input channels per staging step.-#define TILE 16u+/// The arena again, four fp16 elements per entry, for the wide activation loads.+///+/// The same binding as `arena`; reads only, and only of ranges no op in this dispatch writes, so+/// the alias is never a hazard. Indexing needs the element offset divisible by four, which the+/// arena's 16-byte allocation alignment and a position count divisible by four together give.+layout(std430, binding = 0) readonly buffer ArenaQuad {+ f16vec4 arena_quad[];+}; -/// The staged blocks. `TILE * TILE` each, so 2 KB of shared memory at fp32.-shared float weight_tile[TILE][TILE];-shared float input_tile[TILE][TILE];+/// Output channels and positions per workgroup: 8 x 8 invocations of 4 x 4 each.+///+/// **Must equal `CONV_POINT_INT8_TILE` in `nets`**, which counts the workgroups.+#define TILE 32u++/// Channels and positions per invocation.+#define BLOCK 4u void main() {- // The tile this workgroup owns. Uniform across the workgroup, so returning early cannot- // strand a barrier — every invocation takes the same branch. uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { return; } uint positions = p.out_h * p.out_w; uint tiles_across = (positions + TILE - 1u) / TILE;- uint channel_base = (tile / tiles_across) * TILE;- uint position_base = (tile % tiles_across) * TILE;-- // This invocation's 2 x 2 corner of the tile. uint lid = gl_LocalInvocationID.x;- uint local_row = (lid >> 3u) * 2u;- uint local_col = (lid & 7u) * 2u;-- float acc[2][2] = { { 0.0, 0.0 }, { 0.0, 0.0 } };+ uint channel_base = (tile / tiles_across) * TILE + (lid >> 3u) * BLOCK;+ uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK; - for (uint k0 = 0u; k0 < p.in_c; k0 += TILE) {- // Stage both blocks. 256 elements each, 64 invocations, so four apiece. Out-of-range- // entries are zeroed rather than skipped, which lets the inner loop run unguarded.- for (uint n = 0u; n < 4u; n++) {- uint slot = lid + n * 64u;- uint r = slot >> 4u;- uint c = slot & 15u;-- uint channel = channel_base + r;- uint k_weight = k0 + c;- weight_tile[r][c] = (channel < p.out_c && k_weight < p.in_c)- ? float(int8_at(p.weight, channel * p.in_c + k_weight))- : 0.0;+ // Rows past the last channel read the last row instead, so the inner loop needs no guard;+ // their results are discarded at the store.+ uint rows[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ rows[i] = min(channel_base + i, p.out_c - 1u);+ }+ // Columns past the last position are read as zero and never stored.+ bool live[BLOCK];+ for (uint j = 0u; j < BLOCK; j++) {+ live[j] = position_base + j < positions;+ } - uint k_input = k0 + r;- uint position = position_base + c;- input_tile[r][c] = (k_input < p.in_c && position < positions)- ? float(arena[p.in0 + k_input * positions + position])- : 0.0;+ float acc[BLOCK][BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ for (uint j = 0u; j < BLOCK; j++) {+ acc[i][j] = 0.0; }- barrier();+ } - for (uint k = 0u; k < TILE; k++) {- float a0 = weight_tile[local_row][k];- float a1 = weight_tile[local_row + 1u][k];- float b0 = input_tile[k][local_col];- float b1 = input_tile[k][local_col + 1u];- acc[0][0] += a0 * b0;- acc[0][1] += a0 * b1;- acc[1][0] += a1 * b0;- acc[1][1] += a1 * b1;+ bool wide = (p.in_c & 15u) == 0u && (positions & 3u) == 0u && (p.in0 & 3u) == 0u+ && (p.weight & 3u) == 0u;+ if (wide) {+ // Whole quads only: the position count is a multiple of four, so a quad is live or not.+ if (position_base >= positions) {+ return;+ }+ uint quads = positions >> 2u;+ uint quad = (p.in0 + position_base) >> 2u;+ uint rows16[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ rows16[i] = (p.weight + ((rows[i] * p.in_c) >> 2u)) >> 2u;+ }+ vec4 sum[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ sum[i] = vec4(0.0);+ }+ for (uint k = 0u; k < p.in_c; k += 16u) {+ uvec4 w[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ w[i] = weights128[rows16[i] + (k >> 4u)];+ }+ for (uint t = 0u; t < 16u; t++) {+ vec4 x = vec4(arena_quad[quad + (k + t) * quads]);+ for (uint i = 0u; i < BLOCK; i++) {+ float tap = float(bitfieldExtract(int(w[i][t >> 2u]), int((t & 3u) << 3u), 8));+ sum[i] += tap * x;+ }+ }+ }+ for (uint i = 0u; i < BLOCK; i++) {+ for (uint j = 0u; j < BLOCK; j++) {+ acc[i][j] = sum[i][j];+ }+ }+ } else if ((p.in_c & 3u) == 0u) {+ uint words[BLOCK];+ for (uint i = 0u; i < BLOCK; i++) {+ words[i] = p.weight + ((rows[i] * p.in_c) >> 2u);+ }+ for (uint k = 0u; k < p.in_c; k += 4u) {+ float x[4][BLOCK];+ for (uint t = 0u; t < 4u; t++) {+ uint at = p.in0 + (k + t) * positions + position_base;+ for (uint j = 0u; j < BLOCK; j++) {+ x[t][j] = live[j] ? float(arena[at + j]) : 0.0;+ }+ }+ for (uint i = 0u; i < BLOCK; i++) {+ int word = int(weights32[words[i] + (k >> 2u)]);+ for (uint t = 0u; t < 4u; t++) {+ float w = float(bitfieldExtract(word, int(t << 3u), 8));+ for (uint j = 0u; j < BLOCK; j++) {+ acc[i][j] += w * x[t][j];+ }+ }+ }+ }+ } else {+ for (uint k = 0u; k < p.in_c; k++) {+ uint at = p.in0 + k * positions + position_base;+ float x[BLOCK];+ for (uint j = 0u; j < BLOCK; j++) {+ x[j] = live[j] ? float(arena[at + j]) : 0.0;+ }+ for (uint i = 0u; i < BLOCK; i++) {+ float w = float(int8_at(p.weight, rows[i] * p.in_c + k));+ for (uint j = 0u; j < BLOCK; j++) {+ acc[i][j] += w * x[j];+ }+ } }- // Both barriers are needed: the second stops the next step's staging from overwriting- // tiles an invocation is still reading.- barrier(); } - for (uint i = 0u; i < 2u; i++) {- uint channel = channel_base + local_row + i;+ for (uint i = 0u; i < BLOCK; i++) {+ uint channel = channel_base + i; if (channel >= p.out_c) { continue; } float scale = float(weights[p.act_weight + channel]); float offset = float(weights[p.bias + channel]);- for (uint j = 0u; j < 2u; j++) {- uint position = position_base + local_col + j;- if (position >= positions) {+ for (uint j = 0u; j < BLOCK; j++) {+ if (!live[j]) { continue; }- uint index = channel * positions + position;+ uint index = channel * positions + position_base + j; float value = quant_store(fused_store(acc[i][j] * scale + offset, p.act, channel, index)); arena[p.out_at + index] = float16_t(value); }
library/ml/src/main/rust/src/nets/mod_part11.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part11.rs b/library/ml/src/main/rust/src/nets/mod_part11.rsindex 7ffde08f1c..e5870ce1f7 100644--- a/library/ml/src/main/rust/src/nets/mod_part11.rs+++ b/library/ml/src/main/rust/src/nets/mod_part11.rs@@ -51,6 +51,23 @@ pub(crate) mod tests { "the Q2_K gemv shader and Rust disagree on channels per workgroup" ); }+ /// `CONV_POINT_INT8_TILE` must equal `TILE` in the int8 tiled shader.+ ///+ /// Rust dispatches one workgroup per `TILE x TILE` block and the shader owns that block, so a+ /// mismatch leaves part of every projection unwritten - plausible wrong numbers, not an error.+ #[test]+ fn the_int8_tile_matches_its_shader() {+ let path = std::path::Path::new(env!("CARGO_MANIFEST_DIR"))+ .join("shaders")+ .join("conv_point_int8.comp");+ let source = std::fs::read_to_string(&path).expect("the shader reads");+ let tile: u32 = source+ .lines()+ .find_map(|line| line.trim_start().strip_prefix("#define TILE"))+ .and_then(|rest| rest.trim().trim_end_matches('u').parse().ok())+ .expect("conv_point_int8.comp declares TILE");+ assert_eq!(tile, CONV_POINT_INT8_TILE);+ } use super::*; /// A [`WeightSource`] that knows only shapes.
library/ml/src/main/rust/src/nets/mod_part3.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part3.rs b/library/ml/src/main/rust/src/nets/mod_part3.rsindex 7feb44fa2b..225d0de3d8 100644--- a/library/ml/src/main/rust/src/nets/mod_part3.rs+++ b/library/ml/src/main/rust/src/nets/mod_part3.rs@@ -5,6 +5,12 @@ /// drift apart. const CONV_POINT_TILE: u32 = 16; +/// `TILE` in `shaders/conv_point_int8.comp`: output channels and positions per workgroup.+///+/// Separate from [`CONV_POINT_TILE`] because the int8 shader dropped shared-memory tiling for+/// register blocking and covers a larger tile - see its header. **Must equal `TILE` there.**+const CONV_POINT_INT8_TILE: u32 = 32;+ /// `ROWS` in `shaders/conv_vec_int8.comp`: output channels per workgroup. /// /// That shader is dispatched one workgroup per group of this many channels, so [`Builder::emit`]
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 027124c581..b69aa93757 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -44,6 +44,8 @@ impl<'a> Builder<'a> { // shader instead. It is the whole of a SMaLL-100 decode step. let vector = tiled && positions == 1; let tiles = so.c.div_ceil(CONV_POINT_TILE) * positions.div_ceil(CONV_POINT_TILE);+ let int8_tiles =+ so.c.div_ceil(CONV_POINT_INT8_TILE) * positions.div_ceil(CONV_POINT_INT8_TILE); // One workgroup per row-group: 2 channels for int8, 8 for int4 and Q2_K — // the three gemv shaders diverged, so each kind counts its own rows. let rows = match quant {@@ -72,7 +74,8 @@ impl<'a> Builder<'a> { // Workgroups for the staged kinds, output elements for the untiled one. let count = match kind { Kind::ConvVecInt8 | Kind::ConvVecInt4 | Kind::ConvVecQ2K => rows,- Kind::ConvPointInt8 | Kind::ConvPointInt4 | Kind::ConvPointQ2K => tiles,+ Kind::ConvPointInt8 => int8_tiles,+ Kind::ConvPointInt4 | Kind::ConvPointQ2K => tiles, _ => so.len(), }; ops.push(Op::Dispatch {
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 18a49296bd..388291d435 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part4.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part4.rs@@ -398,6 +398,48 @@ fn a_tiled_int8_convolution_agrees_with_the_reference() { compare("a tiled int8 convolution", plan, weights.data().to_vec(), &[&input]); } +#[test]+#[ignore = "needs a Vulkan device"]+fn a_tiled_int8_convolution_agrees_across_many_tiles_and_both_loops() {+ // Several 32 x 32 workgroup tiles in both axes, each axis ending on a partial one, run once+ // through each of the shader's three loops: sixteen-tap wide loads (`in_c % 16 == 0` and a+ // width divisible by four), the packed-word loop (`in_c % 4 == 0`) and the element-at-a-time+ // one. A residual-free projection with no activation, so a wrong tap shows directly in the+ // numbers rather than being squashed by a saturating activation.+ for (out_channels, in_channels, width) in [(70u32, 64u32, 76u32), (70, 64, 75), (45, 30, 40)] {+ let kernel: Vec<i8> = (0..(out_channels * in_channels) as i32)+ .map(|i| ((i * 53) % 251 - 125) as i8)+ .collect();+ let scales: Vec<f32> = (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 7) 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.4)),+ ],+ );+ let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+ let input = spread((in_channels * width) as usize, 0.2);+ let mut builder = Builder::new(&weights);+ let first = builder.input(Shape::new(in_channels, 1, width));+ 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 fixture plan builds");+ let what = format!("a tiled int8 convolution {out_channels}x{in_channels} over {width}");+ compare(&what, plan, weights.data().to_vec(), &[&input]);+ }+}+ #[test] #[ignore = "needs a Vulkan device"] fn report_the_tiled_int8_convolution_against_the_tiled_fp16_one() {