Skip to main content
Whisper uses a Transformer-based encoder-decoder architecture optimized for speech recognition tasks. Understanding the architecture helps with model customization, debugging, and advanced use cases.

Model Dimensions

Each Whisper model is defined by a ModelDimensions 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

The AudioEncoder processes mel spectrograms into audio features:

Encoder Processing Steps

  1. 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)
  2. Positional Encoding: Sinusoidal embeddings added to features
  3. Transformer Blocks: Self-attention and feedforward layers
  4. Layer Normalization: Final normalization layer

Text Decoder

The TextDecoder generates text tokens autoregressively:

Decoder Processing Steps

  1. Token Embeddings: Convert token IDs to embeddings
  2. Positional Embeddings: Add position information
  3. Self-Attention: Attend to previous tokens (causal)
  4. Cross-Attention: Attend to encoder outputs
  5. Feedforward: Process through MLP
  6. 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:
The encoder uses self-attention where each position attends to all positions in the audio features:
The decoder uses causal self-attention where each token can only attend to previous tokens:
The decoder also uses cross-attention to attend to encoder outputs:

Residual Attention Block

Each transformer layer is a ResidualAttentionBlock:

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.
For production deployments, consider using the turbo model with KV caching enabled for optimal speed.