Model Dimensions
Each Whisper model is defined by aModelDimensions dataclass that specifies the architecture parameters:
whisper/model.py
These dimensions are automatically loaded from the model checkpoint and accessible via
model.dims.Encoder-Decoder Structure
Whisper consists of two main components:Audio Encoder
TheAudioEncoder processes mel spectrograms into audio features:
Encoder Processing Steps
-
Convolution Layers: Two 1D convolutions with GELU activation
- First conv: kernel_size=3, padding=1 (preserves length)
- Second conv: kernel_size=3, stride=2 (downsamples by 2x)
- Positional Encoding: Sinusoidal embeddings added to features
- Transformer Blocks: Self-attention and feedforward layers
- Layer Normalization: Final normalization layer
Text Decoder
TheTextDecoder generates text tokens autoregressively:
Decoder Processing Steps
- Token Embeddings: Convert token IDs to embeddings
- Positional Embeddings: Add position information
- Self-Attention: Attend to previous tokens (causal)
- Cross-Attention: Attend to encoder outputs
- Feedforward: Process through MLP
- Output Projection: Project to vocabulary logits
Attention Mechanism
Multi-Head Attention
Whisper uses standard multi-head attention with optional scaled dot-product attention (SDPA) for efficiency:Self-Attention (Encoder)
Self-Attention (Encoder)
The encoder uses self-attention where each position attends to all positions in the audio features:
Causal Self-Attention (Decoder)
Causal Self-Attention (Decoder)
The decoder uses causal self-attention where each token can only attend to previous tokens:
Cross-Attention (Decoder)
Cross-Attention (Decoder)
The decoder also uses cross-attention to attend to encoder outputs:
Residual Attention Block
Each transformer layer is aResidualAttentionBlock:
Positional Encoding
Sinusoidal Positional Embeddings (Encoder)
The encoder uses fixed sinusoidal positional embeddings:Learned Positional Embeddings (Decoder)
The decoder uses learned positional embeddings:Key-Value Caching
Whisper supports KV caching for efficient autoregressive generation:KV caching stores previously computed key and value tensors, avoiding redundant computation during autoregressive decoding.
Alignment Heads
Whisper uses specific attention heads for word-level timestamp alignment:Model Properties
Advanced Usage
Custom Forward Pass
Accessing Internal Components
Performance Optimizations
Scaled Dot-Product Attention
Uses PyTorch’s optimized SDPA when available for faster attention computation.
KV Caching
Caches key and value tensors to avoid redundant computation during generation.
Mixed Precision
Supports FP16/BF16 for reduced memory usage and faster inference.
Batch Processing
Can process multiple audio samples in parallel for improved throughput.