10-605 / 10-405
Machine Learning from Large Datasets
Recap: the course so far
2
This week
3
Today
Follows Fall 2024 lectures from Prof. Smith closely
“Recap”: Autodiff for Logistic Regression
5
Generalized logistic
where:
Multiclass (K classes)
x.dot(W) : gives vector of K scalars
w is a D x K matrix
Compare predictions softmax(xW) to y
Extension: Deeper Logistic Regression
6
h1 = softmax(dot(x, w1))
h2 = softmax(dot(h1, w2))
…
h5 = softmax(dot(h4, w5))
r = l2NormSq(w1) + …. + l2NormSq(w5)
loss = crossEntropy(h5, y) + r
h1 = reLU(dot(x, w1) + b1)
h2 = reLU(dot(h1, w2) + b2)
…
h5 = reLU(dot(h4, w5) + b5)
r = l2NormSq(w1) + …. + l2NormSq(w5)
loss = crossEntropy(h5, y) + r
Not all data is linear …
REC ALL
Deeper models can learn non-linear hypotheses….
Non-convex loss
CH AL L E NGE
Types of stationary points:
∥x − x̂∥ ≤ ϵ
∥x − x̂∥ ≤ ϵ
Non-convexity
Types of stationary points:
CH AL L E NGE
Non-convexity
What makes non-convex optimization difficult?
CH AL L E NGE
Most modern ML methods are parallel variants of mini-batch SGD
11
Streaming SGD:
x1
x2
x3
w
x4
x5
x6
12
Streaming SGD:
X1
X2
X3
w
X4
X5
X6
Minibatch examples X, y
Limitations are
Methods for distributed optimization
less local computation, more communication
more local computation, less communication
stochastic gradient descent
gradient descent
divide-and-conquer / one-shot averaging
mini-batch SGD
local-updating methods
REC ALL
Mini-batch SGD
REC ALL
Gradient Descent Update:
wi+1 = wi − αi ∇f(wi)
Objective we want to solve:
min f(w)
w
n
where f(w) := ∑ fj(w)
j=1
Stochastic Gradient Descent (SGD) Update:
wi+1 = wi − αi ∇fj(wi)
with j sampled at random
Mini-batch SGD Update:
i
i+1 i i ℬ i
w = w − α ∇f (w )
with mini-batch Bi ⊆{1,… n} sampled at random
How to parallelize mini-batch SGD?
Parallelization scheme can vary based on:
QU ES TION
*Note: While we motivate these approaches with mini-batch SGD, they are applicable to the other first-order optimization methods (AdaGrad, RMSProp, Adam, etc.)
Data parallel vs. model parallel
Data parallel
Model parallel
Also - hybrid schemes!
Model parallel
intra-layer parallelism: units within one layer are partitioned
inter-layer parallelism: entire layers are partitioned
Tensor parallelism is a kind of intra-layer parallelism
Pipeline parallelism schedules inter-layer parallelism
https://docs.graphcore.ai/projects/tf-model-parallelism/en/latest/pipelining.html
Pipeline parallelism schedules inter-layer parallelism
https://docs.graphcore.ai/projects/tf-model-parallelism/en/latest/pipelining.html
Example of combined/hybrid parallelism: FSDP
Which strategy to use? (In Pytorch)
https://pytorch.org/tutorials/beginner/dist_overview.html
How to parallelize mini-batch SGD?
Parallelization scheme can vary based on:
QU ES TION
Recap: Synchronous vs. asynchronous
asynchronous
Image: Dimitris Papailiopoulos
synchronous
Recap: Asynchronous ML
Image: Dimitris Papailiopoulos
if no conflicts (e.g., when the shared parameter, x, is very sparse), asynchrony is great!
recall: HogWild!
Recommended when:
For GPUs: Synchronous vs. asynchronous
asynchronous
Image: Dimitris Papailiopoulos
synchronous
How to parallelize mini-batch SGD?
Parallelization scheme can vary based on:
QU ES TION
Recap: Tree AllReduce
Common pattern in ML:
1.
…
Tree vs Ring AllReduce
Common pattern in ML:
High-even clusters (e.g. TPU Pods) have high-bandwidth connections between some (not all pairs) of accelerators
Common topology: a ring / mesh / torus in 2D or 3D.
Ring all-reduce has different goals
Baidu, Horovod (Uber), 2017
Ring AllReduce
Ring AllReduce
Ring AllReduce
Everyone sends small messages to the next worker in the ring
Everyone picks a different subset of the parameters to send
Ring AllReduce
Workers reduce their local content with the incoming messages
a0 + b0
c1 + b1
c2 + d2
a3 + d3
Ring AllReduce
Everyone sends another slice to the next worker in the ring
a0 + b0
c1 + b1
c2 + d2
a3 + d3
…sending the most reduced slice they can
Ring AllReduce
Everyone sends another slice to the next worker in the ring
a0 + b0
c1 + b1
c2 + d2
a3 + d3
…sending the most reduced slice they can
Iterate until all slices are full reduced (all data has gone the way around the ring)
Ring AllReduce
Then continue passing the fully-reduced data until everybody has seen it
Ring AllReduce
Q: how many rounds of communication?
Q: how much is communicated per round per node?
2 (m-1)
k / m
m = ring cycle
k = size of param vector
Outline
One major issue with mini-batch SGD
Systems perspective: large batches are good
Two problems with large batches:
Diminishing returns
large batches perform worse when they lack diversity, e.g., when data is replicated by some factor (R)
Yin et al., AISTATS 2018
a potential explanation: gradient diversity, i.e.,
the degree to which gradients differ from one another
Poor generalization
EVEN WORSE
large batch methods converge to lower test accuracy than small batches
Keskar, et al, ICLR 2017
Recap: Stochastic gradient descent
sample a “minibatch” of examples B<<N
approximate the gradient using them
“on average” this is the right direction
take a step in that direction
repeat….
43
For non-convex problems SGD/GD find a local minimum. SGD tends to find “flat”, not “sharp” local minima.
Variants of SGD are almost always used in deep learning.
Outline
Non-convexity
RECALL
What makes non-convex optimization difficult?
Techniques for large-scale optimization
Reduce computation:
•
•
•
Parallelize/distribute computation Use stochastic methods
Use first-order methods (i.e., no 2nd-order or higher gradients)
Reduce communication:
•
•
Keep large objects local Reduce iterations
REC ALL
Reducing iterations
We are interested in optimization techniques that reduce the total number of iterations until convergence
However, these techniques often come at a price: more computation
Will discuss two popular techniques for DL that aim to reduce iterations with little additional computation:
Gradient descent
We can move anywhere in Rd
Negative gradient is direction of
steepest descent!
2D Example:
•
•
Function values are in black/white and black represents higher values
Arrows are gradients
"Gradient2" by Sarang. Licensed under CC BY-SA 2.5 via Wikimedia Commons http://commons.wikimedia.org/wiki/File:Gradient2.svg#/media/File:Gradient2.svg
Update Rule: wt+1 = wt − α ∇f(wt)
Negative Slope
Step Size
REC ALL
Motivation: Newton’s method
issue: gradient may be the same despite varying curvature
Newton’s method uses second order information to estimate the curvature of the function being minimized (i.e., the loss)
Motivation: Newton’s method
issue: gradient may be the same despite varying curvature
Newton’s method uses second order information to estimate the curvature of the function being minimized (i.e., the loss)
Motivation: Newton’s method
issue: gradient may be the same despite varying curvature
Newton step makes more progress
Newton’s method uses second order information to estimate the curvature of the function being minimized (i.e., the ERM objective)
Motivation: Newton’s method
Newton’s method
−1
wt+1 = wt − α ( ∇2f(wt)) ∇f(wt)
can we do better? idea: use second order information (i.e., the Hessian)
can significantly reduce number of iterations needed for convergence however, comes at a cost: need to invert k×k matrix → O(k3)
often too expensive for neural networks (k can be in the millions)
Gradient descent
REC ALL
wt+1 = wt − α ∇f(wt)
Motivation: Newton’s method
But – for functions with n inputs the “second derivative” is too large.
Hessian matrix:
Motivation: Newton’s method
Hessian is worse than the Jaccobian (from autodiff)
AdaGrad
A CHEAPER APPRO A CH
AdaGrad:
else
wt+1
t
= w − αG
−1
t
t
∇f(w )
(Gt)ii = ∑
t
j=1
∇f(w )
2
j i
, 0
where
Duchi et al, 2011
RMSProp
RMSProp:
w = w − αG
−1
t+1 t t
t
∇f(w )
(Gt)ii = (gt)i , 0
where
else
i
(gt) = β(gt−1)i + (1 − β)( ∇f(wt)i)2
AN A LT ERN ATI VE T O AD A GRAD
Hinton [Coursera], 2012
Momentum
Polyak momentum
wt+1 = wt − α ∇f(wt) + β(wt − wt−1)
AN O T HER APPRO A CH
Polyak ‘64
Nesterov accelerated gradient
Nesterov momentum
wt+1 = wt − α ∇f(wt + β(wt − wt−1)) + β(wt − wt−1)
Nesterov ’83
evaluate gradient at ‘look-ahead’ point
who is Adam:
ON E L A S T M E THOD: C OM B IN E EV ER Y THIN G!
Adam:
w = w − αG
−1
v
t+1 t t t
(Gt)ii =
(gt)i , 0
where
else
i
(gt) = β (g
1 t−1 i 1 t i
) + (1 − β )( ∇f(w ) )2
vt = β2vt−1 + (1 − β2) ∇f(wt)
RMSProp update
Momentum
Early stopping
ON E L A S T TRIC K …
Outline
Training in the Foundation Model Era: Fine-Tuning
How are foundation models changing deep learning?
https://dataforest.ai/blog/ai-foundation-models-for-big-business-innovation
Classical ML
model
data
training
ML in the foundation model era
pretrained
lots of data!
pretraining
aligned
alignment
finetuned
flnetuning
How do we ‘finetune’ models?
Start with a model trained on a lot of data
Using the pretrained model as a starting point, update the weights by fitting the model to a small amount of finetuning data (e.g., running a small number of steps of gradient descent)
Credit: AssemblyAI
How do we ‘finetune’ models?
Start with a model trained on a lot of data
Using the pretrained model as a starting point, update the weights by fitting the model to a small amount of finetuning data (e.g., running a small number of steps of gradient descent)
Credit: Prasad Mahamulkar
Gradient descent for model training
Start at a random point, w0
Repeat
Determine a descent direction Choose a step size
Update model using gradient calculated from training data
Until stopping criterion is satisfied
Update Rule:
wi+1 = wi — ai∇f(wi)
REC ALL
f(w)
w1 w0
w* … w2
Gradient descent for model finetuning
with a pretrained model, wpre
Start at a random point, w0
Repeat
Determine a descent direction Choose a step size
Update model using gradient
calculated from training finetuning data Until stopping criterion is satisfied
w
f(w)
w1 w0
w* … w2
Update Rule:
wi+1 = wi — ai∇f(wi)
Fine-tuning example: BERT (2019)
Training in the Foundation Model Era: Efficient Fine-Tuning
Potential issue: pre-trained models are getting large!
340M parameters
175B parameters
How do we ‘finetune’ large models?
When finetuning, we expect the change in weights (orange) to be small
However, updating all of these weights can be expensive!
How can we reduce the number of weights we need to update?
… enter parameter-efficient finetuning
Types of PEFT methods
Linear probing
Credit: Ke et al, 2024, https://arxiv.org/pdf/2402.18905v1
Just update a portion of the weights, e.g., just the last layer (linear probing)
aka Foundation models as features
Adapters
Adapters are new modules are added between layers of a pre-trained network. Original model weights are fixed — just the adapter modules are tuned
Recap (again):�Recovering latent factors in a matrix
80
K * m
n * K
x1 | y1 |
x2 | y2 |
.. | .. |
| |
| |
| |
| |
| |
… | … |
xn | yn |
a1 | a2 | .. | … | am |
b1 | b2 | … | … | bm |
v11 | … | | | |
… | … | | | |
| | | | |
| vij | | | |
| | | … | |
| | | | vnm |
~
LoRA (low rank adaptation) finetuning
Key idea: don’t learn a full d x d matrix of weights, instead learn a low-rank approximation to this matrix!
full finetuning
LoRA
finetuning
LoRA (low rank adaptation) finetuning
LoRa scaling: default value of alpha/r is 1. However, can be tuned to trade-off emphasis on pretrained weights vs. updated weights
QLoRA: Quantized LoRA
Key idea: quantize pretrained weights so they are very small / memory efficient Hope is that LoRA finetuning can help to offset issues with lower precision
https://github.com/bitsandbytes-foundation/bitsandbytes