LeetLLM
My PlanLearnGlossaryTracksPracticeBlog
LeetLLM

Your go-to resource for mastering AI & LLM systems.

Product

  • Learn
  • Glossary
  • Tracks
  • Practice
  • Blog
  • RSS

Legal

  • Terms of Service
  • Privacy Policy

© 2026 LeetLLM. All rights reserved.

All Topics
Your Progress
0%

0 of 196 articles completed

🛠️Computing Foundations0/9
Git, Shell, Linux for AIDocker for Reproducible AIPython for AI EngineeringNumPy and Tensor ShapesCUDA for ML TrainingMPS & Metal for ML on MacData Structures for AISQL and Data ModelingAlgorithms for ML Engineers
📊Math & Statistics0/9
Gradients and BackpropVectors, Matrices & TensorsLinear Algebra for MLAdam, Momentum, SchedulersProbability for Machine LearningBayesian Inference from EvidenceStatistics and UncertaintyDistributions and SamplingHypothesis Tests, Intervals, and pass@k
📚Preparation & Prerequisites0/13
Neural Networks from ScratchCNNs from ScratchTraining & BackpropagationSoftmax, Cross-Entropy & OptimizationRNNs, LSTMs, GRUs, and Sequence ModelingAutoencoders and VAEsThe Transformer Architecture End-to-EndLanguage Modeling & Next TokensFrom GPT to Modern LLMsPrompt Engineering FundamentalsCalling LLM APIs in ProductionFirst AI App End-to-EndThe LLM Lifecycle
🧮ML Algorithms & Evaluation0/15
Linear Regression from ScratchLogistic Regression and MetricsDecision Trees, Forests, and BoostingReinforcement Learning BasicsValidation and LeakageStatistical Learning and GeneralizationConformal Prediction and UncertaintyClustering and PCAProbabilistic Generative ModelsCore Retrieval AlgorithmsDecoding AlgorithmsExperiment Design and A/B TestingCausal Inference and CounterfactualsPyTorch Training LoopsDataset Pipelines and Data Quality
📦Production ML Systems0/6
Feature Engineering for Production MLBatch and Streaming Feature PipelinesGradient Boosted Trees in ProductionRanking and Recommendation SystemsForecasting and Anomaly DetectionMonitoring Predictive Models
🧪Core LLM Foundations0/8
The Bitter Lesson & ComputeBPE, WordPiece, and SentencePieceStatic to Contextual EmbeddingsPerplexity & Model EvaluationFile Ingestion for AIChunking StrategiesLLM Benchmarks & LimitationsInstruction Tuning & Chat Templates
🧰Applied LLM Engineering0/25
Dimensionality Reduction for EmbeddingsCoT, ToT & Self-Consistency PromptingFunction Calling & Tool UseMCP & Tool Protocol StandardsContext EngineeringPrompt Injection DefenseResponsible AI GovernancePrivacy-Preserving Machine LearningData Labeling and Human FeedbackEvaluating AI AgentsProduction RAG PipelinesHybrid Search: Dense + SparseReranking and Cross-Encoders for RAGRAG Evaluation for Reliable AnswersLLM-as-a-Judge EvaluationBias & Fairness in LLMsHallucination Detection & MitigationLLM Observability & MonitoringExperiment Tracking with MLflow and W&BPrompt Optimization with DSPyModel Versioning & DeploymentSemantic Caching & Cost OptimizationLLM Cost Engineering & Token EconomicsModel Gateways, Routing, and FallbacksDesign an Automated Support Agent
🎓Portfolio Capstones0/9
Capstone: Delivery ETA PredictionCapstone: Product RankingCapstone: Demand ForecastingCapstone: Image Damage ClassifierCapstone: Production ML PipelineCapstone: Document QACapstone: Eval DashboardCapstone: Fine-Tuned ClassifierCapstone: Reproducible ML Study
🧠Transformer Deep Dives0/8
Sentence Embeddings & Contrastive LossEmbedding Similarity & QuantizationScaled Dot-Product AttentionVision Transformers and Image EncodersPositional Encoding: RoPE & ALiBiLayer Normalization: Pre-LN vs Post-LNMechanistic InterpretabilityDecoding Strategies: Greedy to Nucleus
🧬Advanced Training & Adaptation0/17
Scaling Laws & Compute-Optimal TrainingPre-training Data at ScaleBuild GPT from Scratch LabJAX for PyTorch ResearchersContinued Pretraining for Domain ShiftSynthetic Data PipelinesSupervised Fine-Tuning PipelineMixed Precision TrainingDistributed Training: FSDP & ZeROLoRA & Parameter-Efficient TuningTraining Run OperationsReward Modeling from Preference DataRLHF & DPO AlignmentConstitutional AI & Red TeamingRLVR & Verifiable RewardsKnowledge Distillation for LLMsModel Merging and Weight Interpolation
🤖Advanced Agents & Retrieval0/16
Vector DB Internals: HNSW & IVFAdvanced RAG: HyDE & Self-RAGGraphRAG & Knowledge GraphsRAG Security & Access ControlStructured Output GenerationReAct & Plan-and-ExecuteGuardrails & Safety FiltersCode Generation & SandboxingComputer-Use / GUI / Browser AgentsHuman-in-the-Loop Agent ArchitectureAI Coding Workflow with AgentsAgent Memory & PersistenceAgent Failure & RecoveryRecursive Language Models (RLM)Multi-Agent OrchestrationCapstone: Production Agent
⚡Inference & Production Scale0/31
Inference: TTFT, TPS & KV CacheMulti-Query & Grouped-Query AttentionKV Cache & PagedAttentionPrefix Caching and Prompt CachingFlashAttention & Memory EfficiencyContinuous Batching & SchedulingScaling LLM InferenceModel Parallelism for LLM InferenceAccelerator Architecture Field GuideCorrect GPU Profiling and BenchmarkingGPU Kernel Performance Engineering LabGPU Parallel Primitives LabGPU Compilers and Kernel LanguagesGEMM From Naive CUDA to Tensor CoresLow-Precision GPU ProgrammingGPU Runtime Execution LabAI-Generated Kernel EngineeringModel Quantization: GPTQ, AWQ & GGUFLocal LLM DeploymentSLM Specialization & Edge DeploymentSpeculative DecodingLong Context Window ManagementMixture of Experts ArchitectureMamba & State Space ModelsReasoning & Test-Time ComputeAdvanced MLOps & DevOps for AIGPU Serving & AutoscalingGPU Failure OperationsDistributed Inference Data PlaneServing Benchmark EngineeringA/B Testing for LLMs
🏗️System Design Capstones0/9
Content Moderation SystemCode Completion SystemMulti-Tenant LLM PlatformLLM-Powered Search EngineVision-Language Models & CLIPMultimodal LLM ArchitectureDiffusion Models: Images & TextReal-Time Voice AI AgentReasoning Agent System Design
🎤AI Lab Interviewing0/4
AI Lab Coding Interview: Python SystemsAI Lab System Design InterviewAI Lab Behavioral InterviewAI Lab Technical Presentation
🔬Project Deep Dives0/17
Deep Dive - vLLMDeep Dive - SkyRLDeep Dive - FlashAttentionDeep Dive - FlashInferDeep Dive - DeepGEMMDeep Dive - NCCLDeep Dive - MegatronDeep Dive - DeepSpeedDeep Dive - RayDeep Dive - MLflowDeep Dive - PyTorchDeep Dive - TransformersDeep Dive - SGLangDeep Dive - slimeDeep Dive - DeepEPDeep Dive - TinkerDeep Dive - Light-PEFT
Back to Topics
LearnPreparation & PrerequisitesFrom GPT to Modern LLMs
📝EasyNLP Fundamentals

From GPT to Modern LLMs

Trace how decoder-only models grew into modern LLMs, then inspect scaling, instruction tuning, open weights, MoE, and serving tradeoffs with runnable examples.

27 min read
Learning path
Step 27 of 196 in the full curriculum
Language Modeling & Next TokensPrompt Engineering Fundamentals

Personalize this lesson

Adapt explanations and teaching visuals to your background and preferred voice.

GPT stands for Generative Pre-trained Transformer, the flagship family pioneered by OpenAI; large language model (LLM) is the broader technical category. Every decoder-only model runs on a single loop: predict the next token given prior tokens. Yet software teams rely on them to classify pull requests, extract identifiers from stack traces, draft root-cause explanations, and write bug fixes. How does one unidirectional prediction loop become an interactive general-purpose interface?

Picture continuous integration run RUN-842. It terminates abruptly with an unhandled exception at auth_fixture_test.py:41: expired auth fixture. Your team needs one developer assistant to tackle three distinct jobs:

  1. Label an incoming code review comment ("This docs page is stale") as Docs.
  2. Extract the exact run identifier RUN-842 from a noisy 2,000-line build log.
  3. Answer an engineer who asks why the test suite failed on the release branch.

A pure next-token predictor sounds too narrow for all three tasks. Classical machine learning systems would train three separate pipelines: a text classifier, a token-span extractor, and a seq2seq summary model. Modern LLMs replace those bespoke architectures with a single text-to-text contract: feed the task in text, let the model generate the continuation.

