Skip to content

12.2 BERT: the bidirectional encoder and masked language modeling

The GPT of the previous section reads left to right. That is exactly what generation needs and exactly what classification does not: to decide whether “not bad at all” is positive, every word should see every other word. A bidirectional encoder, though, cannot be trained by next-token prediction, because each position could simply read the answer. BERT’s fix is to hide some tokens and ask for them back.

Concepts from other sections (14)

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.

The system needs this encoder twice. ELECTRA trains a small BERT as its generator and a BERT-shaped discriminator, and the classification heads of fine-tuning heads and the linear-head export, including the usage-policy head the gateway evaluates, sit on top of it. Its tokenizer is the WordPiece tokenizer of Part 1.

This section builds on multi-head attention, learned positional encodings, the encoder of the encoder-decoder Transformer, and the Part 0 foundations (Tensor, ops, cross_entropy, layers, safetensors). From Mathematics it uses categorical sampling (sample_categorical), PCG32, and normal_init. The reference implementation is python/tinyllm/obj/bert.py (mlm_mask, special_ids, mlm_batch, BertConfig, BertLayer, BertEncoder, BertForMLM, hf_bert_encoder_sd, load_hf_bert, save_bert, load_bert), with the contract in course/contracts/py/tinyllm/obj/bert.pyi.

Learning objectives

  • Explain why a bidirectional encoder needs a corruption objective rather than next-token prediction.
  • Apply the 15% pick and 80/10/10 replacement rule by hand, draw for draw, in the fixed op order.
  • Derive the exact shares a picked position shows, including the 1/V1/V corrections.
  • Build BERT’s post-LN encoder with exact GELU, three summed embeddings, and a tied MLM head with its own bias.
  • Load a Hugging Face BERT checkpoint into either the MLM model or a bare encoder.

Here is the notation for this section.

SymbolMeaningType / shape
xtx_tinput id at position ttint
st∈{0,1}s_t \in \{0, 1\}special position ([CLS], [SEP], [PAD], …): never pickedbool
pppicking probability, 0.15float
uuone uniform draw from the PCG32float in [0,1)[0, 1)
VVvocabulary sizeint
yty_tlabel: the original id at picked positions, −100-100 elsewhereint
ttt\text{tt}_ttoken type (segment A = 0, B = 1)int
Wword,Wpos,WtypeW_{word}, W_{pos}, W_{type}the three embedding tablesfloat32[V, d], [L, d], [2, d]
Φ\Phithe standard normal CDF; exact GELU is u Φ(u)u\,\Phi(u)

The encoder sums three embeddings and normalizes them:

h0=LN(Wword[x]+Wpos[0..T−1]+Wtype[tt]),h_0 = \text{LN}(W_{word}[x] + W_{pos}[0..T-1] + W_{type}[\text{tt}]),

then applies LL post-LN layers: h←LN(h+Attn(h))h \leftarrow \text{LN}(h + \text{Attn}(h)), h←LN(h+W2 GELU(W1h))h \leftarrow \text{LN}(h + W_2\,\text{GELU}(W_1 h)). The activation is the exact GELU u Φ(u)u\,\Phi(u) (Hugging Face’s "gelu"), and LayerNorm uses ϵ=10−12\epsilon = 10^{-12}. The attention mask covers padding only, with shape [B, 1, 1, T]: every real position reads every other real position, to its left and to its right. Changing the last word changes the first position’s output, which a GPT can never do.

