ml: build the next supertonic sampler plan while the text encoder runs
- Commit
23773990c37721d6f584c1e1903293b2134177aa- Author
- vayun-mathur <[email protected]>
- When
- 2026-10-26 01:33 UTC (1 month ago)
- Parents
d1305f5
Diff
library/ml/src/main/rust/examples/profile_supertonic.rs
diff --git a/library/ml/src/main/rust/examples/profile_supertonic.rs b/library/ml/src/main/rust/examples/profile_supertonic.rsindex a2f1449306..0508ba56f0 100644--- a/library/ml/src/main/rust/examples/profile_supertonic.rs+++ b/library/ml/src/main/rust/examples/profile_supertonic.rs@@ -175,6 +175,27 @@ impl supertonic::Stages for Timed { } out }+ fn text_then(+ &mut self,+ lanes: &[f32],+ style: &[f32],+ sampler_shape: (u32, u32),+ ) -> Result<Vec<f32>, String> {+ if self.sampler.holds(sampler_shape) {+ return self.text(lanes, style);+ }+ // As bridge_part1.rs: the sampler plan builds on another thread while the text encoder runs.+ let (plan, offsets) = self.sampler.planner();+ let (text, built) = std::thread::scope(|scope| {+ let building = scope.spawn(move || plan(&offsets, sampler_shape));+ let text = self.text(lanes, style);+ (text, building.join())+ });+ if let Ok(Ok(built)) = built {+ self.sampler.adopt(sampler_shape, built);+ }+ text+ } fn sampler( &mut self,
library/ml/src/main/rust/src/bridge_part1.rs
diff --git a/library/ml/src/main/rust/src/bridge_part1.rs b/library/ml/src/main/rust/src/bridge_part1.rsindex b2ccb35eea..5b690c621f 100644--- a/library/ml/src/main/rust/src/bridge_part1.rs+++ b/library/ml/src/main/rust/src/bridge_part1.rs@@ -304,6 +304,29 @@ impl supertonic::Stages for SupertonicNets<'_> { out } + fn text_then(+ &mut self,+ lanes: &[f32],+ style: &[f32],+ sampler_shape: (u32, u32),+ ) -> Result<Vec<f32>, String> {+ if self.sampler.holds(sampler_shape) {+ return self.text(lanes, style);+ }+ // The sampler's plan for a new sentence is tens of ms of host work; build it on another+ // thread while the text encoder runs on the GPU. Same pure function, so same plan.+ let (plan, offsets) = self.sampler.planner();+ let (text, built) = std::thread::scope(|scope| {+ let building = scope.spawn(move || plan(&offsets, sampler_shape));+ let text = self.text(lanes, style);+ (text, building.join())+ });+ if let Ok(Ok(built)) = built {+ self.sampler.adopt(sampler_shape, built);+ }+ text+ }+ fn sampler( &mut self, latent: &[f32],
library/ml/src/main/rust/src/post/supertonic.rs
diff --git a/library/ml/src/main/rust/src/post/supertonic.rs b/library/ml/src/main/rust/src/post/supertonic.rsindex e5b02257bd..43bcaaa4d5 100644--- a/library/ml/src/main/rust/src/post/supertonic.rs+++ b/library/ml/src/main/rust/src/post/supertonic.rs@@ -352,6 +352,18 @@ pub trait Stages { /// The text encoder, returning `[256, chars]`. fn text(&mut self, lanes: &[f32], style: &[f32]) -> Result<Vec<f32>, String>; + /// [`Stages::text`], told the `(frames, chars)` the sampler will run at next, so a stage can+ /// prepare the sampler's plan while the text encoder runs. Provided: plain [`Stages::text`].+ fn text_then(+ &mut self,+ lanes: &[f32],+ style: &[f32],+ sampler_shape: (u32, u32),+ ) -> Result<Vec<f32>, String> {+ let _ = sampler_shape;+ self.text(lanes, style)+ }+ /// One guidance branch of the sampler, returning `[144, frames]`. #[allow(clippy::too_many_arguments)] fn sampler(
library/ml/src/main/rust/src/post/supertonic_part1.rs
diff --git a/library/ml/src/main/rust/src/post/supertonic_part1.rs b/library/ml/src/main/rust/src/post/supertonic_part1.rsindex daae5803d9..8fc097933d 100644--- a/library/ml/src/main/rust/src/post/supertonic_part1.rs+++ b/library/ml/src/main/rust/src/post/supertonic_part1.rs@@ -60,7 +60,7 @@ pub fn synthesise( let log_seconds = stages.duration(&embed_lanes(&with_token), &voice.duration)?; let frames = duration_net::latent_frames(duration_net::seconds(log_seconds) / SPEED); - let conditioning_text = stages.text(&embed_lanes(&ids), &voice.text)?;+ let conditioning_text = stages.text_then(&embed_lanes(&ids), &voice.text, (frames, chars))?; let query_angles = rotary_angles(&conditioning.theta, frames)?; let key_angles = rotary_angles(&conditioning.theta, chars)?;
library/ml/src/main/rust/src/vulkan/reshape.rs
diff --git a/library/ml/src/main/rust/src/vulkan/reshape.rs b/library/ml/src/main/rust/src/vulkan/reshape.rsindex 95efa97af9..12566931ef 100644--- a/library/ml/src/main/rust/src/vulkan/reshape.rs+++ b/library/ml/src/main/rust/src/vulkan/reshape.rs@@ -146,4 +146,27 @@ impl<S: Copy + PartialEq> Reshaped<S> { } Ok(&mut self.net) }++ /// What [`Reshaped::at`] would call to build a plan it does not hold, so a caller can build+ /// one off the thread that owns the net. See [`Reshaped::adopt`].+ pub fn planner(&self) -> (fn(&Offsets, S) -> Result<Plan, String>, Offsets) {+ (self.plan, self.offsets.clone())+ }++ /// Whether [`Reshaped::at`] would re-record nothing or reuse a plan already built for `shape`.+ pub fn holds(&self, shape: S) -> bool {+ shape == self.at || self.built.iter().any(|(key, _)| *key == shape)+ }++ /// Hand over a plan built elsewhere from [`Reshaped::planner`] at `shape`, so the next+ /// [`Reshaped::at`] there only re-records. The plan must be exactly what the planner returns.+ pub fn adopt(&mut self, shape: S, plan: Plan) {+ if self.holds(shape) {+ return;+ }+ if self.built.len() == BUILT_PLANS {+ self.built.remove(0);+ }+ self.built.push((shape, plan));+ } }