Overview

Appendix E Parameter-efficient Finetuning with LoRA

This appendix introduces low-rank adaptation (LoRA) as a practical, parameter-efficient way to finetune large language models on task-specific data. Instead of updating all pretrained weights, LoRA learns a compact, low-dimensional approximation of the weight updates, which captures the most influential directions of change while keeping compute and storage demands low. Because the learned adapters are kept separate from the original model, one can customize a single base model for multiple tasks or clients by storing only the small LoRA weights, and the approach applies to both classification-style finetuning and supervised instruction finetuning.

Conceptually, LoRA replaces direct weight updates with the product of two small matrices and combines their contribution with the frozen pretrained weights during the forward pass. A pair of hyperparameters controls capacity and influence: rank determines the adapter size (and thus trainable parameter count), while alpha scales the adapter’s contribution. In practice, the appendix shows how to implement a LoRA layer, wrap existing Linear layers with a Linear-with-LoRA module, and recursively swap these throughout the transformer’s attention, feed-forward, and output layers. After freezing the original GPT-2 weights, only the LoRA parameters are trained; with rank=16 and alpha=16, this reduces trainable parameters from about 124 million to roughly 2.7 million, and the initialization preserves original behavior until training begins.

The appendix then reprises the chapter 6 workflow on SMS spam classification: preparing the dataset, loading a pretrained GPT-2 small model, replacing the output head for classification, and training only the LoRA adapters. Although LoRA adds a bit of forward-pass overhead, the savings during backpropagation make it especially attractive for larger models. The resulting model converges quickly and achieves very high performance—perfect training accuracy and around 97–98% on validation and test—demonstrating strong task adaptation with a tiny fraction of the original parameters. Overall, LoRA delivers a compelling balance of efficiency, scalability, and effectiveness for finetuning large language models.

Figure E.1 A comparison between weight update methods: regular finetuning and LoRA. On the left, regular finetuning involves updating the pretrained weight matrix W directly with ΔW. On the right, LoRA uses two smaller matrices A and B to approximate ΔW, where the product AB is added to W, and r denotes the inner dimension, a tunable hyperparameter.
Figure E.2 The LoRA matrices A and B are applied to the layer inputs and are involved to compute the model outputs. The inner dimension r of these matrices serves as a setting that adjusts the number of trainable parameters by varying the sizes of A and B.
Figure E.3 Illustration of the integration of LoRA into a model layer. The original pretrained weights (W) of a layer are combined with the outputs from LoRA matrices (A and B), which approximate the weight update matrix (∆W). The final output is calculated by adding the output of the adapted layer (using LoRA weights) to the original output.
Figure E.4 A diagram representing the architecture of the GPT model. It highlights the parts of the model where Linear layers are being upgraded to LinearWithLoRA layers for parameter-efficient finetuning.
Figure E.5 The training and validation loss curves over six epochs for a machine learning model. Initially, both training and validation loss decrease sharply, then they level off, indicating the model is converging, which means that it is not expected to improve noticeably with further training.

FAQ

What is LoRA and why is it useful for finetuning large language models?LoRA (low-rank adaptation) is a parameter-efficient finetuning method that adapts a pretrained model by learning a low-rank approximation of the full weight update. Instead of updating all weights, LoRA learns two small matrices whose product approximates the update, drastically reducing trainable parameters, compute, and storage while preserving base model weights.
How does LoRA change the weight update compared to full finetuning?In full finetuning, weights are updated as W_updated = W + ΔW. LoRA replaces ΔW with a low-rank factorization AB, yielding W_updated = W + AB. During a forward pass, this manifests as xW + xAB, where xAB is the LoRA contribution added to the original layer output.
What do the rank (r) and alpha (α) hyperparameters control in LoRA?- Rank (r) sets the inner dimension of the A and B matrices and determines how many additional parameters LoRA introduces. Higher r increases capacity and compute. - Alpha (α) scales the LoRA contribution, controlling how strongly the adapter influences the layer output. Common practice is to set α equal to, half of, or double the rank (for example, r=16, α=16).
Why is the LoRA B matrix typically initialized to zeros?Initializing B to zeros makes AB initially zero, so adding AB to the original layer output leaves the pretrained model unchanged before training. This is why accuracy measured right after adding LoRA (but before finetuning) matches the original model’s accuracy.
Which parts of a GPT-style model are typically adapted with LoRA?LoRA commonly wraps all Linear layers, including: - Attention projections: W_query, W_key, W_value, and the output projection - Feed-forward network layers - The final output head This broad coverage captures the main trainable components while keeping the base weights frozen.
How do you integrate LoRA into an existing model in practice?- Freeze all original model parameters (requires_grad = False) - Replace each Linear layer with a LinearWithLoRA wrapper that adds a LoRA branch (xA B scaled by α) to the original Linear output - Train only the LoRA parameters (and any newly added heads) using your optimizer - Keep the pretrained weights intact; the LoRA adapters can be saved and applied on demand
What are the computational and storage benefits of LoRA?- Far fewer trainable parameters (for example, about 2.67M vs 124M in the provided GPT2-small setup, ~50× reduction) - Lower memory and optimizer state requirements - Easier multi-tenant customization: store small LoRA adapters per task/customer rather than full copies of the base model
Does LoRA speed up training and inference?LoRA adds a small forward-pass cost, but it substantially reduces backpropagation costs by training far fewer parameters. For larger models, this typically yields faster training than full finetuning. Inference can apply LoRA additively with minimal overhead, and some frameworks allow merging adapters into weights to eliminate runtime cost.
What were the results of LoRA finetuning on the spam classification example?After finetuning with LoRA (r=16, α=16), the reported accuracies were approximately: - Training: 100.00% - Validation: 96.64% - Test: 98.00% This demonstrates strong performance despite training only the small LoRA adapters instead of the full model.
Is LoRA applicable beyond classification, for example to instruction finetuning?Yes. Although demonstrated on spam classification, LoRA is equally applicable to supervised instruction finetuning and other task-specific adaptations, offering efficient, modular customization without altering the base model weights.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Build a Large Language Model (From Scratch) ebook for free