Dive into Deep Learning
Slides
Courses
GitHub
Discuss
PDF
PyTorch
JAX
TensorFlow
MXNet
Parts (PyTorch)
Basics
Advanced
Language Models
Mathematics for Deep Learning
Notebooks
PyTorch
JAX
TensorFlow
MXNet
en · English
zh · 中文
pt · Português
tr · Türkçe
vi · Tiếng Việt
ko · 한국어
ja · 日本語
en · English (old)
Language Models
29
Efficient Inference
index.html
Preface
Installation
Notation
Introduction
Basics
1
Preliminaries
1.1
Data Manipulation
1.2
Data Preprocessing
1.3
Linear Algebra
1.4
Calculus
1.5
Automatic Differentiation
1.6
Probability and Statistics
2
Linear Regression
2.1
Linear Regression
2.2
Synthetic Data
2.3
Linear Regression Implementation from Scratch
2.4
Object-Oriented Design for Implementation
2.5
Concise Implementation of Linear Regression
2.6
Generalization
2.7
Weight Decay
3
Linear Classification
3.1
Softmax Regression
3.2
Image Classification Data
3.3
Softmax Regression Implementation
3.4
Measuring Classifiers
3.5
Generalization in Classification
3.6
Environment and Distribution Shift
4
Multilayer Perceptrons
4.1
Multilayer Perceptrons
4.2
Implementation
4.3
Backpropagation
4.4
Numerical Stability and Initialization
4.5
Generalization in Deep Learning
4.6
Dropout
4.7
Predicting House Prices on Kaggle
5
Computation
5.1
Modules and Model Construction
5.2
Parameters, State, and Memory
5.3
Initialization
5.4
Custom Layers and Functions
5.5
Numerical Formats
5.6
Saving, Loading, and Pretrained Weights
5.7
GPUs, Devices, and Memory
5.8
Reproducibility and Inspection
6
Convolutional Neural Networks
6.1
From Fully Connected Layers to Convolutions
6.2
Convolutions for Images
6.3
Padding, Stride, and Dilation
6.4
Multiple Input and Multiple Output Channels
6.5
Pooling
6.6
LeNet
7
Modern Convolutional Neural Networks
7.1
AlexNet and Learned Image Representations
7.2
Blocks, Bottlenecks, and Branches: VGG, NiN, GoogLeNet
7.3
Normalization Layers
7.4
Residual Networks
7.5
Efficient Convnets
7.6
Modern Training Recipes for Convnets
7.7
ConvNeXt: Modernizing a ResNet Step by Step
7.8
Convolutional Network Design Spaces
8
Sequence Models
8.1
Working with Sequences
8.2
From Text to Tokens
8.3
Language Models
8.4
Recurrent Neural Networks
8.5
Backpropagation Through Time
8.6
Decoding and Generation
Advanced
9
Optimization Algorithms
9.1
Landscapes and Gradient Descent
9.2
Stochastic and Minibatch Gradient Descent
9.3
Two Test Models
9.4
Momentum
9.5
Adam and AdamW
9.6
Muon
9.7
Schedules
9.8
Batch Size
9.9
Scaling Up
9.10
Practice
9.11
Further Reading
10
Attention
10.1
Queries, Keys, and Values
10.2
Attention Scoring and Masking
10.3
Self-Attention and Multiple Heads
10.4
Positional Information
10.5
The Cost of Attention
10.6
Windowed and Linear Attention
10.7
Attention Circuits
10.8
Further Reading
11
Transformers
11.1
The Transformer Block
11.2
Decoder-Only Language Models (GPT)
11.3
The KV Cache and Cache Reduction
11.4
Encoders, Decoders, and Cross-Attention
11.5
Vision Transformer (ViT)
11.6
Scaling Laws
11.7
Mixture of Experts (MoE)
11.8
Further Reading
12
State Space Models
12.1
Gated Recurrent Networks
12.2
Sequence-to-Sequence Learning
12.3
Linear Recurrences and Parallel Scans
12.4
State Space Models
12.5
Selective State Space Models
12.6
Matrix-State Recurrences
12.7
The Delta Rule
12.8
Hybrid Architectures
12.9
Further Reading
13
Computational Performance
13.1
The Performance Model
13.2
Hardware
13.3
Computation Graphs and Compilation
13.4
Memory and Precision
13.5
Multi-GPU from First Principles
13.6
Data Parallelism in Practice
13.7
Sharding the Training State
13.8
Case Study: Optimizing a Transformer
13.9
Further Reading
14
Reinforcement Learning
14.1
Markov Decision Processes
14.2
Dynamic Programming
14.3
Temporal-Difference Learning
14.4
Exploration
14.5
Learning from Demonstrations
14.6
Optimal Control, LQR, and Model Predictive Control
14.7
Policy Gradient
14.8
Baselines and Variance Reduction
14.9
Function Approximation in Reinforcement Learning
15
Deep Reinforcement Learning
15.1
Actor-Critic Methods and Multi-Step Returns
15.2
Trust Regions and Proximal Policy Optimization
15.3
KL-Regularized Policy Optimization
15.4
Deep Q-Networks
15.5
Soft Actor-Critic
15.6
Learned Models and Planning
15.7
Offline Reinforcement Learning
15.8
Reinforcement Learning for Sequence Generation
15.9
Further Reading
16
Generative Adversarial Networks
16.1
Generative Adversarial Networks
16.2
Adversarial Objectives and Divergences
16.3
Relativistic Objectives
16.4
Gradient Penalties and Convergence
16.5
Adversarial Image Generation
16.6
Conditional Generation
16.7
Adversarial Losses Beyond GANs
16.8
Further Reading
17
Diffusion Models
17.1
Denoising and the Score
17.2
Gaussian Paths: Diffusion and Flow Matching
17.3
Sampling by Integration
17.4
Diffusion for Images
17.5
Samplers, Inversion and Editing
17.6
Likelihood, Weighting and the Design Space
17.7
Latent Diffusion and Transformers
17.8
Few-Step Generation
17.9
Discrete Diffusion
17.10
Further Reading
Language Models
18
Working with Pretrained Language Models
18.1
Pretrained Models with Hugging Face
18.2
A Released Checkpoint in the Book’s Own Code
18.3
Prompting and In-Context Learning
18.4
Scoring Language Models
18.5
Evaluation Suites, Judges and Error Bars
18.6
Interpretability
18.7
Models behind an API
19
Encoders, Embeddings and Retrieval
19.1
Word Embeddings
19.2
Pretrained Encoders
19.3
Text Embeddings from Contrastive Training
19.4
Sparse, Dense and Approximate Retrieval
19.5
Retrieval-Augmented Generation
19.6
Failures and Fixes in Retrieval-Augmented Generation
19.7
Further Reading
20
Data for Language Models
20.1
Document Extraction from Web Crawls
20.2
Quality Filtering
20.3
Deduplication and Decontamination
20.4
Data Mixtures
20.5
Synthetic and Rephrased Data
20.6
Tokenizers at Scale
21
Pretraining
21.1
The Pretraining Data Path
21.2
The Reference Pretraining Run
21.3
Low-Precision Training
21.4
Continued Pretraining and Mid-Training
21.5
Long-Context Training
21.6
Training Budgets beyond the Compute Optimum
22
Hardware for Training and Inference
22.1
Accelerator Design
22.2
Kernels, Tiles and Fusion
22.3
Training, Prefill and Decode Workloads
22.4
Memory and Storage Tiers
22.5
Data Paths inside a Server
22.6
Scale-Up Domains and Scale-Out Networks
22.7
Collective Communication at Scale
22.8
Energy, Power and Cost
23
Parallelism Beyond One Node
23.1
Multi-Node Jobs and Hang Diagnosis
23.2
Sharded Data Parallelism at Scale
23.3
Tensor and Sequence Parallelism
23.4
Pipeline Parallelism
23.5
Context Parallelism
23.6
Expert Parallelism
23.7
Composed Parallelism and Topology
23.8
Profiling and Performance Debugging of Distributed Runs
23.9
Checkpoints at Scale
23.10
Failures, Elasticity and Goodput
24
Fine-Tuning
24.1
Full Fine-Tuning
24.2
Supervised Instruction Tuning
24.3
Instruction Data
24.4
Low-Rank Adaptation
24.5
Quantized Fine-Tuning
24.6
Fine-Tuning at Scale and Toolchains
24.7
Merging and Retaining Capabilities
25
Preference Optimization and Reinforcement Learning
25.1
Reward Models
25.2
RLHF with PPO at Language-Model Scale
25.3
Direct Preference Optimization and Its Variants
25.4
RL with Verifiable Rewards
25.5
RL Training Systems
25.6
Reward Overoptimization
25.7
Safety Evaluation and Red-Teaming
25.8
Further Reading
26
Distillation and Reasoning
26.1
Knowledge Distillation
26.2
On-Policy Distillation
26.3
Training Reasoning Models
26.4
Verifiers and Process Supervision
26.5
Parallel Test-Time Compute
26.6
Sequential and Adaptive Test-Time Compute
27
Agents and Tool Use
27.1
Tool Calling
27.2
Structured Outputs and Constrained Decoding
27.3
Agent Loops and Harnesses
27.4
Training Agents with Reinforcement Learning
27.5
Evaluating and Securing Agents
28
Model Compression
28.1
Quantization for Inference
28.2
Activation Outliers and Equivalent Rescaling
28.3
Reconstruction-Based Quantization
28.4
Rotations and Incoherence
28.5
Quantization-Aware Training and Native Low-Bit Formats
28.6
Pruning and Sparsity
28.7
Further Reading
29
Efficient Inference
29.1
A Fast Decoder on One GPU
29.2
Decode Attention Kernels
29.3
Deterministic and Batch-Invariant Inference
29.4
Speculative Decoding
29.5
Drafters and Multi-Token Prediction
29.6
KV-Cache Quantization
29.7
KV-Cache Eviction and Selective Reading
29.8
Inference on CPUs and Devices
30
Serving Systems
30.1
Workloads and Service Objectives
30.2
Continuous Batching and Chunked Prefill
30.3
Paged KV Memory
30.4
Prefix Caching and Shared-Prefix Workloads
30.5
Scheduling under Overload
30.6
KV-Cache Offload, Stores and Prefix-Aware Routing
30.7
Tensor and Expert Parallelism for Serving
30.8
Prefill and Decode Disaggregation
30.9
Serving Many Adapters
31
Advanced Architectures
31.1
Architecture Configurations
31.2
Mixture-of-Experts Design at Scale
31.3
Latent Attention and Cross-Layer KV Sharing
31.4
Softmax Attention Refinements
31.5
Learned Sparse Attention
31.6
Hybrid Models at Scale
31.7
The Residual Stream across Depth
31.8
Further Reading
Image Models
32
Computer Vision
32.1
Image Augmentation
32.2
Fine-Tuning
32.3
Object Detection and Bounding Boxes
32.4
Anchor Boxes
32.5
Multiscale Object Detection
32.6
The Object Detection Dataset
32.7
Single Shot Multibox Detection
32.8
Region-based CNNs (R-CNNs)
32.9
Semantic Segmentation and the Dataset
32.10
Transposed Convolution
32.11
Fully Convolutional Networks
32.12
Neural Style Transfer
32.13
Image Classification (CIFAR-10) on Kaggle
32.14
Dog Breed Identification (ImageNet Dogs) on Kaggle
Mathematics for Deep Learning
33
Linear Algebra
33.1
Vectors, Angles, and Projections
33.2
Matrices as Linear Maps
33.3
Eigendecompositions
33.4
Spectra in Practice
33.5
Singular Value Decomposition and Low-Rank Approximation
33.6
Further Reading
34
Calculus and Automatic Differentiation
34.1
Single Variable Calculus
34.2
Multivariable Calculus
34.3
The Chain Rule on Computational Graphs
34.4
Matrix Calculus
34.5
Automatic Differentiation
34.6
Integral Calculus
34.7
Expectations and Their Gradients
34.8
Further Reading
35
Optimization
35.1
Gradient Descent and Curvature
35.2
Curvature Methods: Newton, Quasi-Newton, and Trust Regions
35.3
Convexity
35.4
Stochastic Gradient Methods
35.5
Adaptive Methods
35.6
Constrained Optimization
35.7
Duality
35.8
Numerical Stability and Conditioning
35.9
Further Reading
36
Probability and Statistical Learning
36.1
Random Variables
36.2
Joint Distributions and Transformations
36.3
Distributions
36.4
The Exponential Family
36.5
Statistics
36.6
Maximum Likelihood
36.7
Latent Variables, EM, and the ELBO
36.8
Bayesian Computation
36.9
Concentration Inequalities
36.10
Uniform Convergence and Generalization
36.11
Naive Bayes
36.12
Further Reading
37
Information Theory and Divergences
37.1
Entropy, Cross-Entropy, and KL Divergence
37.2
Codes, Compression, and Perplexity
37.3
f-Divergences
37.4
Test Functions and Transport
37.5
Score-Based Comparisons
37.6
Mutual Information
37.7
Estimating Mutual Information
37.8
Further Reading
38
Dynamics: Differential Equations and Generative Flows
38.1
Ordinary Differential Equations
38.2
Numerical Solvers
38.3
Neural ODEs, Adjoints, and Continuous Flows
38.4
Stochastic Differential Equations
38.5
The Fokker–Planck Equation and Probability Flow
38.6
Score Matching and Diffusion Models
38.7
Flow Matching and Optimal Transport
38.8
Further Reading
Tools for Deep Learning
39
Tools for Deep Learning
39.1
Notebooks
39.2
Colab and Kaggle
39.3
Cloud Computing
39.4
Hardware
39.5
Ecosystem
39.6
Distributed Model Training
39.7
Model Serving
39.8
Contributor Guide
39.9
Utility Functions and Classes
39.10
The
d2l
API Reference
Attic
40
Gaussian Processes
40.1
Introduction to Gaussian Processes
40.2
Gaussian Process Priors
40.3
Gaussian Process Inference
41
Hyperparameter Optimization
41.1
What Is Hyperparameter Optimization?
41.2
Hyperparameter Optimization API
41.3
Asynchronous Random Search
41.4
Multi-Fidelity Hyperparameter Optimization
41.5
Asynchronous Successive Halving
42
Recommender Systems
42.1
Overview of Recommender Systems
42.2
The MovieLens Dataset
42.3
Matrix Factorization
42.4
AutoRec: Rating Prediction with Autoencoders
42.5
Personalized Ranking for Recommender Systems
42.6
Neural Collaborative Filtering for Personalized Ranking
42.7
Sequence-Aware Recommender Systems
42.8
Feature-Rich Recommender Systems
42.9
Factorization Machines
42.10
Deep Factorization Machines
References
Language Models
29
Efficient Inference
29.8
Inference on CPUs and Devices
29.7
KV-Cache Eviction and Selective Reading
30
Serving Systems