LLM Training
Pretraining Language Models β Learning Language from the Internet
Pre-training is the foundational stage of LLM development, where models learn general language representations from massive text corpora. This guide covers training objectives, loss functions, and the scaling laws that govern this process.
- CLM vs MLM β Causal Language Modeling dominates modern LLM training
- Scaling Laws β Model size and data should scale equally with compute
- Data Quality β Deduplication and curation matter as much as quantity
Scale is not just a featureβit is the strategy.
Pre-training Language Models
Pre-training is the foundational stage of LLM development, where models learn general language representations from massive text corpora. This tutorial covers the objectives, loss functions, and scaling laws that govern this process.
Language Modeling Objectives
Causal Language Modeling (CLM)
CLM is the objective used by GPT, LLaMA, and most modern LLMs. The model predicts the next token given all previous tokens.
Masked Language Modeling (MLM)
MLM is the objective used by BERT. The model predicts randomly masked tokens given the bidirectional context.
Cross-Entropy Loss
The training objective for language models is typically the cross-entropy loss between the model's predicted distribution and the true token distribution.
Perplexity
Perplexity is the standard evaluation metric for language models, measuring how well the model predicts the test data.
Intuitively, perplexity represents the average branching factor---the number of equally likely next tokens the model considers at each position.
Training Data
The quality and diversity of training data are critical for LLM performance.
Data Sources
- Web crawls: Common Crawl, C4, RefinedWeb
- Books: Books3, Gutenberg
- Code: GitHub, StackOverflow
- Academic: arXiv, S2ORC
- Wikipedia: Multiple languages
Data Quality Pipeline
- Deduplication: Remove duplicate documents (exact and fuzzy)
- Filtering: Remove low-quality, toxic, or PII content
- Re-weighting: Adjust domain proportions based on quality
- Tokenization: Convert to token sequences
Chinchilla Scaling Laws
The Chinchilla paper (Hoffmann et al., 2022) established optimal scaling relationships between model size and data size.
This implies that for optimal performance, model size and data size should scale equally with compute. This challenged the prior paradigm of training very large models on insufficient data.
Chinchilla vs GPT-3
| Model | Parameters | Tokens | Tokens/Param | FLOPs |
|---|---|---|---|---|
| GPT-3 | 175B | 300B | 1.7 | 3.1e23 |
| Chinchilla | 70B | 1.4T | 20 | 5.0e23 |
Chinchilla achieves better performance with fewer parameters but more data, demonstrating the importance of data scaling.
Curriculum Learning
Curriculum learning involves presenting training data in a structured order, from easy to hard examples.
Practical Example: Pre-training with HuggingFace
from transformers import (
AutoModelForCausalLM,
AutoTokenizer,
TrainingArguments,
Trainer,
DataCollatorForLanguageModeling
)
from datasets import load_dataset
model_name = "meta-llama/Llama-2-7b-hf"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(model_name)
dataset = load_dataset("wikitext", "wikitext-103-raw-v1", split="train")
def tokenize(examples):
return tokenizer(examples["text"], truncation=True, max_length=2048)
tokenized = dataset.map(tokenize, batched=True, remove_columns=["text"])
training_args = TrainingArguments(
output_dir="./llama2-pretrained",
per_device_train_batch_size=4,
gradient_accumulation_steps=8,
learning_rate=2e-4,
warmup_steps=1000,
max_steps=100000,
fp16=True,
logging_steps=100,
save_steps=10000,
optim="adamw_torch",
weight_decay=0.1,
lr_scheduler_type="cosine",
)
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized,
data_collator=DataCollatorForLanguageModeling(tokenizer, mlm=False),
)
trainer.train()
Practice Exercises
-
Mathematical: Calculate the perplexity of a model with cross-entropy loss of 3.2 nats. How does this compare to a model with loss of 2.8 nats?
-
Analysis: Given a compute budget of 1e24 FLOPs, what is the Chinchilla-optimal model size and training data size? How does this compare to GPT-3's configuration?
-
Implementation: Implement a simple CLM training loop from scratch using PyTorch. Train a small model on a text file and track perplexity over training.
-
Research: Compare the data mixing proportions used in LLaMA 2, Mistral, and Qwen. How do their domain weights differ?
What to Learn Next
-> Fine-Tuning LLMs Customizing language models for your specific tasks and domains.
-> LoRA and PEFT Efficient fine-tuning without full retraining using low-rank adaptation.
-> QLoRA and Quantization Running LLMs on consumer hardware with INT4 quantization.
-> RLHF and Alignment Making LLMs safe and helpful through reinforcement learning from human feedback.
-> Constitutional AI Reducing dependence on human annotation through AI self-alignment.
-> Scaling Laws and Chinchilla Understanding the mathematical relationships governing model performance.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type β Always start by categorizing the problem
- Incorrect formula application β Verify the formula applies to your specific case
- Unit errors β Always check units are consistent throughout
- Sign errors β Pay careful attention to positive and negative values
- Rounding too early β Keep full precision until the final answer
- Forgetting constraints β Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic β Build on the foundation laid here
- Next Topic β See how this concept extends to more advanced material
- Practice Problems β Additional exercises with solutions
- Video Explanation β Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions