Skip to content
mentoor

Topic 1 of 5 · 4 min read

How low-rank adapters work

Goal: Calculate the trainable parameter count of a LoRA configuration and explain how rank and alpha change the update.

LoRA freezes a layer's original weights and learns the change to them as the product of two small matrices, so you train a tiny fraction of the parameters and keep the base model untouched.

Check yourself · 1 of 3

In LoRA, which weights receive gradient updates during training?

Choose one answer

Why it matters

Full fine-tuning updates every weight and needs gradients and optimizer state for all of them. For a multi-billion-parameter model that means several times the model's size in memory, plus a full copy of the model for every task. LoRA (Hu et al., 2021) trains a small add-on instead. The add-on is a file of a few megabytes to a few hundred megabytes, and the base model is shared.

How it works

Take one weight matrix W with shape d_out x d_in. Full tuning learns a change ΔW of the same shape. LoRA writes the change as ΔW = B x A, where A is r x d_in and B is d_out x r. The number r is the rank and is small, often 8 to 64.

  • W stays frozen. Only A and B get gradients.
  • B starts at zero and A starts random, so at step 0 the model behaves exactly like the base model.
  • The output is W x + (alpha / r) * B A x. The factor alpha / r is the scale of the update.
  • Trainable parameters per adapted matrix: r * (d_in + d_out).

For a 4096 x 4096 matrix, full tuning is 16,777,216 weights. At r = 8, LoRA trains 8 * (4096 + 4096) = 65,536. If you adapt the query and value projections in 32 layers, that is 64 matrices and about 4.2 million trainable parameters.

Higher rank means more capacity to learn, more memory and a bigger adapter file. Alpha sets how loudly the update speaks. A common habit is alpha = r or alpha = 2 * r. If you raise r and leave alpha fixed, the scale shrinks, so change them with that in mind.

Before and after

Weak: "LoRA with r = 256 on every layer will always beat r = 8." Rank 256 on a 4096 x 4096 matrix trains 2,097,152 parameters per matrix, 32 times the r = 8 count, and on a small dataset it mostly adds overfitting risk and cost.

Better: start at r = 16, adapt the attention and MLP projections, measure on a held-out eval, and raise the rank only if the eval shows the adapter is too small to learn the task.

from peft import LoraConfig
cfg = LoraConfig(r=16, lora_alpha=32, lora_dropout=0.05,
                 target_modules=["q_proj", "v_proj"], task_type="CAUSAL_LM")

What goes wrong

  • Rank as a quality dial. Bigger is not better by default. A task that is mostly style or format needs little capacity.
  • Changing r without alpha. The scale alpha / r changes with it, and a learning rate that worked before may now be too weak or too strong.
  • Expecting new knowledge. An adapter reshapes behaviour well. It is a poor way to load large amounts of new facts.

Examples

Counting trainable parameters

A model has 32 layers and you adapt q_proj and v_proj, each 4096 x 4096, with r = 16.

Per matrix: 16 * (4096 + 4096) = 131,072. Matrices: 32 layers * 2 = 64. Total: 64 * 131,072 = 8,388,608 trainable parameters.

Doubling the rank to 32 doubles that to 16,777,216, and the adapter file roughly doubles with it.

Alpha and the scale factor

Run one uses r = 8, alpha = 16, so the scale is 2. Run two raises to r = 32 and keeps alpha = 16, so the scale drops to 0.5. The second run has four times the capacity but each update is a quarter as loud, and it may learn more slowly. Keeping alpha = 2 * r holds the scale at 2.

Key points

  • LoRA freezes the base weights and learns a low-rank update B x A for each adapted matrix.
  • Trainable parameters per adapted matrix equal r * (d_in + d_out), far fewer than d_in * d_out.
  • B starts at zero, so a fresh adapter leaves the base model's behaviour unchanged.
  • The update is multiplied by alpha / r, so changing r without alpha changes the update's strength.
  • Rank sets capacity: raise it only when a held-out eval shows the adapter is too small for the task.

Common mistakes

  • Treating a higher rank as automatically better and paying for capacity the task does not need.
  • Raising r and leaving alpha and the learning rate unchanged, which silently changes the scale of the update.
  • Expecting a small adapter to absorb a large body of new facts instead of a new behaviour or format.
  • Forgetting that the adapter is useless without the exact base model it was trained on.

Check yourself · 2 of 3

A 2048 x 2048 weight matrix gets a LoRA update with r = 4. How many trainable parameters does the adapter add for that matrix?

Choose one answer

Check yourself · 3 of 3

You raise rank from 8 to 32 and keep lora_alpha at 16. What happens to the scale applied to the update?

Choose one answer

Practice items, not counted towards your mastery.

Start here