The original Transformer debuted in 2017 as a dual-stack machine translation system with an encoder and a decoder.[1]Reference 1Attention Is All You Need.https://arxiv.org/abs/1706.03762 Subsequent research split those components apart, scaled their parameters, aligned their behavior with human expectations, and overhauled their runtime mechanics. We'll track RUN-842 through every stage of this evolution, examining how each architectural breakthrough removed an engineering bottleneck while introducing fresh serving tradeoffs.

One generation loop, many tasks

Return to RUN-842. In a traditional supervised setup, a classification model predicts logits over a closed set of three labels: {"Bug", "Docs", "Security"}. An extraction model outputs character spans [start_idx, end_idx] pointing into the input log. A generative interface handles both by treating every job as prompt continuation.

A causal decoder reads text from left to right and samples the next token. The system prompt and demonstrations define the meaning of the continuation slot. When the model sees Label:, the next token should be Docs. When it sees Extracted Run ID:, the continuation should be RUN-842. When asked an open-ended question, it emits an explanatory sentence token by token.

A software assistant encounters three standard task shapes:

  1. Classification: A review note states, "This docs page is stale." You want the category label Docs.
  2. Extraction: A CI trace contains RUN-842 inside a stack frame. You want the exact identifier.
  3. Generation: An on-call engineer asks, "Why did tests fail?" You want a concise diagnosis grounded in the trace.

An encoder model like BERT can handle the first two tasks with custom classification and span heads, but can't generate fluent explanations. A decoder-only model expresses all three through few-shot prompting:

text
1Classify the review note into Bug, Docs, or Security. 2 3Note: The auth test fails on main. 4Label: Bug 5 6Note: This docs page is stale. 7Label: Docs 8 9Note: The API key is printed in logs. 10Label: Security 11 12--- 13 14Note: The README still mentions the old CLI flag. 15Label:

The model ingests the prompt prefix, attends across the task rules and demonstrations, and predicts the continuation at the empty Label: slot. The prompt pattern sets up an inductive bias for Docs.

A unified text-to-text interface simplifies system architecture, but it introduces validation requirements. A classifier with a fixed output layer can only emit valid class labels; a generative model might emit Documentation, Doc, or a conversational preamble like Sure, the label is Docs. The calling application must validate and normalize the generated text against its schema.

One decoder interface branching into three output shapes: an illustrative next-token label distribution selecting Docs, a highlighted CI run ID copied from a log line, and a four-step generated reply whose context grows by one token per row.
The same continuation slot accommodates a discrete label, a copied identifier, or an autoregressive explanation. Each displayed word stands in for tokenizer pieces; actual completions often span several decode steps.

Prompt framing turns classification into next-token scoring. The expected answer is Docs:

prompt-framing-as-generation.py
1def build_review_label_prompt(note: str) -> str: 2 examples = [ 3 ("The auth test fails on main.", "Bug"), 4 ("This docs page is stale.", "Docs"), 5 ("The API key is printed in logs.", "Security"), 6 ] 7 header = "Classify the review note into Bug, Docs, or Security." 8 shots = "\n\n".join( 9 f"Note: {text}\nLabel: {label}" for text, label in examples 10 ) 11 return f"{header}\n\n{shots}\n\n---\n\nNote: {note}\nLabel:" 12 13prompt = build_review_label_prompt("The README still mentions the old CLI flag.") 14print(prompt) 15print("pattern_target=Docs") 16print("why=the prompt pattern ends with a label slot")
Prompt framing output
1Classify the review note into Bug, Docs, or Security. 2 3Note: The auth test fails on main. 4Label: Bug 5 6Note: This docs page is stale. 7Label: Docs 8 9Note: The API key is printed in logs. 10Label: Security 11 12--- 13 14Note: The README still mentions the old CLI flag. 15Label: 16pattern_target=Docs 17why=the prompt pattern ends with a label slot

Why can a decoder-only model classify a review note even though its pretraining objective was next-token prediction?

Answer

The prompt establishes an in-context demonstration pattern ending at Label:. Next-token prediction places high probability mass on valid category words matching the demonstrations. The application still needs to parse the generated token and verify it belongs to the allowed label set.

The fork: reading and writing split apart

Why did the industry converge on decoder-only models for conversational assistants when BERT dominated natural language understanding in 2018? The answer lies in the attention mask.

The 2017 Transformer consisted of two stacks: an encoder that processed the full source sentence bidirectionally, and an autoregressive decoder that generated target tokens one by one while cross-attending to the encoder representations.[1]Reference 1Attention Is All You Need.https://arxiv.org/abs/1706.03762 In 2018, researchers discovered that you didn't need both stacks for every task:

  1. The encoder-only branch (BERT). Google released BERT in 2018.[2]Reference 2BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.https://arxiv.org/abs/1810.04805 It uses an unmasked Transformer encoder. Every token attends to all tokens in the sequence simultaneously, both left and right. BERT pretrains on Masked Language Modeling (MLM), predicting randomly masked tokens from surrounding bidirectional context. It excels at producing fixed-size vector representations for classification, sentiment analysis, entity extraction, and passage retrieval.[3]Reference 3Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks.https://arxiv.org/abs/1908.10084 It can't generate free-form text because its training objective never learned autoregressive generation.
  2. The decoder-only branch (GPT). OpenAI released GPT-1 in 2018.[4]Reference 4Improving Language Understanding by Generative Pre-Training.https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf It removes the encoder entirely, keeping only the decoder blocks. A lower-triangular causal mask prevents tokens from attending to future positions. It trains purely on causal language modeling: predict token xtx_txt​ given x<tx_{<t}x<t​.
Production requirementEncoder-only (BERT-style)Decoder-only (GPT-style)
Classify an incident reportStrong native fit; outputs class logits from [CLS] token.Supported via prompt formatting; requires text parsing.
Generate a root-cause explanationRequires adding a separate decoder.Strong native fit; decodes tokens sequentially.
Dense semantic searchStrong fit; creates compact passage embeddings.Capable, but computationally heavier for pure indexing.
Interactive assistantCan't handle free-form conversational turns.Universal interface for multi-turn dialogue.

Causal decoders conquered general-purpose AI because generation subsumes understanding, but understanding doesn't subsume generation. If a model can generate text, you can cast classification, translation, summarization, and reasoning into text prompts. If a model only produces pooled vectors, generating open-ended sentences requires adding a decoder anyway.

A decoder writes left to right

In causal attention, token ttt attends only to positions ≤t\le t≤t. When predicting the token after failed, the model attends to <bos>, tests, and failed. It can't peek at future tokens.

During training, causal masking enables teacher forcing: you pass the full training sequence through the model in one forward pass and compute the loss on all tokens in parallel using an upper-triangular mask of −∞-\infty−∞. During inference, generation runs sequentially: each newly emitted token is appended to the input prefix to predict the following token.

visibility-masks.py
1tokens = ["<bos>", "tests", "failed", "<eos>"] 2n = len(tokens) 3causal = [[1 if col <= row else 0 for col in range(n)] for row in range(n)] 4bidirectional = [[1] * n for _ in range(n)] 5 6print("tokens:", tokens) 7print("causal row for 'failed': ", causal[2]) 8print("bidirectional row for 'failed':", bidirectional[2]) 9print("future visible in causal row:", causal[2][3])
Visibility mask output
1tokens: ['<bos>', 'tests', 'failed', '<eos>'] 2causal row for 'failed': [1, 1, 1, 0] 3bidirectional row for 'failed': [1, 1, 1, 1] 4future visible in causal row: 0

The 0 at index 3 prevents the model from attending to the future <eos> token. Bidirectional encoders allow all-to-all connectivity (1, 1, 1, 1), making them great for full-sentence representation but incapable of causal autoregressive generation.

What mathematical property distinguishes a causal decoder attention matrix from an encoder attention matrix?

Answer

A causal decoder attention matrix is strictly lower-triangular: attention scores for positions j>ij > ij>i are masked with −∞-\infty−∞ (or zeroed out post-softmax), ensuring position iii can't attend to any future position jjj. An encoder attention matrix has no triangular constraint, allowing every position to attend to all other positions.

The life of a token: prefill, KV cache, and decode

Trace the engineer's prompt through the decoder: "Why did tests fail?".

Text enters the tokenizer and breaks into a sequence of discrete token IDs. An embedding lookup table maps each ID to a vector of dimension dmodeld_{\text{model}}dmodel​. Position information is injected into each token's representation.

The tokens flow through NNN stacked decoder layers. Inside each block:

  1. Normalization stabilizes activations across features.
  2. Self-attention projects hidden states into Queries (QQQ), Keys (KKK), and Values (VVV).
  3. Softmax-weighted attention scores combine values across all allowed causal positions.
  4. An output projection maps the attended values back to dmodeld_{\text{model}}dmodel​.
  5. A feed-forward network transforms each position independently through nonlinear activations.

After the final layer normalization, the hidden vector corresponding to the very last token position (hLh_LhL​) projects through the unembedding matrix Wvocab∈Rdmodel×∣V∣W_{\text{vocab}} \in \mathbb{R}^{d_{\text{model}} \times |V|}Wvocab​∈Rdmodel​×∣V∣ to produce raw vocabulary logits.[5]Reference 5GPT-2 Source Implementation.https://github.com/openai/gpt-2/blob/master/src/model.py Applying softmax turns logits into probabilities. The sampling strategy (greedy argmax, temperature scaling, top-ppp) selects the next token: Auth.

