What an encoder-only transformer is
A decoder such as GPT-2 reads text left to right so that it can generate the next token. An encoder reads the whole input at once: every token attends to the tokens on both sides of it, and the output is one context-dependent vector per token. Those vectors serve classification, retrieval and tagging, and are trained by asking the model to fill in hidden words.
This page records ModernBERT-base (22 blocks of width 768, 12 heads of 64, 149,655,232 parameters) filling a masked word in two sentences. In The capital of France is [MASK]. the clue comes before the mask, and the model gives Paris 92.33 %. In The [MASK] barked at the mailman. the clue comes after it, and the model gives dog 80.14 %. A left-to-right model would have to guess the second word before seeing barked.
Masked language modelling
During pre-training a share of the input tokens is replaced by [MASK], and the model is trained to predict the originals from everything that is left. With the masked positions and the visible tokens, the loss is
Only the masked positions are scored, which is why the page sends just the [MASK] row to the output. The MLM head on that row is a dense layer 768 → 768, a GELU and a LayerNorm, followed by a decoder tied to the token embeddings plus a bias, which gives a logit for each of the 50,368 vocabulary entries. Masked-token prediction shows the resulting probabilities.
Bidirectional self-attention
Attention in an encoder has no causal mask. Query scores every key , before or after it:
where and are rotated by RoPE and is 0 for every pair the block may use. The Bidirectional Self Attention matrix therefore goes from dot product to scaling to softmax with no masking stage, and its upper triangle is filled. On the [MASK] row of the second sentence, the tokens of barked lie to the right of the query, and they can still take weight.
Local and global layers
Full attention costs time and memory that grow with the square of the length. ModernBERT makes only every third block global, starting with block 1: blocks 1, 4, 7, …, 22 attend over the whole input with a RoPE base of 160,000. The other 14 blocks are local: each token sees a sliding window of 128 positions, 64 on each side, when , with a RoPE base of 10,000. Local blocks cost time linear in the length; the global ones carry information across the whole input.
Both sentences on this page are shorter than the window, so the sliding mask is all zeros and local blocks see every token too; on the first sentence the chip reads Sliding window 128 · covers all 9 tokens. What differs between the two kinds of block here is the RoPE base and the weights they learned.
Embedding, normalisation and the GeGLU MLP
The embedding is a row of a 50,368 × 768 table followed by a LayerNorm; no position vector is added, since RoPE rotates queries and keys inside attention. Each block is pre-norm, and , with LayerNorms that have a scale but no shift. Block 1 skips its attention LayerNorm, because the embedding LayerNorm has just normalised its input. No projection has a bias.
The MLP is a GeGLU: one matrix maps 768 to two halves of 1152, and the GELU of the first gates the second:
From BERT to ModernBERT
BERT (Devlin et al., 2018) pre-trained a bidirectional transformer encoder with masked language modelling and next-sentence prediction, and set the pattern of pre-training once and fine-tuning for each task. RoBERTa (Liu et al., 2019) found BERT undertrained, dropped next-sentence prediction and trained longer on more data. Longformer (Beltagy et al., 2020) mixed sliding-window and global attention to read long documents.
ModernBERT (Warner et al., 2024) brings the changes of recent decoders to the encoder: RoPE, GeGLU, pre-norm blocks without biases, alternating local and global attention, and a native length of 8,192 tokens, trained on 2 trillion tokens. The LG tab alternates local and global attention in a decoder (Gemma 3), and the Diffusion LM tab uses masked prediction to generate text.
Papers
- Smarter, Better, Faster, Longer: A Modern Bidirectional Encoder for Fast, Memory Efficient, and Long Context Finetuning and Inference ModernBERT, the model this page records: RoPE, GeGLU, bias-free pre-norm blocks and global attention every third block.
- BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding The encoder-only transformer pre-trained by masked language modelling, the design ModernBERT updates.
- RoBERTa: A Robustly Optimized BERT Pretraining Approach Shows that BERT was undertrained: masked language modelling alone, trained longer on more data, does better.
- Longformer: The Long-Document Transformer Sliding-window attention combined with global attention in an encoder, so the cost grows linearly with the length.
- GLU Variants Improve Transformer Gated feed-forward layers, among them GeGLU, the MLP of every ModernBERT block.
- RoFormer: Enhanced Transformer with Rotary Position Embedding Rotary position embedding, which rotates queries and keys so that their dot product depends on relative position.