trying to write a dead-simple peft/lora loop from scratch in raw pytorch without pulling in the massive transformers trainer overhead, just to get a tighter grip on how adapter weights actually hook into base linear layers for structured JSON tasks.
class LoRALinear(nn.Module):
def __init__(self, in_features, out_features, rank=8, alpha=16):
super().__init__()
self.weight = nn.Parameter(torch.randn(out_features, in_features))
self.lora_A = nn.Parameter(torch.zeros(rank, in_features))
self.lora_B = nn.Parameter(torch.zeros(out_features, rank))
self.scaling = alpha / rank
def forward(self, x):
base_out = F.linear(x, self.weight)
lora_out = (x @ self.lora_A.T @ self.lora_B.T) * self.scaling
return base_out + lora_out
loss is literally not moving at all during the first 50 steps on a tiny 500-sample dataset. base model is frozen properly (param.requires_grad = False), but gradients on lora_A and lora_B are just sitting at None or zero. what am i missing here?