ml: fp16 ConvPoint split rows for small GEMMs, supertonic text ConvPoint -20%
- Commit
d1799ab4f283ba009309252b706fb4cb0d0f9dd9- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:51 UTC (1 month ago)
- Parents
2377399
Diff
library/ml/src/main/rust/shaders/conv_point.comp
diff --git a/library/ml/src/main/rust/shaders/conv_point.comp b/library/ml/src/main/rust/shaders/conv_point.compindex c719527131..2e5a5369a3 100644--- a/library/ml/src/main/rust/shaders/conv_point.comp+++ b/library/ml/src/main/rust/shaders/conv_point.comp@@ -45,7 +45,70 @@ layout(std430, binding = 0) readonly buffer ArenaQuad { /// Channels and positions per invocation. #define BLOCK 4u +/// Channels per invocation on the split path (`p.in1 == 1`).+#define SPLIT 2u++// The wide path with two channels per invocation, for GEMMs with too few 32 x 32 tiles to fill+// the GPU (Supertonic's text encoder: 56 positions). Twice the workgroups, each 16 channels x 32+// positions; every output sums the same terms in the same order, so the result is identical.+// Selected only when the wide path's alignment holds (see `nets`).+void split_rows() {+ 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 lid = gl_LocalInvocationID.x;+ uint channel_base = (tile / tiles_across) * (TILE / BLOCK * SPLIT) + (lid >> 3u) * SPLIT;+ uint position_base = (tile % tiles_across) * TILE + (lid & 7u) * BLOCK;+ if (position_base >= positions) {+ return;+ }+ uint rows[SPLIT];+ vec4 sum[SPLIT];+ for (uint i = 0u; i < SPLIT; i++) {+ rows[i] = p.weight + min(channel_base + i, p.out_c - 1u) * p.in_c;+ sum[i] = vec4(0.0);+ }+ uint quads = positions >> 2u;+ uint quad = (p.in0 + position_base) >> 2u;+ for (uint k = 0u; k < p.in_c; k += 8u) {+ uvec4 w[SPLIT];+ for (uint i = 0u; i < SPLIT; i++) {+ w[i] = weights128[(rows[i] + k) >> 3u];+ }+ for (uint t = 0u; t < 8u; t++) {+ vec4 x = vec4(arena_quad[quad + (k + t) * quads]);+ for (uint i = 0u; i < SPLIT; i++) {+ float tap = unpackHalf2x16(w[i][t >> 1u])[t & 1u];+ sum[i] += tap * x;+ }+ }+ }+ for (uint i = 0u; i < SPLIT; i++) {+ uint channel = channel_base + i;+ if (channel >= p.out_c) {+ continue;+ }+ float offset = float(weights[p.bias + channel]);+ for (uint j = 0u; j < BLOCK; j++) {+ uint position = position_base + j;+ if (position >= positions) {+ continue;+ }+ uint index = channel * positions + position;+ float value = fused_store(sum[i][j] + offset, p.act, channel, index);+ arena[p.out_at + index] = float16_t(value);+ }+ }+}+ void main() {+ if (p.in1 == 1u) {+ split_rows();+ return;+ } uint tile = gl_WorkGroupID.y * gl_NumWorkGroups.x + gl_WorkGroupID.x; if (tile >= p.count) { 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 77904f1032..f65b1510bf 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -210,10 +210,24 @@ impl<'a> Builder<'a> { let positions = so.h * so.w; let tiles = so.c.div_ceil(CONV_POINT_TILE) * positions.div_ceil(CONV_POINT_TILE);+ let in0 = at(*input)?;+ // Too few tiles to fill the GPU: two channels per lane (`split_rows` in+ // conv_point.comp), same sums in the same order. Wide alignment only.+ let split = tiles < CONV_POINT_INT8_SPLIT_BELOW+ && in_c % 8 == 0+ && *weight % 8 == 0+ && positions % 4 == 0+ && in0 % 4 == 0;+ let tiles = if split {+ so.c.div_ceil(CONV_POINT_TILE / 2) * positions.div_ceil(CONV_POINT_TILE)+ } else {+ tiles+ }; ops.push(Op::Dispatch { kind: Kind::ConvPoint, push: Push {- in0: at(*input)?,+ in0,+ in1: u32::from(split), out: at(*out)?, weight: *weight, bias: *bias,
library/ml/src/main/rust/src/vulkan/parity_part10.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part10.rs b/library/ml/src/main/rust/src/vulkan/parity_part10.rsindex 010bf5fc80..e7916df1b9 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part10.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part10.rs@@ -233,3 +233,24 @@ fn row_int8_gemms_agree_with_the_reference() { compare(&format!("a row int8 gemm, {what}"), plan, source.data().to_vec(), &[&input]); } }+++/// `conv_point.comp`'s split path (`in1` = 1, two channels per lane) for GEMMs with few tiles:+/// the Supertonic text encoder's 56 positions, 40 output channels leaving the last 16-channel+/// tile part empty, and a fused residual.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_split_pointwise_conv_agrees_with_the_reference() {+ let input = spread(64 * 56, 1.1);+ let source = Invented::new(4);+ let plan = build(&source, &[Shape::new(64, 1, 56)], |b, ids| {+ let x = b.conv(ids[0], 0, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::Relu);+ let y = b.conv(x, 2, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::None);+ b.add(x, y)+ });+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. } if push.in1 == 1+ )));+ compare("a split pointwise conv", plan, source.into_data(), &[&input]);+}\ No newline at end of file
library/ml/src/main/rust/src/vulkan/parity_part6.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part6.rs b/library/ml/src/main/rust/src/vulkan/parity_part6.rsindex d93a99d0d0..6b59c9e6c6 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -412,13 +412,14 @@ fn edge_padded_convs_agree_under_spatial_tiling() { } } /// `conv_point.comp`'s wide path (8 weights per `uvec4`, 4 positions per quad): 64 input-/// channels over 12x12 positions into 40 ragged output channels, with a fused residual.+/// channels over 96x96 positions (576 tiles, above the split threshold) into 40 ragged output+/// channels, with a fused residual. #[test] #[ignore = "needs a Vulkan device"] fn a_wide_pointwise_conv_agrees_with_the_reference() {- let input = spread(64 * 12 * 12, 1.5);+ let input = spread(64 * 96 * 96, 1.5); let source = Invented::new(4);- let plan = build(&source, &[Shape::new(64, 12, 12)], |b, ids| {+ let plan = build(&source, &[Shape::new(64, 96, 96)], |b, ids| { let x = b.conv(ids[0], 0, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::Relu); let y = b.conv(x, 2, 40, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::None); b.add(x, y)@@ -426,7 +427,7 @@ fn a_wide_pointwise_conv_agrees_with_the_reference() { assert!(plan.ops.iter().any(|op| matches!( op, crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. }- if push.weight % 8 == 0 && push.in_c % 8 == 0+ if push.weight % 8 == 0 && push.in_c % 8 == 0 && push.in1 == 0 ))); compare("a wide pointwise conv", plan, source.into_data(), &[&input]); }