requires_grad_(False) freezes the weight and the bias together
Freezing the base layer is one call, and it freezes more than the weight.
lora = LoRALinear(nn.Linear(16, 12), r=4, alpha=8.0)
names = [n for n, p in lora.named_parameters() if p.requires_grad]
print(names, sum(p.numel() for p in lora.parameters() if p.requires_grad))
['A', 'B'] 112
Run this check right after wrapping, every time.
| Parameter | Shape | Numbers | Trains? |
|---|---|---|---|
base.weight | 12 × 16 | 192 | frozen |
base.bias | 12 | 12 | frozen |
A | 4 × 16 | 64 | trains |
B | 12 × 4 | 48 | trains |
| What trains | 112 |
Then hand only those to the optimizer:
opt = torch.optim.Adam([p for p in lora.parameters() if p.requires_grad], lr=lr)
The PyTorch docs name requires_grad as the main way to choose which parts of a model take part in the gradient computation, and freezing part of a pretrained model for fine-tuning is their example. A frozen weight gets no .grad and no optimizer state.
The method's name is not the point. A loop that sets p.requires_grad_(False) for each p in base.parameters() does the same, and the practice's worked solution does exactly that.
In the editor, this LoRALinear.__init__ freezes base.weight alone, so base.bias still trains. Fix it: freeze the whole base layer in one call, so that only A and B train.