Lesson 14 of 23 · LLM preliminaries capstone

Place every component on one model map.

Your win: follow token IDs to logits, separate architecture from training stages, and explain how pretraining, supervised fine-tuning, and generation use the same decoder.

20 minutesNeeds: Lessons 10–13Outcome: enter Module 3 with context
Module bridge: the next nine lessons open the purple “Transformer blocks” stage below and explain every operation inside one block.
Decoder-only language model architecture from token IDs through embeddings, repeated Transformer blocks, final RMSNorm, language-model head, and vocabulary logits
The residual stream keeps shape \([B,T,D]\) through the block stack. The language-model head changes the final axis from model width \(D\) to vocabulary size \(V\).

Keep a five-line shape ledger

StageShapeWhat changes
Token IDs\([B,T]\)Discrete vocabulary indices
Token embeddings\([B,T,D]\)Add one learned feature vector per token
\(N\) decoder blocks\([B,T,D]\)Build contextual representations while preserving the residual shape
Final normalization\([B,T,D]\)Rescale each token's features
LM head logits\([B,T,V]\)Score every vocabulary token at every position

One architecture supports several training stages

1 · Pretraining

Learn broad language patterns from next-token prediction over large text corpora.

Objective: predict the continuation.

2 · Supervised fine-tuning

Continue next-token training on structured prompt-response demonstrations.

Objective: imitate useful responses.

3 · Preference alignment

Use preference data or rewards to favor responses judged more helpful and safe.

Objective: improve behavioral choices.

The model still produces next-token logits in every stage. What changes is the data, loss construction, or optimization procedure.

Optional implementation view · how chat messages become supervised tensors
Workflow converting system, user, and assistant messages into token IDs and completion-only training targets
Completion-only SFT commonly masks system and user targets, so only assistant response tokens contribute to the loss.
Optional implementation view · long conversations become training windows
Sliding-window construction for long supervised fine-tuning conversations with assistant-only loss masks
Overlapping windows fit long conversations into a fixed context length while retaining assistant targets.

Training and generation reuse the forward pass

Training

Forward all positions, compare \([B,T,V]\) logits with shifted targets, backpropagate the loss, update weights.

Generation

Forward the visible context, read final-position logits, sample one ID, append it, repeat without updating weights.

Code checkpoint · the full model in five operations

This is the outer structure completed by the code checkpoints in Module 3. Lesson 23 assembles the pieces and then maps the readable block toward MiniMind's production contract.

Show the model-level forward pass
def forward(self, token_ids):
    x = self.token_embedding(token_ids)  # [B, T] -> [B, T, D]

    for block in self.layers:
        x = block(x)                     # [B, T, D] -> [B, T, D]

    x = self.final_norm(x)               # [B, T, D]
    logits = self.lm_head(x)             # [B, T, D] -> [B, T, V]
    return logits

Trace it: Which operation changes the final axis from \(D\) to \(V\)?

Retrieval check

Where do next-token vocabulary logits first appear?

Practice before moving on

  1. Write the shape chain for \(B=2,T=128,D=512,V=32{,}000\).
  2. Which two axes are preserved from token IDs through logits?
  3. Why must every decoder block return width \(D\)?
  4. Compare the data used for pretraining and supervised fine-tuning.
  5. Name the four operations used only during training after the forward pass begins.
  6. Teach the complete path from a prompt string to one sampled continuation token.
Check solutions
  1. \([2,128]\to[2,128,512]\to[2,128,512]\to[2,128,512]\to[2,128,32000]\).
  2. Batch \(B\) and token position \(T\).
  3. Residual additions and the next block expect the shared residual-stream width.
  4. Pretraining uses broad text continuation data; SFT uses curated prompt-response demonstrations, often with loss focused on assistant tokens.
  5. Target comparison, scalar loss, backward/gradient computation, and optimizer update.
  6. Tokenize → embed → decoder blocks → final norm → LM head → final-position logits → filtering/softmax → sample and append.
Ready for Module 3: you now know what enters a block, why it is trained, and where its output goes. Next, keep a shape ledger while opening the block itself.