Modern Apps git

← Modern-Apps.git

library:ml: opt-in timing and a selectable barrier formulation

Commit
b83d0b8c83f9894faa7d8a8d04f15a2cd00b7c76
Author
vayun-mathur <[email protected]>
When
2026-09-12 03:04 UTC (2 months ago)
Parents
3e52b31

Diff

library/ml/src/main/rust/src/bridge.rs
diff --git a/library/ml/src/main/rust/src/bridge.rs b/library/ml/src/main/rust/src/bridge.rsindex 6d8f522319..19c78103fb 100644--- a/library/ml/src/main/rust/src/bridge.rs+++ b/library/ml/src/main/rust/src/bridge.rs@@ -631,7 +631,13 @@ struct SupertonicNets<'a> {     timing: Timing, } -/// TEMPORARY instrumentation: where an utterance's time goes.+/// Where an utterance's time goes, filled in as the stages run.+///+/// Reported through [`crate::timing`], so it costs a branch per stage unless+/// `MODELRUNNER_TIMING` or `debug.modelrunner.timing` is set. It is kept rather than deleted+/// because the stage split is the whole diagnosis — `analysis/maml_vs_litert.md` turns on the+/// sampler being 92% of an utterance and the barriers being 61% of the sampler, neither of which+/// is visible from a total. #[derive(Default)] struct Timing {     reshape: f64,@@ -892,7 +898,7 @@ fn build_supertonic<'l>(      let device_at = std::time::Instant::now();     let shared = context::shared()?;-    log(&format!("TIMING device {:.0} ms", device_at.elapsed().as_secs_f64() * 1000.0));+    timing!("device {:.0} ms", device_at.elapsed().as_secs_f64() * 1000.0);     let built = std::time::Instant::now();     let duration_net = Reshaped::streamed(         shared.clone(),@@ -901,11 +907,11 @@ fn build_supertonic<'l>(         SMALLEST,         duration_plan,     )?;-    log(&format!("TIMING dp net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0));+    timing!("dp net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0);     let built = std::time::Instant::now();     let text_net =         Reshaped::streamed(shared.clone(), text_weights.offsets(), &text_weights, SMALLEST, text_plan)?;-    log(&format!("TIMING ttl net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0));+    timing!("ttl net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0);     let built = std::time::Instant::now();     let sampler_net = Reshaped::streamed(         shared.clone(),@@ -914,11 +920,11 @@ fn build_supertonic<'l>(         (SMALLEST, SMALLEST),         sampler_plan,     )?;-    log(&format!("TIMING ve net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0));+    timing!("ve net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0);     let built = std::time::Instant::now();     let vocoder_net =         Reshaped::streamed(shared, vocoder_weights.offsets(), &vocoder_weights, SMALLEST, vocoder_plan)?;-    log(&format!("TIMING voc net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0));+    timing!("voc net {:.0} ms", built.elapsed().as_secs_f64() * 1000.0);     let handle = SupertonicHandle {         duration: duration_net,         text: text_net,@@ -1082,8 +1088,8 @@ fn speak_supertonic(             noise.borrow_mut().normal(count)         });     let t = &nets.timing;-    log(&format!(-        "TIMING utterance {:.0} ms: reshape {:.0}, duration {:.0}, text {:.0}, sampler {:.0} over {} calls, vocoder {:.0}",+    timing!(+        "utterance {:.0} ms: reshape {:.0}, duration {:.0}, text {:.0}, sampler {:.0} over {} calls, vocoder {:.0}",         whole.elapsed().as_secs_f64() * 1000.0,         t.reshape,         t.duration,@@ -1091,7 +1097,7 @@ fn speak_supertonic(         t.sampler,         t.sampler_calls,         t.vocoder,-    ));+    );     out } 
library/ml/src/main/rust/src/knobs.rs
diff --git a/library/ml/src/main/rust/src/knobs.rs b/library/ml/src/main/rust/src/knobs.rsnew file mode 100644index 0000000000..61fa78ad7e--- /dev/null+++ b/library/ml/src/main/rust/src/knobs.rs@@ -0,0 +1,61 @@+//! Debug settings, read from the environment or from a system property.+//!+//! Two sources because there are two ways this code runs and neither covers the other:+//!+//! * `MODELRUNNER_<NAME>` in the environment. The measurement loop is the cross-compiled test+//!   binary, and `adb shell MODELRUNNER_TIMING=1 ./mr_test ...` is how it is set. Also the only+//!   one that works on a host build.+//! * `debug.modelrunner.<name>`, on Android. An app is not launched from a shell and has no+//!   environment to set, so an env var alone would leave the shipped path unmeasurable.+//!   `adb shell setprop debug.modelrunner.barrier global` reaches it.+//!+//! Every one of these is off by default and none changes what a release build computes. They+//! exist because this runtime's cost is concentrated somewhere a profile of the arithmetic does+//! not show — see `analysis/maml_vs_litert.md` — so the alternatives have to be switchable at run+//! time to be compared honestly. Rebuilding between variants is how the first attempt at this+//! measured a shader change and called it a barrier change.+//!+//! Callers cache their own answer. Nothing here does, because the lookups happen once per process+//! at a natural point and a shared cache would need a lock for no benefit.++/// The value of a debug setting, from either source, or `None` if it is not set.+pub fn get(name: &str) -> Option<String> {+    let env = format!("MODELRUNNER_{}", name.to_uppercase());+    if let Some(value) = std::env::var_os(env).and_then(|v| v.into_string().ok()) {+        return Some(value);+    }+    property(&format!("debug.modelrunner.{name}"))+}++/// Whether a setting is present at all, whatever its value.+pub fn is_set(name: &str) -> bool {+    get(name).is_some()+}++/// Read an Android system property.+///+/// `__system_property_get` writes at most `PROP_VALUE_MAX` bytes including the terminator and+/// returns the length without it. It is in libc, which is linked unconditionally.+#[cfg(target_os = "android")]+fn property(name: &str) -> Option<String> {+    extern "C" {+        fn __system_property_get(name: *const u8, value: *mut u8) -> i32;+    }+    /// `PROP_VALUE_MAX` from `sys/system_properties.h`.+    const VALUE_MAX: usize = 92;+    let mut key: Vec<u8> = name.as_bytes().to_vec();+    key.push(0);+    let mut value = [0u8; VALUE_MAX];+    // SAFETY: the name is NUL-terminated and the buffer is `PROP_VALUE_MAX`, which is the most+    // the call may write.+    let len = unsafe { __system_property_get(key.as_ptr(), value.as_mut_ptr()) };+    if len <= 0 {+        return None;+    }+    String::from_utf8(value[..len as usize].to_vec()).ok()+}++#[cfg(not(target_os = "android"))]+fn property(_name: &str) -> Option<String> {+    None+}
library/ml/src/main/rust/src/lib.rs
diff --git a/library/ml/src/main/rust/src/lib.rs b/library/ml/src/main/rust/src/lib.rsindex 687e5a6894..a336eb01c3 100644--- a/library/ml/src/main/rust/src/lib.rs+++ b/library/ml/src/main/rust/src/lib.rs@@ -45,11 +45,18 @@ //! needs a `dlltool` on `PATH` to build; the NDK's `llvm-dlltool` works under that name.  pub mod gate;+pub mod knobs; pub mod logmel; pub mod microfrontend; pub mod nets; pub mod post; pub mod preprocess;+// `#[macro_use]` so `timing!` is in scope for the modules declared after this one, which is+// every module that measures anything. `#[macro_export]` alone puts it at the crate root for+// other crates - the `examples/` are separate crates - but does not bring the bare name into+// scope here.+#[macro_use]+pub mod timing; pub mod weights;  pub mod vulkan;
library/ml/src/main/rust/src/timing.rs
diff --git a/library/ml/src/main/rust/src/timing.rs b/library/ml/src/main/rust/src/timing.rsnew file mode 100644index 0000000000..ce46195646--- /dev/null+++ b/library/ml/src/main/rust/src/timing.rs@@ -0,0 +1,65 @@+//! Opt-in timing, silent unless it is asked for.+//!+//! This runtime's cost is dominated by things that do not show up in a profile of the arithmetic+//! — see `analysis/maml_vs_litert.md`, where 74% of an utterance turned out to be the pipeline+//! barrier between one op and the next — so the numbers have to come from the runtime itself.+//! They are also useless in aggregate: "inference took 5.5 seconds" says nothing, while "the+//! sampler ran 32 times at 158 ms of which 96 ms was barriers" says everything.+//!+//! Switched on by `MODELRUNNER_TIMING` or `debug.modelrunner.timing`; see [`crate::knobs`] for+//! why there are two.+//!+//! Read once and cached. The answer cannot change usefully within a process, and this is called+//! from inside the per-op recording loop where a lookup per call would be part of what it is+//! trying to measure.++use std::sync::OnceLock;++/// Whether timing was asked for. Cached; see the module header.+pub fn enabled() -> bool {+    static ON: OnceLock<bool> = OnceLock::new();+    *ON.get_or_init(|| crate::knobs::is_set("timing"))+}++/// Write one line wherever this platform's logs go.+///+/// Deliberately not behind [`enabled`]: the [`timing!`] macro checks that before formatting, so a+/// disabled build pays a load and a branch rather than an allocation.+pub fn emit(message: &str) {+    write(message);+}++/// One timed line, formatted only if timing is on.+///+/// The guard is outside the `format!` on purpose. These sites are inside the per-op loop, and+/// formatting a string that is then thrown away would cost more than the op being measured.+#[macro_export]+macro_rules! timing {+    ($($arg:tt)*) => {+        if $crate::timing::enabled() {+            $crate::timing::emit(&format!($($arg)*));+        }+    };+}++#[cfg(target_os = "android")]+fn write(message: &str) {+    #[link(name = "log")]+    extern "C" {+        fn __android_log_write(priority: i32, tag: *const u8, text: *const u8) -> i32;+    }+    /// `ANDROID_LOG_INFO`. Not `ERROR`, which is what the first cut of this used and which put a+    /// per-utterance red line in every log this app touches.+    const INFO: i32 = 4;+    let mut text: Vec<u8> = message.as_bytes().to_vec();+    text.push(0);+    // SAFETY: both pointers are to NUL-terminated buffers that outlive the call.+    unsafe {+        let _ = __android_log_write(INFO, b"ModelRunner\0".as_ptr(), text.as_ptr());+    }+}++#[cfg(not(target_os = "android"))]+fn write(message: &str) {+    eprintln!("{message}");+}
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 ca6adb0186..02a2b8dc22 100644--- a/library/ml/src/main/rust/src/vulkan/run.rs+++ b/library/ml/src/main/rust/src/vulkan/run.rs@@ -28,7 +28,7 @@ //! design. About 350 barriers per inference is the cost of that simplicity, and it is the //! first thing to look at if U^2-Netp is slower than it should be. -use std::sync::Arc;+use std::sync::{Arc, OnceLock};  use ash::vk; @@ -41,23 +41,154 @@ use super::context::Context; use super::pipeline::{Pipelines, MAX_WORKGROUPS_PER_DIM, WORKGROUP}; use super::segment::Segments; -/// TEMPORARY instrumentation.-#[cfg(target_os = "android")]-fn tlog(message: &str) {-    #[link(name = "log")]-    extern "C" {-        fn __android_log_write(priority: i32, tag: *const u8, text: *const u8) -> i32;+use crate::timing;++/// How [`Net::barrier_over`] spells the dependency between two ops.+///+/// Selected by `MODELRUNNER_BARRIER` or `debug.modelrunner.barrier`, defaulting to+/// [`Formulation::Range`], which is what this runtime has always done.+///+/// # Only four of these are correct+///+/// [`Formulation::Range`], [`Formulation::Whole`], [`Formulation::Global`] and+/// [`Formulation::AllCommands`] all compute right answers and differ only in how much the driver+/// is told. [`Formulation::Narrow`], [`Formulation::GlobalNarrow`] and [`Formulation::None`] do+/// not: [`Formulation::selected`] will not return them outside a `debug_assertions` build.+///+/// An earlier version of this comment claimed every variant but [`Formulation::None`] was correct.+/// A sweep of all seven showed otherwise — see `analysis/maml_vs_litert.md` section 6.+///+/// # Why it is a run-time knob+///+/// Nothing in the Vulkan spec says which spelling of the same dependency a given driver makes+/// cheap, so it has to be measured — and measured *within one process*, because comparing two+/// builds is how an earlier attempt attributed a shader regression to the barrier.+///+/// The sweep it was built for found nothing to choose between them: 5,073 / 5,091 / 5,095 /+/// 5,096 ms across the four correct variants on a Tensor G4, a spread of 0.45% against a noise+/// floor of 2%. Cost is also not proportional to the byte range — [`Formulation::Global`] names no+/// range at all and [`Formulation::Whole`] names the entire arena, and they land 4 ms apart. The+/// knob is kept so the sweep can be re-run on a new device, not because a winner is expected.+#[derive(Clone, Copy, Debug, PartialEq, Eq)]+pub enum Formulation {+    /// A buffer barrier over the op's own output range. The default and the historical behaviour.+    Range,+    /// A buffer barrier over the whole arena. What this used to do before `barrier_over` narrowed+    /// it; worth re-measuring now that the arena is 540 KB for Supertonic rather than Gemma's+    /// 303 MB.+    Whole,+    /// A global memory barrier: no buffer, no range for the driver to walk.+    Global,+    /// **Incorrect on this runtime**, and debug-only. [`Formulation::Global`] with only the+    /// compute-to-compute dependency named. See [`Formulation::Narrow`] for why naming less here+    /// is wrong rather than merely leaner.+    GlobalNarrow,+    /// **Incorrect on this runtime**, and debug-only. A buffer barrier over the op's range naming+    /// only `SHADER_WRITE` to `SHADER_READ` — the compute-to-compute dependency and nothing else.+    ///+    /// That is not enough, because **a copy is also an op here**. The input upload and the output+    /// readback are `TRANSFER` work recorded into the same command buffer as the dispatches, so+    /// dropping the transfer stage and its two accesses from the mask leaves them unordered+    /// against the dispatches either side. The dependency this omits is a real one, not a+    /// redundant one the spec lets you elide.+    ///+    /// It surfaced as a short utterance — 43,008 frames delivered instead of 150,528, because the+    /// duration model reads an unsynchronised upload and asks for 14 latent frames instead of 49+    /// — but that is the symptom. The missing transfer edge is the fault, and it would show up+    /// somewhere else entirely on a net that uploads different things.+    Narrow,+    /// `ALL_COMMANDS` on both sides. The bluntest correct answer, as a control.+    AllCommands,+    /// **No barrier at all. Produces wrong results.** Debug-only.+    ///+    /// Here so the ceiling can be measured in the same process as the variants being compared+    /// against it, which is the only way to know the ceiling has not moved under them. Never+    /// select this outside a measurement.+    ///+    /// It is not currently a *valid* ceiling: like [`Formulation::Narrow`] it corrupts the+    /// duration prediction, so it computes 14 latent frames where a correct run computes 49 and+    /// its 1,160 ms is 3.5x less work rather than the same work without barriers. Any figure for+    /// what the barrier costs needs this path pinned to the known-good frame count first.+    None,+}++impl Formulation {+    /// The selected variant, read once.+    ///+    /// Cached because this is called once per op inside `record`, where a property lookup per+    /// call would be a meaningful share of what is being measured.+    pub fn selected() -> Formulation {+        static CHOICE: OnceLock<Formulation> = OnceLock::new();+        *CHOICE.get_or_init(|| {+            let name = crate::knobs::get("barrier").unwrap_or_default();+            let chosen = match name.trim() {+                "" | "range" => Formulation::Range,+                "whole" => Formulation::Whole,+                "global" => Formulation::Global,+                "all-commands" => Formulation::AllCommands,+                // The three that compute wrong answers are measurement tools, and a shipped+                // build has no business selecting one. Gated at the point of selection rather+                // than on the variants so the enum has one shape in both profiles and callers+                // never need a `cfg` to match on it.+                #[cfg(debug_assertions)]+                "global-narrow" => Formulation::GlobalNarrow,+                #[cfg(debug_assertions)]+                "narrow" => Formulation::Narrow,+                #[cfg(debug_assertions)]+                "none" => Formulation::None,+                #[cfg(not(debug_assertions))]+                wrong @ ("global-narrow" | "narrow" | "none") => {+                    timing!("barrier formulation {wrong:?} computes wrong output and is \+                             debug-only, using range");+                    Formulation::Range+                }+                other => {+                    timing!("unknown barrier formulation {other:?}, using range");+                    Formulation::Range+                }+            };+            if chosen != Formulation::Range {+                timing!("barrier formulation {chosen:?}");+            }+            chosen+        })     }-    let tag = b"ModelRunner\0";-    let mut text: Vec<u8> = message.as_bytes().to_vec();-    text.push(0);-    unsafe {-        let _ = __android_log_write(6, tag.as_ptr(), text.as_ptr());++    /// The source and destination pipeline stages.+    fn stages(self) -> (vk::PipelineStageFlags, vk::PipelineStageFlags) {+        let compute = vk::PipelineStageFlags::COMPUTE_SHADER;+        let both = compute | vk::PipelineStageFlags::TRANSFER;+        match self {+            Formulation::Narrow | Formulation::GlobalNarrow => (compute, compute),+            Formulation::AllCommands => {+                (vk::PipelineStageFlags::ALL_COMMANDS, vk::PipelineStageFlags::ALL_COMMANDS)+            }+            _ => (both, both),+        }     }-} -#[cfg(not(target_os = "android"))]-fn tlog(_message: &str) {}+    /// The source and destination access masks.+    fn accesses(self) -> (vk::AccessFlags, vk::AccessFlags) {+        match self {+            // Exactly the dependency a dispatch has on the dispatch before it, and nothing else.+            Formulation::Narrow | Formulation::GlobalNarrow => (+                vk::AccessFlags::SHADER_WRITE,+                vk::AccessFlags::SHADER_READ | vk::AccessFlags::SHADER_WRITE,+            ),+            Formulation::AllCommands => {+                (vk::AccessFlags::MEMORY_WRITE, vk::AccessFlags::MEMORY_READ | vk::AccessFlags::MEMORY_WRITE)+            }+            // The historical masks: a copy is also an op here, so transfer is named on both sides.+            _ => (+                vk::AccessFlags::SHADER_WRITE | vk::AccessFlags::TRANSFER_WRITE,+                vk::AccessFlags::SHADER_READ+                    | vk::AccessFlags::SHADER_WRITE+                    | vk::AccessFlags::TRANSFER_READ+                    | vk::AccessFlags::TRANSFER_WRITE,+            ),+        }+    }+}  /// Values a recorded command buffer reads from memory rather than from its own recording. ///@@ -209,7 +340,7 @@ impl Net {             params.buffer,             segments.all(),         )?;-        tlog(&format!("TIMING pipelines {:.0} ms", staged.elapsed().as_secs_f64() * 1000.0));+        timing!("pipelines {:.0} ms", staged.elapsed().as_secs_f64() * 1000.0);          // `RESET_COMMAND_BUFFER`, so a net can re-record without reallocating.         let pool_info = vk::CommandPoolCreateInfo::default()@@ -241,11 +372,11 @@ impl Net {          let staged = std::time::Instant::now();         net.upload_weights(weights)?;-        tlog(&format!(-            "TIMING upload {} MB in {:.0} ms",+        timing!(+            "upload {} MB in {:.0} ms",             weights_bytes / (1024 * 1024),             staged.elapsed().as_secs_f64() * 1000.0-        ));+        );         net.command_buffer = net.allocate_command_buffer()?;         net.fence = net.create_fence()?;         // Written before the first record so a shader reading it never sees uninitialised@@ -253,12 +384,12 @@ impl Net {         net.set_params(StepParams::default())?;         let staged = std::time::Instant::now();         net.record()?;-        tlog(&format!(-            "TIMING record {:.0} ms for {} ops, arena {} KB",+        timing!(+            "record {:.0} ms for {} ops, arena {} KB",             staged.elapsed().as_secs_f64() * 1000.0,             net.plan.ops.len(),             arena_bytes / 1024-        ));+        );         Ok(net)     } @@ -359,8 +490,8 @@ impl Net {             self.poisoned = true;             return Err(e);         }-        tlog(&format!(-            "TIMING rebuild-record {:.0} ms for {} ops, arena {} KB, dispatches {}, invocations {}",+        timing!(+            "rebuild-record {:.0} ms for {} ops, arena {} KB, dispatches {}, invocations {}",             staged.elapsed().as_secs_f64() * 1000.0,             self.plan.ops.len(),             self.arena.size / 1024,@@ -373,7 +504,7 @@ impl Net {                     _ => 0,                 })                 .sum::<u64>()-        ));+        );         Ok(())     } @@ -623,13 +754,10 @@ impl Net {                 // A zero-length range is not a barrier at all, and a shape this could not read                 // is a plan bug rather than something to guess around - so fall back to the                 // whole arena, which is always correct if slower.-                // A dispatch only needs a compute-to-compute dependency; a copy is the only op-                // here that writes through the transfer stage.-                let transfer = matches!(*op, Op::Copy { .. });                 if size == 0 || offset + size > self.arena.size {                     self.barrier(buffer);                 } else {-                    self.barrier_masked(buffer, offset, size, transfer);+                    self.barrier_over(buffer, offset, size);                 }             } @@ -700,48 +828,56 @@ impl Net {     /// An op only ever makes visible what it wrote, so the range is its own output. Anything     /// earlier was already made visible by the barrier that followed *it*.     ///-    /// # Safety+    /// # How it is spelled is a variable, and it is switchable     ///-    /// `buffer` must be inside a `begin`/`end` pair, and the range must be inside the arena.-    unsafe fn barrier_over(&self, buffer: vk::CommandBuffer, offset: u64, size: u64) {-        // SAFETY: as the compute-only form, which this widens to cover a transfer on either side.-        unsafe { self.barrier_masked(buffer, offset, size, true) }-    }--    /// [`Net::barrier_over`], optionally without the transfer stage on either side.+    /// It is tempting to read a dispatch's dependency on the dispatch before it as only+    /// `SHADER_WRITE` to `SHADER_READ`, making the transfer stage and the two transfer accesses+    /// more than the spec requires. That is wrong here, and [`Formulation::Narrow`] is the+    /// measurement proving it: a copy is also an op in this runtime, so the transfer edge is load+    /// bearing and dropping it corrupts the output.+    ///+    /// Whether a *correct* spelling is cheaper than another correct one is a property of the+    /// driver rather than of the spec. On a Tensor G4 the answer turned out to be no — the four+    /// correct variants sit within 0.45% of each other. See [`Formulation`] and+    /// `analysis/maml_vs_litert.md` section 6.     ///-    /// A dispatch's dependency on the dispatch before it is `SHADER_WRITE` to `SHADER_READ`-    /// across `COMPUTE_SHADER`, and naming `TRANSFER` as well makes the driver order the op-    /// against the transfer queue too. Only an [`Op::Copy`] and the input and output copies need-    /// that, and they are a handful of the ops in a plan against one barrier per dispatch.+    /// [`Formulation`] makes the choice a run-time knob rather than a rebuild. That is not+    /// convenience: the first attempt at this measured a rebuild against a rebuild, a shader edit+    /// silently failed to recompile in between, and the shader's 45% was attributed to the+    /// barrier. Switching within one process makes the comparison interleavable and immune to+    /// that. See `analysis/maml_vs_litert.md`.     ///     /// # Safety     ///-    /// As [`Net::barrier_over`].-    unsafe fn barrier_masked(-        &self,-        buffer: vk::CommandBuffer,-        offset: u64,-        size: u64,-        transfer: bool,-    ) {-        let stages = if transfer {-            vk::PipelineStageFlags::COMPUTE_SHADER | vk::PipelineStageFlags::TRANSFER-        } else {-            vk::PipelineStageFlags::COMPUTE_SHADER-        };-        let src_access = if transfer {-            vk::AccessFlags::SHADER_WRITE | vk::AccessFlags::TRANSFER_WRITE-        } else {-            vk::AccessFlags::SHADER_WRITE-        };-        let dst_access = if transfer {-            vk::AccessFlags::SHADER_READ-                | vk::AccessFlags::SHADER_WRITE-                | vk::AccessFlags::TRANSFER_READ-                | vk::AccessFlags::TRANSFER_WRITE+    /// `buffer` must be inside a `begin`/`end` pair, and the range must be inside the arena.+    unsafe fn barrier_over(&self, buffer: vk::CommandBuffer, offset: u64, size: u64) {+        let formulation = Formulation::selected();+        if formulation == Formulation::None {+            return;+        }+        let (src_stage, dst_stage) = formulation.stages();+        let (src_access, dst_access) = formulation.accesses();+        // A global memory barrier names no buffer and no range, so the driver has nothing to+        // walk. If the cost scales with the range, this is where that shows up.+        if formulation == Formulation::Global || formulation == Formulation::GlobalNarrow {+            let barrier = vk::MemoryBarrier::default()+                .src_access_mask(src_access)+                .dst_access_mask(dst_access);+            self.context.device.cmd_pipeline_barrier(+                buffer,+                src_stage,+                dst_stage,+                vk::DependencyFlags::empty(),+                std::slice::from_ref(&barrier),+                &[],+                &[],+            );+            return;+        }+        let (offset, size) = if formulation == Formulation::Whole {+            (0, vk::WHOLE_SIZE)         } else {-            vk::AccessFlags::SHADER_READ | vk::AccessFlags::SHADER_WRITE+            (offset, size)         };         let barrier = vk::BufferMemoryBarrier::default()             .src_access_mask(src_access)@@ -753,8 +889,8 @@ impl Net {             .size(size);         self.context.device.cmd_pipeline_barrier(             buffer,-            vk::PipelineStageFlags::COMPUTE_SHADER | vk::PipelineStageFlags::TRANSFER,-            vk::PipelineStageFlags::COMPUTE_SHADER | vk::PipelineStageFlags::TRANSFER,+            src_stage,+            dst_stage,             vk::DependencyFlags::empty(),             &[],             std::slice::from_ref(&barrier),