Lensa ML
Lensa ML

LoRA & Fine-Tuning

Step 1 of 6

The Fine-Tuning Problem

Why updating all parameters is expensive

Model size (billions)7
Full Fine-Tuning: update ALL parameters7B paramsEvery block must be updated, stored, and differentiatedGPU memory needed
TOTAL PARAMS
7B
WEIGHTS MEMORY
28.0 GB
TRAINING MEMORY
56.0 GB

A 7B-parameter model is relatively small. Full fine-tuning needs ~56.0 GB just for optimizer states and gradients — that's already a serious GPU.

Step 1 of 6: The Fine-Tuning Problem

Why updating all parameters is expensive

Model size (billions)7
Full Fine-Tuning: update ALL parameters7B paramsEvery block must be updated, stored, and differentiatedGPU memory needed
TOTAL PARAMS
7B
WEIGHTS MEMORY
28.0 GB
TRAINING MEMORY
56.0 GB

A 7B-parameter model is relatively small. Full fine-tuning needs ~56.0 GB just for optimizer states and gradients — that's already a serious GPU.

Step 2 of 6: Low-Rank Intuition

Weight updates are low-rank

Rank r1
ΔW ≈ sum of 1 rank-1 outer productApproximated ΔW matrix30% quality
RANK
1
APPROX QUALITY
30%
TERMS
1

With rank 1, we capture only the dominant patterns in ΔW. The approximation is coarse (30%) but uses very few parameters — just 1 pairs of vectors.

Step 3 of 6: LoRA Decomposition

ΔW ≈ B × A

Rank r4
ΔW ≈ B × AΔW512×512B512×4×A4×512Parameter comparison1.56%
FULL PARAMS
262K
LORA PARAMS
4.1K
REDUCTION
1.56%
RANK
4

With rank 4, LoRA uses only 4.1K parameters instead of 262K — a 98.4% reduction. Matrix B is 512×4 and A is 4×512; their product reconstructs the 512×512 update.

Step 4 of 6: Where to Apply LoRA

Choosing transformer components

Component: Q (Query)0
Transformer BlockMulti-Head AttentionQ◆ LoRAKVOFeed-Forward NetworkQ (Query): 512 → 512 (rank 8)8,192 trainable params
COMPONENT
Q
FULL PARAMS
262K
LORA PARAMS
8.2K

Applying LoRA to the Q (Query) projection in attention. Research shows Q and V adapters give the best results for most tasks, but adding K helps for complex reasoning.

Step 5 of 6: Training LoRA

Frozen weights, trainable adapters

Training step0
W₀frozen🔒+B×ATask Accuracy50%95%
STEP
0
ACCURACY
30.0%
W₀ STATUS
Frozen

Before training, B is initialized to zero and A to random small values, so B×A = 0 — the model starts identical to the original. Only B and A will receive gradients; W₀ stays frozen.

Step 6 of 6: Merging & Stacking

Combining and swapping adapters

Scaling factor α1.00
W_final = W₀ + α · B · AW₀+α=1.00W_mergedSwap adapters per task — same base modelSentimentLoRA #1SummarizeLoRA #2Code GenLoRA #3Base Model (shared)
ALPHA (α)
1.00
PERFORMANCE
93.0%
ADAPTERS
3 tasks

At α = 1.00, you get the standard LoRA merge. The key advantage: you can hot-swap lightweight adapters (~MBs) for different tasks while keeping one shared base model (~GBs).