Inference splits into two distinct execution phases:

  1. The prefill phase (compute-bound): The model processes all TTT prompt tokens in parallel. Because the full prompt is available upfront, matrix multiplications take the form [B,T,dmodel]×[dmodel,dhidden][B, T, d_{\text{model}}] \times [d_{\text{model}}, d_{\text{hidden}}][B,T,dmodel​]×[dmodel​,dhidden​]. These large matrix-matrix multiplications (GEMM) saturate GPU tensor cores and run compute-bound. During prefill, the model computes and saves the Key and Value vectors for all TTT prompt tokens into GPU memory: the KV cache.
  2. The decode phase (memory-bandwidth bound): To generate the next token, the model executes a forward pass for only a single new token (T=1T=1T=1). Instead of re-running attention over all past tokens from scratch (which would take O(T2)O(T^2)O(T2) compute per sequence), the layer loads the cached Key and Value vectors from high-bandwidth memory (HBM), computes Query vectors for the single new token, appends the new Key and Value to the cache, and computes attention scores. Because batch size is small and sequence length is 1, decode steps run matrix-vector multiplications (GEMV). Tensor cores sit underutilized while the GPU waits for weights and cache tensors to stream across memory buses.
Three-step decoder token lifecycle: Prefill computes parallel hidden states and caches 5 K/V positions; Sampling selects the next token Auth leaving cache length 5; Decode processes Auth in the next forward pass and extends the KV cache to 6 positions.
Prefill generates the first answer token while populating the KV cache. The subsequent decode forward pass processes only the single newly sampled token, loading prior K/V states from memory and appending the new position.

Inspect the output projection and cache dimensions with concrete Python calculations:

decoder-output-and-cache-shapes.py
1import math 2 3prompt_tokens, d_model, vocab_size = 5, 4, 4 4last_hidden = [0.6, -0.2, 0.4, 0.1] 5output_rows = [ 6 [0.8, 0.1, -0.2, 0.0], 7 [0.2, 0.5, 0.1, -0.1], 8 [-0.1, 0.2, 0.4, 0.3], 9 [0.0, -0.3, 0.2, 0.4], 10] 11labels = ["Auth", "The", "I", "Fix"] 12logits = [sum(h * w for h, w in zip(last_hidden, row)) for row in output_rows] 13peak = max(logits) 14exps = [math.exp(x - peak) for x in logits] 15z = sum(exps) 16probs = [e / z for e in exps] 17 18print("prompt tokens:", prompt_tokens, "d_model:", d_model, "vocab:", vocab_size) 19print("last-position logits:", [round(x, 2) for x in logits]) 20print("argmax:", labels[logits.index(max(logits))]) 21print("prob sum:", round(sum(probs), 3)) 22layers, kv_heads, head_dim = 4, 2, 2 23print("cached K/V shape per layer:", (2, 1, kv_heads, prompt_tokens, head_dim)) 24assert math.isclose(sum(probs), 1.0) 25assert logits.index(max(logits)) == 0
Decoder shape trace output
1prompt tokens: 5 d_model: 4 vocab: 4 2last-position logits: [0.38, 0.05, 0.09, 0.18] 3argmax: Auth 4prob sum: 1.0 5cached K/V shape per layer: (2, 1, 2, 5, 2)

Notice the sequence length discrepancy right after sampling. Selecting Auth adds it to the text output, bringing the sequence to 6 tokens. The KV cache still contains only 5 positions. The next decode step takes Auth as its single input token, computes its Key and Value vectors, and expands the cache to length 6.

Why does the decode phase underutilize GPU tensor cores compared to the prefill phase?

Answer

Prefill processes all prompt tokens at once, executing large matrix-matrix multiplications (GEMM) with high arithmetic intensity. The decode phase passes only a single token at a time (T=1T=1T=1), resulting in matrix-vector operations (GEMV). The GPU spends most of its clock cycles streaming model weights and accumulated KV cache tensors from High Bandwidth Memory into on-chip cache, running memory-bandwidth bound.

The evolution trajectory: GPT-1 to aligned assistants

Between 2018 and 2023, the decoder-only architecture moved through four major eras. Each stage addressed a key bottleneck in how language models acquire and apply knowledge.

Timeline of six LLM evolutionary milestones: GPT-1 (unsupervised pretraining and task-specific fine-tuning), GPT-2 (zero-shot multitask transfer), GPT-3 (in-context few-shot emergence at 175B), Chinchilla (compute-optimal token-parameter balancing), InstructGPT (RLHF alignment for user intent), and Modern Frontier (RMSNorm, SwiGLU, RoPE, GQA, and MoE).
Six milestones in the modern LLM evolution. The progression shifted from task-specific parameter fine-tuning toward universal in-context prompting, compute-optimal dataset sizing, and preference alignment.

1. GPT-1: Generative pretraining + task fine-tuning (2018)

Before GPT-1, natural language processing relied on task-specific models trained on small supervised datasets, or static word embeddings like Word2Vec and GloVe. Radford et al. (2018) showed that unsupervised next-token pretraining across a large text corpus (BooksCorpus, ~5 GB) produces general representations.[4]Reference 4Improving Language Understanding by Generative Pre-Training.https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf

GPT-1 consisted of a 12-layer decoder with 117 million parameters. Its core workflow was a two-stage process:

  • Unsupervised pretraining: Train the decoder on raw text using standard autoregressive cross-entropy loss.
  • Supervised fine-tuning: Discard the language modeling head, attach a task-specific linear projection layer, and update all model weights on labeled classification or entailment datasets.

Fine-tuning required separate checkpoints for every downstream task. It showed pretraining worked, but generation wasn't yet the operational interface.

2. GPT-2: Zero-shot transfer without task-specific weights (2019)

Radford et al. (2019) made a bold conceptual leap with GPT-2: language models are unsupervised multitask learners.[6]Reference 6Language Models are Unsupervised Multitask Learners.https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf By expanding parameter scale to 1.5 billion parameters and training on WebText (40 GB of scraped internet text), they discovered that models can solve downstream tasks without updating weights or swapping linear heads.

Instead of training a dedicated sentiment head, you condition the model on a prompt: "Input: The service was awful. Sentiment:" The model predicts "negative" purely via zero-shot continuation. GPT-2 proved that task conditioning could be expressed entirely in natural language.

3. GPT-3: In-context few-shot emergence (2020)

Brown et al. (2020) expanded the architecture by two orders of magnitude, training GPT-3 with 175 billion parameters on 300 billion tokens.[7]Reference 7Language Models are Few-Shot Learners.https://arxiv.org/abs/2005.14165 At 175B parameters, in-context learning emerged as a reliable capability.

Instead of gradient updates, you provide 2 to 3 demonstrations directly in the prompt prefix. The model uses self-attention across the demonstration tokens to bind the task rules dynamically at inference time. Prompting became a new programming model: you configure the model by describing the task rather than collecting thousands of labeled examples to fine-tune weights.

4. InstructGPT & ChatGPT: Alignment via RLHF (2022)

GPT-3 unlocked in-context capability, but using base models was frustrating. Because base models optimize next-token prediction across raw web pages, they replicate internet text conventions rather than acting as helpful assistants. Prompting a base model with "Explain this test failure" frequently caused it to append more test errors, generate a fabricated forum discussion, or spit out spam.

To transform an autocomplete engine into a cooperative assistant, Ouyang et al. (2022) introduced InstructGPT, establishing the Reinforcement Learning from Human Feedback (RLHF) pipeline:[8]Reference 8Training Language Models to Follow Instructions with Human Feedback (InstructGPT).https://arxiv.org/abs/2203.02155

  1. Supervised Fine-Tuning (SFT): Human annotators write thousands of high-quality demonstrations of instructions and ideal responses. The base model fine-tunes on this instruction dataset, learning the conversational assistant format.
  2. Reward Modeling (RM): The SFT model generates multiple candidate responses for various prompts. Human labelers rank these candidates from best to worst. A reward model trains on these pairwise comparisons to output a scalar score predicting human preference.
  3. Reinforcement Learning (PPO): The SFT model acts as an RL policy, updated via Proximal Policy Optimization (PPO) to maximize the reward model score while penalizing drift from the initial policy using a Kullback-Leibler (KL) divergence penalty.

On human evaluations, annotators consistently preferred outputs from a 1.3-billion parameter InstructGPT model over those from the 175-billion parameter raw GPT-3 base model.[8]Reference 8Training Language Models to Follow Instructions with Human Feedback (InstructGPT).https://arxiv.org/abs/2203.02155 Alignment demonstrated that raw parameter scale is incomplete without behavioral calibration.

Inspect how pairwise preference records evaluate whether chosen completions adhere to ground-truth evidence:

