Skip to main content

Whisper

The main Whisper model class that performs speech recognition. Inherits from torch.nn.Module.

Constructor

ModelDimensions
required
Model dimensions configuration containing architecture parameters

Attributes

ModelDimensions
The model dimensions used to initialize the encoder and decoder
AudioEncoder
The audio encoder that processes mel spectrograms into features
TextDecoder
The text decoder that generates text tokens from audio features
torch.Tensor
Sparse tensor indicating which attention heads to use for time alignment. By default, uses the last half of decoder layers.

Properties

torch.device
The device (CPU/GPU) where the model parameters are stored
bool
Returns True if the model supports multiple languages (vocab size >= 51865)
int
Number of languages supported by the model

Methods

embed_audio

Encode audio mel spectrogram into features.
torch.Tensor
Mel spectrogram with shape (batch_size, n_mels, n_ctx)
torch.Tensor
Encoded audio features with shape (batch_size, n_audio_ctx, n_audio_state)

logits

Compute logits for next token prediction.
torch.Tensor
Text tokens with shape (batch_size, seq_len)
torch.Tensor
Encoded audio features from embed_audio()
torch.Tensor
Logits for next token with shape (batch_size, seq_len, vocab_size)

forward

Full forward pass through encoder and decoder.
torch.Tensor
Mel spectrogram input
torch.Tensor
Text tokens
torch.Tensor
Decoder output logits

set_alignment_heads

Set custom alignment heads for cross-attention analysis.
bytes
Base85-encoded, gzip-compressed boolean array specifying which attention heads to use

install_kv_cache_hooks

Install hooks for key-value caching to speed up autoregressive decoding.
Optional[dict]
Existing cache dictionary to extend, or None to create new cache
Dict[nn.Module, torch.Tensor]
Dictionary mapping key/value projection modules to cached tensors
List[RemovableHandle]
List of PyTorch hook handles that can be used to remove the hooks

transcribe

Transcribe audio to text with timestamps and metadata. See the transcribe documentation for details.

decode

Decode mel spectrogram(s) to text. See the decode documentation for details.

detect_language

Detect the spoken language in audio. Returns language tokens and probability distributions.

ModelDimensions

Dataclass containing model architecture dimensions.

Fields

int
required
Number of mel filterbank channels (typically 80)
int
required
Audio context length - number of frames in the encoder
int
required
Hidden dimension size of the audio encoder
int
required
Number of attention heads in the audio encoder
int
required
Number of layers in the audio encoder
int
required
Vocabulary size - determines if model is multilingual (>= 51865)
int
required
Text context length - maximum sequence length for the decoder
int
required
Hidden dimension size of the text decoder
int
required
Number of attention heads in the text decoder
int
required
Number of layers in the text decoder

Usage Examples

Loading a Model

Using Model Components

Creating Custom Model

Notes

  • The Whisper class is typically loaded using whisper.load_model() rather than instantiated directly
  • Models are available in sizes: tiny, base, small, medium, large
  • Multilingual models have vocab size >= 51865 and support 99 languages
  • English-only models are more accurate for English but don’t support other languages
  • Use model.to(device) to move the model to GPU for faster inference
  • The encoder processes 30-second audio chunks at a time
  • KV cache hooks are used internally by the decode function for efficiency