Sub topic 5
Rank, alpha, dropout and target modules
This chapter assumes you know basic PyTorch, how matrix multiplication and matrix rank work, and the layout of a transformer block (attention projections and feed-forward layers). It builds on the LoRA update W + (alpha/r)BA from the previous chapter. Four settings control that update: rank, alpha, dropout and target modules.
Rank (r)
The rank r is the inner dimension of the two adapter matrices. For a layer with weight shape (out, in), A has shape (r, in) and B has shape (out, r). The adapter adds r * (in + out) trainable parameters per adapted layer.
A higher rank lets the update express more directions of change, but it also costs more memory and gives more room to overfit. Common values are 8, 16 and 64. Start at 8 or 16 and raise it only if training loss stays high on a task that needs more capacity.
Alpha and the scale
Alpha is a constant that sets the scale of the update: scale = alpha / r. The adapter output is multiplied by this scale before it is added to the base output.
Changing r while keeping alpha fixed changes the scale. For example, r=8, alpha=16 gives a scale of 2, while r=64, alpha=16 gives 0.25. A common starting rule is alpha = 2 * r, which keeps the scale at 2. Treat alpha and the learning rate together, because both change how large the update steps are.
import torch.nn as nn
def lora_param_count(in_features, out_features, r):
return r * (in_features + out_features)
for r in (4, 8, 16, 64):
params = lora_param_count(2048, 2048, r)
full = 2048 * 2048
print(f"r={r:3d} scale(alpha=2r)={2.0} params={params:,} ({params / full:.2%} of full layer)")For a 2048 by 2048 layer, rank 16 trains about 1.6% of the layer's parameters.
Dropout
lora_dropout applies dropout to the input of the adapter path only. The frozen base path is not affected. It acts as a light regularizer. Values of 0.05 to 0.1 are typical. With small datasets that overfit quickly, a higher value can help. With large datasets you can set it to 0.
Target modules
target_modules lists the layer names that receive adapters. Which layers you choose matters as much as the rank.
| Choice | Layers | Trade-off |
|---|---|---|
| Attention only | `q_proj`, `v_proj` | Fewest parameters, original LoRA setup |
| All attention | `q_proj`, `k_proj`, `v_proj`, `o_proj` | Moderate cost, good default |
| All linear | `"all-linear"` | Includes MLP layers, highest capacity and memory |
Adapting the MLP layers often gives better results than raising the rank on attention layers alone. Layer names differ between model families, so check them before you configure anything.
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("Qwen/Qwen2.5-0.5B-Instruct")
names = {n.split(".")[-1] for n, m in model.named_modules() if m.__class__.__name__ == "Linear"}
print(sorted(names))A target name that matches no layer raises an error in PEFT. A name that matches fewer layers than you expected silently trains less than you planned, so always check print_trainable_parameters().
from peft import LoraConfig
config = LoraConfig(
r=16,
lora_alpha=32,
lora_dropout=0.05,
target_modules=["q_proj", "k_proj", "v_proj", "o_proj"],
task_type="CAUSAL_LM",
)How the settings interact
Common mistakes
- Raising
rwithout adjustingalpha, which shrinks the scale and slows learning. - Copying
target_modulesfrom another model family whose layer names differ. - Tuning several settings at once, so you cannot tell which one helped.
Exercise
A config uses r=32 and lora_alpha=16. What is the scale, and what happens to it if you raise r to 64 without changing alpha?
Show answer
The scale is 16/32 = 0.5. At r=64 it becomes 16/64 = 0.25, so the adapter update is halved. To keep the scale at 0.5 you would raise alpha to 32.