Because every position can see every other, the objective has to hide information. Masking visits positions in C order (row by row):

  1. A special position draws nothing; its label is −100-100.
  2. Otherwise draw uu. If u≥pu \ge p the position is not picked; its label is −100-100.
  3. Otherwise set the label yt=xty_t = x_t (the original) and draw the action aa = sample_categorical([0.8, 0.1, 0.1], u') by inverse CDF.
  4. If a=0a = 0, show [MASK]; if a=1a = 1, show rng.below(V), a uniform random id; if a=2a = 2, show xtx_t itself.

Why not always show [MASK]? Because [MASK] never appears when the encoder is fine-tuned or used, so a model trained only on [MASK] positions would learn that real tokens need no thought. The random 10% teaches it that any visible token may be wrong, and the kept 10% teaches it that a visible token may be right. The label is always the original token, also at kept positions, because those positions are predictions too. Draws happen only where the steps say, so the same seed gives the same batch in Python, Rust, and Go.

One subtlety affects how you test the rates. The random id is uniform over all VV ids, so it can be [MASK] or the original token by chance. The exact shares of what a picked position shows are therefore 0.8+0.1/V0.8 + 0.1/V for [MASK], 0.1(V−2)/V0.1 (V - 2)/V for another token, and 0.1+0.1/V0.1 + 0.1/V for itself. A statistical check of the rates must use these, not the nominal 80/10/10.

The corrupted batch goes through the encoder, and a small head turns each position back into a distribution over the vocabulary:

zt=LN(GELU(WTht+bT)) Wword⊤+bdec,L=1∣{t:yt≠−100}∣∑yt≠−100−log⁡softmax⁡(zt)yt.z_t = \text{LN}(\text{GELU}(W_T h_t + b_T))\,W_{word}^\top + b_{dec}, \qquad \mathcal{L} = \frac{1}{|\{t : y_t \ne -100\}|} \sum_{y_t \ne -100} -\log \operatorname{softmax}(z_t)_{y_t}.

The decoder weight is the word table (tied), and only its bias bdecb_{dec} is new. Positions that were not picked contribute no loss and no gradient, through cross_entropy’s ignore_index.

Loading BERT is easier than loading GPT-2. Hugging Face’s BERT uses nn.Linear, so there are no transposes (unlike GPT-2’s Conv1D). The keys are embeddings.{word,position,token_type}_embeddings, embeddings.LayerNorm, encoder.layer.i.attention.self.{query,key,value}, attention.output.dense, attention.output.LayerNorm, intermediate.dense, output.dense, output.LayerNorm, and for the head cls.predictions.transform.dense, transform.LayerNorm, and cls.predictions.bias. Hugging Face’s attention mask adds the most negative float instead of −∞-\infty; a padded key gets weight 0 either way.

The op order is easiest to internalize by running it. Take ids [CLS] the cat sat [SEP] [PAD] = (2,5,6,7,3,0)(2, 5, 6, 7, 3, 0), [MASK] = 4, V=40V = 40, p=0.15p = 0.15, and a generator that will return the uniforms 0.05,0.50,0.90,0.10,0.850.05, 0.50, 0.90, 0.10, 0.85 and then below(40) = 9. The table traces each position.

positiontokenspecial?drawspicked?actionshownlabel
0[CLS]yesnone2-100
1thenou=0.05<0.15u = 0.05 < 0.15; aa: 0.50<0.80.50 < 0.8yes[MASK]45
2catnou=0.90u = 0.90no6-100
3satnou=0.10u = 0.10; aa: 0.8≤0.85<0.90.8 \le 0.85 < 0.9; below = 9yesrandom97
4[SEP]yesnone3-100
5[PAD]yesnone0-100

The result is inputs (2,4,6,9,3,0)(2, 4, 6, 9, 3, 0) and labels (−100,5,−100,7,−100,−100)(-100, 5, -100, 7, -100, -100), using exactly five uniforms and one below.

def mlm_mask(ids, special_mask, mask_id: int, vocab: int, p: float, rng) -> tuple[NDArray, NDArray] # inputs, labels
def special_ids(tok: WordPieceTokenizer) -> list[int]
def mlm_batch(tok, texts, max_len: int, p: float, rng) -> dict[str, NDArray] # input_ids, token_type_ids, attention_mask, labels
@dataclass
class BertConfig: vocab: int; max_len: int = 512; d_model: int = 768; n_heads: int = 12; n_layers: int = 12
d_ff: int = 3072; type_vocab: int = 2; dropout: float = 0.1; ln_eps: float = 1e-12
class BertEncoder(Module): def forward(self, ids, token_type_ids=None, attn_mask=None) -> Tensor
class BertForMLM(Module): def forward(self, ids, token_type_ids=None, attn_mask=None, labels=None) -> (logits, loss)
def load_hf_bert(model, sd) -> None; def save_bert(model, dir); def load_bert(dir) -> BertForMLM

Masking is a pure function of ids, a special mask, and a generator; mlm_batch wraps it to go straight from text. The encoder and the MLM model are separate classes so that downstream tasks can start from a bare encoder.

The design follows from a chain of requirements.

  1. Classification wants every token to see every other, so the encoder attends both ways and masks only padding. Then next-token prediction is impossible (each position can read the answer), so the objective must hide information: corrupt some inputs and predict the originals.
  2. Only corrupted positions are informative, so only they are labelled; all others get −100-100 and drop out of the loss. Special positions ([CLS], [SEP], [PAD]) are never picked, because predicting them teaches nothing about language.
  3. If the corruption were always [MASK], the encoder could ignore every visible token, and [MASK] never appears downstream. Mixing in random tokens (10%) and the token itself (10%) forces the model to judge every visible token. The label is always the original, including at kept positions.
  4. Reproducibility across languages demands a fixed draw order: positions in C order, specials draw nothing, one uniform to pick, and only for picked positions one uniform for the action and, for a random action, one below(V). In the worked example that is exactly five uniforms and one below.
  5. The architecture is the 2017 encoder in post-LN with BERT’s details: exact GELU, three summed embeddings (word, position, type) followed by a LayerNorm, and ϵ=10−12\epsilon = 10^{-12}.
  6. The MLM head reuses the word table as its output matrix, so only a dense transform, a LayerNorm, and a bias are new. Hugging Face uses nn.Linear, so the loader is a pure rename with no transposes.

One loop over the flattened batch puts every draw exactly where the op order says:

MASK, RANDOM, KEEP = 0, 1, 2
SPLIT = np.array([0.8, 0.1, 0.1]) # what a picked position shows: [MASK], random, itself
def mlm_mask(ids, special_mask, mask_id, vocab, p, rng):
x = np.asarray(ids)
sp = np.asarray(special_mask, dtype=bool)
# ... dtype, shape, p and mask_id checks ...
inputs = x.astype(np.int64).copy() # never modify the caller's ids
labels = np.full(x.shape, -100, dtype=np.int64)
# reshape(-1) of a fresh C-contiguous array is a view: writing flat_* writes inputs/labels.
flat_in, flat_lab, flat_sp = inputs.reshape(-1), labels.reshape(-1), sp.reshape(-1)
for i in range(flat_in.size): # C order; a special position draws nothing
if flat_sp[i]:
continue
if rng.uniform() >= p: # draw 1: picked with probability p
continue
flat_lab[i] = flat_in[i] # the loss asks for the ORIGINAL token here
action = sample_categorical(SPLIT, rng.uniform()) # draw 2 (inverse CDF)
if action == MASK:
flat_in[i] = mask_id
elif action == RANDOM:
flat_in[i] = rng.below(vocab) # draw 3, only for RANDOM
# KEEP: the input shows the token itself, but it is still predicted.
return inputs, labels

The label is written before the input is overwritten, which is what makes it the original rather than what was shown.

mlm_batch tokenizes, truncates while keeping [SEP] last, pads, and marks as special every special id and every padded position:

special = np.isin(ids, special_ids(tok)) | ~attn # specials and padding never picked
vocab = max(tok.vocab.values()) + 1
inputs, labels = mlm_mask(ids, special, mask_id, vocab, p, rng)
class BertLayer(Module):
# ... attn, attn_norm, ff1, ff2, ff_norm; every Linear at N(0, 0.02^2) ...
def forward(self, x: Tensor, mask: NDArray) -> Tensor:
# Post-LN, as the 2017 transformer (norm="post"), exact GELU.
x = self.attn_norm(x + self.drop(self.attn(x, x, mask)))
return self.ff_norm(x + self.drop(self.ff2(F.gelu(self.ff1(x)))))
def forward(self, ids, token_type_ids=None, attn_mask=None):
# ... int [B, T] check ...
tt = np.zeros_like(ids) if token_type_ids is None else np.asarray(token_type_ids) # default: segment A
am = np.ones(ids.shape, dtype=bool) if attn_mask is None else np.asarray(attn_mask).astype(bool)
# ... shape checks ...
x = self.pos_emb(self.word_emb(ids)) + self.type_emb(tt) # word + position + type
x = self.drop(self.emb_norm(x))
# Every query may read every real key, left and right: no causal mask.
mask = am[:, None, None, :]
for layer in self.layers:
x = layer(x, mask)
return x

F.gelu without approximate is the exact u Φ(u)u\,\Phi(u). It differs from GPT-2’s tanh form by about 10−310^{-3}, which matters for parity with Hugging Face.

class BertForMLM(Module):
def __init__(self, cfg, rng=None):
# ...
self.bert = BertEncoder(cfg, r)
self.transform = Linear(cfg.d_model, cfg.d_model, rng=r)
# ... N(0, 0.02^2) init ...
self.transform_norm = LayerNorm(cfg.d_model, cfg.ln_eps)
# The decoder's weight IS word_emb.weight (tied); only its bias is new.
self.decoder_bias = Tensor(np.zeros(cfg.vocab, dtype=np.float32), requires_grad=True)
def forward(self, ids, token_type_ids=None, attn_mask=None, labels=None):
h = self.bert(ids, token_type_ids, attn_mask)
h = self.transform_norm(F.gelu(self.transform(h)))
logits = F.matmul(h, F.transpose(self.bert.word_emb.weight, 0, 1)) + self.decoder_bias
if labels is None:
return logits, None
# Only the picked positions carry a label; the rest are -100.
return logits, cross_entropy(logits, np.asarray(labels).astype(np.int64), ignore_index=-100)

Keeping the encoder under the attribute bert gives bert.-prefixed keys, so a bare BertEncoder (used by ELECTRA and the fine-tuning heads) and the MLM model share one key mapping.

hf_bert_encoder_sd is a table of renames, one per sublayer:

for i in range(n_layers):
h, m = f"encoder.layer.{i}.", f"layers.{i}."
# HF's BERT uses nn.Linear ([out, in]): no transpose, unlike GPT-2's Conv1D.
for ours, theirs in (
("attn.q_proj", "attention.self.query"),
("attn.k_proj", "attention.self.key"),
("attn.v_proj", "attention.self.value"),
("attn.out_proj", "attention.output.dense"),
("attn_norm", "attention.output.LayerNorm"),
("ff1", "intermediate.dense"),
("ff2", "output.dense"),
("ff_norm", "output.LayerNorm"),
):
out[f"{m}{ours}.weight"] = _hf(sd, f"{h}{theirs}.weight")
out[f"{m}{ours}.bias"] = _hf(sd, f"{h}{theirs}.bias")

load_hf_bert dispatches on the module type. A BertForMLM gets the encoder keys prefixed with bert. plus cls.predictions.transform.* and cls.predictions.bias; a BertEncoder gets the encoder keys alone, and the head keys in the checkpoint are simply not read; anything else is a TypeError. _hf accepts keys with or without Hugging Face’s bert. prefix. The full solution is in bert.py.

Masking costs O(BT)O(BT) with at most three draws per position. The encoder is O(L(T2d+Td dff))O(L(T^2 d + T d\, d_{ff})), and the head adds O(TVd)O(TVd), though only 15% of positions carry loss.

Masking is a Python loop rather than vectorized rng calls because vectorizing would draw an action uniform for every position, and the stream would no longer match Rust and Go. Determinism across languages is worth more than speed here, and masking is cheap next to the forward pass. Masking happens at batch time with a fresh rng per batch, not as a precomputed masked corpus, so each pass over the data sees new masks (RoBERTa’s dynamic masking) for free. The tied decoder matches Hugging Face’s checkpoints and halves the output parameters compared with a separate V×dV \times d matrix. The tradeoff accepted is that only 15% of positions produce a gradient signal; ELECTRA fixes that inefficiency.

A correct solution has to pass the following checks.

CaseKind of checkChecksWhy it matters downstream
the hand-worked maskunitthe worked batch, draw for draw, with a generator that fails on any extra draweveryone agrees on the op order
a kept token is still predictedunita kept token is still labelledthe 10% that teaches “visible may be right”
the op order on the real generatorgoldena [4, 32] batch equals the op order replayed on the frozen PCG32the same batch in every language
masking ratesstatisticalpicked share within 4 sd of pp; the exact shown shares by chi-squarethe objective’s proportions
agreement with Hugging Face BERTgoldena random tiny BertForMaskedLM: logits, MLM loss, and gradients through the loaderreal BERT checkpoints load (ELECTRA, fine-tuning heads)
one layer against the formuladifferentialone layer in float64 numpy with the exact GELU and post-LNthe activation and the norm placement
padding is never readpropertyids at padded positions change no real outputbatches of different lengths
reads both directionspropertythe last token changes the first positionbidirectional, unlike GPT
token types are addedpropertysegment B changes the output; default is Asentence-pair tasks
loss only at labelled positionsunitthe loss is the cross-entropy at the labelled positionno loss from visible tokens
the decoder is tied and has a biasunitparameter names and order; no decoder weightcheckpoint keys for ELECTRA and the zoo
batches from textunitWordPiece ids, padding, attention mask; specials never picked at p=1p = 1batches straight from text
a bare encoder from an MLM checkpoint; roundtripunitload_hf_bert into a BertEncoder; the model directory roundtripsclassifiers start from pretrained encoders
bad argumentsboundarybad ids, shapes, rates, mask id, module typeerrors, not silent garbage

The model tests in test_bert.py use two oracles: the op order replayed on a second PCG32 from the same seed, and one layer plus the head in float64 numpy with math.erf.

Many masking bugs leave the rates looking right while changing what the model learns or which batch it sees. The common ones are these.

PitfallSymptomHow the solution avoids it
special or padded positions pickedthe model learns to predict [CLS] and [PAD]mlm_mask skips special_mask positions before drawing; mlm_batch marks specials and padding
every position labelledthe loss is dominated by copying visible tokenslabels start at −100-100 and only picked positions are overwritten
random and keep swappedthe rates look right; the batch differs from every other implementationSPLIT = [0.8, 0.1, 0.1] with action order MASK, RANDOM, KEEP
the label is what was shown, not the originalthe model learns to predict [MASK]flat_lab[i] = flat_in[i] before the input changes
a second uniform drawn for every positiona different stream from the second position onthe action uniform is drawn only after a position is picked
GPT-2’s tanh GELU instead of the exact oneHF parity off by about 10−310^{-3}F.gelu with the default exact form
pre-LN instead of BERT’s post-LNa different model; HF disagreesBertLayer is norm(x + sublayer(x))
padding keys not maskedoutputs depend on the batch’s longest sentencemask = am[:, None, None, :] in every layer
a causal mask copied from GPTthe encoder is one-directionalthe mask is padding only
token types never addedsentence pairs look like one sentence+ self.type_emb(tt) before the embedding LayerNorm
the decoder bias left outHF disagreesdecoder_bias is added to the tied logits and loaded from cls.predictions.bias

Warning: Drawing the action uniform for every position, picked or not, is the easiest way to vectorize masking and the easiest way to break cross-language determinism. The rates still pass a statistical test; only a draw-for-draw golden comparison catches it.

ELECTRA uses a small BertForMLM as its generator and a BertEncoder as its discriminator, both masked with mlm_mask. The fine-tuning heads put sequence and token classification heads on BertEncoder, and the model zoo reports its classification rows.

In production, Hugging Face’s DataCollatorForLanguageModeling (in transformers/data/data_collator.py) masks at batch time and adds whole-word masking. RoBERTa replaced static masking with a new mask every epoch, and later work found higher rates, such as 40%, help large models. ModernBERT adds RoPE, GeGLU, alternating local and global attention, and an 8k context. And ELECTRA’s replaced-token detection, the next section, puts a loss on every position instead of 15%.

Further reading:

  • Devlin et al., “BERT” (NAACL 2019), section 3.1 and appendix A.1.
  • Liu et al., “RoBERTa” (2019), section 4.1 (dynamic masking).
  • Wettig et al. (2023), on 40% masking.
  • Warner et al. (2024), ModernBERT.
  • Clark et al. (2020), ELECTRA.
  • Masked language modeling (MLM): training an encoder to recover the original tokens at corrupted positions.
  • 80/10/10 rule: a picked position shows [MASK], a random token, or itself with probabilities 0.8, 0.1, 0.1.
  • Op order: the fixed sequence of random draws that makes masking reproducible across implementations.
  • Post-LN: normalization applied after the residual addition, LN(x+f(x))\text{LN}(x + f(x)).
  • Token type embedding: a learned vector added per segment (A or B) for sentence-pair inputs.
  • BERT keeps the 2017 encoder (post-LN, exact GELU) and reads in both directions; only padding is masked.
  • The objective is fill-in-the-blank: pick 15% of the non-special positions, show [MASK] at 80%, a random token at 10%, the token itself at 10%, and predict the original token at exactly the picked positions.
  • One fixed op order over one PCG32 stream makes masking reproducible in every implementation, and its rates are checked statistically against the exact shares.
  • The MLM head transforms, normalizes, and reuses the word table as its decoder, with its own bias.
  • Hugging Face’s BERT uses nn.Linear, so loading is a pure rename.
  1. Why must the label be written before the input is overwritten?
    AnswerThe label is the original token. Writing it after the [MASK] or random replacement would label the position with what was shown, and the model would learn to predict [MASK].
  2. Why does a picked position show [MASK] with probability 0.8+0.1/V0.8 + 0.1/V, not exactly 0.8?
    AnswerThe random action draws uniformly over all $V$ ids, which includes the [MASK] id with probability $1/V$. The same happens for the original token, so "itself" is shown with probability $0.1 + 0.1/V$.
  3. What changes in the random stream if the action uniform is drawn for every position, picked or not?
    AnswerEvery unpicked position consumes an extra draw, so from the second non-special position on, all later picks and actions come from different numbers. The rates still look right, but the batch differs from every other implementation of the op order.
  4. Why does mlm_batch truncate a long row as ids[: max_len - 1] + ids[-1:] instead of ids[:max_len]?
    Answer`encode(add_special=True)` ends every row with [SEP]. Plain slicing would cut it off, so long rows would end mid-sentence with no separator, unlike every row the model sees otherwise. Keeping the last id preserves the `[CLS] ... [SEP]` frame.
  5. Why does loading a BERT checkpoint need no transposes when GPT-2 needed them?
    AnswerHugging Face's BERT is written with `nn.Linear`, which stores weights as `[out, in]` like the solution's `Linear`. GPT-2 uses `Conv1D`, which stores `[in, out]`.