Sequence-to-Sequence Speech Models
“From waveforms to words, and back again.”
TL;DR
Sequence-to-sequence models replaced hand-crafted speech pipelines with end-to-end neural networks for ASR (LAS, Whisper), TTS (Tacotron 2, FastSpeech 2), and speech translation (Translatotron). The evolution from HMM-GMM through CTC and RNN-Transducer to Conformer progressively solved streaming, alignment, and robustness challenges. Self-supervised pretraining with Wav2Vec 2.0 and HuBERT gives strong results with minimal labeled data, while audio-language models like AudioLM blur the boundary between speech and NLP. For how these models handle multiple tasks simultaneously, see the multi-task learning article.

1. The Seq2Seq Paradigm
Traditional speech systems were pipelines:
- ASR: Acoustic Model + Language Model + Decoder.
- TTS: Text Analysis + Acoustic Model + Vocoder.
Seq2Seq unifies this into a single neural network:
- Input: Sequence (audio features or text).
- Output: Sequence (text or audio features).
- No hand-crafted features or rules.
2. ASR: Listen, Attend, and Spell (LAS)
Architecture:
- Listener (Encoder): Pyramid LSTM processes audio.
- Reduces time resolution: each of the 3 pyramid layers halves the frame rate, 8x in total.
- Attender: Attention mechanism focuses on relevant audio frames.
- Speller (Decoder): LSTM generates characters/words.
Attention:
\[e_{t,i} = \text{score}(s_{t-1}, h_i), \qquad \alpha_t = \text{softmax}(e_t), \qquad c_t = \sum_i \alpha_{t,i} h_i\]Where:
s_{t-1}: Previous decoder state.h_i: Encoder hidden state at timei.c_t: Context vector (weighted sum of encoder states).
3. TTS: Tacotron 2
Goal: Text → Mel-Spectrogram → Waveform.
Architecture:
- Encoder: Character embeddings → convolutions → bidirectional LSTM.
- Attention: Location-sensitive attention over encoder states.
- Decoder: Autoregressive LSTM that predicts one mel-spectrogram frame per step.
- Vocoder (WaveNet/HiFi-GAN): Mel → Waveform.
Key Innovation: A simple seq2seq model predicting mel-spectrograms, paired with a WaveNet vocoder conditioned on them, reached a MOS of 4.53 against 4.58 for professional recordings. (Predicting several frames per step, the “reduction factor”, came from the original Tacotron; Tacotron 2 dropped it.)
4. Speech Translation: Direct S2ST
Traditional: Audio → ASR → Text → MT → Text → TTS → Audio. Direct: Audio → Audio (no text intermediate).
Advantages:
- Preserves prosody (emotion, emphasis).
- Works for unwritten languages.
Model: Translatotron (Google).
- Encoder: Audio (Spanish).
- Decoder: Spectrogram (English).
5. Attention Mechanisms
1. Content-Based Attention
- Decoder decides where to attend based on content.
- Problem: Can attend to same location twice (repetition).
2. Location-Aware Attention
- Uses previous attention weights to guide current attention.
- Encourages monotonic progression (left-to-right).
3. Monotonic Attention
- Enforces strict left-to-right alignment.
- Good for streaming ASR (can’t look ahead).
6. Challenges
1. Exposure Bias
- During training, decoder sees ground truth.
- During inference, decoder sees its own (possibly wrong) predictions.
- Fix: Scheduled sampling.
2. Alignment Failures
- Attention might skip words or repeat.
- Fix: Guided attention (force diagonal alignment early in training).
3. Long Sequences
- Attention is O(N^2) in memory.
- Fix: Chunked attention, or use CTC for ASR.
7. Modern Approach: Transformer-Based
Whisper (OpenAI):
- Encoder-Decoder Transformer.
- Trained on 680k hours.
- Handles ASR, Translation, Language ID in one model.
Advantages:
- Parallel training (unlike RNN).
- Better long-range dependencies.
8. Deep Dive: Attention Alignment Visualization
In ASR, attention should be monotonic (left-to-right).
Good Alignment:
Audio frames: [a1][a2][a3][a4][a5]
Text: [ h ][ e ][ l ][ l ][ o ]
Attention: ████
░░░░████
░░░░████
░░░░████
░░░░████
Bad Alignment (Skipping):
Audio frames: [a1][a2][a3][a4][a5]
Text: [ h ][ e ][ l ][ l ][ o ]
Attention: ████
░░░░████
░░░░████ ← Skipped a3!
Fix: Guided attention loss forces diagonal alignment early in training.
9. Deep Dive: Streaming ASR with Monotonic Attention
Problem: Standard attention looks at the entire input. Can’t stream.
Monotonic Chunkwise Attention (MoChA, Chiu and Raffel, 2018):
- At each decoder step, scan forward from the frame attended at the previous step.
- At each encoder frame, a sigmoid “stop here” probability,
p_stop = sigmoid(g(h_j, s_{t-1})), decides whether to halt. - Once halted, apply soft attention over a small fixed-size chunk of frames ending at that position.
Advantage: Can process audio in real-time (with bounded latency).
10. Deep Dive: Tacotron 2 Architecture Details
Encoder:
- Character embeddings → 3 Conv layers → Bidirectional LSTM.
- Output: Encoded representation of text.
Decoder:
- Prenet: 2 FC layers with dropout (helps with generalization).
- LSTM stack: 2 unidirectional LSTM layers (1024 units). Popular implementations, such as NVIDIA’s, split these into an “attention RNN” and a “decoder RNN”.
- Output: Predicts 80-dim mel-spectrogram frame.
Postnet:
- 5 Conv layers.
- Refines the mel-spectrogram (adds high-frequency details).
Stop Token:
- Decoder also predicts “Should I stop?” at each step.
- Training: Sigmoid BCE loss.
11. Deep Dive: Vocoder Evolution
Goal: Mel-Spectrogram → Waveform.
WaveNet (2016):
- Autoregressive CNN.
- Generates one sample at a time (16kHz = 16,000 samples/sec).
- Slow: The original autoregressive WaveNet ran far slower than real time.
WaveGlow (2018):
- Flow-based model.
- Parallel generation (all samples at once).
- Fast: Real-time on GPU.
HiFi-GAN (2020):
- GAN-based.
- Fast: The paper reports about 168x faster than real-time on a V100 GPU.
- Quality: MOS close to the recordings in the paper (4.36 vs 4.45 on LJSpeech).
12. System Design: Low-Latency TTS
Scenario: Voice assistant (Alexa, Siri).
Requirements:
- Latency: < 300ms from text to first audio chunk.
- Quality: Natural, expressive.
Architecture:
- Text Normalization: “Dr.” → “Doctor”, “$100” → “one hundred dollars”.
- Grapheme-to-Phoneme (G2P): “read” → /riːd/ or /rɛd/ (context-dependent).
- Prosody Prediction: Predict pitch, duration, energy.
- Acoustic Model: FastSpeech 2 (non-autoregressive, parallel).
- Vocoder: HiFi-GAN.
Streaming:
- Generate mel-spectrogram in chunks (e.g., 50ms).
- Vocoder processes each chunk as it arrives, with a few frames of overlapping context at chunk edges to avoid clicks.
13. Deep Dive: Translatotron (Direct S2ST)
Challenge: No parallel S2ST data (Audio_Spanish → Audio_English).
Solution: Weak supervision.
- Use ASR to get Spanish text.
- Use MT to get English text.
- Use TTS to get English audio.
- Train Translatotron on (Audio_Spanish, Audio_English) pairs.
Architecture:
- Encoder: Processes Spanish audio.
- Decoder: Generates English mel-spectrogram.
- Speaker Encoder: Preserves source speaker’s voice.
14. Code: Simple Seq2Seq ASR
import torch
import torch.nn as nn
class Seq2SeqASR(nn.Module):
def __init__(self, input_dim, hidden_dim, vocab_size):
super().__init__()
# Encoder: Bi-LSTM
self.encoder = nn.LSTM(input_dim, hidden_dim, num_layers=3,
batch_first=True, bidirectional=True)
# Attention (additive): scores depend on the current decoder state
self.attn_enc = nn.Linear(hidden_dim * 2, hidden_dim)
self.attn_dec = nn.Linear(hidden_dim, hidden_dim)
self.attn_v = nn.Linear(hidden_dim, 1)
# Decoder: LSTM
self.decoder = nn.LSTM(vocab_size + hidden_dim * 2, hidden_dim,
num_layers=2, batch_first=True)
# Output projection
self.fc = nn.Linear(hidden_dim, vocab_size)
def forward(self, audio_features, text_input):
# text_input: one-hot previous characters (teacher forcing), (B, U, vocab_size)
# Encode audio
encoder_out, _ = self.encoder(audio_features) # (B, T, 2*H)
enc_proj = self.attn_enc(encoder_out) # (B, T, H), computed once
# Decode
outputs = []
hidden = None
dec_state = encoder_out.new_zeros(encoder_out.size(0), 1, self.fc.in_features) # (B, 1, H)
for t in range(text_input.size(1)):
# Compute attention against the current decoder state
attn_scores = self.attn_v(torch.tanh(enc_proj + self.attn_dec(dec_state))).squeeze(-1) # (B, T)
attn_weights = torch.softmax(attn_scores, dim=1) # (B, T)
context = torch.bmm(attn_weights.unsqueeze(1), encoder_out) # (B, 1, 2*H)
# Decoder input: previous char + context
decoder_input = torch.cat([text_input[:, t:t+1], context], dim=-1)
# Decoder step
decoder_out, hidden = self.decoder(decoder_input, hidden)
dec_state = decoder_out
# Predict next char
logits = self.fc(decoder_out)
outputs.append(logits)
return torch.cat(outputs, dim=1)
15. Production Considerations
- Model Size: Quantize to INT8 for mobile deployment.
- Latency: Use non-autoregressive models (FastSpeech, Conformer-CTC).
- Personalization: Fine-tune on user’s voice (few-shot learning).
- Multilingual: Train on 100+ languages (mSLAM, Whisper).
16. Deep Dive: The Evolution of Seq2Seq in Speech
To understand where we are, we must understand the journey.
1. HMM-GMM (1980s-2010):
- Acoustic Model: Gaussian Mixture Models (GMM) modeled the probability of audio features given a phoneme state.
- Sequence Model: Hidden Markov Models (HMM) modeled the transition between states.
- Pros: Mathematically rigorous, worked on small data.
- Cons: Assumed independence of frames (false), complex training pipeline.
2. DNN-HMM (2010-2015):
- Replaced GMM with Deep Neural Networks (DNN).
- Impact: Large drop in WER (roughly 30% relative on Switchboard in Seide et al., 2011).
- Cons: Still relied on HMM for alignment.
3. CTC (2006, popularized 2015):
- Connectionist Temporal Classification.
- First “End-to-End” loss.
- Allowed predicting sequences shorter than input without explicit alignment.
- Cons: Conditional independence assumption (output at time
tdepends only on input, not previous outputs).
4. LAS (2016):
- Listen-Attend-Spell.
- Brought attention-based encoder-decoders to large-vocabulary ASR (Chorowski et al. had applied attention to phoneme recognition in 2015).
- Pros: No independence assumption.
- Cons: Not streaming (needs full audio to attend).
5. RNN-T (2012, popularized 2018):
- RNN Transducer.
- Adds a prediction network to CTC-style frame alignment, so each output depends on previous labels while decoding stays streamable.
- Result: Widely used for streaming and on-device ASR; Google’s 2019 on-device Pixel recognizer is a published example.
6. Transformer & Conformer (2017-Present):
- Self-attention replaces RNNs.
- Conformer: Combines CNN (local patterns) with Transformer (global context).
17. Deep Dive: Connectionist Temporal Classification (CTC)
Problem: Audio has T frames (e.g., 1000). Text has L characters (e.g., 50). T ≫ L. How do we map them?
CTC Solution:
- Introduce a blank token
ε. - Output sequence length is
T. - Collapse: Merge repeats, then remove blanks.
h h e e l l l l o→helo(the repeatedls merge into one)h h ε e e l l ε l l o→hello(a blank between them keeps bothls)
Loss Function:
\[P(Y \mid X) = \sum_{A \in \mathcal{B}^{-1}(Y)} \prod_{t=1}^{T} P(a_t \mid X)\]- Sum over all valid alignments
Athat collapse toY; each alignment’s probability is a product of per-frame probabilities (the conditional independence assumption). - Computed efficiently using Dynamic Programming (Forward-Backward algorithm).
Pros:
- Fast inference (O(1) per step).
- Streaming friendly.
Cons:
- Can’t model language (e.g., “pair” vs “pear” sounds same). Needs external Language Model.
18. Deep Dive: RNN-Transducer (RNN-T)
Architecture:
- Encoder (Audio): Processes audio frames
x_t. Producesh_t^{enc}. - Prediction Network (Text): Processes previous non-blank output
y_{u-1}. Producesh_u^{pred}. (Like an LM). -
Joint Network: Combines them (tanh is the usual nonlinearity; some implementations use ReLU).
\[z_{t,u} = \tanh\left(W_{\text{enc}} h_t^{\text{enc}} + W_{\text{pred}} h_u^{\text{pred}}\right), \qquad P(y \mid t,u) = \text{softmax}(W_{\text{out}} z_{t,u})\]
Inference (Greedy):
- If output is non-blank (
y), feed to Prediction Network, incrementu. Stay at audio framet. - If output is blank (
ε), move to next audio framet+1.
Why it wins:
- Streaming: Encoder is causal.
- Accuracy: Prediction network models label dependencies (unlike CTC).
- Latency: Controllable.
19. Deep Dive: Transformer vs Conformer
Transformer:
- Great at global context (Self-Attention).
- Bad at local fine-grained details (needs deep layers to see local patterns).
- Positional encodings are brittle for varying audio lengths.
Conformer (Convolution-augmented Transformer):
- Macaron Style: Feed-Forward -> Multi-Head Attention -> Conv Module -> Feed-Forward.
- Conv Module: Pointwise Conv -> Gated Linear Unit (GLU) -> Depthwise Conv -> BatchNorm -> Swish -> Pointwise Conv.
- Why:
- CNNs capture local features (formants, transitions).
- Transformers capture global semantics.
- Result: State of the art on LibriSpeech in 2020 (1.9% / 3.9% WER on test-clean / test-other with a language model).
20. Deep Dive: FastSpeech 2 (Non-Autoregressive TTS)
Problem with Tacotron 2:
- Autoregressive (slow).
- Unstable attention (skipping/repeating).
- Hard to control prosody (speed, pitch).
FastSpeech 2 Architecture:
- Encoder: Feed-Forward Transformer.
- Variance Adaptor:
- Duration Predictor: How many frames does this phoneme last? (Trained on forced alignment).
- Pitch Predictor: Predicts F0 contour.
- Energy Predictor: Predicts volume.
- Adds embeddings of these predictions to the encoder output.
- Length Regulator: Expands hidden states based on duration (e.g., “a” lasts 5 frames -> repeat vector 5 times).
- Decoder: Feed-Forward Transformer.
Pros:
- Fast: Parallel generation.
- Robust: No attention failures.
- Controllable: Can explicitly set “speak 1.2x faster” or “raise pitch”.
21. Deep Dive: VITS (Conditional Variational Autoencoder)
VITS (Conditional Variational Autoencoder with Adversarial Learning for End-to-End Text-to-Speech) was a landmark end-to-end TTS model in 2021 and remains a common baseline.
Key Idea: Combine Acoustic Model and Vocoder into one model: a conditional VAE with a normalizing-flow prior, trained adversarially.
Architecture:
- Posterior Encoder: Encodes linear spectrogram into latent
z. - Prior Encoder: Predicts distribution of
zfrom text (conditioned on alignment). - Decoder (Generator): Transforms
zinto waveform (HiFi-GAN style). - Stochastic Duration Predictor: Models duration uncertainty.
Training:
- Reconstruction Loss: Mel-spectrogram L1 loss.
- KL Divergence: Between Posterior and Prior.
- Adversarial Loss: Discriminator tries to distinguish real vs generated audio.
Result: The paper reports MOS close to the recordings on LJSpeech (4.43 vs 4.46).
22. System Design: Real-Time Speech Translation System
Scenario: “Universal Translator” device. English Audio -> Spanish Audio.
Constraints:
- Latency: < 2 seconds lag.
- Compute: Edge device (limited).
Architecture Choices:
Option A: Cascade (ASR -> MT -> TTS)
- Pros: Modular. Can use SOTA for each.
- Cons: Error propagation. Latency adds up. Loss of paralinguistics (tone).
Option B: Direct S2ST (Audio -> Audio)
- Pros: Fast. Preserves tone.
- Cons: Data scarcity. Hard to train.
Hybrid Design (Production):
- Streaming ASR (RNN-T): Generates English text stream.
- Streaming MT: Translates partial sentences. “Hello” -> “Hola”.
- Incremental TTS: Generates audio for “Hola” immediately.
Wait-k Policy:
- MT model waits for
kwords before translating. - Balances context (accuracy) vs latency.
23. Case Study: OpenAI Whisper
Goal: Robust ASR that works on “in the wild” audio.
Data:
- 680,000 hours of web audio.
- Weak Supervision: Transcripts that accompany audio on the web (noisy). Transcripts that looked machine-generated were filtered out.
- Multitask:
- Multilingual Transcription.
- Any-to-English Translation.
- Language Identification.
- Voice Activity Detection.
Architecture:
- Standard Encoder-Decoder Transformer.
- Input: Log-Mel Spectrogram (30 seconds).
- Output: Text tokens.
Key Features:
- Task Tokens:
<|startoftranscript|> <|en|> <|transcribe|> <|notimestamps|>. - Long-form: Processes 30s chunks. Uses previous chunk’s text as prompt (context).
Impact:
- Zero-shot performance on many datasets is competitive with supervised models, and far more robust outside LibriSpeech.
- Showed that scale and diversity of weakly supervised data can matter more than architectural novelty.
24. Case Study: Google USM (Universal Speech Model)
Goal: One model pre-trained on 300+ languages and fine-tuned for ASR in 100+.
Architecture:
- Conformer (2 Billion parameters), pre-trained on 12M hours of unlabeled YouTube audio.
- MOST (Multi-Objective Supervised pre-Training):
- BEST-RQ: Self-supervised loss (BERT-style on audio).
- Text-Injection: Train on text-only data (using shared encoder layers).
- ASR: Supervised loss on labeled audio.
Result:
- Google reports under 30% average WER on YouTube captions across 73 languages, and lower WER than Whisper on the languages it compared (2023).
25. Deep Dive: Evaluation Metrics
ASR:
- WER (Word Error Rate): \(\text{WER} = \frac{S + D + I}{N}\)
- S: Substitutions, D: Deletions, I: Insertions, N: words in the reference. Insertions mean WER can exceed 100%.
- CER (Character Error Rate): For languages without spaces (Chinese).
- RTF (Real-Time Factor): Processing time divided by audio duration. RTF < 1 means faster than real time.
TTS:
- MOS (Mean Opinion Score): Humans rate naturalness 1-5.
- MCD (Mel Cepstral Distortion): Objective distance between generated and ground truth spectrograms.
ST (Speech Translation):
- BLEU: Standard MT metric.
26. Code: Implementing Beam Search for Seq2Seq
Greedy decoding (pick max prob) is suboptimal. Beam search explores k paths.
import torch
def beam_search_decoder(model, encoder_out, beam_width=3, max_len=50):
# Start with [SOS] token
# Beam: list of (sequence, score, hidden_state)
start_token = model.vocab['<sos>']
beam = [([start_token], 0.0, None)]
for _ in range(max_len):
candidates = []
for seq, score, hidden in beam:
if seq[-1] == model.vocab['<eos>']:
candidates.append((seq, score, hidden))
continue
# Predict next token
last_token = torch.tensor([[seq[-1]]])
output, new_hidden = model.decoder(last_token, hidden, encoder_out)
log_probs = torch.log_softmax(output, dim=-1)
# Get top k
topk_probs, topk_ids = log_probs.topk(beam_width)
for i in range(beam_width):
token = topk_ids[0][i].item()
prob = topk_probs[0][i].item()
candidates.append((seq + [token], score + prob, new_hidden))
# Select top k candidates
beam = sorted(candidates, key=lambda x: x[1], reverse=True)[:beam_width]
# Check if all finished
if all(c[0][-1] == model.vocab['<eos>'] for c in beam):
break
return beam[0][0] # Return best sequence
27. Production: Serving Speech Models with Triton
NVIDIA Triton Inference Server is a common choice for deploying speech models.
Pipeline:
- Feature Extractor (Python Backend): Audio -> Mel-Spec.
- Encoder (TensorRT): Mel-Spec -> Hidden States.
- Decoder (TensorRT): Hidden States -> Text (Beam Search).
Dynamic Batching:
- Triton groups requests that arrive within a configurable queue delay (often a few milliseconds) into a batch.
- Increases GPU utilization significantly.
Ensemble Model:
- Define a DAG of models.
- Client sends audio, Triton handles the flow.
28. Deep Dive: End-to-End Speech-to-Speech Translation (S2ST)
Unit-Based S2ST:
- Instead of spectrograms, predict discrete acoustic units (from HuBERT or k-means).
- Advantage: Discrete tokens allow using standard Transformer (like NLP).
- Vocoder: Unit HiFi-GAN converts discrete units to waveform.
SpeechMatrix (Meta, 2023):
- Speech-to-speech translations mined from European Parliament recordings: 136 language pairs, about 418k hours.
- Uses speech encoders trained into the LASER sentence-embedding space to find matching sentences in different languages.
29. Deep Dive: Self-Supervised Learning (SSL) in Speech
Problem: Labeled data (audio + text) is expensive. Unlabeled audio is free.
Wav2Vec 2.0 (Meta):
- Idea: Mask parts of the audio latent space and predict the quantized representation of the masked part.
- Contrastive Loss: Identify the correct quantized vector among distractors.
- Result: After pre-training on 53k hours of unlabeled audio, fine-tuning on just 10 minutes of labeled data gave 4.8 / 8.2 WER on LibriSpeech test-clean / test-other in the paper.
HuBERT (Hidden Unit BERT):
- Idea: Offline clustering (k-means) to generate pseudo-labels: MFCCs in the first iteration, then the model’s own intermediate features in later iterations.
- Masked Prediction: BERT-style objective. Predict the cluster ID of masked frames.
- Result: Matches or beats Wav2Vec 2.0 on the LibriSpeech fine-tuning benchmarks.
Impact on Seq2Seq:
- Initialize the Encoder with Wav2Vec 2.0 / HuBERT.
- Add a random Decoder.
- Fine-tune on ASR task.
- Benefit: Drastic reduction in required labeled data.
30. Deep Dive: Multilingual Seq2Seq
Goal: One model for 100 languages.
Challenges:
- Data Imbalance: For example, a corpus might hold hundreds of thousands of hours of English but only a few hundred of Swahili.
- Script Diversity: Latin, Cyrillic, Chinese, Arabic.
- Phonetic Overlap: “P” in English ≠ “P” in French (English /p/ is aspirated at the start of a stressed syllable; French /p/ is not).
Solutions:
- Language ID Token:
<|en|>,<|fr|>. - Shared Vocabulary: SentencePiece (BPE) trained on all languages.
-
Balancing Sampling: Up-sample low-resource languages during training.
\[p_l \propto \left(\frac{N_l}{N_{\text{total}}}\right)^{\alpha}\]where α < 1 (e.g., 0.3) flattens the distribution.
- Adapter Modules: Small, language-specific layers inserted into the frozen giant model.
31. Deep Dive: End-to-End SLU (Spoken Language Understanding)
Traditional: Audio → ASR → Text → NLP → Intent/Slots. E2E SLU: Audio → Intent/Slots.
Why E2E?
- Error Robustness: ASR might transcribe “play jazz” as “play jas”. NLP fails. E2E model learns acoustic features of “jazz”.
- Paralinguistics: “Yeah right” (sarcastic) -> Negative Sentiment. Text-only NLP misses this.
Architecture:
- Encoder: Pre-trained Wav2Vec 2.0.
- Decoder: Predicts semantic frame directly.
[INTENT: PlayMusic] [ARTIST: The Beatles] [GENRE: Rock]
Challenges:
- Lack of labeled Audio-to-Semantics data.
- Solution: Transfer learning from ASR models.
32. Deep Dive: Attention Visualization Code
Visualizing attention maps is the best way to debug Seq2Seq models.
import matplotlib.pyplot as plt
import seaborn as sns
def plot_attention(attention_matrix, input_text, output_text):
"""
attention_matrix: (Output_Len, Input_Len) numpy array
"""
plt.figure(figsize=(10, 8))
sns.heatmap(attention_matrix, cmap='viridis',
xticklabels=input_text, yticklabels=output_text)
plt.xlabel('Encoder Input')
plt.ylabel('Decoder Output')
plt.show()
# Example usage
# attn = model.get_attention_weights(...)
# plot_attention(attn, audio_frames, predicted_words)
What to look for:
- Diagonal: Good alignment.
- Vertical Line: Decoder is stuck on one frame (repeating output).
- Horizontal Line: Encoder frame is ignored.
- Fuzzy: Weak attention (low confidence).
33. Deep Dive: Handling Long-Form Audio
Standard Transformers have O(N^2) attention complexity. 30s audio = 1500 frames. 1 hour = 180,000 frames.
Strategies:
- Chunking (Whisper):
- Slice audio into 30s segments.
- Transcribe independently.
- Problem: Context cut off at boundaries.
- Fix: Pass previous segment’s text as prompt.
- Streaming (RNN-T):
- Process frame-by-frame. Infinite length.
- Problem: No future context.
- Sparse Attention (BigBird / Longformer):
- Attend only to local window + global tokens.
- O(N) complexity.
- Block-Processing (Emformer):
- Block-wise processing with memory bank for history.
34. Deep Dive: The Mathematics of Transformers in Speech
Why did Transformers replace RNNs?
1. Self-Attention Complexity:
- RNN: O(N) sequential operations. Cannot parallelize.
- Transformer: O(1) sequential operations (parallelizable). O(N^2) memory.
- Benefit: We can train on 1000 GPUs efficiently.
2. Positional Encodings in Speech:
- Text uses absolute sinusoidal encodings.
- Speech Problem: Audio length varies wildly. 10s vs 10m.
- Solution: Relative Positional Encoding.
- Instead of adding
P_ito inputX_i, add a biasb_{i-j}to the attention scoreA_{ij}. - Allows the model to generalize to audio lengths unseen during training.
- Instead of adding
3. Subsampling:
- Audio is high-frequency (100 frames/sec). Text is low-frequency (roughly 2-3 words, or 12-15 characters, per second).
- Conv Subsampling: First 2 layers of Encoder are strided Convolutions (stride 2x2 = 4x reduction).
- Reduces sequence length N → N/4. Reduces attention cost N² → (N/4)² = N²/16.
35. Deep Dive: Troubleshooting Seq2Seq Models
1. The “Hallucination” Problem:
- Symptom: Model outputs “Thank you Thank you Thank you” during silence.
- Cause: Decoder language model is too strong; it predicts likely text even without acoustic evidence.
- Fix:
- Voice Activity Detection (VAD): Filter out silence.
- Coverage Penalty: Penalize attending to the same frames repeatedly.
2. The “NaN” Loss:
- Symptom: Training crashes.
- Cause: Exploding gradients in LSTM or division by zero in BatchNorm.
- Fix:
- Gradient Clipping (norm 1.0).
- Warmup learning rate.
- Check for empty transcripts in data.
- CTC loss is infinite when a transcript has more tokens than the subsampled input has frames; drop those utterances or set
zero_infinity=Truein PyTorch’sCTCLoss.
3. The “Babble” Problem:
- Symptom: Output is gibberish.
- Cause: CTC alignment failed or Attention didn’t converge.
- Fix:
- Curriculum Learning: Train on short utterances first, then long.
- Guided Attention Loss: Force diagonal alignment for first few epochs.
36. Deep Dive: The Future - Audio-Language Models
The boundary between Speech and NLP is blurring.
Tokenizers: Audio becomes discrete tokens through self-supervised “semantic” units (HuBERT, w2v-BERT) and/or neural codec tokens (SoundStream, EnCodec).
AudioLM (Google):
- Treats audio as a sequence of discrete tokens: w2v-BERT semantic tokens for content and SoundStream codec tokens for acoustic detail.
- Uses GPT-style decoders to generate those tokens.
- Capabilities:
- Speech Continuation: Given 3s of speech, continue speaking in the same voice and style.
- Piano Continuation: The same recipe works for music.
- AudioLM has no text input. Text-conditioned successors such as VALL-E (which clones a voice from a 3-second prompt) and SPEAR-TTS do zero-shot TTS.
SpeechGPT (Fudan University):
- Fine-tunes LLaMA on paired speech-text data.
- Modality Adaptation: Teaches the LLM to understand and produce discrete HuBERT units.
- Result: A chatbot that takes speech in and produces speech out, through text intermediate steps.
Implication:
- Seq2Seq models (Encoder-Decoder) might be replaced by Decoder-only LLMs that handle all modalities (Text, Audio, Image) in a unified token space.
37. Ethical Considerations in Seq2Seq Speech
1. Deepfakes & Voice Cloning:
- Models like Vall-E can clone a voice from 3 seconds of audio.
- Risk: Fraud (fake CEO calls), harassment, disinformation.
- Mitigation:
- Watermarking: Embed inaudible signals in generated audio.
- Detection: Train classifiers to detect synthesis artifacts.
- Access control: “Know Your Customer” checks and consent verification for voice-cloning APIs.
2. Bias in ASR:
- Models trained on LibriSpeech (audiobooks) fail on AAVE (African American Vernacular English) or Indian accents.
- Fix: Diverse training data. “Fairness-aware” loss functions that penalize disparity between groups.
3. Privacy:
- Smart speakers listen constantly.
- Fix: On-Device Processing. Run the Seq2Seq model on the phone’s NPU, never sending audio to the cloud.
38. Further Reading
To master Seq2Seq speech models, these papers are essential:
- “Listen, Attend and Spell” (Chan et al., 2016): The attention-based encoder-decoder that made end-to-end large-vocabulary ASR practical.
- “Natural TTS Synthesis by Conditioning WaveNet on Mel Spectrogram Predictions” (Shen et al., 2018): The Tacotron 2 paper.
- “Attention Is All You Need” (Vaswani et al., 2017): The Transformer paper (foundation of modern speech).
- “Conformer: Convolution-augmented Transformer for Speech Recognition” (Gulati et al., 2020): Still one of the most common ASR encoders.
- “Wav2Vec 2.0: A Framework for Self-Supervised Learning of Speech Representations” (Baevski et al., 2020): The SSL revolution.
- “Whisper: Robust Speech Recognition via Large-Scale Weak Supervision” (Radford et al., 2022): How scale beats architecture.
39. Summary
| Task | Model | Input | Output |
|---|---|---|---|
| ASR | LAS, Whisper | Audio | Text |
| TTS | Tacotron 2 | Text | Audio |
| ST | Translatotron | Audio (L1) | Audio (L2) |
| Vocoder | HiFi-GAN | Mel-Spec | Waveform |
| Streaming | RNN-T | Audio Stream | Text Stream |
| Fast TTS | FastSpeech 2 | Text | Mel-Spec |
| SSL | Wav2Vec 2.0 | Masked Audio | Quantized Vector |
| E2E SLU | Wav2Vec 2.0 encoder + semantic decoder | Audio | Intent/Slots |
| Troubleshooting | VAD, Gradient Clipping | - | - |
| Future | AudioLM | Discrete Tokens | Discrete Tokens |
FAQ
What is the difference between CTC and attention-based ASR?
CTC enforces monotonic alignment between audio and text and enables streaming inference, but assumes conditional independence between output tokens at each time step. Attention-based models like LAS capture label dependencies and long-range context but require the full input before decoding. Hybrid CTC-Attention training combines both strengths for production systems.
Why did Conformer replace Transformers for speech recognition?
Conformer augments Transformer self-attention with convolution modules in a macaron-style architecture where each block contains Feed-Forward, Multi-Head Attention, Convolution, and Feed-Forward sublayers. CNNs capture local acoustic features like formants and transitions while Transformers handle global semantics, achieving state-of-the-art results that neither architecture achieves alone.
How does Tacotron 2 generate natural-sounding speech?
Tacotron 2 uses a character encoder followed by location-sensitive attention and an autoregressive LSTM decoder that predicts mel-spectrogram frames one at a time. A post-net refines the output, and a vocoder like HiFi-GAN converts the spectrogram to a waveform. Location-sensitive attention prevents word skipping and repeating by using previous alignment weights.
What is RNN-Transducer and why is it the standard for streaming ASR?
RNN-T combines a causal audio encoder with a prediction network that models label dependencies, joined by a network that produces output probabilities. Unlike attention-based models, it processes audio frame-by-frame with controllable latency, making it a common choice for on-device streaming ASR; Google’s on-device Pixel recognizer is a published example.