Refinements on the Kyutai STT integration after debugging session: - dtype: BF16 on accelerators (matches checkpoint storage), F32 on CPU. Previously F16 on Metal which can overflow in the LM's RmsNorm. - examples/stt_demo: pad input with 0.5s silence suffix per the HF stt_config.audio_delay_seconds, matching the Python reference loop. - src/stt.rs: tightened module docs with debugging notes for the remaining all-pad-output issue. Removed RTX_STT_DEBUG callback path (was useful for one-off debugging; can be re-added with cleaner shape). Status: weights load cleanly, LM forward advances every frame, but predictions are all-pad on real speech. Bisection plan documented in the module rustdoc — next session should diff against the official delayed-streams-modeling Python reference at frame-by-frame granularity. 77 lib tests + 2 stt tests all pass. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
119 lines
4.1 KiB
Rust
119 lines
4.1 KiB
Rust
//! Streaming STT demo: transcribe a WAV via Kyutai's 1B en/fr model.
|
|
//!
|
|
//! First run downloads ~3 GB from `kyutai/stt-1b-en_fr` to the HF cache.
|
|
//!
|
|
//! Usage:
|
|
//! ```
|
|
//! cargo run -p rtx-csm --release --features metal --example stt_demo -- \
|
|
//! --in /tmp/csm_24k.wav
|
|
//! ```
|
|
//!
|
|
//! Note: until sentencepiece detok is wired (Phase 6a polish), the output
|
|
//! is raw token IDs per word. The first version is intentionally minimal —
|
|
//! demonstrates that the streaming pipeline is connected end to end.
|
|
|
|
use anyhow::Result;
|
|
use clap::Parser;
|
|
use rtx_csm::{audio_io, stt::{AsrEvent, Stt, SAMPLE_RATE}};
|
|
use std::path::PathBuf;
|
|
|
|
#[derive(Debug, Parser)]
|
|
#[command(name = "stt_demo")]
|
|
struct Cli {
|
|
/// Input WAV (any rate / channels — resampled to 24 kHz mono).
|
|
#[arg(long = "in")]
|
|
input: PathBuf,
|
|
/// Force CPU device.
|
|
#[arg(long)]
|
|
cpu: bool,
|
|
}
|
|
|
|
fn main() -> Result<()> {
|
|
tracing_subscriber::fmt().init();
|
|
let cli = Cli::parse();
|
|
|
|
let device = if cli.cpu {
|
|
candle_core::Device::Cpu
|
|
} else if candle_core::utils::metal_is_available() {
|
|
candle_core::Device::new_metal(0)?
|
|
} else {
|
|
candle_core::Device::Cpu
|
|
};
|
|
println!("device: {device:?}");
|
|
|
|
let t = std::time::Instant::now();
|
|
let mut stt = Stt::load_default(&device)?;
|
|
println!("loaded Kyutai STT 1B en/fr in {:.2}s", t.elapsed().as_secs_f32());
|
|
|
|
// Load WAV at 24 kHz mono (Mimi's expected input rate).
|
|
let samples = audio_io::load_mono_at_rate(&cli.input, SAMPLE_RATE)?;
|
|
println!(
|
|
"loaded {}: {} samples ({:.2}s @ {} Hz)",
|
|
cli.input.display(),
|
|
samples.len(),
|
|
samples.len() as f32 / SAMPLE_RATE as f32,
|
|
SAMPLE_RATE
|
|
);
|
|
|
|
// The 1B en/fr STT model expects:
|
|
// - 0.0 seconds of silence prefix (no warmup needed)
|
|
// - 0.5 seconds of silence suffix (= 6.25 frames @ 12.5 Hz, round up to 7)
|
|
// to flush the asr_delay-buffered predictions at end of audio.
|
|
// Without the suffix the model produces only pad tokens. See HF
|
|
// config.json `stt_config` and the reference Python script.
|
|
const PREFIX_SILENCE_SECS: f32 = 0.0;
|
|
const SUFFIX_SILENCE_SECS: f32 = 0.5;
|
|
let mut audio_with_padding =
|
|
vec![0.0f32; (PREFIX_SILENCE_SECS * SAMPLE_RATE as f32) as usize];
|
|
audio_with_padding.extend_from_slice(&samples);
|
|
audio_with_padding
|
|
.extend(std::iter::repeat(0.0f32).take((SUFFIX_SILENCE_SECS * SAMPLE_RATE as f32) as usize));
|
|
println!(
|
|
"padded with {:.1}s prefix + {:.1}s suffix silence -> {} samples",
|
|
PREFIX_SILENCE_SECS,
|
|
SUFFIX_SILENCE_SECS,
|
|
audio_with_padding.len()
|
|
);
|
|
|
|
// Stream in 1-second chunks so we can observe streaming behavior.
|
|
let chunk_size = SAMPLE_RATE as usize;
|
|
let mut all_events: Vec<AsrEvent> = Vec::new();
|
|
let t = std::time::Instant::now();
|
|
for (i, chunk) in audio_with_padding.chunks(chunk_size).enumerate() {
|
|
let evs = stt.step_pcm(chunk)?;
|
|
let n_step = evs.iter().filter(|e| matches!(e, AsrEvent::Step { .. })).count();
|
|
let n_word = evs.iter().filter(|e| matches!(e, AsrEvent::Word { .. })).count();
|
|
let n_end = evs.iter().filter(|e| matches!(e, AsrEvent::EndWord { .. })).count();
|
|
println!("[chunk {i}] events: step={n_step} word={n_word} endword={n_end}");
|
|
all_events.extend(evs);
|
|
}
|
|
let evs_finish = stt.finish()?;
|
|
all_events.extend(evs_finish);
|
|
println!("inference: {:.2}s", t.elapsed().as_secs_f32());
|
|
|
|
// Summary: print all Word events. Step events are noisy (one per frame).
|
|
let mut words = 0usize;
|
|
for ev in &all_events {
|
|
match ev {
|
|
AsrEvent::Word {
|
|
tokens,
|
|
start_time,
|
|
..
|
|
} => {
|
|
println!(
|
|
" word @ {:.2}s: tokens={:?}",
|
|
start_time, tokens
|
|
);
|
|
words += 1;
|
|
}
|
|
AsrEvent::EndWord { stop_time, .. } => {
|
|
println!(" end_word @ {:.2}s", stop_time);
|
|
}
|
|
AsrEvent::Step { .. } => {}
|
|
}
|
|
}
|
|
println!("total words detected: {}", words);
|
|
|
|
Ok(())
|
|
}
|