preference-records.py
1comparisons = [ 2 { 3 "prompt": "CI run RUN-842: auth_fixture_test.py:41 reports 'expired auth fixture'. Explain the failure.", 4 "chosen": "RUN-842 failed at auth_fixture_test.py:41: the log reports an expired auth fixture.", 5 "rejected": "The build failed for an unknown reason.", 6 "required_fact": "expired auth fixture", 7 }, 8 { 9 "prompt": "Review note says the API key is printed in logs. Classify the issue.", 10 "chosen": "Security: redact the API key and rotate the exposed credential.", 11 "rejected": "Docs: update the README wording.", 12 "required_fact": "API key", 13 }, 14] 15 16for row in comparisons: 17 keeps_required_fact = ( 18 row["required_fact"] in row["prompt"] 19 and row["required_fact"] in row["chosen"] 20 ) 21 print(f"prompt={row['prompt'][:22]}... chosen_keeps_fact={keeps_required_fact}") 22 print(" preferred:", row["chosen"])
Preference record output
1prompt=CI run RUN-842: auth_f... chosen_keeps_fact=True 2 preferred: RUN-842 failed at auth_fixture_test.py:41: the log reports an expired auth fixture. 3prompt=Review note says the A... chosen_keeps_fact=True 4 preferred: Security: redact the API key and rotate the exposed credential.

Understanding the distinction between base, instruct, and chat models helps teams select the right checkpoint:

Checkpoint typeTraining objectiveExpected behaviorProduction role
Base modelPure causal language modeling on web-scale text.Continues input text matching internet distribution; may emit transcripts or lists.Foundation for domain continued pretraining and custom fine-tuning.
Instruct modelSFT on instruction-following datasets.Answers direct single-turn commands (summarization, extraction).Ideal for single-turn extraction and deterministic classification tasks.
Chat modelSFT + RLHF / DPO with multi-turn role formatting.Follows system guidelines, maintains conversation context, and applies safety checks.Default choice for interactive developer assistants and agents.

A chat template serializes multi-turn conversations into a single token string using special control tokens (such as <|im_start|>system and <|im_end|>).[9]Reference 9Transformers Documentation: Chat templates.https://huggingface.co/docs/transformers/main/en/chat_templating Applying a chat template formats the prompt correctly for an aligned model, but it won't give a raw base model instruction-following behavior that it was never trained to exhibit.

Why does human preference alignment make a 1.3B InstructGPT model more useful than a 175B raw GPT-3 base model for answering user questions?

Answer

A base model is an unsupervised autocomplete engine trained to predict web text: when asked a question, it may continue with related questions, alternative drafts, or internet fluff. InstructGPT's SFT and RLHF stages train the model specifically on the behavioral contract of answering requests directly, accurately, and concisely.

Scaling laws and compute optimality

As teams trained larger models, an engineering question arose: given a fixed computational budget (measured in floating-point operations, or FLOPs), how should you divide your budget between adding parameters and feeding more training tokens?

Kaplan et al. (2020) at OpenAI published early power-law scaling relationships for transformer language models.[10]Reference 10Scaling Laws for Neural Language Modelshttps://arxiv.org/abs/2001.08361 Their empirical findings suggested that cross-entropy loss scales as a power law primarily with parameter count NNN, and that when compute budgets increase, teams should invest the vast majority of compute into scaling model size rather than dataset size:

N∝C0.73,D∝C0.27N \propto C^{0.73}, \quad D \propto C^{0.27}N∝C0.73,D∝C0.27

This drove the industry to build massive models trained on modest datasets. DeepMind built Gopher with 280 billion parameters trained on 300 billion tokens.[11]Reference 11Training Compute-Optimal Large Language Models.https://arxiv.org/abs/2203.15556 GPT-3 allocated 175 billion parameters to 300 billion tokens: a ratio of only ~1.7 tokens per parameter.

In 2022, Hoffmann et al. identified a flaw in Kaplan's analysis: the learning rate schedule had not been tuned independently for each training duration, causing longer runs on smaller models to underperform.[11]Reference 11Training Compute-Optimal Large Language Models.https://arxiv.org/abs/2203.15556 DeepMind trained over 400 models spanning 70M to 16B parameters across various token budgets to establish the Chinchilla scaling laws:

N∝C0.5,D∝C0.5N \propto C^{0.5}, \quad D \propto C^{0.5}N∝C0.5,D∝C0.5

To train compute-optimally, for every doubling of model parameters, the number of training tokens should also double. Compute-optimal models require approximately 20 tokens per parameter:

ModelParametersTraining tokensTokens per parameterCompute efficiency
Gopher280B300B~1.1Severely token-starved
GPT-3175B300B~1.7Token-starved
Chinchilla70B1.4T20.0Compute-optimal frontier
LLaMA 165B1.4T21.5Inference-optimal design
LLaMA 38B15.0T1,875.0Over-trained serving specialist

DeepMind built Chinchilla (70B parameters, 1.4T tokens) using the exact same compute budget as Gopher (280B parameters, 300B tokens). Chinchilla outperformed Gopher across downstream benchmarks including MMLU, Big-bench, and GSM8K.[11]Reference 11Training Compute-Optimal Large Language Models.https://arxiv.org/abs/2203.15556

tokens-per-parameter.py
1runs = { 2 "Gopher": {"params_b": 280, "tokens_b": 300}, 3 "GPT-3": {"params_b": 175, "tokens_b": 300}, 4 "Chinchilla": {"params_b": 70, "tokens_b": 1400}, 5 "LLaMA-3-8B": {"params_b": 8, "tokens_b": 15000}, 6} 7 8for name, run in runs.items(): 9 ratio = run["tokens_b"] / run["params_b"] 10 print(f"{name:12s} tokens/param={ratio:7.1f}")
Scaling allocation output
1Gopher tokens/param= 1.1 2GPT-3 tokens/param= 1.7 3Chinchilla tokens/param= 20.0 4LLaMA-3-8B tokens/param= 1875.0

Training compute versus lifetime serving costs

The Chinchilla ratio represents training-compute optimality: minimizing the loss achieved for a given training budget. In real systems, total cost includes inference serving.

Meta's LLaMA paper pushed this insight further.[12]Reference 12LLaMA: Open and Efficient Foundation Language Models.https://arxiv.org/abs/2302.13971 A 7B or 8B model trained on 15 trillion tokens (like LLaMA 3) costs significantly more training compute than Chinchilla's recommendation. But that 8B model fits onto a single GPU and serves millions of user queries at a fraction of the hardware cost required to host a 70B model. Over-training compact models on high-quality data is an intentional systems strategy to minimize lifetime inference cost.

Why do modern production labs intentionally train 8B models on 15 trillion tokens, defying Chinchilla's compute-optimal 20:1 ratio?

Answer

Chinchilla optimality minimizes training FLOPs to achieve a target loss. Once a model is deployed to production, inference costs quickly dominate training costs. An over-trained 8B model delivers the accuracy of a 70B model while fitting on single-GPU instances with dramatically lower latency and memory costs.

Modern frontier architectures: RMSNorm, SwiGLU, and RoPE

The vanilla Transformer architecture proposed by Vaswani et al. in 2017 is no longer what modern frontier models execute. Open-source foundations such as LLaMA, Mistral, Gemma, and DeepSeek converged on three core architectural upgrades:

1. Root Mean Square Normalization (RMSNorm)

Standard LayerNorm computes both the mean μ\muμ and variance σ2\sigma^2σ2 across the feature dimension ddd of hidden vector xxx:[13]Reference 13LayerNormhttps://docs.pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html

μ=1d∑i=1dxi,σ2=1d∑i=1d(xi−μ)2,LN(x)=x−μσ2+ϵ⊙γ+β\mu = \frac{1}{d} \sum_{i=1}^d x_i, \quad \sigma^2 = \frac{1}{d} \sum_{i=1}^d (x_i - \mu)^2, \quad \text{LN}(x) = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} \odot \gamma + \betaμ=d1​∑i=1d​xi​,σ2=d1​∑i=1d​(xi​−μ)2,LN(x)=σ2+ϵ​x−μ​⊙γ+β

Zhang & Sennrich (2019) demonstrated that the mean-centering step (x−μx - \mux−μ) contributes almost nothing to gradient stabilization during training.[14]Reference 14Root Mean Square Layer Normalization.https://arxiv.org/abs/1910.07467 What stabilizes training is scaling the input vector by the root mean square of its activations.

RMSNorm discards the mean calculation and the learned additive bias β\betaβ, scaling strictly by root mean square energy:[14]Reference 14Root Mean Square Layer Normalization.https://arxiv.org/abs/1910.07467[15]Reference 15RMSNormhttps://docs.pytorch.org/docs/stable/generated/torch.nn.RMSNorm.html

RMS(x)=1d∑i=1dxi2+ϵ,RMSNorm(x)=xRMS(x)⊙γ\text{RMS}(x) = \sqrt{\frac{1}{d} \sum_{i=1}^d x_i^2 + \epsilon}, \quad \text{RMSNorm}(x) = \frac{x}{\text{RMS}(x)} \odot \gammaRMS(x)=d1​∑i=1d​xi2​+ϵ​,RMSNorm(x)=RMS(x)x​⊙γ

RMSNorm reduces memory bandwidth traffic by avoiding a two-pass reduction across the vector. It achieves identical training stability while executing 10% to 50% faster on modern accelerators.

2. SwiGLU activations

