C.W.K.
Stream
Lesson 06 of 07 · published

Fine-Tuning with LoRA

~9 min · keras-nlp

Level 0Keras Apprentice
0 XP0/97 lessons0/20 achievements
0/120 XP to next level120 XP to go0% complete

Why full fine-tuning hurts

Fine-tuning every weight of a large model means storing a gradient and an optimizer state for each one — that's the line item that blows past a single GPU's memory long before the model itself does. LoRA (Low-Rank Adaptation) sidesteps it with a clever bet: freeze the original weights entirely, and learn a pair of small low-rank matrices that get added alongside them. You're training a few hundred thousand parameters instead of hundreds of millions, but the model behaves as if you'd tuned the whole thing.

One line, two big wins

In KerasHub it's backbone.enable_lora(rank=r) — then you call fit() exactly as before and only the adapters update (Code section). The rank is the dial: higher rank means more capacity to adapt but more parameters to train; 4–16 covers most cases. Two payoffs fall out for free. First, memory: trainable params drop by orders of magnitude, so a model that needed a cluster now fits on one card. Second, storage: you save just the tiny adapter weights, not a full model copy — so ten fine-tunes of the same base cost ten small files, not ten full checkpoints.

QLoRA: stack it with quantization

QLoRA goes one step further — first quantize() the frozen backbone to int8 (or int4) to shrink the resident model, then add LoRA adapters on top. The base is tiny and frozen, the adapters are tiny and trainable, and quality stays close to full fine-tuning. This is the combination that put 7B-class fine-tuning on consumer hardware.

Code

Enable LoRA, then stack quantization for QLoRA·python
# Enable LoRA on the model backbone
classifier.backbone.enable_lora(rank=4)

# Check trainable params — dramatically reduced!
print(classifier.summary())
# Total params: 110M, Trainable: ~300K (0.3%!)

# QLoRA: Quantize first, then LoRA
classifier.backbone.quantize("int8")     # Reduce model size
classifier.backbone.enable_lora(rank=4)  # Add adapters
# Even less memory, nearly same quality

External links

Exercise

Load Gemma-2b. Enable LoRA(rank=8). Compare backbone.count_params() before and after — note the trainable parameter drop from 2B to ~few million.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.