Modern Apps git

← Modern-Apps.git

ml: keep one recording per plan; NLLB per-sentence switch 30 -> 10 ms

Commit
4b5b20d87b85f8f6be55b26cddb455b9a31aa370
Author
vayun-mathur <[email protected]>
When
2026-10-26 00:00 UTC (1 month ago)
Parents
4384070

Diff

library/ml/src/main/rust/src/vulkan/parity_part5.rs
diff --git a/library/ml/src/main/rust/src/vulkan/parity_part5.rs b/library/ml/src/main/rust/src/vulkan/parity_part5.rsindex 38ac97c160..1eefd64a60 100644--- a/library/ml/src/main/rust/src/vulkan/parity_part5.rs+++ b/library/ml/src/main/rust/src/vulkan/parity_part5.rs@@ -35,6 +35,49 @@ fn a_rebuilt_net_agrees_with_the_reference_at_each_shape() {     } } +#[test]+#[ignore = "needs a Vulkan device"]+fn switching_back_to_a_plan_swaps_in_its_kept_recording() {+    // Encode/decode alternation: the second visit to each plan must reuse its recording, and a+    // growth must drop every kept one, since they name the arena it frees.+    let gamma = spread(8, 1.1);+    let beta = spread(8, 2.3);+    let given = Given::new(&[(vec![8], gamma), (vec![8], beta)])+        .expect("the fixture tensors are consistent");+    let data = given.data().to_vec();+    let at = |width: u32| {+        build(&given, &[Shape::new(8, 1, width)], |b, ids| {+            let normed = b.layer_norm(ids[0], 0, 1e-5);+            b.add(normed, ids[0])+        })+    };+    let check = |net: &mut Net, width: u32| {+        let plan = at(width);+        let input = spread(8 * width as usize, width as f32);+        let host = run_multi(&plan, &data, &[&input]).expect("the interpreter runs the plan");+        let got = net.infer_raw(&input).expect("the net submits and reads back");+        matches(&format!("a net switched to {width}"), &host, &got);+    };++    let weights = Weights::from_data(data.clone());+    let mut net = Net::new(device(), at(3), &weights, RESCALE_ONLY).expect("it records");+    check(&mut net, 3);+    for width in [9u32, 3, 9, 3, 9] {+        net.rebuild(at(width)).expect("the net switches");+        check(&mut net, width);+    }+    assert!(net.keeps_recording(&at(3)), "the narrow plan's recording was dropped");++    net.rebuild(at(17)).expect("the net grows");+    check(&mut net, 17);+    assert!(!net.keeps_recording(&at(3)), "a recording survived the arena it names");+    assert!(!net.keeps_recording(&at(9)), "a recording survived the arena it names");+    for width in [3u32, 17, 3] {+        net.rebuild(at(width)).expect("the net switches after growing");+        check(&mut net, width);+    }+}+ /// The shipped Supertonic text encoder and vocoder, on the device against the interpreter. /// /// The fixtures above are single ops over a handful of channels, and `onnx_parity.py` compares the
library/ml/src/main/rust/src/vulkan/run.rs
diff --git a/library/ml/src/main/rust/src/vulkan/run.rs b/library/ml/src/main/rust/src/vulkan/run.rsindex 03ea0f7f38..30ebcb6e60 100644--- a/library/ml/src/main/rust/src/vulkan/run.rs+++ b/library/ml/src/main/rust/src/vulkan/run.rs@@ -288,6 +288,20 @@ pub struct Net {     /// time, and nothing else can reach this pool.     command_pool: vk::CommandPool,     command_buffer: vk::CommandBuffer,+    /// Other plans' recordings, oldest first, each still valid against the current arena,+    /// staging buffer and descriptor sets. A rebuild into one of these plans swaps buffers+    /// instead of re-recording.+    ///+    /// Recording is not cheap on Mali: the first time a plan with shared-memory dispatches is+    /// recorded into a command buffer the driver spends ~1.7 ms per such dispatch (NLLB's first+    /// DecodeStep record took 166 ms, every later switch 10-40 ms).+    recorded: Vec<(Plan, vk::CommandBuffer)>,+    /// Command buffers holding no valid recording, kept because a buffer the driver has already+    /// grown records faster than a fresh one.+    spare: Vec<vk::CommandBuffer>,+    /// Whether any plan installed so far pinned tensors, so the arena holds state a growth+    /// must carry over.+    pinned_seen: bool,     fence: vk::Fence,     /// Timestamp query pool for per-op timings, or null when disabled.     ///