Early transformers used standard ReLU or GELU activations inside a two-layer feed-forward network: FFN(x)=activation(xW1)W2\text{FFN}(x) = \text{activation}(x W_1) W_2FFN(x)=activation(xW1​)W2​.

Dauphin et al. and Shazeer (2020) demonstrated that Gated Linear Units (GLUs), which compute the elementwise product of two linear projections where one projection acts as a continuous gate, significantly improve model capacity.[16]Reference 16GLU Variants Improve Transformerhttps://arxiv.org/abs/2002.05202

SwiGLU uses the Swish (or SiLU) activation function Swish(z)=z⋅σ(z)\text{Swish}(z) = z \cdot \sigma(z)Swish(z)=z⋅σ(z) as the gating mechanism:

SwiGLU(x)=(Swish(xWgate)⊙(xWup))Wdown\text{SwiGLU}(x) = \left( \text{Swish}(x W_{\text{gate}}) \odot (x W_{\text{up}}) \right) W_{\text{down}}SwiGLU(x)=(Swish(xWgate​)⊙(xWup​))Wdown​

Because SwiGLU introduces three weight matrices (Wgate,Wup∈Rdmodel×dffW_{\text{gate}}, W_{\text{up}} \in \mathbb{R}^{d_{\text{model}} \times d_{\text{ff}}}Wgate​,Wup​∈Rdmodel​×dff​ and Wdown∈Rdff×dmodelW_{\text{down}} \in \mathbb{R}^{d_{\text{ff}} \times d_{\text{model}}}Wdown​∈Rdff​×dmodel​) instead of two, models resize the intermediate hidden dimension dffd_{\text{ff}}dff​ from 4dmodel4 d_{\text{model}}4dmodel​ down to approximately 83dmodel\frac{8}{3} d_{\text{model}}38​dmodel​ (usually rounded to a multiple of 64 or 256). This keeps parameter count and training FLOPs identical to standard MLPs while lowering perplexity.

3. Rotary Position Embeddings (RoPE)

Original Transformers used absolute positional encodings: learned or sinusoidal vectors added directly to input token embeddings: hi=ei+pih_i = e_i + p_ihi​=ei​+pi​. Absolute encodings don't explicitly encode relative distance, and they struggle to extrapolate when sequence lengths exceed training contexts.

Su et al. (2021) introduced Rotary Position Embeddings (RoPE).[17]Reference 17RoFormer: Enhanced Transformer with Rotary Position Embedding.https://arxiv.org/abs/2104.09864 Instead of adding positional vectors at the input layer, RoPE rotates 2D coordinate pairs in the Query and Key vectors inside each attention head by a position-dependent angle mθim\theta_imθi​:

qm=RΘ,mdq,kn=RΘ,ndk\mathbf{q}_m = R_{\Theta, m}^d \mathbf{q}, \quad \mathbf{k}_n = R_{\Theta, n}^d \mathbf{k}qm​=RΘ,md​q,kn​=RΘ,nd​k

Because rotation matrices are orthogonal and multiplicative (RΘ,mTRΘ,n=RΘ,n−mR_{\Theta, m}^T R_{\Theta, n} = R_{\Theta, n - m}RΘ,mT​RΘ,n​=RΘ,n−m​), the resulting attention dot product depends strictly on relative position:

⟨qm,kn⟩=qTRΘ,n−mk\langle \mathbf{q}_m, \mathbf{k}_n \rangle = \mathbf{q}^T R_{\Theta, n - m} \mathbf{k}⟨qm​,kn​⟩=qTRΘ,n−m​k

If token 14 attends to token 12, their dot product depends only on their relative distance 14−12=214 - 12 = 214−12=2. It produces the exact same relative geometric transformation as token 4 attending to token 2. RoPE equips models with natural attention decay across long sequences and supports context length extension via RoPE frequency interpolation.

Run the mechanics of RMSNorm and SwiGLU in Python:

frontier-architectures-rmsnorm-swiglu.py
1import math 2 3def rms_norm(x: list[float], gamma: list[float], eps: float = 1e-6) -> list[float]: 4 d = len(x) 5 rms = math.sqrt(sum(xi ** 2 for xi in x) / d + eps) 6 return [(xi / rms) * gi for xi, gi in zip(x, gamma)] 7 8def silu(z: float) -> float: 9 return z / (1.0 + math.exp(-z)) 10 11x = [1.2, -0.8, 2.5, -1.5] 12gamma = [1.0, 1.0, 1.0, 1.0] 13normed = rms_norm(x, gamma) 14 15w_gate = [0.5, -0.3] 16w_up = [1.2, 0.8] 17gate_val = sum(xi * w for xi, w in zip(x[:2], w_gate)) 18up_val = sum(xi * w for xi, w in zip(x[:2], w_up)) 19swiglu_val = silu(gate_val) * up_val 20 21print("rms_norm output:", [round(v, 4) for v in normed]) 22print("rms verification:", round(math.sqrt(sum(v**2 for v in normed) / len(normed)), 4)) 23print("gate:", round(gate_val, 4), "up:", round(up_val, 4), "swiglu:", round(swiglu_val, 4))
Frontier mechanics output
1rms_norm output: [0.7379, -0.4919, 1.5372, -0.9223] 2rms verification: 1.0 3gate: 0.84 up: 0.8 swiglu: 0.4694

Why does RoPE rotate Query and Key representations instead of adding positional vectors to token embeddings?

Answer

Adding absolute position vectors mixes positional coordinates into semantic representations and forces the model to learn relative distances through layers of matrix transformations. RoPE rotates 2D coordinate pairs directly within attention heads, guaranteeing that the inner product between Query at position mmm and Key at position nnn depends purely on their relative offset (m−n)(m - n)(m−n).

Attention scaling and KV cache bandwidth reduction

During generation, every newly emitted token requires fetching the entire accumulated KV cache from GPU memory. For a 70B parameter model serving a batch of requests with 8,192 tokens of context, the KV cache alone can consume dozens of gigabytes of VRAM.

The exact memory footprint of the KV cache across all layers is:

MemoryKV=2×B×L×nlayers×nkv_heads×dhead×P\text{Memory}_{\text{KV}} = 2 \times B \times L \times n_{\text{layers}} \times n_{\text{kv\_heads}} \times d_{\text{head}} \times PMemoryKV​=2×B×L×nlayers​×nkv_heads​×dhead​×P

Where BBB is batch size, LLL is sequence length, nlayersn_{\text{layers}}nlayers​ is layer count, nkv_headsn_{\text{kv\_heads}}nkv_heads​ is the number of Key/Value heads, dheadd_{\text{head}}dhead​ is head dimension, PPP is precision bytes (2 bytes for FP16/BF16), and the factor of 2 accounts for Keys and Values.

To tame this memory bottleneck, researchers developed three attention head topologies:

  1. Multi-Head Attention (MHA): Each query head has an independent key head and value head (nkv_heads=nq_headsn_{\text{kv\_heads}} = n_{\text{q\_heads}}nkv_heads​=nq_heads​). For 32 query heads, you cache 32 key heads and 32 value heads.
  2. Multi-Query Attention (MQA): Shazeer (2019) proposed sharing a single key head and a single value head across all query heads (nkv_heads=1n_{\text{kv\_heads}} = 1nkv_heads​=1).[18]Reference 18Fast Transformer Decoding: One Write-Head is All You Need.https://arxiv.org/abs/1911.02150 This reduces the KV cache size by 32×32\times32×, but the severe compression can lead to quality degradation on complex reasoning and code retrieval tasks.
  3. Grouped-Query Attention (GQA): Ainslie et al. (2023) introduced the sweet spot: group query heads into GGG partitions (e.g., 8 KV heads for 32 query heads, a 4:1 ratio).[19]Reference 19GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.https://arxiv.org/abs/2305.13245 GQA slashes the KV cache memory footprint and memory bandwidth demand by 75% while matching MHA quality across downstream tasks.

Compare cache sizes across architectures with concrete hardware numbers:

gqa-kv-cache-accounting.py
1batch, layers, tokens, head_dim, bytes_per_value = 8, 32, 8192, 128, 2 2query_heads = 32 3 4for label, kv_heads in [("MHA", 32), ("GQA", 8), ("MQA", 1)]: 5 cached_values = batch * layers * tokens * kv_heads * head_dim * 2 6 cache_gib = cached_values * bytes_per_value / (1024 ** 3) 7 relative = kv_heads / query_heads 8 print(f"{label}: kv_heads={kv_heads:2d} cache={cache_gib:5.2f} GiB relative={relative:.3f}")
KV cache comparison output
1MHA: kv_heads=32 cache=32.00 GiB relative=1.000 2GQA: kv_heads= 8 cache= 8.00 GiB relative=0.250 3MQA: kv_heads= 1 cache= 1.00 GiB relative=0.031

RoPE preserves relative distance invariance across varying sequence positions:

rope-relative-position.py
1import math 2 3def rotate(vector: tuple[float, float], position: int, theta: float = 0.4) -> tuple[float, float]: 4 angle = position * theta 5 cosine, sine = math.cos(angle), math.sin(angle) 6 x, y = vector 7 return (cosine * x - sine * y, sine * x + cosine * y) 8 9def dot(left: tuple[float, float], right: tuple[float, float]) -> float: 10 return left[0] * right[0] + left[1] * right[1] 11 12query = (1.0, 0.2) 13key = (0.3, 0.9) 14same_gap_early = dot(rotate(query, 4), rotate(key, 2)) 15same_gap_late = dot(rotate(query, 14), rotate(key, 12)) 16different_gap = dot(rotate(query, 14), rotate(key, 2)) 17print("same gap, early:", round(same_gap_early, 6)) 18print("same gap, late: ", round(same_gap_late, 6)) 19print("different gap: ", round(different_gap, 6)) 20print("same gap equal:", math.isclose(same_gap_early, same_gap_late))
RoPE relative position output
1same gap, early: 0.936998 2same gap, late: 0.936998 3different gap: -0.794779 4same gap equal: True

Alongside GQA, two runtime systems innovations make modern serving viable:

  • FlashAttention: An exact attention implementation by Dao et al. (2022) that tiles matrix computations into GPU SRAM blocks, reducing high-bandwidth memory access from O(T2)O(T^2)O(T2) to O(T)O(T)O(T).[20]Reference 20FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.https://arxiv.org/abs/2205.14135
  • PagedAttention: Developed by Kwon et al. (2023) for vLLM, it treats KV cache memory like an operating system's paged virtual memory.[21]Reference 21Efficient Memory Management for Large Language Model Serving with PagedAttention.https://arxiv.org/abs/2309.06180 By breaking the cache into non-contiguous physical memory blocks, it eliminates external fragmentation and boosts concurrent serving throughput.

How does Grouped-Query Attention (GQA) reduce decode latency compared to Multi-Head Attention (MHA)?

Answer

The decode phase is memory-bandwidth bound: the GPU spends most of its time streaming past Key and Value vectors from HBM into on-chip cache. In GQA, multiple query heads share a single KV head (e.g. 4:1 ratio), shrinking KV cache memory traffic by 75%. Less data moving across the memory bus directly speeds up token generation.

Sparse architectures: Mixture of Experts (MoE)

Dense models route every token through all parameters. A 70B dense model executes 70 billion parameter calculations for every token. As context lengths and batch sizes grow, the compute cost becomes prohibitive.

Shazeer et al. (2017) introduced the sparsely gated Mixture of Experts layer, modernised by Mixtral 8x7B (Jiang et al., 2024) and DeepSeek models.[22]Reference 22Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer.https://arxiv.org/abs/1701.06538[23]Reference 23Mixtral of Experts.https://arxiv.org/abs/2401.04088

An MoE model replaces standard feed-forward layers with EEE independent expert networks. A gating router WgW_gWg​ computes a probability distribution over the experts and dispatches each token to only the top-kkk experts (typically k=1k=1k=1 or k=2k=2k=2):

H(x)=Softmax(Top-k(x⋅Wg)),y=∑i∈Top-kH(x)i⋅Experti(x)H(x) = \text{Softmax}(\text{Top-}k(x \cdot W_g)), \quad y = \sum_{i \in \text{Top-}k} H(x)_i \cdot \text{Expert}_i(x)H(x)=Softmax(Top-k(x⋅Wg​)),y=∑i∈Top-k​H(x)i​⋅Experti​(x)

The fundamental MoE tradeoff: VRAM vs FLOPs

Mixtral 8x7B contains 47 billion total parameters across its 8 experts per layer. But for any given token, only 2 experts activate, touching approximately 13 billion parameters.

The decoupling is stark:

  • Active parameters (~13B): Dictates compute FLOPs and execution latency per token.
  • Total parameters (~47B): Dictates GPU VRAM capacity requirements. All 47B parameters must reside in VRAM simultaneously. You need enough GPUs to store 47B parameters (at least 90 GB in FP16), even though each token only touches 13B of them.

Without an auxiliary load-balancing loss, routers suffer from winner-take-all collapse: the gating network routes all tokens to 1 or 2 favored experts, leaving the remaining experts untrained and starved of gradients. Training with a load-balancing loss encourages an even distribution of tokens across all available experts.

Inspect top-2 expert routing in code:

top-two-expert-router.py
1import math 2 3experts = ["E0", "E1", "E2", "E3"] 4tokens = ["import", "assertion"] 5router_logits = [ 6 [2.4, 0.3, 0.5, -0.2], 7 [0.1, 2.2, 0.7, 0.0], 8] 9 10for token, logits in zip(tokens, router_logits): 11 ranked = sorted(range(len(logits)), key=lambda i: logits[i], reverse=True)[:2] 12 selected = [logits[i] for i in ranked] 13 peak = max(selected) 14 weights = [math.exp(value - peak) for value in selected] 15 total = sum(weights) 16 weights = [weight / total for weight in weights] 17 assert len(ranked) == 2 and math.isclose(sum(weights), 1.0) 18 routes = list(zip((experts[i] for i in ranked), (round(weight, 3) for weight in weights))) 19 print(f"{token:10s} -> {routes}")
MoE routing output
1import -> [('E0', 0.87), ('E2', 0.13)] 2assertion -> [('E1', 0.818), ('E2', 0.182)]

Why can an engineer run a 47B MoE model faster than a 47B dense model, but still need the same number of GPUs to host it?

Answer

An MoE model only activates a subset of its parameters per token (e.g. 13B active out of 47B total), so it requires far fewer floating-point operations per decode step. However, because different tokens in a sequence route to different experts unpredictably, all 47B parameter weights must reside in GPU memory simultaneously.

Reasoning models and test-time compute

Standard autoregressive generation emits the final response immediately after ingesting the prompt. For complex code diagnosis, mathematical derivation, or multi-step logic, jumping directly to an answer frequently produces plausible-sounding hallucinations.

In 2024 and 2025, reasoning models like OpenAI o1 and DeepSeek-R1 introduced a shift: trading test-time inference compute for solution accuracy.[24]Reference 24Learning to reason with LLMshttps://openai.com/index/learning-to-reason-with-llms/[25]Reference 25DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning.https://arxiv.org/abs/2501.12948

Instead of answering instantly, a reasoning model generates an internal chain of thought before emitting its final answer. The model can:

  1. Formulate a hypothesis and test it against the evidence.
  2. Backtrack when a reasoning path leads to a contradiction.
  3. Compare stack frames, commit histories, and test configurations systematically.

OpenAI exposes this behavior via the reasoning.effort API parameter.[26]Reference 26Reasoning modelshttps://developers.openai.com/api/docs/guides/reasoning For our RUN-842 assistant, a standard model might immediately answer that the test failed due to a timeout. A reasoning model spends extra test-time compute verifying the stack trace, noting that line 41 explicitly reports an expired auth fixture, and producing a grounded root-cause analysis.

Reasoning tokens are billable tokens that consume time and context window space. Teams should evaluate reasoning effort against latency budgets on hard incident cases rather than defaulting to maximum effort for routine tasks.

How do reasoning models differ from standard chat models during inference?

Answer

Standard chat models output their final answer token by token immediately following the prompt. Reasoning models spend additional inference-time compute generating internal chain-of-thought tokens (deliberating, testing alternative hypotheses, and checking for errors) before producing the final response.

Open weights, licensing, and local serving

In early 2023, Meta released the original LLaMA family of models.[12]Reference 12LLaMA: Open and Efficient Foundation Language Models.https://arxiv.org/abs/2302.13971 This release unlocked open-weight model deployment: teams could download checkpoint weights directly, run them on private clusters, quantize them to 4-bit precision, and fine-tune them using Low-Rank Adaptation (LoRA).

When evaluating downloadable models, software teams must distinguish between three terms:

CategoryDefinitionWhat to inspect
Open weightsThe model's learned weight checkpoints are publicly downloadable.Commercial use restrictions, monthly active user thresholds, and acceptable use policies.
Open sourceCode and architecture meet the OSI definition (e.g., Apache 2.0, MIT).Dependencies, compiler compatibility, and integration libraries.
Open dataThe underlying pretraining dataset is accessible and verifiable.Data licensing, copyright status, filtering recipes, and safety boundaries.

Two 2026 model families highlight why weight availability and serving practicalities require separate checks:

Model releasePublished claimsOperational realities
GLM-5.2744B total parameters, ~40B active per token; 1M token context window under an MIT license.[27]Reference 27GLM-5 Repositoryhttps://github.com/zai-org/GLM-5[28]Reference 28GLM-5.2 Model Cardhttps://huggingface.co/zai-org/GLM-5.2In BF16 precision, 744B parameters require roughly 1.49 TB of raw weight storage before accounting for KV cache memory. Serving requires multi-node GPU clusters with fast tensor parallelism.
DeepSeek V4 Flash 0731284B total parameters, 13B active per token; MIT license with attached speculative decoding.[29]Reference 29DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligencehttps://arxiv.org/abs/2606.19348[30]Reference 30DeepSeek-V4-Flash-0731 Model Cardhttps://huggingface.co/deepseek-ai/DeepSeek-V4-Flash-0731The speculative decoding module requires specialized inference runtimes. Serving throughput depends heavily on whether your hardware configuration accommodates the sparse expert communication overhead.

Hosting open weights ensures that proprietary CI logs never leave your VPC boundary. But your team assumes operational ownership of GPU procurement, cluster uptime, driver updates, and throughput optimization.

Why is calling an open-weight model "open source" often inaccurate?

Answer

Open source requires open code, permissive licensing without commercial discrimination, and reproducible build pipelines. Many open-weight releases provide downloadable neural network weights but restrict specific commercial uses, set user caps, or keep pretraining data, training code, and filtering algorithms proprietary.

Evaluating a released model on your workload

Don't select a model for your production assistant based on public leaderboard screenshots. Public benchmarks measure broad capabilities on standardized test sets, not your proprietary logs, company idioms, or latency constraints.

Construct a representative evaluation dataset:

  • 50 failing CI logs requiring exact root-cause extraction.
  • 50 code review notes requiring discrete triage categorization.
  • 50 multi-turn debugging questions requiring cited file paths.

Define an explicit multi-criteria scoring objective. Model selection is a constrained optimization problem:

model-workload-scorecard.py
1candidates = [ 2 {"name": "hosted-fast", "quality": 0.86, "p95_ms": 250, "cost": 0.50, "license_ok": True}, 3 {"name": "hosted-deep", "quality": 0.94, "p95_ms": 1200, "cost": 1.50, "license_ok": True}, 4 {"name": "local-restricted", "quality": 0.90, "p95_ms": 500, "cost": 0.20, "license_ok": False}, 5] 6 7def score(model: dict, latency_penalty: float, cost_penalty: float) -> float: 8 if not model["license_ok"]: 9 return float("-inf") 10 return 100 * model["quality"] - latency_penalty * model["p95_ms"] - cost_penalty * model["cost"] 11 12scenarios = { 13 "live code help": (0.03, 8.0), 14 "nightly audit": (0.003, 4.0), 15} 16 17for scenario, penalties in scenarios.items(): 18 eligible = [model for model in candidates if model["license_ok"]] 19 if not eligible: 20 raise ValueError("no candidate satisfies the license constraint") 21 ranked = sorted(eligible, key=lambda model: score(model, *penalties), reverse=True) 22 winner = ranked[0] 23 print(f"{scenario:13s} winner={winner['name']} score={score(winner, *penalties):.2f}")
Workload scorecard output
1live code help winner=hosted-fast score=74.50 2nightly audit winner=hosted-deep score=84.40

For interactive code completion, latency is paramount: hosted-fast wins because an extra 950 ms delay destroys the developer flow. For a nightly test audit running asynchronously, accuracy dominates: hosted-deep wins.

Why can a model with 94% benchmark accuracy lose to a model with 86% accuracy in a production scorecard?

Answer

Production scorecards account for multi-dimensional constraints including p95 response latency, per-request serving costs, and licensing restrictions. A model with slightly higher accuracy that takes 1.2 seconds to respond or costs 3x more can be disqualified for real-time developer workflows.

Context window scaling and lost in the middle

Modern LLMs advertise context windows spanning 128k to over 1 million tokens. But having a million-token context window doesn't mean the model retrieves information uniformly across all positions.

Liu et al. (2023) documented "Lost in the Middle": language models recall information placed near the extreme beginning (primacy effect) or extreme end (recency effect) of long contexts with high fidelity, but accuracy drops sharply when key information is buried in the middle 20% to 80% of the prompt.[31]Reference 31Lost in the Middle: How Language Models Use Long Contextshttps://aclanthology.org/2024.tacl-1.9/

U-shaped retrieval accuracy curve showing high performance when evidence is placed at the start (76%) or end (72%) of the context window, but dropping significantly in the middle (34%). The right panel illustrates the document slot positions and primacy versus recency attention bias.
Retrieval accuracy follows a distinct U-shaped curve across long prompt contexts. Information placed at the start benefits from early attention sinks; information placed at the end benefits from immediate proximity to generation tokens.

Three factors drive this degradation:

  1. Primacy bias & attention sinks: The initial tokens in a prompt receive disproportionate attention mass across all transformer layers (acting as attention sinks).
  2. Recency bias: Tokens placed immediately before the generation slot sit fresh in local attention, allowing direct attention transfer without degradation across long causal chains.
  3. Attention diffusion: In the middle of a 20-document context, softmax weights diffuse across hundreds of distractor tokens, diluting the signal of the target passage.

Practical prompt engineering mitigations

When feeding large logs or multiple retrieved documents to your developer assistant:

  1. Place system instructions and governing policies at the very top of the prompt.
  2. Place the specific question or task instruction at the very end.
  3. If retrieval yields several candidate snippets, place the most relevant evidence passage either right at the start or directly above the final question.
evidence-near-question.py
1policy = "Rule: explain CI failures using cited log lines." 2older_notes = [ 3 "History: dependency cache restored.", 4 "History: formatter completed.", 5 "History: unit tests started.", 6] 7case = "Evidence: RUN-842 failed at auth_fixture_test.py:41 with expired auth fixture." 8question = "Question: explain the failure and cite the evidence." 9 10prompt_lines = [policy, *older_notes, case, question] 11for position, line in enumerate(prompt_lines, start=1): 12 print(f"{position}: {line}") 13print("evidence_adjacent_to_question:", prompt_lines[-2] == case)
Context packing output
11: Rule: explain CI failures using cited log lines. 22: History: dependency cache restored. 33: History: formatter completed. 44: History: unit tests started. 55: Evidence: RUN-842 failed at auth_fixture_test.py:41 with expired auth fixture. 66: Question: explain the failure and cite the evidence. 7evidence_adjacent_to_question: True

Placing the decisive RUN-842 evidence adjacent to the question mitigates attention diffusion, ensuring the model grounds its response in the actual log error.

Why does retrieval-augmented generation (RAG) remain relevant even when models support 1-million-token context windows?

Answer

RAG filters out thousands of irrelevant distractor tokens, preventing the "Lost in the Middle" retrieval degradation. It also reduces KV cache memory consumption, lowers API billing costs, and improves generation latency by keeping prompt contexts focused on relevant passages.

Match the architecture to the job

Use this decision table when selecting models for engineering pipelines:

Pipeline taskRecommended architecturePrimary selection rationale
Large-scale log embedding & clusteringEncoder-only (e.g. BERT / ModernBERT)Fast bidirectional encoding; outputs compact fixed-size vectors for cosine similarity index.
Real-time code autocompleteCompact dense decoder (e.g. 1B to 3B GQA)Sub-50ms latency; minimal memory bandwidth footprint on local developer machines.
Multi-turn CI debugging assistantAligned chat decoder with GQA (8B to 70B)Follows conversational instructions; robust in-context grounding and code generation.
High-throughput multi-task API gatewaySparse Mixture of Experts (MoE)High total knowledge capacity with low active FLOPs per token during concurrent serving.
Complex multi-file refactoring & mathReasoning model (test-time compute)Spends inference compute deliberating across dependencies before generating the diff.

Common misconceptions

MisconceptionObservable symptomRoot causeEngineering fix
"GPT means any LLM."Engineers refer to open-weight LLaMA checkpoints as "GPTs".GPT is OpenAI's brand name for its proprietary autoregressive model series.Use "decoder-only model" or "LLM" for the general architectural category.
"BERT is completely obsolete."Teams deploy 70B generative models to perform simple log classification.Decoders dominate media coverage.Use lightweight encoder models for embedding search, reranking, and discrete label classification.
"A bigger model always wins."Selecting a 70B model trained on 300B tokens over a 7B model trained on 15T tokens.Assuming parameter count equals intelligence.Check dataset token volume and quality. Over-trained compact models frequently outperform starved massive models.
"Open-weight means free commercial use."A startup builds a commercial product on a model with non-commercial license terms.Confusing public model weights with open-source licensing.Read the model card and license terms before integrating into commercial products.
"Context length guarantees recall."Placing a stack trace in the middle of a 100k-token prompt and wondering why the model misses it.Conflating context capacity with attention retrieval fidelity.Place decisive evidence adjacent to the query or near prompt boundaries, and test recall empirically.

Practice: diagnose the failing assistant

Your first prototype of the RUN-842 assistant exhibits three operational failures:

  1. Behavioral failure: When an engineer sends "Explain why RUN-842 failed at line 41", the model responds by outputting: "Explain why RUN-843 failed at line 12\nExplain why RUN-844 failed at line 99".
  2. Retrieval failure: When you provide a 20-line snippet, the model correctly identifies the expired auth fixture. When you provide the complete 50,000-line build log containing the exact same snippet in the middle, the model claims: "The build succeeded without errors".
  3. Memory failure: A newly proposed 47B MoE model advertises 13B active parameters per token. Your ops team attempts to deploy it on a single GPU with 24 GB of VRAM, and the container crashes immediately with an OutOfMemory CUDA error.

How do you diagnose and fix each of the three failures?

Answer

  1. The first issue indicates a raw base model executing text continuation instead of instruction following. Switch to an aligned instruct or chat checkpoint and verify your application wraps user input with the model's required chat template.
  2. The second issue is a classic "Lost in the Middle" attention degradation failure. Use a log parser or RAG pipeline to isolate the relevant stack trace snippet and place it immediately above the user's question.
  3. The third issue confuses active parameters with memory footprint. Even though the MoE model only computes 13B parameters per token, all 47B parameters must reside in GPU memory simultaneously. In FP16, 47B parameters require ~94 GB of VRAM. Deploy across multiple GPUs or use 4-bit quantization.
Complete the lesson

Mastery Check

Answer every question, then check your score. Score 75% or higher to mark this lesson complete.

1.A team wants a single model to classify review comments, extract run IDs from traces, and write failure explanations. Why is a causal decoder-only model the standard default for this mixed workload?

Correct answer: A causal decoder can express classification, extraction, and explanation through text-to-text prompt continuation without changing output layers.

Decoder-only autoregressive models unify varied tasks into a single text-in, text-out interface: the prompt establishes the task framing, and the next-token prediction loop generates the answer.

2.During LLM inference, how do the computational profiles of the prefill phase and the decode phase differ?

Correct answer: Prefill is compute-bound (GEMM across prompt tokens); decode is memory-bandwidth bound (GEMV loading weights and KV cache per token).

Prefill processes all prompt tokens in parallel using large matrix-matrix multiplications that saturate tensor cores. Decode generates one token at a time, spending most clock cycles streaming model weights and cached KV vectors across memory buses.

3.Hoffmann et al. (2022) introduced the Chinchilla scaling laws. What was their core finding regarding compute-optimal training?

Correct answer: Model parameters and training tokens should scale in roughly equal proportion, requiring about 20 tokens per parameter.

Chinchilla demonstrated that earlier scaling laws left models severely token-starved. For compute optimality, parameters and tokens should scale in roughly equal proportion (~20 tokens per parameter).

4.How does Root Mean Square Normalization (RMSNorm) improve upon standard LayerNorm?

Correct answer: It discards mean-centering and additive bias, scaling strictly by root mean square energy to save memory bandwidth.

Zhang & Sennrich (2019) proved that mean-centering contributes negligible stabilization. RMSNorm scales solely by the root mean square of activations, eliminating a reduction pass and speeding up GPU execution.

5.Why do models adopting SwiGLU activations resize their intermediate hidden dimension from 4 * d_model to roughly 8/3 * d_model?

Correct answer: SwiGLU uses three weight projection matrices instead of two; resizing preserves parameter count and FLOP parity.

Standard MLPs use two weight matrices (up and down), whereas SwiGLU introduces three (gate, up, and down). Scaling intermediate width to ~8/3 d_model keeps total parameters and compute comparable to standard MLPs.

6.What is the primary architectural advantage of Rotary Position Embeddings (RoPE) over absolute positional embeddings?

Correct answer: RoPE rotates Query and Key vector pairs such that their inner product depends strictly on their relative distance (m - n).

RoPE applies orthogonal 2D rotation matrices to Query and Key vectors inside attention heads, ensuring that their dot product depends solely on relative token offset rather than absolute position indices.

7.A model uses 32 query heads and 8 key/value heads. What attention variant is this, and what KV cache memory savings does it provide compared to MHA?

Correct answer: Grouped-Query Attention (GQA); reduces KV cache memory footprint and bandwidth traffic by 75%.

In GQA, multiple query heads share a single KV head (here, 4 query heads per KV head). Because 8 KV heads are stored instead of 32, the KV cache footprint drops to 8/32 = 25% of MHA, yielding a 75% savings.

8.A team deploys a 47B Mixture of Experts (MoE) model with 13B active parameters per token. Which statement accurately describes its hardware requirements?

Correct answer: Inference speed scales with 13B active parameters, but VRAM capacity must accommodate all 47B parameters simultaneously.

MoE decouples active compute from total capacity: each token only executes FLOPs against the top-k routed experts (13B), but all 47B weights must stay resident in GPU VRAM.

9.What is the 'Lost in the Middle' phenomenon observed in long-context language models?

Correct answer: Models recall evidence placed at the start or end of long prompts with high accuracy, but retrieval performance drops sharply in the middle.

Liu et al. (2023) demonstrated that long-context models exhibit a U-shaped performance curve: primacy and recency biases favor prompt edges, while evidence buried among middle distractors suffers attention diffusion.

10.When should an engineering team increase the test-time reasoning effort parameter on an API model?

Correct answer: When solving complex reasoning or multi-file code debugging tasks where verified accuracy justifies extra latency and token cost.

Reasoning models spend extra test-time compute exploring internal hypotheses and checking work. This trades latency and token cost for accuracy, making it valuable for complex diagnosis but wasteful for simple classification.

10 questions remaining.

Next Step
Continue to Prompt Engineering Fundamentals

The `RUN-842` assistant needs more than an aligned model checkpoint: it needs the right evidence, an explicit task specification, and a testable output format. The next lesson applies those controls to an on-call paging decision and explores what happens when critical context is missing.

PreviousLanguage Modeling & Next Tokens
Share this article
XFacebookLinkedInBlueskyRedditHacker NewsEmail
References

Attention Is All You Need.

Vaswani, A., et al. · 2017

https://arxiv.org/abs/1706.03762

BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding.

Devlin, J., et al. · 2019 · NAACL 2019

https://arxiv.org/abs/1810.04805

Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks.

Reimers, N., & Gurevych, I. · 2019 · EMNLP 2019

https://arxiv.org/abs/1908.10084

Improving Language Understanding by Generative Pre-Training.

Radford, A., et al. · 2018

https://cdn.openai.com/research-covers/language-unsupervised/language_understanding_paper.pdf

GPT-2 Source Implementation.

OpenAI · 2019

https://github.com/openai/gpt-2/blob/master/src/model.py

Language Models are Unsupervised Multitask Learners.

Radford, A., et al. · 2019

https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf

Language Models are Few-Shot Learners.

Brown, T., et al. · 2020 · NeurIPS 2020

https://arxiv.org/abs/2005.14165

Training Language Models to Follow Instructions with Human Feedback (InstructGPT).

Ouyang, L., et al. · 2022 · NeurIPS 2022

https://arxiv.org/abs/2203.02155

Transformers Documentation: Chat templates.

Hugging Face · 2026

https://huggingface.co/docs/transformers/main/en/chat_templating

Scaling Laws for Neural Language Models

Kaplan et al. · 2020

https://arxiv.org/abs/2001.08361

Training Compute-Optimal Large Language Models.

Hoffmann, J., et al. · 2022 · NeurIPS 2022

https://arxiv.org/abs/2203.15556

LLaMA: Open and Efficient Foundation Language Models.

Touvron, H., et al. · 2023

https://arxiv.org/abs/2302.13971

LayerNorm

PyTorch Contributors · 2026

https://docs.pytorch.org/docs/stable/generated/torch.nn.LayerNorm.html

Root Mean Square Layer Normalization.

Zhang, B. & Sennrich, R. · 2019 · NeurIPS 2019

https://arxiv.org/abs/1910.07467

RMSNorm

PyTorch Contributors · 2026

https://docs.pytorch.org/docs/stable/generated/torch.nn.RMSNorm.html

GLU Variants Improve Transformer

Shazeer, N. · 2020

https://arxiv.org/abs/2002.05202

RoFormer: Enhanced Transformer with Rotary Position Embedding.

Su, J., et al. · 2021

https://arxiv.org/abs/2104.09864

Fast Transformer Decoding: One Write-Head is All You Need.

Shazeer, N. · 2019 · arXiv preprint

https://arxiv.org/abs/1911.02150

GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints.

Ainslie, J., et al. · 2023 · EMNLP 2023

https://arxiv.org/abs/2305.13245

FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness.

Dao, T., Fu, D. Y., Ermon, S., Rudra, A., & Ré, C. · 2022 · NeurIPS 2022

https://arxiv.org/abs/2205.14135

Efficient Memory Management for Large Language Model Serving with PagedAttention.

Kwon, W., et al. · 2023 · SOSP 2023

https://arxiv.org/abs/2309.06180

Outrageously Large Neural Networks: The Sparsely-Gated Mixture-of-Experts Layer.

Shazeer, N., et al. · 2017 · ICLR 2017

https://arxiv.org/abs/1701.06538

Mixtral of Experts.

Jiang, A. Q., et al. · 2024

https://arxiv.org/abs/2401.04088

Learning to reason with LLMs

OpenAI · 2024

https://openai.com/index/learning-to-reason-with-llms/

DeepSeek-R1: Incentivizing Reasoning Capability in LLMs via Reinforcement Learning.

DeepSeek Team · 2025

https://arxiv.org/abs/2501.12948

Reasoning models

OpenAI · 2026

https://developers.openai.com/api/docs/guides/reasoning

GLM-5 Repository

Z.AI · 2026

https://github.com/zai-org/GLM-5

GLM-5.2 Model Card

Z.AI · 2026

https://huggingface.co/zai-org/GLM-5.2

DeepSeek-V4: Towards Highly Efficient Million-Token Context Intelligence

DeepSeek-AI · 2026

https://arxiv.org/abs/2606.19348

DeepSeek-V4-Flash-0731 Model Card

DeepSeek-AI · 2026

https://huggingface.co/deepseek-ai/DeepSeek-V4-Flash-0731

Lost in the Middle: How Language Models Use Long Contexts

Liu, N.F., et al. · 2024 · TACL 2024

https://aclanthology.org/2024.tacl-1.9/

Discussion

Questions and insights from fellow learners.

Discussion loads when you reach this section.