LLaMA: Open and Efficient Foundation Language Models¶
Authors: Hugo Touvron, Thibaut Lavril, Gautier Izacard, et al. (Meta AI) Published: February 2023 Paper: arxiv.org/abs/2302.13971
Why This Matters¶
LLaMA democratized large language models by proving that smaller, openly accessible models could match or exceed the performance of much larger proprietary models. This breakthrough:
- Made LLMs accessible: Researchers and developers could run models locally
- Proved training matters: Showed that training longer on more data beats just scaling parameters
- Sparked open-source revolution: Led to Alpaca, Vicuna, and hundreds of derivatives
- Applied scaling laws correctly: Used Chinchilla-optimal training (more tokens, not just more parameters)
- Efficient inference: Smaller models mean practical deployment
Real-world impact: - Foundation for LLaMA 2, LLaMA 3 (widely used open models) - Enabled research without billion-dollar compute budgets - Spawned ecosystem of fine-tuned models - Changed industry from "bigger is better" to "trained better is better"
LLaMA showed the AI community that open, efficient models could compete with giants.
The Problem¶
State of LLMs in early 2023:
- Size obsession
- GPT-3: 175B parameters
- PaLM: 540B parameters
-
Models getting bigger and more expensive
-
Limited accessibility
- Proprietary models behind APIs
- Too large for academic research
- Impossible to run locally
-
Expensive inference
-
Undertrained models
- Most models trained on ~300B tokens
- Scaling laws suggested more training data optimal
-
GPT-3 and similar were compute-inefficient
-
Closed development
- No access to model weights
- Can't fine-tune or study internals
- Reproducibility crisis
The questions: - Can smaller models match larger ones with better training? - How small can we go while maintaining performance? - What happens if we train for longer?
Core Innovation¶
Chinchilla-Optimal Training at Multiple Scales¶
The key insight: Train smaller models on more data (following Chinchilla scaling laws).
LLaMA's approach: 1. Multiple model sizes: 7B, 13B, 33B, 65B parameters 2. More training tokens: Up to 1.4 trillion tokens (vs typical 300B) 3. High-quality data: Curated mixture from public sources 4. Efficient architecture: Standard Transformer with optimizations 5. Open release: Weights available for research (later fully open with LLaMA 2)
Comparison to GPT-3: - GPT-3: 175B params, ~300B tokens - LLaMA-13B: 13B params, 1T tokens → matches GPT-3 on many tasks - LLaMA-65B: 65B params, 1.4T tokens → beats GPT-3 and competitors
Result: 13× smaller model with comparable performance through better training.
Model Architecture¶
Transformer Decoder (GPT-style)¶
Base architecture: Standard autoregressive transformer decoder
Key modifications and choices:
1. Pre-normalization (GPT-3 style) - Layer norm before attention/FFN instead of after - Training stability for large models - From GPT-3 and PaLM
2. SwiGLU Activation (PaLM style) - Replace ReLU with SwiGLU - Better performance (from PaLM paper) - FFN dimension: 2.67 × hidden_dim (instead of 4×)
3. Rotary Positional Embeddings (RoPE) - Replace absolute position embeddings - Better extrapolation to longer sequences - From GPTNeo and GPT-J
4. Increased Context Length - 2048 tokens (standard at the time) - Can extrapolate further with RoPE
5. Optimizations - Efficient attention (xformers library) - Activation checkpointing - Mixed precision (bfloat16)
Model Sizes¶
| Model | Params | Layers | Hidden Dim | Heads | Context | Training Tokens |
|---|---|---|---|---|---|---|
| LLaMA-7B | 7B | 32 | 4096 | 32 | 2048 | 1.0T |
| LLaMA-13B | 13B | 40 | 5120 | 40 | 2048 | 1.0T |
| LLaMA-33B | 33B | 60 | 6656 | 52 | 2048 | 1.4T |
| LLaMA-65B | 65B | 80 | 8192 | 64 | 2048 | 1.4T |
Training compute: - 7B: ~82k GPU-hours (A100) - 13B: ~135k GPU-hours - 33B: ~530k GPU-hours - 65B: ~1M GPU-hours
Inference requirements: - 7B: ~14GB RAM (16-bit) - 13B: ~26GB RAM - 65B: ~130GB RAM
Practical deployment: 7B and 13B run on consumer GPUs!
Training Data¶
High-Quality, Public Datasets¶
Total: 1.4 trillion tokens from diverse public sources
Dataset mixture:
| Source | Tokens | Percentage | Description |
|---|---|---|---|
| CommonCrawl | 67% | ~940B | Web crawl data (CCNet, filtered) |
| C4 | 15% | ~210B | Colossal Clean Crawled Corpus |
| GitHub | 4.5% | ~63B | Public code repositories |
| Wikipedia | 4.5% | ~63B | English Wikipedia |
| Books | 4.5% | ~63B | Gutenberg + Books3 |
| ArXiv | 2.5% | ~35B | Scientific papers (LaTeX) |
| StackExchange | 2% | ~28B | Q&A from Stack Exchange |
Key features: - Only public data: Reproducible by anyone - Multilingual: 20+ languages (mostly English) - Diverse domains: Code, science, general knowledge - Heavily filtered: Quality over quantity
Data Processing¶
CommonCrawl filtering: 1. Language identification (fastText) 2. Deduplication (MinHash) 3. Quality filtering (n-gram model classifier) 4. Removed adult content
Tokenization: - BPE tokenizer (SentencePiece) - Vocabulary size: 32k tokens - Handles code and multilingual text
Training Methodology¶
Compute-Optimal Training¶
Following Chinchilla laws: - Train longer than typical models - 1-1.4 trillion tokens (vs GPT-3's ~300B) - ~20× more tokens per parameter than GPT-3
Training configuration: - Optimizer: AdamW (β₁=0.9, β₂=0.95) - Learning rate: Cosine schedule (peak: 3e-4) - Weight decay: 0.1 - Gradient clipping: 1.0 - Warmup: 2000 steps - Batch size: 4M tokens
Hardware: - Meta's RSC cluster - A100 GPUs (80GB) - Thousands of GPUs in parallel
Training time: - 7B: ~21 days - 65B: ~21 days (same wallclock with more GPUs)
Efficiency optimizations: - FSDP (Fully Sharded Data Parallel) - Flash Attention - Activation checkpointing - Mixed precision (bfloat16)
Results and Performance¶
Benchmark Performance¶
Common-sense reasoning (0-shot):
| Model | BoolQ | PIQA | SIQA | HellaSwag | WinoGrande | ARC-e | ARC-c |
|---|---|---|---|---|---|---|---|
| GPT-3 (175B) | 60.5 | 81.0 | - | 78.9 | 70.2 | 68.8 | 51.4 |
| Chinchilla (70B) | 83.7 | 81.8 | - | 80.8 | 74.9 | - | - |
| LLaMA-65B | 86.1 | 82.8 | 80.8 | 84.2 | 77.0 | 78.9 | 56.0 |
| LLaMA-13B | 78.1 | 79.8 | 76.2 | 79.2 | 73.0 | 74.8 | 52.7 |
Closed-book QA (5-shot):
| Model | Natural Questions | TriviaQA |
|---|---|---|
| GPT-3 (175B) | 29.9 | 71.2 |
| Gopher (280B) | 28.2 | 81.3 |
| LLaMA-65B | 31.0 | 85.0 |
| LLaMA-13B | 26.0 | 79.9 |
Reading comprehension:
| Model | RACE-middle | RACE-high |
|---|---|---|
| GPT-3 (175B) | 58.4 | 45.5 |
| LLaMA-65B | 61.8 | 47.9 |
| LLaMA-13B | 57.6 | 46.9 |
Mathematical reasoning (few-shot):
| Model | MATH | GSM8k |
|---|---|---|
| GPT-3 (175B) | - | 34.6 |
| Minerva (540B) | 16.2 | 58.8 |
| LLaMA-65B | 10.6 | 50.9 |
| LLaMA-7B | 2.9 | 11.0 |
Key Findings¶
1. LLaMA-13B matches GPT-3 (175B) - 13× smaller - Better on most tasks - Validates Chinchilla-optimal training
2. LLaMA-65B beats all predecessors - Outperforms Chinchilla (70B) - Competitive with PaLM (540B) on many tasks - 8× smaller than PaLM
3. Size-performance trade-offs - 7B: Great for research, limited capabilities - 13B: Sweet spot for many applications - 33B/65B: State-of-the-art performance
4. Longer training helps across all sizes - Continued improvement past 1T tokens - Hadn't fully converged at end of training
Impact and Influence¶
Immediate Impact (2023)¶
1. Open-source explosion - Alpaca (Stanford): Instruction-tuned LLaMA-7B - Vicuna: Chatbot from LLaMA with user conversations - Koala: Academic chatbot research - GPT4All: Quantized LLaMA for CPUs
2. Research democratization - Universities could afford to experiment - Fine-tuning became accessible - Hundreds of derivatives and adaptations
3. Commercial applications - Startups built on LLaMA derivatives - Enterprise on-premise deployments - Privacy-preserving AI (local inference)
4. Quantization and efficiency - GPTQ, GGML: Run LLaMA on consumer hardware - 4-bit quantization: 7B model in <4GB RAM - Edge deployment (phones, laptops)
Subsequent Releases¶
LLaMA 2 (July 2023): - Fully open license (commercial use allowed) - Improved training (2T tokens) - 7B, 13B, 70B sizes - Instruction-tuned variants (LLaMA 2-Chat)
LLaMA 3 (2024): - Further improvements - Even longer training - Multimodal capabilities
Influence on Field¶
Paradigm shift: - From "biggest model wins" to "best trained wins" - Compute-optimal training becomes standard - Open models competitive with closed
Scaling law validation: - Chinchilla laws proven in practice - Training data as important as parameters - Efficient training over brute force scaling
Accessibility: - Lowered barrier to entry - Enabled research in developing countries - Shift toward open AI development
Limitations¶
1. Hallucination¶
- Still generates plausible but false information
- No grounding mechanism
- Needs RAG or fact-checking for critical applications
2. Outdated Knowledge¶
- Training data cutoff (2022)
- Can't access recent information
- Requires updating or retrieval augmentation
3. Limited Multilingual Performance¶
- Primarily English-focused
- Other languages underrepresented
- Reflects training data bias
4. No Instruction Tuning (Base Model)¶
- Not aligned for chat/assistant use
- Requires RLHF or instruction fine-tuning
- Addressed in LLaMA 2-Chat
5. Toxicity and Bias¶
- Inherits biases from web data
- Can generate harmful content
- Needs safety measures for deployment
6. Context Length¶
- 2048 tokens (short by modern standards)
- Addressed in later versions
- Limits long-document tasks
Practical Applications¶
1. Research and Experimentation¶
from transformers import LlamaForCausalLM, LlamaTokenizer
# Load model (fits on single GPU for 7B/13B)
model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-7b")
tokenizer = LlamaTokenizer.from_pretrained("meta-llama/Llama-7b")
# Generate text
prompt = "The future of AI is"
inputs = tokenizer(prompt, return_tensors="pt")
outputs = model.generate(**inputs, max_length=100)
print(tokenizer.decode(outputs[0]))
2. Fine-Tuning for Specific Domains¶
from peft import LoraConfig, get_peft_model
# Efficient fine-tuning with LoRA
lora_config = LoraConfig(
r=8,
lora_alpha=32,
target_modules=["q_proj", "v_proj"],
lora_dropout=0.1
)
model = get_peft_model(model, lora_config)
# Fine-tune on domain data
# (medical, legal, code, etc.)
3. Quantization for Edge Deployment¶
# GPTQ 4-bit quantization
from auto_gptq import AutoGPTQForCausalLM
model = AutoGPTQForCausalLM.from_quantized(
"TheBloke/Llama-7B-GPTQ",
device="cuda:0",
use_safetensors=True
)
# Now runs in ~4GB RAM!
4. Chatbot Development¶
# Instruction fine-tuning (Alpaca-style)
instruction_template = """Below is an instruction that
describes a task. Write a response that appropriately
completes the request.
### Instruction:
{instruction}
### Response:
"""
# Fine-tune on instruction dataset
5. RAG Systems¶
from langchain.llms import LlamaCpp
from langchain.vectorstores import Chroma
# Local LLaMA for RAG
llm = LlamaCpp(model_path="llama-7b.gguf")
# Combine with retrieval
qa_chain = RetrievalQA.from_chain_type(
llm=llm,
retriever=vectorstore.as_retriever()
)
Technical Details¶
Architecture Pseudocode¶
class LLaMABlock(nn.Module):
def __init__(self, hidden_dim, num_heads):
# Pre-norm architecture
self.attention_norm = RMSNorm(hidden_dim)
self.attention = Attention(hidden_dim, num_heads)
self.ffn_norm = RMSNorm(hidden_dim)
self.ffn = SwiGLU(hidden_dim)
def forward(self, x):
# Attention with residual
h = x + self.attention(self.attention_norm(x))
# FFN with residual
out = h + self.ffn(self.ffn_norm(h))
return out
class Attention(nn.Module):
def __init__(self, hidden_dim, num_heads):
self.q_proj = Linear(hidden_dim, hidden_dim)
self.k_proj = Linear(hidden_dim, hidden_dim)
self.v_proj = Linear(hidden_dim, hidden_dim)
self.o_proj = Linear(hidden_dim, hidden_dim)
self.rotary_emb = RotaryEmbedding(dim=hidden_dim // num_heads)
def forward(self, x):
q = self.q_proj(x)
k = self.k_proj(x)
v = self.v_proj(x)
# Apply rotary embeddings
q, k = self.rotary_emb(q, k)
# Attention
attn_output = scaled_dot_product_attention(q, k, v)
return self.o_proj(attn_output)
class SwiGLU(nn.Module):
def __init__(self, hidden_dim):
ffn_dim = int(2.67 * hidden_dim) # Adjusted for SwiGLU
self.w1 = Linear(hidden_dim, ffn_dim)
self.w2 = Linear(ffn_dim, hidden_dim)
self.w3 = Linear(hidden_dim, ffn_dim)
def forward(self, x):
return self.w2(F.silu(self.w1(x)) * self.w3(x))
Training Optimizations¶
Memory efficiency:
# FSDP (Fully Sharded Data Parallel)
from torch.distributed.fsdp import FullyShardedDataParallel
model = FullyShardedDataParallel(
model,
auto_wrap_policy=transformer_auto_wrap_policy
)
# Activation checkpointing
model.gradient_checkpointing_enable()
# Flash Attention
from flash_attn import flash_attn_func
LLaMA vs Competitors (2023)¶
| Model | Params | Tokens | Access | Performance | Inference Cost |
|---|---|---|---|---|---|
| GPT-3 | 175B | 300B | API only | Baseline | High |
| Chinchilla | 70B | 1.4T | Closed | Better | High |
| PaLM | 540B | 780B | Closed | Better | Very high |
| LLaMA-65B | 65B | 1.4T | Open | Best | Medium |
| LLaMA-13B | 13B | 1T | Open | ~GPT-3 | Low |
| LLaMA-7B | 7B | 1T | Open | Good | Very low |
LLaMA advantages: - Open weights (research license, later fully open) - Multiple sizes for different use cases - Efficient inference - Reproducible training data
Key Takeaways¶
- Training beats size: 13B with good training matches 175B with poor training
- Chinchilla laws work: More tokens > more parameters at same compute
- Open models competitive: Can match proprietary models
- Multiple sizes valuable: 7B for experimentation, 65B for performance
- Public data sufficient: Don't need proprietary data for strong performance
- Accessibility matters: Democratized LLM research and development
Further Reading¶
Original Papers¶
- LLaMA: https://arxiv.org/abs/2302.13971
- LLaMA 2: https://arxiv.org/abs/2307.09288
- Chinchilla (Scaling Laws): https://arxiv.org/abs/2203.15556
Derivative Work¶
- Alpaca: https://crfm.stanford.edu/2023/03/13/alpaca.html
- Vicuna: https://lmsys.org/blog/2023-03-30-vicuna/
- Llama.cpp: https://github.com/ggerganov/llama.cpp
Code and Models¶
- Official LLaMA: https://github.com/facebookresearch/llama
- Hugging Face Transformers: https://huggingface.co/meta-llama
- GPTQ Quantized: https://huggingface.co/TheBloke
Practical Guides¶
- Fine-tuning LLaMA: Hugging Face docs
- Quantization Guide: GPTQ, GGML tutorials
- LLaMA for Research: Papers with Code
Analysis and Reviews¶
- LLaMA Analysis (Yannic Kilcher): YouTube
- Scaling Law Verification: Research blogs
- Open-source Impact Study: Various articles
Published: February 2023 Impact Factor: 2,000+ citations (in <2 years!) Legacy: Democratized LLMs, validated scaling laws, sparked open-source AI revolution, proved smaller well-trained models can compete with giants.
Related in This Collection¶
- Language Models are Few-Shot Learners (GPT-3)
- Training Language Models to Follow Instructions with Human Feedback (InstructGPT)
- Scaling Laws for Neural Language Models
- Retrieval-Augmented Generation for Knowledge-Intensive NLP Tasks (RAG)
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness
- LLaMA 2: Open Foundation and Fine-Tuned Chat Models
- Training Compute-Optimal Large Language Models (Chinchilla)
- RoFormer: Enhanced Transformer with Rotary Position Embedding (RoPE)