Practice in Lab: Project: learn and merge a frozen-base low-rank adapter →
Project: learn and merge a frozen-base low-rank adapter
Low-rank adaptation trains a small update while retaining a frozen base matrix. Zero-initializing one adapter factor makes the first forward pass match the base exactly; the other factor needs nonzero initialization so learning can begin. This project performs actual gradient updates, then merges the learned matrix and checks inference equivalence. It is a local linear adapter with explicit scaling, not a downloaded pretrained language model or a claim about large-model fine-tuning quality. The merged artifact is useful only with the same base, scale and tensor layout.
import torch
a = torch.tensor([[1., 2.]])
b = torch.tensor([[3.], [4.]])
print(b @ a)Review the design
Record adapter rank, scaling, base-model identity and merge policy. Real transformer fine-tuning also needs target-module selection, tokenization and a held-out evaluation.
Your task
For finite base [out,in], xs [N,in] and ys [N,out], train only A [rank,in] and B [out,rank]. Initialize A=randn*.1, B=zeros under a local seed. Predict linear(xs,base)+linear(linear(xs,A),B)/rank; use Adam lr=.05 and mean squared error for positive steps. Base remains frozen and caller lists unchanged. Return base, a, b, merged=base+B@A/rank, history of pre-update losses, final loss and merged predictions as lists/scalars. Rank is a positive integer <=min(out,in). Preserve caller CPU RNG.
The explanation opens here once every check passes.