Zenaique

What does a padding mask zero out, and why is it needed when batching variable length sequences?

Flashcard·Easy·4.0 · 0·~30s·Asked atElevenlabsFreshworksSalesforce·Relevant atMicrosoft
Attempt it
TL;DR

A padding mask zeros out attention to pad tokens added during batching. It adds -inf to pre-softmax scores at pad columns so e^(-inf) = 0 contributes nothing after normalization.

Memory aid
Sign in to see the mnemonic that makes this stick.
Easy to grasp

Picture a teacher running a group quiz where every team must turn in an answer sheet with exactly 100 boxes filled. Some teams only have 30 real answers, so they fill the remaining 70 boxes with the word 'BLANK' to make the sheet look the same size as everyone else's. When the teacher grades by averaging answers, they need to know to skip every box marked BLANK; otherwise the average of every team is dragged toward 'BLANK' and nothing means anything. The padding mask is exactly the rule 'skip BLANK boxes'. Without it, attention happily averages in the pad tokens and the model's output becomes garbage.

Concept explanation~2 min read

Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.

The padding mask is the simplest attention mask conceptually but it has more failure modes in practice than people expect. The concept is one sentence: zero out attention to fake batching tokens. The mechanics involve a specific sequence of operations (additive -inf, softmax, weighted sum) that must compose correctly, and several common bugs (wrong axis, wrong polarity, forgotten cross-attention) recur across implementations.

This deep dive walks the why (variable-length batching), the how (the additive -inf trick), the orthogonality with the causal mask, the modern production replacements (cu_seqlens and continuous batching), and the common bugs that arise when teams roll their own padding-mask implementation.

Mental model: padding mask blocks fake tokens; causal mask blocks future tokens. They are orthogonal and both can apply at once.

Why padding exists and what goes wrong without a mask

GPUs operate on rectangular tensors. A batch of training examples with varying real lengths (12, 47, 8, 33) must be reshaped into one (batch, max_length) tensor before it can be fed through the model. Short examples get padded with a special token (typically [PAD] or token ID 0) up to the batch's max length.

What goes wrong if attention ignores padding

The pad tokens are embedded just like any other token. They have learned (or initialized to zero) embedding vectors that are projected into Q, K, V vectors by the same weight matrices. If attention treats them as legitimate keys:

  • Every real query computes its softmax over all positions including pad.
  • Pad columns get non-trivial softmax weight, especially when pad embeddings happen to project to high-norm K vectors.
  • The attention output is a weighted sum over both real and pad values, contaminated.

The contamination's effect on training depends on what the pad embedding evolves into. If the model is somehow incentivized to use pad tokens as a constant baseline (a kind of attention sink), the bug is silent and the model learns around it. If the pad embedding accumulates gradient signal that pushes it into a degenerate state, training stalls. Both outcomes are bad.

Why aggregate loss is misleading here

As with the packed-sequence bug, aggregate metrics like perplexity may only drop slightly because pad-token positions are a small fraction of the batch. Per-token analysis reveals the regression at real positions whose attention computation is being contaminated.

The fix is one line of code at the attention layer. The bug is one of the easiest to introduce and one of the easiest to miss.

The additive -inf trick
Orthogonality with the causal mask
Modern production replacements and common bugs
Sign in to unlock the full deep dive.

Situations where this technique stops working.

Sign in to see when this approach fails.

2–4 min · Everything important, quickly.

Sign in to see the quick scan of the deep dive.
python
import torch
import torch.nn.functional as F

B, T, n_heads, d_head = 2, 8, 4, 16
# Sequence 0 has 5 real tokens, sequence 1 has 8 real tokens.
real_lengths = torch.tensor([5, 8])

Q = torch.randn(B, n_heads, T, d_head)
K = torch.randn(B, n_heads, T, d_head)
V = torch.randn(B, n_heads, T, d_head)

# Build padding mask: True at real positions, False at pad.
idx = torch.arange(T)[None, :]  # (1, T)
is_real = idx < real_lengths[:, None]  # (B, T)

# Convert to additive: 0 at real, -inf at pad.
additive = torch.where(is_real, 0.0, float('-inf'))  # (B, T)
additive = additive[:, None, None, :]  # (B, 1, 1, T) -- broadcast over heads and queries

scores = Q @ K.transpose(-2, -1) / (d_head ** 0.5)  # (B, n_heads, T, T)
scores = scores + additive  # pad columns become -inf
weights = F.softmax(scores, dim=-1)  # pad columns now exactly 0
output = weights @ V  # (B, n_heads, T, d_head), no contribution from pad keys

Real products, models, and research that use this idea.

  • PyTorch's torch.nn.MultiheadAttention takes a key_padding_mask of shape (B, T) where True means 'this is padding'.
  • Hugging Face Transformers uses attention_mask of shape (B, T) where 1 means 'real token' (opposite polarity).
Sign in to see more production examples.

What an interviewer would ask next. Try answering before peeking at the approach.

QWhy the additive -inf trick instead of multiplying the weights by zero after softmax?
A

If you zeroed weights after softmax, the surviving non-pad weights would still sum to less than 1 (because some weight mass was on pad), and the output magnitude would be biased low. The additive -inf before softmax pushes pad probabilities to exactly 0 while the real probabilities renormalize to sum to 1 cleanly.

2 more follow-ups an interviewer would ask next. Sign in to reveal them.

Red flags & common mistakes

The phrases that signal junior thinking. Click to expand.

Most common mistake

Confusing padding mask with causal mask. Padding hides fake batching tokens; causal hides real future tokens. Both can coexist in the same attention call.

Sign in to see all red flags and common mistakes.

60 second bullets to scan on the way to the call.

  • What a padding mask is and why it is needed for variable-length batches

  • The additive -inf trick before softmax versus multiplicative zero after

Sign in to unlock the revision sheet.

Primary sources. Browse if you want the original framing.

Similar questions

Same topic, related formats. Practice these next.

4 curated
Next question
Explain scaled dot product attention.
Short answer·Medium