<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>How Encoder LLMs Work on Ship of Theseus</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/</link><description>Recent content in How Encoder LLMs Work on Ship of Theseus</description><generator>Hugo -- 0.152.2</generator><language>en-us</language><lastBuildDate>Sun, 23 Aug 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://www.fanfani.net/tutorials/encoder-deep-dive/index.xml" rel="self" type="application/rss+xml"/><item><title>The Big Picture: Encoder vs. Decoder</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-01-the-big-picture/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-01-the-big-picture/</guid><description>&lt;p&gt;Lesson 1 — The Big Picture: Encoder vs. Decoder A 10-lesson technical course on the mechanics of encoder language models. Audience: comfortable with decoder-style LLMs at a high level; new to the specifics of encoders. Prerequisites: linear algebra (matrix products), multivariate calculus (gradients, the chain rule), and basic probability (distributions, expectation). We use standard notation throughout and define it as we go.
Course table of contents # Lesson Core technical content 1 The Big Picture (this file) The object $g(x;\theta):\mathcal V^{n}\to\mathbb R^{n\times d}$; bidirectional context; why there is no autoregressive factorization; the running decoder contrast 2 Tokenization &amp;amp; the Encoder Input WordPiece; [CLS]/[SEP]/[MASK]; token + segment + position embeddings; learned-absolute positions 3 Bidirectional Self-Attention $\mathrm{softmax}(QK^\top/\sqrt{d_k})V$ with no causal mask; the full $n\times n$ matrix; no KV cache; padding masks 4 The Encoder Block &amp;amp; Stack Post-LN vs. pre-LN; the residual stream; contextual embeddings; how [CLS] summarizes a sequence 5 Masked Language Modeling The MLM loss; 80/10/10 corruption; why bidirectionality demands denoising; NSP; masked cross-entropy 6 From Representations to Predictions MLM head + weight tying; [CLS]/mean pooling; token- vs. sequence-level heads 7 Evaluating an Encoder Masked cross-entropy; why perplexity fails; pseudo-perplexity; GLUE/SuperGLUE 8 Pretraining Dynamics AdamW engine; the 15% signal-density problem; sequence packing; the RoBERTa recipe 9 Fine-Tuning Downstream Classification, NER, span QA, sentence pairs; task heads; full FT vs. adapters 10 Practice &amp;amp; Landscape Sentence-BERT; retrieval/RAG; RoBERTa/DeBERTa/ELECTRA/ModernBERT; encoder–decoder; which to choose A Appendix A — Encoder Family BERT → ModernBERT, organized by what each model changed B Appendix B — Encoder vs. Decoder Cheat Sheet Every contrast in the course, in one decision table C Appendix C — Acronyms Every acronym and symbol, expanded in plain language Two invariants run through every lesson and are worth stating before anything else:&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-01-the-big-picture/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Tokenization and the Encoder Input</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-02-tokenization-and-embeddings/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-02-tokenization-and-embeddings/</guid><description>&lt;p&gt;Lesson 2 — Tokenization &amp;amp; the Encoder Input Technical course on encoder LLM mechanics — Lesson 2 of 10.
Recap of Lesson 1 An encoder is a function $g(x;\theta):\mathcal V^{n}\to\mathbb R^{n\times d}$ that maps a token sequence to a matrix of contextual representations, each row informed by the whole input bidirectionally. It has no autoregressive factorization, so it is trained by masked reconstruction rather than next-token prediction (Lesson 5), it produces representations rather than a next-token distribution (so it needs task heads, Lesson 6), and it runs in a single parallel forward pass with no KV cache. We keep the [LEARNED]/[CONFIG]/[STATE] tagging throughout.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-02-tokenization-and-embeddings/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Bidirectional Self-Attention</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-03-bidirectional-self-attention/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-03-bidirectional-self-attention/</guid><description>&lt;p&gt;Lesson 3 — Bidirectional Self-Attention Technical course on encoder LLM mechanics — Lesson 3 of 10.
Recap of Lesson 2 The encoder&amp;rsquo;s input tensor $H^{(0)}\in\mathbb R^{n\times d}$ is the sum of three learned embeddings — token (WordPiece, $V=30{,}522$), segment ($s_i\in\{0,1\}$), and learned-absolute position (capped at $n_{\max}=512$) — with the sequence framed by [CLS], [SEP], [MASK], and [PAD]. Each row knows what token it is and where it sits, but nothing about the other tokens. This lesson derives the operator that fixes that, and shows that the only thing distinguishing encoder attention from decoder attention is the removal of one mask.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-03-bidirectional-self-attention/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>The Encoder Block and Stack</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-04-the-encoder-block-and-stack/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-04-the-encoder-block-and-stack/</guid><description>&lt;p&gt;Lesson 4 — The Encoder Block &amp;amp; Stack Technical course on encoder LLM mechanics — Lesson 4 of 10.
Recap of Lesson 3 Bidirectional self-attention is scaled dot-product attention with the causal mask deleted ($M_{ij}=0$ over real tokens), so the attention matrix is full and every token&amp;rsquo;s vector depends on the whole sequence. This makes the model an encoder, makes next-token training impossible, and removes the KV cache and any ability to generate; the only surviving mask is the padding mask. We now wrap this attention operator into a full block and stack $N$ of them, and ask what the resulting representations are.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-04-the-encoder-block-and-stack/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Masked Language Modeling</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-05-masked-language-modeling/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-05-masked-language-modeling/</guid><description>&lt;p&gt;Lesson 5 — Masked Language Modeling Technical course on encoder LLM mechanics — Lesson 5 of 10.
Recap of Lesson 4 Stacking $N$ bidirectional blocks turns the input $H^{(0)}$ into $H^{(N)}\in\mathbb R^{n\times d}$, a contextual vector per token plus a [CLS] summary register — the encoder&amp;rsquo;s entire output, with no predictions attached. But why does the stack learn useful representations? A randomly initialized encoder produces garbage; the representations become meaningful only because pretraining pushes a learning signal through every weight. This lesson derives that signal. It is the single biggest departure from the decoder, and Lesson 3 already told us why one is needed: bidirectionality kills next-token prediction.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-05-masked-language-modeling/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>From Representations to Predictions</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-06-representations-to-predictions/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-06-representations-to-predictions/</guid><description>&lt;p&gt;Lesson 6 — From Representations to Predictions Technical course on encoder LLM mechanics — Lesson 6 of 10.
Recap of Lesson 5 An encoder is pretrained by masked language modeling: corrupt ~15% of tokens (the 80/10/10 recipe), run the bidirectional stack, and reconstruct the originals with a cross-entropy loss over masked positions only. NSP was an optional second objective, later dropped. But Lesson 5 referred to an &amp;ldquo;MLM head&amp;rdquo; that turns a top-layer vector into a vocabulary distribution, and Lesson 4 left [CLS] as a summary register with nothing attached. This lesson builds the heads: the small networks that convert the encoder&amp;rsquo;s $H^{(N)}\in\mathbb R^{n\times d}$ into actual predictions — and shows how a decoder&amp;rsquo;s single head becomes an encoder&amp;rsquo;s family of heads.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-06-representations-to-predictions/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Loss and Evaluation</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-07-loss-and-evaluation/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-07-loss-and-evaluation/</guid><description>&lt;p&gt;Lesson 7 — Evaluating an Encoder Technical course on encoder LLM mechanics — Lesson 7 of 10.
Recap of Lesson 6 The encoder&amp;rsquo;s representations $H^{(N)}$ become predictions through heads: the MLM head (tied vocabulary projection, pretraining only), pooling + a sequence classifier, a per-token classifier, and a span head. The decoder routes every task through one generative head; the encoder swaps in typed heads. We now ask how to measure an encoder. The decoder has a clean intrinsic yardstick — perplexity — that an encoder cannot use, which forces a different evaluation philosophy: encoders are judged mostly by what they enable downstream.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-07-loss-and-evaluation/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Pretraining Dynamics</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-08-pretraining-dynamics/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-08-pretraining-dynamics/</guid><description>&lt;p&gt;Lesson 8 — Pretraining Dynamics Technical course on encoder LLM mechanics — Lesson 8 of 10.
Recap of Lesson 7 An encoder has no perplexity (no autoregressive joint), so it is judged by held-out MLM loss, pseudo-perplexity as a weak intrinsic stand-in, and — decisively — downstream GLUE/SQuAD/MTEB scores after fine-tuning. To get a model worth evaluating, we must first pretrain it. This lesson covers how MLM pretraining is actually run, reusing the decoder course&amp;rsquo;s optimization engine wholesale and focusing on the few things that genuinely differ — most importantly the consequences of the 15% signal-density problem from Lesson 5.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-08-pretraining-dynamics/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Fine-Tuning Downstream</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-09-finetuning-downstream/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-09-finetuning-downstream/</guid><description>&lt;p&gt;Lesson 9 — Fine-Tuning Encoders for Downstream Tasks Technical course on encoder LLM mechanics — Lesson 9 of 10.
Recap of Lesson 8 MLM pretraining reuses the decoder&amp;rsquo;s AdamW engine; its distinctive cost is signal density — only the masked ~15% of positions yield loss — answered by more data (RoBERTa), dynamic masking, packing, and large batches. Pretraining produces a model whose representations are good but generic: it can reconstruct masked tokens, but it does not yet do your task. This lesson turns the pretrained encoder into a working classifier/tagger/extractor by attaching and training the heads of Lesson 6 — and contrasts this with how a decoder is adapted (SFT, RLHF, or just prompting).&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-09-finetuning-downstream/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Practice and Landscape</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-10-practice-and-landscape/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-10-practice-and-landscape/</guid><description>&lt;p&gt;Lesson 10 — Encoders in Practice &amp;amp; the Modern Landscape Technical course on encoder LLM mechanics — Lesson 10 of 10.
Recap of Lesson 9 Fine-tuning an encoder means discarding the MLM head and attaching a typed task head (pooled classifier, per-token classifier, or span head), then training all of $\theta$ on labeled data — the clean transfer-learning move that contrasts with the decoder&amp;rsquo;s generation-steering (SFT/RLHF/prompting). We have now built the encoder bottom to top. This final lesson zooms out: where encoders genuinely win in production, how the family evolved from BERT to ModernBERT, the encoder–decoder hybrids, and the decision of which to use.&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/lesson-10-practice-and-landscape/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Appendix A — A Field Guide to the Encoder Family</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-a-encoder-family/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-a-encoder-family/</guid><description>&lt;p&gt;Appendix A — A Field Guide to the Encoder Family Technical course on encoder LLM mechanics — Appendix to Lessons 5 and 8.
Where this fits This is a side-quest off Lessons 5 (the MLM objective) and 8 (pretraining dynamics). The names that get thrown around — RoBERTa, ALBERT, ELECTRA, DeBERTa, ModernBERT — are all answers to a limitation that a specific lesson identified. Rather than catalogue them flatly, this appendix organizes the family by what each model changed, along four axes that the lessons already gave us: the objective (Lesson 5), the masking/training (Lesson 8), the position scheme (Lesson 2), and the parameter budget (Lessons 2, 4). Read any row as &amp;ldquo;BERT, but with this fixed.&amp;rdquo;&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-a-encoder-family/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Appendix B — Encoder vs. Decoder</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-b-encoder-vs-decoder/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-b-encoder-vs-decoder/</guid><description>&lt;p&gt;Appendix B — The Encoder vs. Decoder Cheat Sheet Technical course on encoder LLM mechanics — Appendix to all lessons.
Where this fits This appendix collects every contrast drawn across the ten lessons into one place, organized by where in the stack the difference lives. Use it as a lookup table when you need the short answer, with a pointer back to the lesson that derives it. The single root cause to keep in mind: a decoder is causal and generates the next token; an encoder is bidirectional and emits representations. Every row below is a consequence of that one difference (Lesson 1).&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-b-encoder-vs-decoder/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item><item><title>Appendix C — Acronyms</title><link>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-c-acronyms/</link><pubDate>Mon, 01 Jan 0001 00:00:00 +0000</pubDate><guid>https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-c-acronyms/</guid><description>&lt;p&gt;Appendix C — Acronyms &amp;amp; Symbols Technical course on encoder LLM mechanics — reference appendix.
Where this fits A lookup table to keep open alongside the lessons rather than read front to back. It expands every acronym and explains every recurring symbol in plain language, with the lesson where each is introduced.
Acronyms Acronym Expansion Plain meaning Lesson LLM Large Language Model a large neural network over text 1 BERT Bidirectional Encoder Representations from Transformers the original encoder model 1 MLM Masked Language Modeling predict hidden tokens from both sides 5 NSP Next-Sentence Prediction is sentence B the real continuation of A? (dropped) 5 SOP Sentence-Order Prediction are these two sentences in the right order? (ALBERT) 5, A RTD Replaced-Token Detection is each token original or a fake? (ELECTRA) 8, A CLS &amp;ldquo;classification&amp;rdquo; token the position-0 read-out register 2, 4 SEP &amp;ldquo;separator&amp;rdquo; token marks segment boundaries 2 AR Autoregressive the decoder&amp;rsquo;s left-to-right factorization 1, 5 BPE Byte-Pair Encoding the decoder&amp;rsquo;s usual subword tokenizer 2 WordPiece — BERT&amp;rsquo;s likelihood-based subword tokenizer 2 LayerNorm / LN Layer Normalization per-vector normalization in each block 4 post-LN / pre-LN — LayerNorm after vs. inside the residual 4 MLP / FFN Multi-Layer Perceptron / Feed-Forward Network the per-token expansion sub-layer 4 GELU / GeGLU Gaussian Error Linear Unit / Gated GELU block nonlinearities 4, A RoPE Rotary Position Embedding relative positions inside the dot product 2, A KV cache Key/Value cache decoder-only generation cache (encoders lack it) 1, 3 PPL / PPPL Perplexity / Pseudo-Perplexity decoder metric / encoder stand-in 7 GLUE / SuperGLUE General Language Understanding Evaluation the standard understanding benchmark suites 7 SQuAD Stanford Question Answering Dataset extractive-QA benchmark 7 NER / POS Named-Entity Recognition / Part-of-Speech token-labeling tasks 6, 9 NLI Natural Language Inference entailment/contradiction/neutral 7, 9 STS / MTEB Semantic Textual Similarity / Massive Text Embedding Benchmark embedding-quality benchmarks 7 SBERT Sentence-BERT bi-encoder for sentence embeddings 10 RAG Retrieval-Augmented Generation encoder retrieves, decoder generates 10 SFT / RLHF / DPO Supervised Fine-Tuning / Reinforcement Learning from Human Feedback / Direct Preference Optimization decoder alignment methods 9 LoRA Low-Rank Adaptation parameter-efficient fine-tuning 9 T5 / BART Text-to-Text Transfer Transformer / Bidirectional and Auto-Regressive Transformers encoder–decoder models 10 AdamW Adam with decoupled Weight decay the standard optimizer 8 Symbols Symbol Meaning Lesson $g(x;\theta)$ the encoder function: tokens → contextual vectors 1 $f(x;\theta)$ the decoder function: tokens → next-token distribution 1 $\theta$ the learned parameter vector ($P$ of them) 1 $x=(x_1,\dots,x_n)$ input token sequence 1 $\tilde x$ the corrupted (masked) sequence 5 $\mathcal M$ set of masked positions ($\approx 0.15\,n$) 5 $H^{(\ell)}\in\mathbb R^{n\times d}$ the residual stream / hidden states at layer $\ell$ 2, 4 $H^{(N)}$ top-layer contextual representations (the output) 4 $E_{\text{tok}},E_{\text{seg}},E_{\text{pos}}$ token, segment, position embedding matrices 2 $W_Q,W_K,W_V,W_O$ attention projection matrices 3 $A\in\mathbb R^{n\times n}$ the attention matrix (full for encoders) 3 $M$ attention mask ($0$ everywhere for encoder self-attention; $-\infty$ for padding/causal) 3 $W_U$ output (unembedding) projection, often tied to $E_{\text{tok}}$ 6 $\mathbf c$ pooled [CLS] (or mean) vector 6 $V$ vocabulary size ($30{,}522$ for BERT) 2 $d$ model width ($768$ base, $1024$ large) 2, 4 $N$ number of layers ($12$ base, $24$ large) 4 $n,\ n_{\max}$ sequence length; max length ($512$) 2, 3 $\Delta^{V-1}$ probability simplex over the vocabulary 1 $\mathcal L_{\text{MLM}}$ masked-language-modeling loss 5 ← Course index · reference appendix&lt;/p&gt;&lt;p&gt;&lt;a href="https://www.fanfani.net/tutorials/encoder-deep-dive/appendix-c-acronyms/"&gt;Read the full post →&lt;/a&gt;&lt;/p&gt;</description></item></channel></rss>