Estimate the trainable param count for a Llama-3 8B LoRA at r=16 targeting all-linear modules.
Same topic, related formats. Practice these next.
Same topic, related formats. Practice these next.
from peft import LoraConfig import torch # Base: meta-llama/Meta-Llama-3-8B # hidden_size = 4096 # intermediate_size = 14336 # num_layers = 32 # num_kv_heads = 8 (head_dim 128 -> kv_proj out = 1024) # # LoraConfig( # r=16, lora_alpha=32, bias="none", # target_modules="all-linear", # q,k,v,o + gate,up,down per layer # ) # # Report the order-of-magnitude trainable parameter count # (one significant figure is enough).
Each module adds r times (in_dim + out_dim) params. Summed across all-linear targets for 32 layers at r=16, it lands at roughly 84M trainable, about 1% of the 8B base.
Picture each wrapped layer of the model as a doorway. LoRA adds two short ramps next to the doorway: one ramp comes in from the room behind the door, the other ramp leads into the room ahead. Each ramp is the width of its side of the door, and only sixteen planks deep. Add up the planks for every doorway and you find that most of the planks belong to the wide doorways inside the network, the ones connecting to the much wider feed-forward room. Count up all the wide and narrow doorways across every layer of the building and you end up with about eighty-four million planks. That sounds like a lot, but it is only a hundredth of the planks the original building was made from.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example. Click to expand.
Everything you need to truly understand this topic: intuition, mechanics, step by step explanation, code, formulas, and worked example.
Everything important, quickly.
4 min: state the per-module formula, sum across the seven all-linear modules per layer with attention and MLP separately, multiply by r and num_layers, then ground the result as a fraction of the 8B base.
| Config | Trainable params (Llama-3 8B) | Fraction of base |
|---|---|---|
| LoRA r=8 attention-only | ~7M | ~0.1% |
| LoRA r=16 attention-only | ~14M | ~0.2% |
| LoRA r=16 all-linear | ~84M | ~1% |
| LoRA r=64 all-linear | ~336M | ~4% |
| Full fine-tuning | ~8B | 100% |
Real products, models, and research that use this idea.
What an interviewer would ask next. Try answering before peeking at the approach.
Red flags and common mistakes that signal junior thinking. Click to expand.
Forgetting that gate, up, and down MLP projections dominate the count because intermediate_size is 14336. Counting only attention projections gives a number that is several times too small.
The night-before-the-interview bullets. Scan these on the way to the call.
Primary sources. Skim if you want the original framing.