What is LoRA rank r and alpha scaling factor?
Beginner · 2 min read · Fine-Tuning & Adaptation
Open in study mode to save this question and track progressShort answer: Rank defines the inner dimension of low-rank adapter matrices , controlling the number of trainable parameters; alpha () is a scaling hyperparameter that scales the adapter's update weight relative to base model weights during forward propagation.
Detail
Low-Rank Adaptation (LoRA) decomposes weight updates into two low-rank matrices:
Where , , and with .
Original Weight W_0 (d x k) [FROZEN]
+
Adapter Update (d x k) = (B [d x r] * A [r x k]) * (alpha / r)
The Scaling Equation
During forward propagation, the adapted forward pass is:
Parameter Tuning Guidelines
- Rank : Typical values are . Higher rank increases adapter expressiveness but increases VRAM usage.
- Alpha (): Standard heuristic sets (or ). Keeping constant when tuning avoids needing to retune the learning rate.
Example
PyTorch LoRA scaling implementation:
python
import torch
import torch.nn as nn
class LoRALayer(nn.Module):
def __init__(self, in_dim: int, out_dim: int, r: int = 16, alpha: float = 32.0):
super().__init__()
self.r = r
self.alpha = alpha
self.scaling = alpha / r
# Low-rank matrices
self.lora_A = nn.Parameter(torch.randn(r, in_dim) * 0.01)
self.lora_B = nn.Parameter(torch.zeros(out_dim, r))
def forward(self, x: torch.Tensor, base_output: torch.Tensor) -> torch.Tensor:
# Scale low-rank adapter output by (alpha / r)
adapter_output = (x @ self.lora_A.T) @ self.lora_B.T
return base_output + adapter_output * self.scalingInterview tips
- Explain why scaling matters: it stabilizes training when experimenting with different values of rank .
- Highlight that LoRA adapters can be merged back into base weights () for zero-latency inference deployment.