ml: split emit, emit_continued and parity_part6 under the 500-line gate
- Commit
adb428e3087b5a322cd26fa719a8f1daa3a76f3a- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:18 UTC (1 month ago)
- Parents
4165c71
Diff
library/ml/src/main/rust/src/nets/mod.rs
diff --git a/library/ml/src/main/rust/src/nets/mod.rs b/library/ml/src/main/rust/src/nets/mod.rsindex 5ab72470e2..2cf6911e9f 100644--- a/library/ml/src/main/rust/src/nets/mod.rs+++ b/library/ml/src/main/rust/src/nets/mod.rs@@ -480,6 +480,8 @@ include!("mod_part7.rs"); include!("mod_part8.rs"); include!("mod_part9.rs"); include!("mod_part9b.rs");+include!("mod_part9c.rs");+include!("mod_part9d.rs"); include!("mod_part10.rs"); include!("mod_part11.rs"); include!("mod_part12.rs");
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 50847f5801..0b8db092c5 100644--- a/library/ml/src/main/rust/src/nets/mod_part9.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9.rs@@ -416,217 +416,8 @@ impl<'a> Builder<'a> { invocations, }); }- Node::Binary { kind, a, b, out } => {- let so = shape(*out);- ops.push(Op::Dispatch {- kind: *kind,- push: Push {- in0: at(*a)?,- in1: at(*b)?,- out: at(*out)?,- in_c: so.c,- in_h: so.h,- in_w: so.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- count: so.len(),- ..Push::default()- },- invocations: so.len(),- });- }- Node::Concat { parts, out } => {- let base = at(*out)?;- let mut written = 0;- for &part in parts {- let len = shape(part).len();- ops.push(Op::Copy {- src: at(part)?,- dst: base + written,- elems: len,- });- written += len;- }- }- Node::Affine { input, out, scale, shift } => {- let so = shape(*out);- ops.push(Op::Dispatch {- kind: Kind::Affine,- push: Push {- in0: at(*input)?,- out: at(*out)?,- in_c: so.c,- in_h: so.h,- in_w: so.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- param0_bits: scale.to_bits(),- param1_bits: shift.to_bits(),- count: so.len(),- ..Push::default()- },- invocations: so.len(),- });- }- Node::LayerNorm { input, out, gamma, beta, epsilon } => {- let so = shape(*out);- // One invocation per position, each reducing over the channels, so the- // dispatch is the spatial extent rather than the element count - or, for a- // handful of positions, one workgroup each. See `row_reduce`.- let positions = so.h * so.w;- let (kw, invocations) = row_reduce(positions, so.c);- ops.push(Op::Dispatch {- kind: Kind::LayerNorm,- push: Push {- in0: at(*input)?,- out: at(*out)?,- weight: *gamma,- bias: *beta,- in_c: so.c,- in_h: so.h,- in_w: so.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- param1_bits: epsilon.to_bits(),- count: positions,- kw,- ..Push::default()- },- invocations,- });- }- Node::InstanceNorm { input, out, epsilon } => {- let so = shape(*out);- // One invocation per channel, each reducing over its own plane, so- // the dispatch is the channel count rather than the element count —- // or, for large planes, one workgroup each. See `row_reduce_plane`.- let (kw, invocations) = row_reduce_plane(so.c, so.h * so.w);- ops.push(Op::Dispatch {- kind: Kind::InstanceNorm,- push: Push {- in0: at(*input)?,- out: at(*out)?,- in_c: so.c,- in_h: so.h,- in_w: so.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- param1_bits: epsilon.to_bits(),- count: so.c,- kw,- ..Push::default()- },- invocations,- });- }- Node::RmsNorm { input, out, gamma, epsilon, groups, res, quant_scale } => {- let so = shape(*out);- // One invocation per group per position, reducing over that group's channels,- // or one workgroup each when there are few. See `row_reduce`.- let positions = so.h * so.w * groups.max(&1);- let (kw, invocations) = row_reduce(positions, so.c / groups.max(&1));- // A folded residual addend, or NO_FUSE. Same contract as the- // convolution kinds: `fused_store` in the shader adds- // `arena[res + index]`, and `arena_reads` covers the range.- let res_at = match res {- Some(id) => at(*id)?,- None => crate::nets::NO_FUSE,- };- ops.push(Op::Dispatch {- kind: Kind::RmsNorm,- push: Push {- in0: at(*input)?,- out: at(*out)?,- weight: *gamma,- in_c: so.c,- in_h: so.h,- in_w: so.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- group: *groups,- param0_bits: quant_scale.map_or(0, f32::to_bits),- param1_bits: epsilon.to_bits(),- count: positions,- res: res_at,- kw,- ..Push::default()- },- invocations,- });- }- Node::AttnScores { q, k, out, heads, kv_heads, scale } => {- let (si, so) = (shape(*q), shape(*out));- let (in0, in1) = (at(*q)?, at(*k)?);- // The tiled shader reads four queries and four keys per load, so both lengths and- // both bases must divide by four. See `shaders/attn_scores.comp`.- let tiled = so.h >= 4- && so.h % 4 == 0- && so.w % 4 == 0- && in0 % 4 == 0- && in1 % 4 == 0;- let tiles = so.c * so.h.div_ceil(ATTN_TILE) * so.w.div_ceil(ATTN_TILE);- let (kw, count, invocations) =- if tiled { (1, tiles, tiles * 64) } else { (0, so.len(), so.len()) };- ops.push(Op::Dispatch {- kind: Kind::AttnScores,- push: Push {- in0,- in1,- out: at(*out)?,- in_c: si.c,- in_h: si.h,- in_w: si.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- group: *heads,- kv_heads: *kv_heads,- param0_bits: scale.to_bits(),- count,- kw,- ..Push::default()- },- invocations,- });- }- Node::AttnScoresCached { q, cache, out, heads, kv_heads, scale, dynamic, sliding, window } => {- let (sq, so) = (shape(*q), shape(*out));- ops.push(Op::Dispatch {- kind: Kind::AttnScoresCached,- push: Push {- in0: at(*q)?,- in1: at(*cache)?,- out: at(*out)?,- // `d_model`, which doubles as the cache's per-position stride: a cache is- // `[keys, 1, d_model]`, so one position is `in_c` elements.- in_c: sq.c,- in_h: 1,- in_w: 1,- out_c: so.c,- out_h: so.h,- out_w: so.w,- group: *heads,- param0_bits: scale.to_bits(),- count: so.len(),- dyn_keys: u32::from(*dynamic),- kv_heads: *kv_heads,- sliding: u32::from(*sliding),- // Several queries (`so.h`): the per-query range path. Zero otherwise,- // so a decode step's push is unchanged.- dil_h: if so.h > 1 || *window != 0 { so.h } else { 0 },- dil_w: *window,- ..Push::default()- },- invocations: so.len(),- });- } other => {- self.emit_continued(other, at, shape, ops)?;+ self.emit_mid(other, at, shape, ops)?; } } Ok(())
library/ml/src/main/rust/src/nets/mod_part9b.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part9b.rs b/library/ml/src/main/rust/src/nets/mod_part9b.rsindex 76d1ae14de..66afaaba0e 100644--- a/library/ml/src/main/rust/src/nets/mod_part9b.rs+++ b/library/ml/src/main/rust/src/nets/mod_part9b.rs@@ -426,83 +426,7 @@ impl<'a> Builder<'a> { column += sp.w; } }- Node::AttnApply { probs, v, out, heads, kv_heads } => {- let (sv, so) = (shape(*v), shape(*out));- let (in0, in1) = (at(*probs)?, at(*v)?);- // The tiled shader reads both operands four keys per load and owns four channels of- // one head per invocation. A single query would waste three of every four lanes'- // work, so a decode step stays on the per-element shader.- let head_dim = so.c / (*heads).max(1);- let tiled = so.w >= 8- && sv.w % 4 == 0- && head_dim % 4 == 0- && in0 % 4 == 0- && in1 % 4 == 0;- let tiles = so.c.div_ceil(ATTN_TILE) * so.w.div_ceil(ATTN_TILE);- let (kw, count, invocations) =- if tiled { (1, tiles, tiles * 64) } else { (0, so.len(), so.len()) };- ops.push(Op::Dispatch {- kind: Kind::AttnApply,- push: Push {- in0,- in1,- out: at(*out)?,- in_c: so.c,- in_h: so.h,- // The **key** count, which is V's length. `out_w` is the query count,- // and for a cross-attention those differ.- in_w: sv.w,- out_c: so.c,- out_h: so.h,- out_w: so.w,- group: *heads,- kv_heads: *kv_heads,- count,- kw,- ..Push::default()- },- invocations,- });- }- Node::Rotary { input, angles, out, heads, axes } => {- let so = shape(*out);- ops.push(Op::Dispatch {- kind: Kind::Rotary,- push: Push {- in0: at(*input)?,- in1: at(*angles)?,- out: at(*out)?,- // The head width, which is what the frequency index wraps on. Not the- // channel count.- in_c: so.c / heads.max(&1),- out_c: so.c,- out_h: so.h,- out_w: so.w,- group: *heads,- rope_axes: *axes,- count: so.len(),- ..Push::default()- },- invocations: so.len(),- });- }- Node::Constant { out, weight } => {- let so = shape(*out);- ops.push(Op::Dispatch {- kind: Kind::Constant,- push: Push {- out: at(*out)?,- weight: *weight,- out_c: so.c,- out_h: so.h,- out_w: so.w,- count: so.len(),- ..Push::default()- },- invocations: so.len(),- });- }- other => self.emit_top_k(other, at, shape, ops)?,+ other => self.emit_last(other, at, shape, ops)?, } Ok(()) }
library/ml/src/main/rust/src/nets/mod_part9c.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part9c.rs b/library/ml/src/main/rust/src/nets/mod_part9c.rsnew file mode 100644index 0000000000..6995895eaa--- /dev/null+++ b/library/ml/src/main/rust/src/nets/mod_part9c.rs@@ -0,0 +1,225 @@+impl<'a> Builder<'a> {+ fn emit_mid(+ &self,+ node: &Node,+ at: &dyn Fn(Id) -> Result<u32, String>,+ shape: &dyn Fn(Id) -> Shape,+ ops: &mut Vec<Op>,+ ) -> Result<(), String> {+ match node {+ Node::Binary { kind, a, b, out } => {+ let so = shape(*out);+ ops.push(Op::Dispatch {+ kind: *kind,+ push: Push {+ in0: at(*a)?,+ in1: at(*b)?,+ out: at(*out)?,+ in_c: so.c,+ in_h: so.h,+ in_w: so.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ count: so.len(),+ ..Push::default()+ },+ invocations: so.len(),+ });+ }+ Node::Concat { parts, out } => {+ let base = at(*out)?;+ let mut written = 0;+ for &part in parts {+ let len = shape(part).len();+ ops.push(Op::Copy {+ src: at(part)?,+ dst: base + written,+ elems: len,+ });+ written += len;+ }+ }+ Node::Affine { input, out, scale, shift } => {+ let so = shape(*out);+ ops.push(Op::Dispatch {+ kind: Kind::Affine,+ push: Push {+ in0: at(*input)?,+ out: at(*out)?,+ in_c: so.c,+ in_h: so.h,+ in_w: so.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ param0_bits: scale.to_bits(),+ param1_bits: shift.to_bits(),+ count: so.len(),+ ..Push::default()+ },+ invocations: so.len(),+ });+ }+ Node::LayerNorm { input, out, gamma, beta, epsilon } => {+ let so = shape(*out);+ // One invocation per position, each reducing over the channels, so the+ // dispatch is the spatial extent rather than the element count - or, for a+ // handful of positions, one workgroup each. See `row_reduce`.+ let positions = so.h * so.w;+ let (kw, invocations) = row_reduce(positions, so.c);+ ops.push(Op::Dispatch {+ kind: Kind::LayerNorm,+ push: Push {+ in0: at(*input)?,+ out: at(*out)?,+ weight: *gamma,+ bias: *beta,+ in_c: so.c,+ in_h: so.h,+ in_w: so.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ param1_bits: epsilon.to_bits(),+ count: positions,+ kw,+ ..Push::default()+ },+ invocations,+ });+ }+ Node::InstanceNorm { input, out, epsilon } => {+ let so = shape(*out);+ // One invocation per channel, each reducing over its own plane, so+ // the dispatch is the channel count rather than the element count —+ // or, for large planes, one workgroup each. See `row_reduce_plane`.+ let (kw, invocations) = row_reduce_plane(so.c, so.h * so.w);+ ops.push(Op::Dispatch {+ kind: Kind::InstanceNorm,+ push: Push {+ in0: at(*input)?,+ out: at(*out)?,+ in_c: so.c,+ in_h: so.h,+ in_w: so.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ param1_bits: epsilon.to_bits(),+ count: so.c,+ kw,+ ..Push::default()+ },+ invocations,+ });+ }+ Node::RmsNorm { input, out, gamma, epsilon, groups, res, quant_scale } => {+ let so = shape(*out);+ // One invocation per group per position, reducing over that group's channels,+ // or one workgroup each when there are few. See `row_reduce`.+ let positions = so.h * so.w * groups.max(&1);+ let (kw, invocations) = row_reduce(positions, so.c / groups.max(&1));+ // A folded residual addend, or NO_FUSE. Same contract as the+ // convolution kinds: `fused_store` in the shader adds+ // `arena[res + index]`, and `arena_reads` covers the range.+ let res_at = match res {+ Some(id) => at(*id)?,+ None => crate::nets::NO_FUSE,+ };+ ops.push(Op::Dispatch {+ kind: Kind::RmsNorm,+ push: Push {+ in0: at(*input)?,+ out: at(*out)?,+ weight: *gamma,+ in_c: so.c,+ in_h: so.h,+ in_w: so.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ group: *groups,+ param0_bits: quant_scale.map_or(0, f32::to_bits),+ param1_bits: epsilon.to_bits(),+ count: positions,+ res: res_at,+ kw,+ ..Push::default()+ },+ invocations,+ });+ }+ Node::AttnScores { q, k, out, heads, kv_heads, scale } => {+ let (si, so) = (shape(*q), shape(*out));+ let (in0, in1) = (at(*q)?, at(*k)?);+ // The tiled shader reads four queries and four keys per load, so both lengths and+ // both bases must divide by four. See `shaders/attn_scores.comp`.+ let tiled = so.h >= 4+ && so.h % 4 == 0+ && so.w % 4 == 0+ && in0 % 4 == 0+ && in1 % 4 == 0;+ let tiles = so.c * so.h.div_ceil(ATTN_TILE) * so.w.div_ceil(ATTN_TILE);+ let (kw, count, invocations) =+ if tiled { (1, tiles, tiles * 64) } else { (0, so.len(), so.len()) };+ ops.push(Op::Dispatch {+ kind: Kind::AttnScores,+ push: Push {+ in0,+ in1,+ out: at(*out)?,+ in_c: si.c,+ in_h: si.h,+ in_w: si.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ group: *heads,+ kv_heads: *kv_heads,+ param0_bits: scale.to_bits(),+ count,+ kw,+ ..Push::default()+ },+ invocations,+ });+ }+ Node::AttnScoresCached { q, cache, out, heads, kv_heads, scale, dynamic, sliding, window } => {+ let (sq, so) = (shape(*q), shape(*out));+ ops.push(Op::Dispatch {+ kind: Kind::AttnScoresCached,+ push: Push {+ in0: at(*q)?,+ in1: at(*cache)?,+ out: at(*out)?,+ // `d_model`, which doubles as the cache's per-position stride: a cache is+ // `[keys, 1, d_model]`, so one position is `in_c` elements.+ in_c: sq.c,+ in_h: 1,+ in_w: 1,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ group: *heads,+ param0_bits: scale.to_bits(),+ count: so.len(),+ dyn_keys: u32::from(*dynamic),+ kv_heads: *kv_heads,+ sliding: u32::from(*sliding),+ // Several queries (`so.h`): the per-query range path. Zero otherwise,+ // so a decode step's push is unchanged.+ dil_h: if so.h > 1 || *window != 0 { so.h } else { 0 },+ dil_w: *window,+ ..Push::default()+ },+ invocations: so.len(),+ });+ }+ other => {+ self.emit_continued(other, at, shape, ops)?;+ }+ }+ Ok(())+ }+}
library/ml/src/main/rust/src/nets/mod_part9d.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part9d.rs b/library/ml/src/main/rust/src/nets/mod_part9d.rsnew file mode 100644index 0000000000..bd3a967d26--- /dev/null+++ b/library/ml/src/main/rust/src/nets/mod_part9d.rs@@ -0,0 +1,90 @@+impl<'a> Builder<'a> {+ fn emit_last(+ &self,+ node: &Node,+ at: &dyn Fn(Id) -> Result<u32, String>,+ shape: &dyn Fn(Id) -> Shape,+ ops: &mut Vec<Op>,+ ) -> Result<(), String> {+ match node {+ Node::AttnApply { probs, v, out, heads, kv_heads } => {+ let (sv, so) = (shape(*v), shape(*out));+ let (in0, in1) = (at(*probs)?, at(*v)?);+ // The tiled shader reads both operands four keys per load and owns four channels of+ // one head per invocation. A single query would waste three of every four lanes'+ // work, so a decode step stays on the per-element shader.+ let head_dim = so.c / (*heads).max(1);+ let tiled = so.w >= 8+ && sv.w % 4 == 0+ && head_dim % 4 == 0+ && in0 % 4 == 0+ && in1 % 4 == 0;+ let tiles = so.c.div_ceil(ATTN_TILE) * so.w.div_ceil(ATTN_TILE);+ let (kw, count, invocations) =+ if tiled { (1, tiles, tiles * 64) } else { (0, so.len(), so.len()) };+ ops.push(Op::Dispatch {+ kind: Kind::AttnApply,+ push: Push {+ in0,+ in1,+ out: at(*out)?,+ in_c: so.c,+ in_h: so.h,+ // The **key** count, which is V's length. `out_w` is the query count,+ // and for a cross-attention those differ.+ in_w: sv.w,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ group: *heads,+ kv_heads: *kv_heads,+ count,+ kw,+ ..Push::default()+ },+ invocations,+ });+ }+ Node::Rotary { input, angles, out, heads, axes } => {+ let so = shape(*out);+ ops.push(Op::Dispatch {+ kind: Kind::Rotary,+ push: Push {+ in0: at(*input)?,+ in1: at(*angles)?,+ out: at(*out)?,+ // The head width, which is what the frequency index wraps on. Not the+ // channel count.+ in_c: so.c / heads.max(&1),+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ group: *heads,+ rope_axes: *axes,+ count: so.len(),+ ..Push::default()+ },+ invocations: so.len(),+ });+ }+ Node::Constant { out, weight } => {+ let so = shape(*out);+ ops.push(Op::Dispatch {+ kind: Kind::Constant,+ push: Push {+ out: at(*out)?,+ weight: *weight,+ out_c: so.c,+ out_h: so.h,+ out_w: so.w,+ count: so.len(),+ ..Push::default()+ },+ invocations: so.len(),+ });+ }+ other => self.emit_top_k(other, at, shape, ops)?,+ }+ Ok(())+ }+}
library/ml/src/main/rust/src/vulkan/parity.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity.rs b/library/ml/src/main/rust/src/vulkan/parity.rsindex ed554f9f34..1441d0689f 100644--- a/library/ml/src/main/rust/src/vulkan/parity.rs+++ b/library/ml/src/main/rust/src/vulkan/parity.rs@@ -443,5 +443,6 @@ include!("parity_part4.rs"); include!("parity_part9.rs"); include!("parity_part5.rs"); include!("parity_part6.rs");+include!("parity_part10.rs"); include!("parity_part7.rs"); include!("parity_part8.rs");\ No newline at end of file
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.rsnew file mode 100644index 0000000000..010bf5fc80--- /dev/null+++ b/library/ml/src/main/rust/src/vulkan/parity_part10.rs@@ -0,0 +1,235 @@+// Device parity fixtures (vision and Supertonic work). See `parity.rs` for the helpers.+// Blocked, split, full-window and row-path convolutions, int8 and fp16.++/// 2x2 stride-2 transposed convolutions on the blocked path (`in1` = 1): the ppocr_det head+/// pair (24 -> 6 -> 1 channels, ragged channel blocks) over an odd 19x37 input, and a fused+/// residual onto the second.+#[test]+#[ignore = "needs a Vulkan device"]+fn blocked_2x2_transposed_convs_agree_with_the_reference() {+ let input = spread(24 * 19 * 37, 0.5);+ let source = Invented::new(6);+ let plan = build(&source, &[Shape::new(24, 19, 37)], |b, ids| {+ b.spatial_tiling();+ let x = b.conv_transpose(ids[0], 0, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::Relu);+ let y = b.conv_transpose(x, 2, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::None);+ let z = b.add(y, y);+ b.conv_transpose(z, 4, 1, (2, 2), (2, 2), (0, 0, 0, 0), Act::Sigmoid)+ });+ let mut seen = 0;+ for op in &plan.ops {+ if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvTranspose, push, .. } = op {+ assert_eq!(push.in1, 1, "a 2x2 transpose missed the blocked path");+ seen += 1;+ }+ }+ assert_eq!(seen, 3);+ compare("blocked 2x2 transposed convs", plan, source.into_data(), &[&input]);+}++/// `conv_point_int8.comp`'s split path (`in1` = 1, two channels per invocation), which a GEMM+/// with fewer than 256 32 x 32 tiles takes: 40 channels leave the last 16-channel tile part empty,+/// and 52 / 49 positions cover the aligned and the ragged activation loads.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_split_int8_gemm_agrees_with_the_reference() {+ let (out_channels, in_channels) = (40u32, 64u32);+ let kernel: Vec<i8> =+ (0..(out_channels * in_channels) as i32).map(|i| ((i * 37) % 251 - 125) as i8).collect();+ let scales: Vec<f32> = (0..out_channels).map(|c| 0.007_812_5 * (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.5)),+ ],+ );+ let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+ for width in [52u32, 49] {+ let input = spread((in_channels * width) as usize, 1.3);+ 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::Gelu,+ );+ let plan = builder.finish(&[last]).expect("the split plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }+ if push.in1 == 1+ )));+ let what = format!("a split int8 gemm over {width}");+ compare(&what, plan, weights.data().to_vec(), &[&input]);+ }+}++/// `conv_int8.comp`'s blocked path (`in1` = 1): the whisper stem's stride-2 1x3 at a ragged+/// width and channel count, a stride-1 1x3, and a 4x4 stride-4 patch embed (clip) over a+/// ragged map.+#[test]+#[ignore = "needs a Vulkan device"]+fn blocked_int8_convs_agree_with_the_reference() {+ #[allow(clippy::type_complexity)]+ let cases: [(&str, u32, u32, Shape, (u32, u32), (u32, u32), (u32, u32, u32, u32), u32, Act); 3] = [+ ("stride-2 1x3", 10, 6, Shape::new(6, 1, 37), (1, 3), (1, 2), (0, 1, 0, 1), 1, Act::Gelu),+ ("stride-1 1x3", 12, 5, Shape::new(5, 1, 29), (1, 3), (1, 1), (0, 1, 0, 1), 1, Act::None),+ ("4x4 stride-4", 6, 3, Shape::new(3, 18, 22), (4, 4), (4, 4), (0, 0, 0, 0), 1, Act::None),+ ];+ for (what, out_channels, in_channels, shape, kernel, stride, pad, group, act) in cases {+ let taps = (in_channels / group) * kernel.0 * kernel.1;+ let weights: Vec<i8> =+ (0..(out_channels * taps) as i32).map(|i| ((i * 37) % 251 - 125) as i8).collect();+ let scales: Vec<f32> =+ (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 5) as f32)).collect();+ let blob = write_mixed(+ graph::SUPERTONIC_VE,+ &[+ Fixture::I8(vec![out_channels, in_channels / group, kernel.0, kernel.1], weights),+ Fixture::F16(vec![out_channels], scales),+ Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.5)),+ ],+ );+ let source = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+ let input = spread(shape.len() as usize, 0.7);+ let mut builder = Builder::new(&source);+ let first = builder.input(shape);+ let last =+ builder.conv_int8(first, 0, out_channels, kernel, stride, (1, 1), pad, group, act);+ let plan = builder.finish(&[last]).expect("the blocked int8 plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvInt8, push, .. }+ if push.in1 == 1+ )), "{what} missed the blocked path");+ compare(&format!("a blocked int8 {what}"), plan, source.data().to_vec(), &[&input]);+ }+}++/// A 1x1 with a fused PReLU on `conv_point.comp` (MobileFaceNet's projections): 20 channels+/// and 15x15 positions are ragged against the tile, and the slope is read per channel.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_pointwise_conv_with_prelu_agrees_with_the_reference() {+ let input = spread(24 * 15 * 15, 3.0);+ let source = Invented::new(3);+ let plan = build(&source, &[Shape::new(24, 15, 15)], |b, ids| {+ b.conv(ids[0], 0, 20, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::PRelu(2))+ });+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, .. }+ )));+ compare("a pointwise conv with PReLU", plan, source.into_data(), &[&input]);+}++/// Either side of the split threshold: 504 x 481 is 16 x 16 = 256 tiles and stays on the four-row+/// path; 504 x 450 is 16 x 15 = 240 and splits. Ragged on purpose: 504 channels leave the last+/// 16- and 32-channel tiles part empty, and neither width is a multiple of four.+#[test]+#[ignore = "needs a Vulkan device"]+fn int8_gemms_either_side_of_the_split_threshold_agree_with_the_reference() {+ let (out_channels, in_channels) = (504u32, 16u32);+ let kernel: Vec<i8> =+ (0..(out_channels * in_channels) as i32).map(|i| ((i * 41) % 247 - 123) as i8).collect();+ let scales: Vec<f32> = (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 9) 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.9)),+ ],+ );+ let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+ for (width, split) in [(481u32, 0u32), (450, 1)] {+ 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 plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }+ if push.in1 == split+ )), "width {width} should take in1 = {split}");+ let what = format!("an int8 gemm {out_channels}x{width} at in1 = {split}");+ compare(&what, plan, weights.data().to_vec(), &[&input]);+ }+}++/// A kernel covering its whole input (mbf's 7x7 GDC) lowers to `conv_point.comp` over the+/// flattened input; a non-square 5x7 window and 20 output channels keep it ragged.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_full_window_conv_agrees_with_the_reference() {+ let input = spread(24 * 5 * 7, 2.0);+ let source = Invented::new(2);+ let plan = build(&source, &[Shape::new(24, 5, 7)], |b, ids| {+ b.spatial_tiling();+ b.conv(ids[0], 0, 20, (5, 7), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::Relu)+ });+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. }+ if push.in_c == 24 * 5 * 7+ )));+ compare("a full-window conv", plan, source.into_data(), &[&input]);+}++/// The Supertonic vocoder head's shape on the blocked path: one row, a 1x3 edge-padded on the+/// left only (2, 0) with PReLU, over 37 columns and 10 ragged output channels.+#[test]+#[ignore = "needs a Vulkan device"]+fn a_blocked_edge_padded_1x3_agrees_with_the_reference() {+ let input = spread(8 * 37, 0.4);+ let source = Invented::new(3);+ let plan = build(&source, &[Shape::new(8, 1, 37)], |b, ids| {+ b.spatial_tiling();+ b.edge_padding();+ b.conv(ids[0], 0, 10, (1, 3), (1, 1), (1, 1), (0, 2, 0, 0), 1, Act::PRelu(2))+ });+ assert_eq!(conv_selectors(&plan), [0, 1, 0], "the edge-padded 1x3 missed the blocked path");+ compare("a blocked edge-padded 1x3", plan, source.into_data(), &[&input]);+}++/// Whisper's stem shape on `conv_point_int8.comp`'s row path (`in1` = 2): one-row 1x3 int8+/// convolutions padded by one, at stride 1 and 2, over ragged widths and channel counts.+#[test]+#[ignore = "needs a Vulkan device"]+fn row_int8_gemms_agree_with_the_reference() {+ for (what, out_channels, in_channels, width, stride, act) in [+ ("stride 1", 20u32, 16u32, 37u32, 1u32, Act::Gelu),+ ("stride 2", 36, 32, 45, 2, Act::None),+ ] {+ let weights: Vec<i8> = (0..(out_channels * in_channels * 3) as i32)+ .map(|i| ((i * 37) % 251 - 125) as i8)+ .collect();+ let scales: Vec<f32> =+ (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 5) as f32)).collect();+ let blob = write_mixed(+ graph::SUPERTONIC_VE,+ &[+ Fixture::I8(vec![out_channels, in_channels, 1, 3], weights),+ Fixture::F16(vec![out_channels], scales),+ Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.5)),+ ],+ );+ let source = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");+ let input = spread((in_channels * width) as usize, 0.9);+ let mut builder = Builder::new(&source);+ let first = builder.input(Shape::new(in_channels, 1, width));+ let last = builder.conv_int8(+ first, 0, out_channels, (1, 3), (1, stride), (1, 1), (0, 1, 0, 1), 1, act,+ );+ let plan = builder.finish(&[last]).expect("the row gemm plan builds");+ assert!(plan.ops.iter().any(|op| matches!(+ op,+ crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }+ if push.in1 == 2+ )), "{what} missed the row path");+ compare(&format!("a row int8 gemm, {what}"), plan, source.data().to_vec(), &[&input]);+ }+}
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 ec2d056e08..1360cc7446 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part6.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part6.rs@@ -411,239 +411,6 @@ fn edge_padded_convs_agree_under_spatial_tiling() { compare(&what, plan, source.into_data(), &[&input]); } }-/// 2x2 stride-2 transposed convolutions on the blocked path (`in1` = 1): the ppocr_det head-/// pair (24 -> 6 -> 1 channels, ragged channel blocks) over an odd 19x37 input, and a fused-/// residual onto the second.-#[test]-#[ignore = "needs a Vulkan device"]-fn blocked_2x2_transposed_convs_agree_with_the_reference() {- let input = spread(24 * 19 * 37, 0.5);- let source = Invented::new(6);- let plan = build(&source, &[Shape::new(24, 19, 37)], |b, ids| {- b.spatial_tiling();- let x = b.conv_transpose(ids[0], 0, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::Relu);- let y = b.conv_transpose(x, 2, 6, (2, 2), (2, 2), (0, 0, 0, 0), Act::None);- let z = b.add(y, y);- b.conv_transpose(z, 4, 1, (2, 2), (2, 2), (0, 0, 0, 0), Act::Sigmoid)- });- let mut seen = 0;- for op in &plan.ops {- if let crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvTranspose, push, .. } = op {- assert_eq!(push.in1, 1, "a 2x2 transpose missed the blocked path");- seen += 1;- }- }- assert_eq!(seen, 3);- compare("blocked 2x2 transposed convs", plan, source.into_data(), &[&input]);-}--/// `conv_point_int8.comp`'s split path (`in1` = 1, two channels per invocation), which a GEMM-/// with fewer than 256 32 x 32 tiles takes: 40 channels leave the last 16-channel tile part empty,-/// and 52 / 49 positions cover the aligned and the ragged activation loads.-#[test]-#[ignore = "needs a Vulkan device"]-fn a_split_int8_gemm_agrees_with_the_reference() {- let (out_channels, in_channels) = (40u32, 64u32);- let kernel: Vec<i8> =- (0..(out_channels * in_channels) as i32).map(|i| ((i * 37) % 251 - 125) as i8).collect();- let scales: Vec<f32> = (0..out_channels).map(|c| 0.007_812_5 * (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.5)),- ],- );- let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");- for width in [52u32, 49] {- let input = spread((in_channels * width) as usize, 1.3);- 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::Gelu,- );- let plan = builder.finish(&[last]).expect("the split plan builds");- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }- if push.in1 == 1- )));- let what = format!("a split int8 gemm over {width}");- compare(&what, plan, weights.data().to_vec(), &[&input]);- }-}--/// `conv_int8.comp`'s blocked path (`in1` = 1): the whisper stem's stride-2 1x3 at a ragged-/// width and channel count, a stride-1 1x3, and a 4x4 stride-4 patch embed (clip) over a-/// ragged map.-#[test]-#[ignore = "needs a Vulkan device"]-fn blocked_int8_convs_agree_with_the_reference() {- #[allow(clippy::type_complexity)]- let cases: [(&str, u32, u32, Shape, (u32, u32), (u32, u32), (u32, u32, u32, u32), u32, Act); 3] = [- ("stride-2 1x3", 10, 6, Shape::new(6, 1, 37), (1, 3), (1, 2), (0, 1, 0, 1), 1, Act::Gelu),- ("stride-1 1x3", 12, 5, Shape::new(5, 1, 29), (1, 3), (1, 1), (0, 1, 0, 1), 1, Act::None),- ("4x4 stride-4", 6, 3, Shape::new(3, 18, 22), (4, 4), (4, 4), (0, 0, 0, 0), 1, Act::None),- ];- for (what, out_channels, in_channels, shape, kernel, stride, pad, group, act) in cases {- let taps = (in_channels / group) * kernel.0 * kernel.1;- let weights: Vec<i8> =- (0..(out_channels * taps) as i32).map(|i| ((i * 37) % 251 - 125) as i8).collect();- let scales: Vec<f32> =- (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 5) as f32)).collect();- let blob = write_mixed(- graph::SUPERTONIC_VE,- &[- Fixture::I8(vec![out_channels, in_channels / group, kernel.0, kernel.1], weights),- Fixture::F16(vec![out_channels], scales),- Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.5)),- ],- );- let source = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");- let input = spread(shape.len() as usize, 0.7);- let mut builder = Builder::new(&source);- let first = builder.input(shape);- let last =- builder.conv_int8(first, 0, out_channels, kernel, stride, (1, 1), pad, group, act);- let plan = builder.finish(&[last]).expect("the blocked int8 plan builds");- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvInt8, push, .. }- if push.in1 == 1- )), "{what} missed the blocked path");- compare(&format!("a blocked int8 {what}"), plan, source.data().to_vec(), &[&input]);- }-}--/// A 1x1 with a fused PReLU on `conv_point.comp` (MobileFaceNet's projections): 20 channels-/// and 15x15 positions are ragged against the tile, and the slope is read per channel.-#[test]-#[ignore = "needs a Vulkan device"]-fn a_pointwise_conv_with_prelu_agrees_with_the_reference() {- let input = spread(24 * 15 * 15, 3.0);- let source = Invented::new(3);- let plan = build(&source, &[Shape::new(24, 15, 15)], |b, ids| {- b.conv(ids[0], 0, 20, (1, 1), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::PRelu(2))- });- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, .. }- )));- compare("a pointwise conv with PReLU", plan, source.into_data(), &[&input]);-}--/// Either side of the split threshold: 504 x 481 is 16 x 16 = 256 tiles and stays on the four-row-/// path; 504 x 450 is 16 x 15 = 240 and splits. Ragged on purpose: 504 channels leave the last-/// 16- and 32-channel tiles part empty, and neither width is a multiple of four.-#[test]-#[ignore = "needs a Vulkan device"]-fn int8_gemms_either_side_of_the_split_threshold_agree_with_the_reference() {- let (out_channels, in_channels) = (504u32, 16u32);- let kernel: Vec<i8> =- (0..(out_channels * in_channels) as i32).map(|i| ((i * 41) % 247 - 123) as i8).collect();- let scales: Vec<f32> = (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 9) 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.9)),- ],- );- let weights = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");- for (width, split) in [(481u32, 0u32), (450, 1)] {- 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 plan builds");- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }- if push.in1 == split- )), "width {width} should take in1 = {split}");- let what = format!("an int8 gemm {out_channels}x{width} at in1 = {split}");- compare(&what, plan, weights.data().to_vec(), &[&input]);- }-}--/// A kernel covering its whole input (mbf's 7x7 GDC) lowers to `conv_point.comp` over the-/// flattened input; a non-square 5x7 window and 20 output channels keep it ragged.-#[test]-#[ignore = "needs a Vulkan device"]-fn a_full_window_conv_agrees_with_the_reference() {- let input = spread(24 * 5 * 7, 2.0);- let source = Invented::new(2);- let plan = build(&source, &[Shape::new(24, 5, 7)], |b, ids| {- b.spatial_tiling();- b.conv(ids[0], 0, 20, (5, 7), (1, 1), (1, 1), (0, 0, 0, 0), 1, Act::Relu)- });- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPoint, push, .. }- if push.in_c == 24 * 5 * 7- )));- compare("a full-window conv", plan, source.into_data(), &[&input]);-}--/// The Supertonic vocoder head's shape on the blocked path: one row, a 1x3 edge-padded on the-/// left only (2, 0) with PReLU, over 37 columns and 10 ragged output channels.-#[test]-#[ignore = "needs a Vulkan device"]-fn a_blocked_edge_padded_1x3_agrees_with_the_reference() {- let input = spread(8 * 37, 0.4);- let source = Invented::new(3);- let plan = build(&source, &[Shape::new(8, 1, 37)], |b, ids| {- b.spatial_tiling();- b.edge_padding();- b.conv(ids[0], 0, 10, (1, 3), (1, 1), (1, 1), (0, 2, 0, 0), 1, Act::PRelu(2))- });- assert_eq!(conv_selectors(&plan), [0, 1, 0], "the edge-padded 1x3 missed the blocked path");- compare("a blocked edge-padded 1x3", plan, source.into_data(), &[&input]);-}--/// Whisper's stem shape on `conv_point_int8.comp`'s row path (`in1` = 2): one-row 1x3 int8-/// convolutions padded by one, at stride 1 and 2, over ragged widths and channel counts.-#[test]-#[ignore = "needs a Vulkan device"]-fn row_int8_gemms_agree_with_the_reference() {- for (what, out_channels, in_channels, width, stride, act) in [- ("stride 1", 20u32, 16u32, 37u32, 1u32, Act::Gelu),- ("stride 2", 36, 32, 45, 2, Act::None),- ] {- let weights: Vec<i8> = (0..(out_channels * in_channels * 3) as i32)- .map(|i| ((i * 37) % 251 - 125) as i8)- .collect();- let scales: Vec<f32> =- (0..out_channels).map(|c| 0.003_906_25 * (1.0 + (c % 5) as f32)).collect();- let blob = write_mixed(- graph::SUPERTONIC_VE,- &[- Fixture::I8(vec![out_channels, in_channels, 1, 3], weights),- Fixture::F16(vec![out_channels], scales),- Fixture::F16(vec![out_channels], spread(out_channels as usize, 0.5)),- ],- );- let source = Weights::parse(&blob, graph::SUPERTONIC_VE).expect("the fixture blob parses");- let input = spread((in_channels * width) as usize, 0.9);- let mut builder = Builder::new(&source);- let first = builder.input(Shape::new(in_channels, 1, width));- let last = builder.conv_int8(- first, 0, out_channels, (1, 3), (1, stride), (1, 1), (0, 1, 0, 1), 1, act,- );- let plan = builder.finish(&[last]).expect("the row gemm plan builds");- assert!(plan.ops.iter().any(|op| matches!(- op,- crate::nets::Op::Dispatch { kind: crate::nets::Kind::ConvPointInt8, push, .. }- if push.in1 == 2- )), "{what} missed the row path");- compare(&format!("a row int8 gemm, {what}"), plan, source.data().to_vec(), &[&input]);- }-}- /// `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. #[test]