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.
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 = 30shapes = {"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 = 16for 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
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.
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.