Skip to content

Streaming Conformer Speech-to-Speech

Full source in examples/speech_to_speech.rs. Run it with cargo run --release --example speech_to_speech.

What This Shows

  • On-device audio frontend via nn::audio::log_mel_spectrogram (batched stft_power + Hann window + Slaney Mel filterbank executed inside XLA/Metal)
  • JIT-compatible nn::audio::spec_augment applying random contiguous frequency and time masks
  • Streaming causal Conformer blocks combining causal nn::flash_attention, nn::audio::CausalConv1d (fused asymmetric left padding), and channel-last nn::audio::ConformerConvModule
  • Joint training with differentiable log-space nn::audio::ctc_loss (for auxiliary discrete speech-unit alignment) and target mel-spectrogram reconstruction loss

Streaming Audio Frontend and Joint Loss

let src_mel = log_mel_spectrogram(
    &src_wav, cfg.n_fft, cfg.hop_length, cfg.n_mels,
    cfg.sample_rate, 40.0, 7600.0, 1e-5,
);

let train_step = jit(move |(mut m, mut o, s_mel, t_mel, u_tgt, in_l, tgt_l): ...| {
    let aug_mel = spec_augment(&s_mel, 4, 2, 1, 1);
    let (total_loss, grads) = value_and_grad(
        |p: SpeechToSpeechModel| {
            let (unit_logits, pred_mel) = p.forward_mel(&aug_mel, &cfg_t);
            let lp = log_softmax_axis(&unit_logits, 2);
            let l_ctc = ctc_loss(&lp, &u_tgt, &in_l, &tgt_l, 0);
            let l_mel = mse_loss(&pred_mel, &t_mel);
            &l_mel + &l_ctc * 0.25
        },
        &m,
    );
    o.step(&mut m, &grads);
    (m, o, total_loss)
});

Running

cargo run --release --example speech_to_speech
JACOBI_BACKEND=cpu cargo run --release --example speech_to_speech