ml: fix GatedActivate arena reads and graph-section lowering
- Commit
43e8ccb0fdb1ac5369b23a20889c37906b281ef5- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 19:13 UTC (28 days ago)
- Parents
10854ab
Two builder defects the graph tests exposed: - Kind::arena_reads treated GatedActivate as a dense whole-plane read (in_c*in_h*in_w), but its lowering sets in_c to the gate half's element count, so the claimed read ran 4x past the input and the aliasing check rejected a sound plan. The shader reads exactly the two halves: (in0, in_c + count). - Graph::lower replayed conv nodes through the raw offset entry points, which never mark file tensors as read, so Builder::finish's every-tensor rule refused every lowered section at its first kernel. Lowering now names the tensors its nodes consume (weight/scale/bias, gamma/beta) as it replays them, and maps the section quant code 2 to Quant::Q2K instead of I4. Verified: cargo +1.97.0 test --locked --lib --tests in library/ml/src/main/rust — 633 + 19 + 5 passed, 0 failed.
Diff
library/ml/src/main/rust/src/nets/mod_part1.rs
diff --git a/library/ml/src/main/rust/src/nets/mod_part1.rs b/library/ml/src/main/rust/src/nets/mod_part1.rsindex 5b32fda0e1..8bd0027a3f 100644--- a/library/ml/src/main/rust/src/nets/mod_part1.rs+++ b/library/ml/src/main/rust/src/nets/mod_part1.rs@@ -464,6 +464,12 @@ impl Kind { } Reads::Ranges(ranges) }+ // Both halves of the fused [gate | up] projection: `in_c` is the+ // gate half's element count (the partner stride), `count` the+ // output's, so the read is exactly the two halves — not the+ // generic `dense`, which would multiply the stride back up by+ // the spatial extent and claim reads past the input's end.+ Kind::GatedActivate => one(push.in0, push.in_c + push.count), // The whole input plane, whatever the kernel touches. Kind::MaxPool | Kind::AvgPool@@ -480,7 +486,6 @@ impl Kind { | Kind::Quantize | Kind::QuantizeBlocks | Kind::Activate- | Kind::GatedActivate | Kind::MulScalar | Kind::Clamp => one(push.in0, dense), // As above, plus a folded residual addend (see `Push::res`): the
library/ml/src/main/rust/src/weights_part6.rs
diff --git a/library/ml/src/main/rust/src/weights_part6.rs b/library/ml/src/main/rust/src/weights_part6.rsindex f6360412b7..48c06cc78d 100644--- a/library/ml/src/main/rust/src/weights_part6.rs+++ b/library/ml/src/main/rust/src/weights_part6.rs@@ -49,6 +49,19 @@ impl Graph { })?; Ok(found.word_offset()) };+ // The raw conv entry points below resolve offsets without asking the+ // builder's weight source, so nothing would mark the consumed file+ // tensors as read and `finish`'s every-tensor rule would refuse every+ // lowered section at its first kernel. Lowering is the authority on+ // what the pass reads — its nodes — so name them as they are replayed.+ // (`host_tensor` only shape-checks and marks; the emitter's host list+ // is derived as read-minus-node-consumed, so device weights named+ // here are never misreported as host tensors.)+ let mark_read = |builder: &mut crate::nets::Builder, index: u32| {+ if let Ok(found) = table.tensor(index as usize) {+ builder.host_tensor(index as usize, &found.dims[..found.rank as usize]);+ }+ }; let act_of = |code: u32| -> Result<Act, String> { match code { 0 => Ok(Act::None),@@ -106,6 +119,11 @@ impl Graph { *group, *pad_edge, );+ mark_read(builder, *weight);+ mark_read(builder, *bias);+ if *act_weight != NO_TENSOR {+ mark_read(builder, *act_weight);+ } ids[*out as usize] = Some(produced); } GraphNode::ConvInt8 {@@ -140,8 +158,16 @@ impl Graph { *dilation, *pads, *group,- if *quant == 0 { crate::nets::Quant::I8 } else { crate::nets::Quant::I4 },+ match *quant {+ 0 => crate::nets::Quant::I8,+ 1 => crate::nets::Quant::I4,+ 2 => crate::nets::Quant::Q2K,+ other => return Err(format!("quant code {other}")),+ }, );+ mark_read(builder, *weight);+ mark_read(builder, *scale);+ mark_read(builder, *bias); ids[*out as usize] = Some(produced); } GraphNode::Add { a, b, out } => {@@ -159,6 +185,8 @@ impl Graph { offset(table, *beta)?, f32::from_bits(*epsilon_bits), );+ mark_read(builder, *gamma);+ mark_read(builder, *beta); ids[*out as usize] = Some(produced); } GraphNode::GlobalAvgPool { input, out } => {