AI Hints
Home›Fine-Tuning LLMs Tutorial›LoRA Explained
Fine-Tuning LLMs · Lesson 42 of 120

Rank and Alpha

The two LoRA settings.

⏱ 2 min read▶ Video at 1:07:44🎓 Free
Watch this lesson in the Fine-Tuning LLMs courseWatch on YouTube ↗Full Fine-Tuning LLMs course

LoRA has two main settings. The rank, which we've met, and alpha. Choosing them well is easier than it sounds.

  • Two main settings
  • Rank and alpha
  • Easier than it sounds

Rank and alpha

The rank, r, sets how many numbers LoRA can train. Higher rank means more capacity to learn, but more memory. Alpha controls how strongly the update is applied. The update is multiplied by alpha divided by r. A popular rule is to set alpha to twice the rank.

keyvalue
rcapacity: how many numbers
Higher rlearns more, uses more memory
lora_alphastrength of the update
Scalealpha ÷ r, often alpha = 2r

Count the cost of each rank

Let's count the trainable numbers, for different ranks. Our model has thirty layers. We put LoRA on two grids per layer, q proj and v proj. Each has a number of inputs, and outputs. For each rank, B and A together hold r times inputs plus outputs. Run it. Even at rank sixty four, we train under three percent of the model. At rank eight, about a third of one percent. And with alpha sixteen, rank eight doubles the update's strength.

app.pypython
layers = 30
shapes = {"q_proj": (576, 576), "v_proj": (576, 192)}      # (numbers in, numbers out)

for r in [4, 8, 16, 64]:
    total = layers * sum(r * (n_in + n_out) for n_in, n_out in shapes.values())
    print(f"rank {r:2}: {total:>9,} trainable  ({total / 134_515_008:.2%} of the model)")
alpha = 16
for r in [8, 16]:
    print(f"rank {r}, alpha {alpha}: the update is multiplied by {alpha / r}")
Output
rank  4:   230,400 trainable  (0.17% of the model)
rank  8:   460,800 trainable  (0.34% of the model)
rank 16:   921,600 trainable  (0.69% of the model)
rank 64: 3,686,400 trainable  (2.74% of the model)
rank 8, alpha 16: the update is multiplied by 2.0
rank 16, alpha 16: the update is multiplied by 1.0

Good starting values

Here are good starting values. Rank eight, or sixteen, works for most tasks. Set alpha to twice the rank. Use a little dropout, like zero point zero five, which randomly switches off parts of the adapter during training, to fight overfitting. Only raise the rank if the model can't learn your task.

SettingStart with
r8 or 16
lora_alpha2 × r
lora_dropout0.05
Raise r when…the task is hard to learn

Summary

Let's recap. Rank sets LoRA's capacity. Alpha sets the strength, as alpha over r. And rank eight or sixteen, with alpha twice the rank, is a great start. Next, which layers LoRA should attach to.

Key takeaways
  • r = capacity
  • alpha / r = strength
  • r 8–16, alpha 2r
Next up: Which Layers? — target_modules