PythiloLesson 9 of 9 · progress kept in this browser

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)
This example uses torch. Run it in the Pythilo app, where these features are available.
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.

This lesson uses torch, which Python in this browser does not include. Read it here, and run and check it in the Pythilo app on iPhone or iPad.

All lessons