Modern Apps git

← Modern-Apps.git

ml: supertonic vocoder at even frames (exact, causal), vocoder -14%

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

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 2619b2d484..a2f1449306 100644--- a/library/ml/src/main/rust/examples/profile_supertonic.rs+++ b/library/ml/src/main/rust/examples/profile_supertonic.rs@@ -253,12 +253,12 @@ impl supertonic::Stages for Timed {      fn vocoder(&mut self, latent: &[f32], frames: u32) -> Result<Vec<f32>, String> {         let reshaping = Instant::now();-        let net = self.vocoder.at(frames)?;+        let net = self.vocoder.at(supertonic_vocoder::padded_frames(frames))?;         let each = ms(reshaping);         self.reshape_ms += each;         self.reshape_vocoder_ms += each;         let unpacking = Instant::now();-        let unpacked = supertonic_vocoder::unpack_latent(latent, frames as usize)?;+        let unpacked = supertonic_vocoder::unpack_latent_padded(latent, frames as usize)?;         self.unpack_ms += ms(unpacking);         let running = Instant::now();         let channelled = one_output(net.infer_raw(&unpacked)?)?;@@ -267,7 +267,7 @@ impl supertonic::Stages for Timed {             self.vocoder_times = times;         }         let interleaving = Instant::now();-        let out = supertonic_vocoder::interleave(&channelled);+        let out = supertonic_vocoder::interleave_padded(&channelled, frames as usize)?;         self.interleave_ms += ms(interleaving);         Ok(out)     }
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 c5363e1de9..b2ccb35eea 100644--- a/library/ml/src/main/rust/src/bridge_part1.rs+++ b/library/ml/src/main/rust/src/bridge_part1.rs@@ -403,7 +403,7 @@ impl supertonic::Stages for SupertonicNets<'_> {      fn vocoder(&mut self, latent: &[f32], frames: u32) -> Result<Vec<f32>, String> {         let reshaping = std::time::Instant::now();-        let net = self.vocoder.at(frames)?;+        let net = self.vocoder.at(supertonic_vocoder::padded_frames(frames))?;         self.timing.reshape += reshaping.elapsed().as_secs_f64() * 1000.0;         let running = std::time::Instant::now();         // Two marshalling steps, both the host's job — `nets::supertonic_vocoder::build`'s own doc@@ -416,9 +416,9 @@ impl supertonic::Stages for SupertonicNets<'_> {         //    to AudioTrack unchanged plays each channel plane as if it were consecutive samples, so         //    every 512th value is a real neighbour and the rest is a periodic artefact — a loud tone         //    at the frame rate rather than speech.-        let unpacked = supertonic_vocoder::unpack_latent(latent, frames as usize)?;+        let unpacked = supertonic_vocoder::unpack_latent_padded(latent, frames as usize)?;         let channelled = one_output(net.infer_raw(&unpacked)?)?;-        let out = supertonic_vocoder::interleave(&channelled);+        let out = supertonic_vocoder::interleave_padded(&channelled, frames as usize)?;         self.timing.vocoder += running.elapsed().as_secs_f64() * 1000.0;         Ok(out)     }
library/ml/src/main/rust/src/nets/supertonic_vocoder.rs
diff --git a/library/ml/src/main/rust/src/nets/supertonic_vocoder.rs b/library/ml/src/main/rust/src/nets/supertonic_vocoder.rsindex da32491fc8..8b42ec7df5 100644--- a/library/ml/src/main/rust/src/nets/supertonic_vocoder.rs+++ b/library/ml/src/main/rust/src/nets/supertonic_vocoder.rs@@ -143,7 +143,7 @@ impl Layers {     } } -pub use super::supertonic_vocoder_extra::unpack_latent;+pub use super::supertonic_vocoder_extra::{interleave_padded, padded_frames, unpack_latent, unpack_latent_padded};  /// Read the plan's `[512, 1, T]` output as a waveform: `sample[t * 512 + s] = out[s][t]`. pub fn interleave(channelled: &[f32]) -> Vec<f32> {
library/ml/src/main/rust/src/nets/supertonic_vocoder_extra.rs
diff --git a/library/ml/src/main/rust/src/nets/supertonic_vocoder_extra.rs b/library/ml/src/main/rust/src/nets/supertonic_vocoder_extra.rsindex 6af8e66c9b..b9d7701bc5 100644--- a/library/ml/src/main/rust/src/nets/supertonic_vocoder_extra.rs+++ b/library/ml/src/main/rust/src/nets/supertonic_vocoder_extra.rs@@ -1,4 +1,4 @@-use super::supertonic_vocoder::{COMPRESS, LATENT, PACKED};+use super::supertonic_vocoder::{COMPRESS, LATENT, PACKED, SAMPLES_PER_POSITION};  /// Unpack a `[144, frames]` latent into the `[24, 6 * frames]` the plan reads. ///@@ -25,3 +25,75 @@ pub fn unpack_latent(latent: &[f32], frames: usize) -> Result<Vec<f32>, String>     }     Ok(out) }++/// Frames the vocoder plan runs at: `frames` rounded up to even, so its `6 * frames` positions+/// are a multiple of four and every int8 GEMM takes `conv_point_int8.comp`'s aligned path.+///+/// Exact: every convolution here is causal (all padding on the left, see `along`) and the layer+/// norms are over channels, so no real position ever reads the padded tail.+pub fn padded_frames(frames: u32) -> u32 {+    frames.next_multiple_of(2)+}++/// [`unpack_latent`], widened to [`padded_frames`] with a zero tail on every channel.+pub fn unpack_latent_padded(latent: &[f32], frames: usize) -> Result<Vec<f32>, String> {+    let unpacked = unpack_latent(latent, frames)?;+    let compress = COMPRESS as usize;+    let real = frames * compress;+    let padded = padded_frames(frames as u32) as usize * compress;+    let mut out = vec![0.0f32; LATENT as usize * padded];+    for (row, chunk) in unpacked.chunks_exact(real).enumerate() {+        out[row * padded..row * padded + real].copy_from_slice(chunk);+    }+    Ok(out)+}++/// [`super::supertonic_vocoder::interleave`] of a [`padded_frames`] plan's `[512, 1, 6 * padded]`+/// output, dropping the tail: `sample[t * 512 + s] = out[s][t]` for the first `6 * frames` `t`.+pub fn interleave_padded(channelled: &[f32], frames: usize) -> Result<Vec<f32>, String> {+    let compress = COMPRESS as usize;+    let channels = SAMPLES_PER_POSITION as usize;+    let real = frames * compress;+    let padded = padded_frames(frames as u32) as usize * compress;+    if real == 0 || channelled.len() != channels * padded {+        return Err(format!("{} values are not {channels} rows of {padded} positions", channelled.len()));+    }+    let mut out = vec![0.0f32; channels * real];+    for (channel, row) in channelled.chunks_exact(padded).enumerate() {+        for (position, &value) in row[..real].iter().enumerate() {+            out[position * channels + channel] = value;+        }+    }+    Ok(out)+}+#[cfg(test)]+mod padded_tests {+    use super::*;++    #[test]+    fn frames_round_up_to_even() {+        assert_eq!(padded_frames(49), 50);+        assert_eq!(padded_frames(50), 50);+        assert_eq!((padded_frames(49) * COMPRESS) % 4, 0);+    }++    #[test]+    fn the_padded_latent_is_the_unpacked_one_plus_a_zero_tail() {+        let frames = 3;+        let latent: Vec<f32> = (0..PACKED as usize * frames).map(|v| v as f32 + 1.0).collect();+        let plain = unpack_latent(&latent, frames).expect("unpacks");+        let wide = unpack_latent_padded(&latent, frames).expect("unpacks padded");+        let (real, padded) = (frames * COMPRESS as usize, 4 * COMPRESS as usize);+        assert_eq!(wide.len(), LATENT as usize * padded);+        for row in 0..LATENT as usize {+            assert_eq!(&wide[row * padded..row * padded + real], &plain[row * real..(row + 1) * real]);+            assert!(wide[row * padded + real..(row + 1) * padded].iter().all(|&v| v == 0.0));+        }+        let out: Vec<f32> = (0..SAMPLES_PER_POSITION as usize * padded).map(|v| v as f32).collect();+        let real_rows: Vec<f32> = out.chunks_exact(padded).flat_map(|row| &row[..real]).copied().collect();+        assert_eq!(+            interleave_padded(&out, frames).expect("interleaves"),+            super::super::supertonic_vocoder::interleave(&real_rows)+        );+    }+}\ No newline at end of file