Build Your Own LLM, Lesson 10: Fine-Tuning a Pre-Trained Model With LoRA

Build Your Own LLM, Lesson 10: Fine-Tuning a Pre-Trained Model With LoRA 1

📎 This article includes 4 downloadable practice files ↓

⏱ 2 min read

Retraining every weight of a big model needs huge GPUs. LoRA (Low-Rank Adaptation) freezes the original weights and learns small “adapter” matrices instead, often under 1% of the parameters, with results close to full fine-tuning.

Advertisement
In this article
  1. The idea
  2. On real open models
  3. Practice

The idea

A weight update ΔW for a layer is approximated by two thin matrices: ΔW = B·A, where A is r × in and B is out × r, with a small rank r (4-16). B starts at zero, so the adapted model starts exactly as the original.

class LoRALinear(nn.Module):
    def __init__(self, base, r=4, alpha=8):
        super().__init__()
        self.base = base                                   # frozen
        self.A = nn.Parameter(torch.randn(r, base.in_features) * 0.01)
        self.B = nn.Parameter(torch.zeros(base.out_features, r))
        self.scale = alpha / r
    def forward(self, x):
        return self.base(x) + (x @ self.A.T @ self.B.T) * self.scale

In the script, LoRA on the attention layers trains 6,912 of 355,584 parameters (1.9%), and loss on the new text drops from 6.9 to about 0.15 in 150 steps.

On real open models

Libraries like Hugging Face peft do the same with a few lines (LoraConfig(r=8, target_modules=["q_proj","v_proj"])). Combined with 4-bit loading (QLoRA), 7-8B parameter models can be fine-tuned on a single consumer GPU.

💡 Fine-tuning teaches style, format and narrow tasks well. To give a model new facts that change (prices, policies), retrieval (RAG) is usually better than fine-tuning.

More: LoRA fine-tuning explained.

Practice

Run after lesson 7, then change r to 1 and 16 and compare the trainable parameter count and loss.

📎 Practice files for this article

⬇ Download all 4 files (ZIP · 5 KB)

  • 🐍
    tinygpt.py (shared model code)The GPT model, tokenizer, batching and generation used in lessons 6-12.
    ⬇ PY · 4 KB
  • 🐍
    Lesson 10 scriptTested script for this lesson.
    ⬇ PY · 2 KB
  • 📄
    Expected outputWhat the script printed in our test run (your numbers will be close).
    ⬇ TXT · 131 B
  • 📄
    sample.txtSmall training text; swap in any public-domain book for better results.
    ⬇ TXT · 83 KB

Free to use for learning. Files with macros (.bas) are plain text — import them with Alt+F11 → File → Import File, and always test on a copy.

✨ Ask AI about this article

Stuck on a step? Ask a question and the AI answers using this article.

Free · AI can be wrong

Leave a Reply

Your email address will not be published. Required fields are marked *