LoRA Mathematics › LoRA Gradient Computation
Language AI HandbookAll chapters LoRA Mathematics › LoRA Gradient ComputationOne section · Sections in this chapter6 / 12 ↓
Reading tools Definitions & inline links
Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.
Article links
Make inline references clickable
Gradient flow through LoRA reveals the training dynamics and connects to the optimization concepts from Part X. The gradient formulas explain why the zero initialization of does not prevent learning, how and co-evolve during training, and why the optimization objective makes LoRA training well-behaved.
Understanding gradients has practical value for LoRA. The specific structure of the gradient formulas determines which parameters learn first, how quickly the two matrices co-adapt, and what happens when you change the scaling factor or rank. Practitioners who understand gradient flow can diagnose training problems more easily and tune LoRA more effectively.
Gradient Derivation
Consider the forward pass for a single layer with LoRA adaptation:
where:
- : the output vector of the adapted layer
- : the pre-trained weight matrix (frozen, no gradients computed)
- : the input vector to the layer
- : the scaling factor
- : the rank
- : the LoRA up-projection and down-projection matrices (both trainable)
Let be the loss function we are minimizing. To update and via gradient descent, we need and . The chain rule traces how parameter changes propagate through the computation to affect the loss: a small change in changes the intermediate activation , which changes the LoRA output , which changes the layer output , which eventually changes the loss .
First, let us define the upstream gradient. Let be the gradient of the loss with respect to the layer's output. This gradient comes from the layers above in the network and tells us how changes in affect the loss. It is computed by standard backpropagation through the rest of the network and is the "signal" that drives adaptation in both and .
For the gradient with respect to , we apply the chain rule. The LoRA contribution to is . Treating the product as a fixed vector (since we are differentiating with respect to , not ), the derivative of with respect to has a clean outer product form:
where:
- : the loss function
- : the upstream gradient vector from layers above
- : the intermediate vector (input projected down to rank )
- : the transpose of the intermediate vector
- : the scaling factor
- : the rank
The gradient of is an outer product between the upstream gradient and the intermediate representation . Each entry of the gradient measures how much increasing that entry would affect the loss, which depends on how much gradient signal arrives at output dimension (captured by ) and how active the corresponding bottleneck dimension was (captured by ). Dimensions that were heavily activated and carry strong gradient signal receive large updates, while inactive or gradient-free dimensions receive small updates.
For processing batches efficiently, we extend this to matrix form:
where:
- : loss function
- : scaling factor
- : rank
- : matrix of upstream gradients for the batch, with one column per example
- : matrix of input vectors for the batch, with one column per example
- : down-projection matrix
For the gradient with respect to , we again apply the chain rule, but now we trace through both the multiplication by and the multiplication by :
where:
- : transpose of the up-projection matrix, which back-projects the upstream gradient into the bottleneck space
- : the loss function
- : the scaling factor
- : the rank
- : the upstream gradient vector
- : the input vector to the layer
- : the transpose of the input vector
This formula shows that the gradient with respect to involves two stages: first, the upstream gradient is projected back through using , which maps the output-space gradient into the bottleneck space. Then, this bottleneck-space gradient is combined with the input to form the outer product that updates . In matrix form for batch processing:
where:
- : transpose of the up-projection matrix
- : upstream gradient matrix (one column per batch example)
- : transpose of the input batch matrix
Gradient Flow Properties
Several properties of these gradients are noteworthy and reveal important aspects of LoRA's training dynamics. These properties have direct practical implications for how training progresses and what kinds of convergence behavior you should expect.
Independence from : The gradients and are independent of . Frozen weights affect the upstream gradient through their contribution to the forward pass, but they never appear explicitly in the LoRA gradient formulas. This separation enables efficient training where is never updated. You do not need to compute or store gradients for the much larger frozen weight matrices, which is the key source of LoRA's memory savings during training.
Coupled dynamics: Although and are separate parameters, their gradients are intimately coupled:
- depends on through the term
- depends on through the term
This coupling means and co-evolve during training, with 's current value determining 's gradient and vice versa. This is similar to the dynamics in other factorized parameterizations like matrix factorization for recommendation systems, leading to interesting optimization behavior including potential saddle points and multiple stable solutions that represent the same effective weight update through different factorizations.
Zero initialization dynamics: At initialization (), the gradients behave asymmetrically:
- (generally non-zero, since is random and is non-zero)
- (exactly zero because )
This asymmetry is important: updates immediately from the first gradient step, while has zero gradient initially. After the first update step, , and both matrices begin receiving non-zero gradients. The initial zero-gradient phase for is brief, lasting only a single step in theory, though in practice the gradients for remain small until has moved appreciably away from zero. This "bootstrap" phase, where adapts alone before both matrices co-evolve, is a direct consequence of the asymmetric initialization.
Computational Cost of Gradient Computation
LoRA's gradient computations are efficient, requiring operations proportional to the parameter count rather than the full weight matrix size. This efficiency is not accidental; it is a direct consequence of the factorized structure and is the key reason LoRA training is so much faster than full fine-tuning.
Computing where involves the following costs: during the forward pass, we must store , adding minimal memory overhead of just numbers per training example. The gradient computation itself is an outer product between vectors of dimension and , requiring operations and creating a matrix that is directly usable as the gradient for .
Computing requires first computing , which is a matrix-vector product costing operations (transforming the -dimensional upstream gradient into an -dimensional signal using ), followed by forming the outer product with , which costs operations and produces an matrix that is the gradient for .
The total gradient computation is therefore , matching the parameter count. This is essentially optimal: we need at least one operation per parameter to compute a gradient, and LoRA achieves this lower bound up to constant factors. This efficiency extends to the memory required for gradient storage, which also scales with rather than , explaining why LoRA reduces GPU memory requirements by over 99% compared to full fine-tuning for large models.