15.2 Whisper encoder: conv stem, sinusoids, pre-LN blocks
The audio frontend turns any WAV file into the picture Whisper expects. This section builds the half of Whisper that looks at that picture. The encoder runs once per 30 s window and produces 1500 vectors that every decoding step in Whisper decoder and checkpoint weights and Whisper decoding and long-form transcription attends to.
Concepts from other sections (15)
This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.
- LayerNorm: per-row normalization by the population mean and variance with inside the square root, then an affine map. Read more: 6.5 Module system and basic layers
- pre-LN: with one final LN per stack; trains without warmup. Read more: 11.5 Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing
- Log-mel: Whisper’s features, of the mel spectrogram with a floor, an 8-unit clip, and the map . Read more: 18.6 Mel scale and filterbanks in Mathematics
- GELU: , the input weighted by the standard normal CDF; its tanh approximation is a different function. Read more: 7.3 Activation Functions and Their Derivatives in Mathematics
- frequency ladder: the geometric sequence of angular speeds, from 1 down to about . Read more: 11.4 Positional encodings: sinusoidal, learned
- Residual stream: the running sum a Transformer block adds its attention and MLP outputs to. Read more: 13.4 Variance propagation and initialization in Mathematics
- logits: unnormalized log-scores that softmax turns into probabilities; here the table stores . Read more: 6.1 Byte bigram: counts, logits, safetensors v0
- state dict: an ordered mapping from dotted parameter names to arrays, used for checkpoints. Read more: 6.5 Module system and basic layers
- causal mask: the rule ; each position reads itself and the past. Read more: 11.2 Masks: causal, padding, sliding window, additive
- strict loading: rejecting any missing, unexpected or wrongly shaped tensor rather than leaving parameters at random values. Read more: 15.3 Whisper decoder and checkpoint weights
- post-LN: , the paper’s placement; needs warmup. Read more: 11.5 Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing
- cross-attention: attention whose queries come from the decoder and whose keys and values come from the encoded audio window, with no mask. Read more: 15.3 Whisper decoder and checkpoint weights
- op library (
F): the module of differentiable functions, each a NumPy forward plus one closed-form VJP joined byfrom_op. Read more: 6.3 The op library and gradcheck_all - fp16: a 16-bit format with 5 exponent bits and 10 mantissa bits; finer precision but a maximum of 65504 and a smallest subnormal of . Read more: 17.1 Mixed precision, loss scaling, gradient accumulation, and activation checkpointing
- Self-attention: a layer in which each position computes a weighted average of all positions’ values, weighted by query-key similarity. Read more: Chapter 2: Deep Learning
Nothing in the encoder is new. The stem is a convolution from Conv layers and pooling on autograd, the blocks use Multi-head attention, LayerNorm and MLPs come from Module system and basic layers, and the position table is a close cousin of the one in Positional encodings: sinusoidal, learned. What is new is fitting these parts to a published checkpoint exactly. Three small conventions (concatenated sinusoids, a key projection without bias, pre-LN with a final norm) decide whether 37.8 million loaded weights compute anything meaningful. Get one wrong and the model loads, runs, and transcribes nothing.
Besides the sections above, the encoder uses conv_out_size from Convolution as a linear map and PCG32 from Modular arithmetic, hashing, and PCG32 in Python, both in Mathematics, plus gelu and transpose from The op library and gradcheck_all and Tensor from Tensor, broadcasting backward, and no_grad. It is also worth having Scaled dot-product attention in mind. The result is checked against Hugging Face’s tiny Whisper within . The solution is encoder.py, which defines WhisperConfig (with from_hf), whisper_sinusoids, WhisperAttention (no key bias), WhisperEncoderLayer and WhisperEncoder; the contract is encoder.pyi.
Learning objectives
- Derive why two width-3
Conv1dlayers, the second with stride 2, turn 3000 mel frames into exactly 1500 positions.- Compute Whisper’s sinusoid table by hand and explain how it differs from the interleaved table of the original Transformer.
- Explain why Whisper’s key projection can drop its bias while the query and value projections cannot.
- Describe the pre-LN block structure and why it needs a final LayerNorm.
- Explain why model parity uses the checkpoint’s stored position table rather than a freshly computed one.
The notation
Section titled “The notation”| Symbol | Meaning | Type / shape |
|---|---|---|
| log-mel input | float32[N, n_mels, 2A] | |
audio positions (n_audio_ctx, 1500) | int | |
| , | width and heads | int |
| sinusoid table | float32[A, d] | |
| hidden state after block | float32[N, A, d] |
The convolutional stem
Section titled “The convolutional stem”Speech features are local: a phone lasts tens of milliseconds. So before attention sees anything, two Conv1d layers with kernel 3 and padding 1 mix each frame with its neighbours:
With the output size formula , the first convolution keeps 3000 frames and the second gives . Each of the 1500 positions therefore covers 20 ms instead of 10. Halving the length quarters the cost of attention, which is the whole point of the stride. The GELU is the exact form , not the tanh approximation.
Sinusoidal positions, Whisper’s way
Section titled “Sinusoidal positions, Whisper’s way”After the stem, a fixed position table is added once. With channels and half , Whisper uses , frequencies for , and
Compare this with the sinusoidal table in Positional encodings and you find two differences. The sines and cosines are two blocks, not interleaved pairs. And the frequency ladder ends exactly at , because the step is divided by half , not by half. The table is fixed and never trained.
There is one numerical wrinkle. Computed in float32, an angle near 1499 rad carries the error of one ulp of exp, about after the multiplication, and different libraries’ float32 exp differ by exactly that ulp. A table computed here and one computed by PyTorch can therefore disagree at the level in the last rows. Checkpoints store the table they were trained with, so the loader in the next section checks the stored table against whisper_sinusoids (within ) and then uses the stored one.
Pre-LN blocks
Section titled “Pre-LN blocks”The rest of the encoder is a stack of blocks, each
and the encoder ends with one more LayerNorm. Because each branch normalizes only its own input, the residual stream itself is never normalized between blocks, and the final norm is what brings it to a sensible scale before the decoder reads it.
Attention has no mask: every position attends to all 1500. It is ordinary multi-head attention with , and output biases but no bias. The reason is a small piece of algebra worth remembering: a bias on keys adds the same amount to every score of a query, and the softmax cancels any constant shift, so Whisper drops it. Parameter names follow Hugging Face (self_attn.k_proj.weight, self_attn_layer_norm, fc1, fc2, final_layer_norm, layer_norm) so that a checkpoint loads by name.
Working it by hand
Section titled “Working it by hand”Take a table for channels. Then half , , and . Row is , so row 1 is and row 2 is . The interleaved table of the original Transformer would put second.
The stem’s lengths for a 30 s window: conv 1 maps 3000 frames to , and conv 2 maps them to , exactly the table’s rows. A 2998-frame input would give 1499 positions and is refused, as Hugging Face refuses it. On a small test model with , 16 frames give 8 positions.
Designing the API
Section titled “Designing the API”The configuration defaults to whisper-tiny’s sizes, and the encoder maps [N, n_mels, 2A] to [N, A, d]:
@dataclassclass WhisperConfig: # whisper-tiny's sizes by default n_mels=80; n_audio_ctx=1500; d=384; n_heads=6; enc_layers=4; dec_layers=4 n_text_ctx=448; vocab=51865; d_ff=1536 @classmethod def from_hf(cls, config_json: str) -> "WhisperConfig": ... def validate(self) -> None: ...def whisper_sinusoids(length, channels, max_timescale=10000.0) -> NDArray: ...class WhisperAttention(MultiHeadAttention): ... # k_proj without biasclass WhisperEncoderLayer(Module): ...class WhisperEncoder(Module): def forward(self, mel) -> Tensor: ... # [N, n_mels, 2A] -> [N, A, d]A correct encoder reproduces the hand table for 4 channels, and matches Hugging Face’s stored tables within for short tables and at 1500 rows (which is why the loader keeps the stored copy). The stem must turn frames into positions and refuse other lengths, mel counts and ranks, since only 30 s windows are valid. The full encoder must match Hugging Face’s tiny Whisper on a real clip within ; that is what the decoder’s logits and the later Rust encoder’s parity both rest on. It must also handle 128 mels and batches (a batch of two, items independent). Every parameter name and shape must match, with no k_proj.bias, so loading by name works. WhisperConfig.from_hf must read Hugging Face config keys and refuse variants that would load wrong. Two structural properties make good tests: with zeroed branches the output is layer_norm(stem + positions), which pins the pre-LN structure; and the last frame must reach the first output, which shows there is no mask.
Building it step by step
Section titled “Building it step by step”The reasoning chain:
- The checkpoint fixes the architecture. The goal is not a good speech encoder but the exact function Hugging Face’s
WhisperEncodercomputes with given weights. Every convention (layout of the table, which projections have biases, where the norms are) must match, and names must match Hugging Face’s so that a state dict loads by name with no mapping table. - Length is fixed by the table. The position table has rows and is added elementwise, so the stem must output exactly positions. By the output size formula, only frames give , so any other length is refused instead of padded or truncated silently.
- Positions are data, not parameters. The table is a deterministic function of . Computing it in the constructor is enough for random-init models, and the loader swaps in the checkpoint’s stored copy for parity.
- The key bias is redundant. A key bias adds to every score of query , the same for all keys, and softmax is invariant to adding a constant to all its inputs. So
k_projhas no bias, and loading must not expect one. - Pre-LN puts the norm inside the branch. The residual stream is never normalized between blocks; each branch reads and adds its output back. That leaves the stream unnormalized at the end, which is why a final LayerNorm is needed.
- The encoder hears the whole window. Speech recognition benefits from right context, and the window is processed once, so attention takes no mask.
The sinusoid table
Section titled “The sinusoid table”Angles are computed in float32 exactly as Whisper does; sin and cos are then evaluated in float64 and rounded once, so the only remaining error is the float32 exp ulp discussed above.
def whisper_sinusoids(length, channels, max_timescale=10000.0): if channels % 2 or channels < 4: # two equal halves; half - 1 must be > 0 raise ValueError(...) # ... reject length < 1 half = channels // 2 # half - 1, not half: the last frequency is exactly 1 / max_timescale. inc = np.float32(math.log(max_timescale) / (half - 1)) inv = np.exp(-inc * np.arange(half, dtype=np.float32)) t = (np.arange(length, dtype=np.float32)[:, None] * inv[None, :]).astype(np.float64) # Concatenated blocks [sin | cos], not the original Transformer's interleaved pairs. return np.concatenate([np.sin(t), np.cos(t)], axis=1).astype(np.float32)For : half = 2, inc = ln(10000), inv = (1, 1e-4), and row 1 is [sin 1, sin 1e-4, cos 1, cos 1e-4], the hand table. channels >= 4 is required because with half = 1 the step divides by zero.
Attention without a key bias
Section titled “Attention without a key bias”Subclassing MultiHeadAttention keeps the attention math in one place; only the key projection is replaced.
class WhisperAttention(MultiHeadAttention): def __init__(self, d, n_heads, rng=None): r = rng if rng is not None else PCG32(0).substream("init") super().__init__(d, n_heads, bias=True, rng=r) # q, v, out with biases # Re-assigning keeps k_proj's place in the registration order. self.k_proj = Linear(d, d, bias=False, rng=r)The block
Section titled “The block”Two pre-LN residual branches. Attention is called as self_attn(h, h) with no mask: queries and keys are the same normalized sequence, and every position sees every other.
class WhisperEncoderLayer(Module): def __init__(self, cfg, rng=None): # ... names are HF's, so a checkpoint loads by name self.self_attn = WhisperAttention(cfg.d, cfg.n_heads, rng=r) self.self_attn_layer_norm = LayerNorm(cfg.d) self.fc1 = Linear(cfg.d, cfg.d_ff, rng=r) self.fc2 = Linear(cfg.d_ff, cfg.d, rng=r) self.final_layer_norm = LayerNorm(cfg.d) # the MLP's pre-norm, despite the name
def forward(self, x): h = self.self_attn_layer_norm(x) x = x + self.self_attn(h, h) # unmasked self-attention h = self.final_layer_norm(x) return x + self.fc2(F.gelu(self.fc1(h))) # exact GELUNote the naming trap: Hugging Face’s final_layer_norm inside a layer is the MLP branch’s pre-norm, not the encoder’s final norm. The encoder’s final norm is the top-level layer_norm.
The encoder
Section titled “The encoder”Validate the shape, run the stem, add positions, run the blocks, and normalize once at the end.
def forward(self, mel): x = mel if isinstance(mel, Tensor) else Tensor(np.asarray(mel, dtype=np.float32)) cfg = self.cfg if x.ndim != 3 or x.shape[1] != cfg.n_mels: raise ValueError(...) frames = x.shape[2] a = conv_out_size(conv_out_size(frames, 3, 1, 1), 3, 2, 1) # for the message if frames != 2 * cfg.n_audio_ctx: # only 2A frames fill the A-row table raise ValueError(...) x = F.gelu(self.conv1(x)) # [N, d, 2A], k=3 pad=1 x = F.gelu(self.conv2(x)) # [N, d, A]; k=3 stride=2 pad=1 x = F.transpose(x, 1, 2) + self.positions # [N, A, d] for layer in self.layers: x = layer(x) return self.layer_norm(x) # pre-LN needs a final normThe config
Section titled “The config”WhisperConfig.from_hf reads Hugging Face’s key names (num_mel_bins, max_source_positions, d_model, …) and refuses variants this code would load wrongly: a non-whisper model_type, decoder heads or FFN size different from the encoder’s (the solution uses one value for both), an activation other than gelu, and scale_embedding. validate requires positive sizes, d divisible by n_heads, and an even d for the two halves of the table. The full solution is encoder.py.
Why this design
Section titled “Why this design”The stem costs . Each block costs for attention and for the MLP; the stride-2 convolution halves and therefore quarters the attention term.
The alternatives all fail in instructive ways. Reusing the interleaved table, or giving k_proj a bias “for symmetry”, produces a model that loads (or nearly loads) and outputs nonsense. A name-mapping layer between the solution’s names and Hugging Face’s would work, but it is one more place to get wrong; using Hugging Face’s names directly makes loading a dictionary lookup. Accepting any frame count and slicing the table would hide a misconfigured frontend.
Two tradeoffs are accepted. Only full 30 s windows are accepted, as in Whisper itself, and shorter audio is padded by the frontend. Encoder and decoder share head count and FFN width, which holds for all published Whisper sizes.
What can go wrong
Section titled “What can go wrong”The dangerous mistakes here are the ones that load cleanly. Interleaving sin and cos as in the original Transformer gives a model that loads fine and transcribes nothing; putting the cosines first does the same, more subtly. The solution concatenates [np.sin(t), np.cos(t)] with sines first, and divides the frequency step by half - 1 so that the last frequency really is . Forgetting to add the positions at all leaves a model that cannot tell when a sound happened.
Warning: A causal mask copied from the decoder means early frames never hear later ones. The encoder calls
self_attn(h, h)with no mask.
The remaining mistakes, and how the solution avoids each:
| Mistake | Symptom | Fix in the solution |
|---|---|---|
| second conv without stride 2 | 3000 positions against a 1500-row table | conv2 is Conv1d(d, d, 3, stride=2, pad=1) |
| tanh GELU in the stem | outputs off by about | F.gelu is the exact |
a bias on k_proj | strict loading fails on an unexpected key | Linear(d, d, bias=False) |
| post-LN blocks | normalized residual stream, wrong outputs | x + branch(LN(x)) |
| no final LayerNorm | unnormalized states reach the decoder | returns self.layer_norm(x) |
| accepting zero layers | an encoder that is only a stem | validate requires every size |
| accepting a shorter window | positions silently misaligned | requires exactly frames |
| accepting an odd width | sin and cos halves of unequal size | validate requires an even d |
Where this leads
Section titled “Where this leads”Whisper decoder and checkpoint weights composes this encoder into Whisper, runs it once per window, and hands its output to every decoder layer’s cross-attention. Later, the engine’s Rust encoder on candle is checked against these same outputs.
Looking back, the pieces came from across the book: Conv1d(n_mels, d, 3, pad=1) and Conv1d(d, d, 3, stride=2, pad=1) from the convolution layers, conv_out_size for the length check, MultiHeadAttention as the base class, Linear, LayerNorm, ModuleList and Module from the module system, the exact gelu and transpose from the op library, Tensor at the input, and PCG32(0).substream("init") when no rng is given.
Further reading
Section titled “Further reading”- Radford et al., “Robust speech recognition via large-scale weak supervision” (2022), section 2.2 and figure 1.
- Gulati et al., “Conformer” (2020), for what a speech encoder can add: convolution modules inside every block, not only at the input.
- Xiong et al., “On layer normalization in the transformer architecture” (2020), for pre-LN.
- Hugging Face’s
WhisperEncoderadds SDPA and flash attention, gradient checkpointing and a conv stem in fp16; seetransformers/models/whisper/modeling_whisper.py. whisper.cppimplements the same encoder in C with quantized weights and Metal or CUDA kernels; seeggerganov/whisper.cpp,whisper_build_graph_encoder.- Distil-Whisper and
faster-whisperadd batched chunks and CTranslate2 kernels; the 30 s window itself never changes. Seehuggingface/distil-whisper.
Key terms
Section titled “Key terms”- conv stem: the two width-3
Conv1dlayers with GELU that precede attention; the second’s stride 2 halves the sequence length. - concatenated sinusoids: Whisper’s position layout, all sines then all cosines, with frequencies .
- pre-LN block: a residual block whose branches normalize their input,
x + branch(LN(x)), leaving the stream itself unnormalized. - key bias redundancy: a bias on keys shifts every score of a query equally, so softmax cancels it.
Summary
Section titled “Summary”- The stem is two width-3
Conv1ds with exact GELU; the second has stride 2, so 3000 mel frames (10 ms each) become 1500 positions (20 ms each). - Whisper’s position table concatenates all the sines and then all the cosines, with frequencies ; the original Transformer’s table interleaves them.
- The blocks are pre-LN with unmasked self-attention and a final LayerNorm, and the key projection has no bias.
- A float32 table at angle 1499 rad is only good to about across libraries, so model parity uses the checkpoint’s stored table.
- Only exactly frames are accepted; anything else is a frontend bug made visible.
Review questions
Section titled “Review questions”-
Why can Whisper drop the key bias but not the query or value bias?
Answer
A key bias adds to all scores of a query equally, and softmax cancels a constant shift. A query bias adds , which differs per key, and a value bias changes the output vectors; neither cancels.
-
Why does the frequency step divide by
half - 1?Answer
So that the last frequency, , is exactly . Dividing by
half(the original Transformer’s convention) stops one step short, and every column but the first has a different frequency from the one the checkpoint was trained with. -
What would a zero-initialized attention and MLP branch produce, and why does that test the structure?
Answer
With both branches outputting zero, each block is the identity on the residual stream, so the output is
layer_norm(stem + positions). A post-LN block would normalize the stream at every layer and a missing final norm would leave it unnormalized, so either mistake changes this output. -
Why refuse 2998 frames instead of letting the stem produce 1499 positions?
Answer
The positions are added row by row from a 1500-row table, and the checkpoint was trained only on full windows. Accepting a shorter input would need a slice of the table, silently changing what each position means to the model; refusing makes a frontend bug visible.
-
Compute row 2 of the 4-channel table.
Answer
With , row 2 is .