REAL PYTORCH · CPU TRAINING AND AUTOGRAD

Project: train and reload a local DPO policy with LoRA ownership

Implement the full synthetic preference experiment with the supplied local conditional token model and data helpers. First supervised-train the local base only on training chosen responses, freeze it as the reference, clone it and train only the two rank-two adapter matrices with response-token DPO at beta=.5. Select by validation preference loss, evaluate separate prompt identities, and export a real serialized model/reference/initialization artifact. Report actual loss, preference wins, reference-to-policy KL, parameter ownership, frozen-base hashes and reload equality. Preserve the reference report schema and prevent test preferences from fitting or selecting weights. The base is locally trained on generated pairs, not a downloaded pretrained LLM.

Run this Lab in Pythilo

Open Labs, choose its topic and tap Download entire topic, or Download entire category for all its Labs, including the required Python packages, then load the starter or worked answer and tap Trace. Progress, pause, resume and removal are in Downloads. Independent checks execute real CPU training and gradients.

No GPU, external model weights or API key is required. After downloading the topic and packages, this Lab runs offline.

PyTorch is unavailable in this browser runtime. The code and lessons are readable here; execution and checks run in the app.

Pythilo on the App Store →

Read lesson: Project: train and reload a local DPO policy with LoRA ownership →

Your task

Implement the full synthetic preference experiment with the supplied local conditional token model and data helpers. First supervised-train the local base only on training chosen responses, freeze it as the reference, clone it and train only the two rank-two adapter matrices with response-token DPO at beta=.5. Select by validation preference loss, evaluate separate prompt identities, and export a real serialized model/reference/initialization artifact. Report actual loss, preference wins, reference-to-policy KL, parameter ownership, frozen-base hashes and reload equality. Preserve the reference report schema and prevent test preferences from fitting or selecting weights. The base is locally trained on generated pairs, not a downloaded pretrained LLM.

Starter

import math
import torch
from torch.nn import functional as F

def dpo_loss(policy, reference, targets, beta=.1):
    if policy.ndim!=4 or policy.shape[1]!=2 or min(policy.shape)==0 or policy.shape[-1]<2 or reference.shape!=policy.shape or targets.shape!=policy.shape[:-1]:raise ValueError('Expected [batch, pair=2, time, vocabulary] aligned logits and targets.')
    if not policy.is_floating_point() or not reference.is_floating_point() or targets.dtype!=torch.long or not torch.isfinite(policy).all() or not torch.isfinite(reference).all() or not math.isfinite(beta) or beta<=0:raise ValueError('Expected finite floating logits, int64 labels and beta > 0.')
    mask=targets!=-100
    if (mask.sum(-1)==0).any() or ((targets[mask]<0)|(targets[mask]>=policy.shape[-1])).any():raise ValueError('Every response needs valid in-vocabulary targets.')
    safe=targets.masked_fill(~mask,0)
    policy_sum=(policy.log_softmax(-1).gather(-1,safe.unsqueeze(-1)).squeeze(-1)*mask).sum(-1)
    reference_sum=(reference.detach().log_softmax(-1).gather(-1,safe.unsqueeze(-1)).squeeze(-1)*mask).sum(-1)
    margin=(policy_sum[:,0]-policy_sum[:,1])-(reference_sum[:,0]-reference_sum[:,1])
    result=F.softplus(-beta*margin).mean()
    return result

from torch.nn import functional as F
import copy
import hashlib
import io
import json

def data_hash(rows):
    return hashlib.sha256(json.dumps(rows,sort_keys=True,separators=(',',':')).encode()).hexdigest()

def state_hash(state):
    return data_hash({key:value.detach().cpu().tolist() for key,value in state.items()})

def export_payload(payload):
    stream=io.BytesIO()
    torch.save(payload,stream)
    return stream.getvalue()

def read_payload(blob):
    return torch.load(io.BytesIO(blob),map_location='cpu',weights_only=True)

def check_steps(steps):
    if type(steps)!=int or not 1<=steps<=500:raise ValueError('Choose 1..500 actual optimizer steps.')

def override_rows(original,replacement):
    if replacement is None:return original
    if not isinstance(replacement,list) or len(replacement)!=len(original):raise ValueError('Evaluation replacement must preserve row identities.')
    if [r['id'] for r in replacement]!=[r['id'] for r in original]:raise ValueError('Evaluation row identities must remain unchanged.')
    return copy.deepcopy(replacement)


class PreferencePolicy(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.embedding=torch.nn.Embedding(3,4)
        self.base=torch.nn.Linear(6,3)
        self.adapter_a=torch.nn.Parameter(torch.randn(2,6)*.05)
        self.adapter_b=torch.nn.Parameter(torch.zeros(3,2))
    def forward(self,features,responses):
        previous=torch.zeros_like(responses)
        previous[:,:,1:]=responses[:,:,:-1]
        context=features[:,None,None,:].expand(-1,2,responses.shape[2],-1)
        hidden=torch.cat([context,self.embedding(previous)],-1)
        return self.base(hidden)+F.linear(F.linear(hidden,self.adapter_a),self.adapter_b)

def make_data(seed=7):
    result={}
    for split,n,offset in [('train',96,0),('validation',48,1000),('test',64,2000)]:
        rng=torch.Generator().manual_seed(seed+offset)
        features=torch.randn(n,2,generator=rng)
        rows=[]
        for i,x in enumerate(features.tolist()):
            token=1 if sum(x)>=0 else 2
            rows.append({'id':f'{split}-prompt-{i}','x':x,'chosen':[token,token],'rejected':[3-token,3-token]})
        result[split]=rows
    return result

def tensors(rows):
    return torch.tensor([r['x'] for r in rows]),torch.tensor([[r['chosen'],r['rejected']] for r in rows])

def pair_metrics(model,reference,rows):
    x,y=tensors(rows)
    with torch.no_grad():
        p=model(x,y);ref=reference(x,y)
        logps=p.log_softmax(-1).gather(-1,y.unsqueeze(-1)).squeeze(-1).sum(-1)
        probability=ref.softmax(-1)
        kl=(probability*(ref.log_softmax(-1)-p.log_softmax(-1))).sum(-1).mean().item()
        return {'preference_loss':dpo_loss(p,ref,y,.5).item(),'win_rate':(logps[:,0]>logps[:,1]).float().mean().item(),'reference_to_policy_kl':kl}

def train_policy(model,reference,train,validation,steps):
    x,y=tensors(train)
    with torch.no_grad():ref=reference(x,y)
    optimizer=torch.optim.Adam([model.adapter_a,model.adapter_b],lr=.04)
    history=[];best=None;best_score=float('inf')
    for step in range(steps):
        optimizer.zero_grad(set_to_none=True)
        loss=dpo_loss(model(x,y),ref,y,.5)
        loss.backward();optimizer.step()
        score=pair_metrics(model,reference,validation)['preference_loss']
        if score<best_score:
            best_score=score;best=copy.deepcopy(model.state_dict());best_step=step+1
        if step%20==0 or step==steps-1:
            history.append({'step':step+1,'loss':loss.item(),'validation_loss':score})
    model.load_state_dict(best)
    return best_step,history

def run_experiment(seed=7,steps=100,test_override=None):
    # TODO: implement run_experiment under the declared contract.
    return None

if __name__=='__main__':
    print('Implement run_experiment, then run the lesson checks.')
Inspect the worked answer
import math
import torch
from torch.nn import functional as F

def dpo_loss(policy, reference, targets, beta=.1):
    if policy.ndim!=4 or policy.shape[1]!=2 or min(policy.shape)==0 or policy.shape[-1]<2 or reference.shape!=policy.shape or targets.shape!=policy.shape[:-1]:raise ValueError('Expected [batch, pair=2, time, vocabulary] aligned logits and targets.')
    if not policy.is_floating_point() or not reference.is_floating_point() or targets.dtype!=torch.long or not torch.isfinite(policy).all() or not torch.isfinite(reference).all() or not math.isfinite(beta) or beta<=0:raise ValueError('Expected finite floating logits, int64 labels and beta > 0.')
    mask=targets!=-100
    if (mask.sum(-1)==0).any() or ((targets[mask]<0)|(targets[mask]>=policy.shape[-1])).any():raise ValueError('Every response needs valid in-vocabulary targets.')
    safe=targets.masked_fill(~mask,0)
    policy_sum=(policy.log_softmax(-1).gather(-1,safe.unsqueeze(-1)).squeeze(-1)*mask).sum(-1)
    reference_sum=(reference.detach().log_softmax(-1).gather(-1,safe.unsqueeze(-1)).squeeze(-1)*mask).sum(-1)
    margin=(policy_sum[:,0]-policy_sum[:,1])-(reference_sum[:,0]-reference_sum[:,1])
    result=F.softplus(-beta*margin).mean()
    return result

from torch.nn import functional as F
import copy
import hashlib
import io
import json

def data_hash(rows):
    return hashlib.sha256(json.dumps(rows,sort_keys=True,separators=(',',':')).encode()).hexdigest()

def state_hash(state):
    return data_hash({key:value.detach().cpu().tolist() for key,value in state.items()})

def export_payload(payload):
    stream=io.BytesIO()
    torch.save(payload,stream)
    return stream.getvalue()

def read_payload(blob):
    return torch.load(io.BytesIO(blob),map_location='cpu',weights_only=True)

def check_steps(steps):
    if type(steps)!=int or not 1<=steps<=500:raise ValueError('Choose 1..500 actual optimizer steps.')

def override_rows(original,replacement):
    if replacement is None:return original
    if not isinstance(replacement,list) or len(replacement)!=len(original):raise ValueError('Evaluation replacement must preserve row identities.')
    if [r['id'] for r in replacement]!=[r['id'] for r in original]:raise ValueError('Evaluation row identities must remain unchanged.')
    return copy.deepcopy(replacement)


class PreferencePolicy(torch.nn.Module):
    def __init__(self):
        super().__init__()
        self.embedding=torch.nn.Embedding(3,4)
        self.base=torch.nn.Linear(6,3)
        self.adapter_a=torch.nn.Parameter(torch.randn(2,6)*.05)
        self.adapter_b=torch.nn.Parameter(torch.zeros(3,2))
    def forward(self,features,responses):
        previous=torch.zeros_like(responses)
        previous[:,:,1:]=responses[:,:,:-1]
        context=features[:,None,None,:].expand(-1,2,responses.shape[2],-1)
        hidden=torch.cat([context,self.embedding(previous)],-1)
        return self.base(hidden)+F.linear(F.linear(hidden,self.adapter_a),self.adapter_b)

def make_data(seed=7):
    result={}
    for split,n,offset in [('train',96,0),('validation',48,1000),('test',64,2000)]:
        rng=torch.Generator().manual_seed(seed+offset)
        features=torch.randn(n,2,generator=rng)
        rows=[]
        for i,x in enumerate(features.tolist()):
            token=1 if sum(x)>=0 else 2
            rows.append({'id':f'{split}-prompt-{i}','x':x,'chosen':[token,token],'rejected':[3-token,3-token]})
        result[split]=rows
    return result

def tensors(rows):
    return torch.tensor([r['x'] for r in rows]),torch.tensor([[r['chosen'],r['rejected']] for r in rows])

def pair_metrics(model,reference,rows):
    x,y=tensors(rows)
    with torch.no_grad():
        p=model(x,y);ref=reference(x,y)
        logps=p.log_softmax(-1).gather(-1,y.unsqueeze(-1)).squeeze(-1).sum(-1)
        probability=ref.softmax(-1)
        kl=(probability*(ref.log_softmax(-1)-p.log_softmax(-1))).sum(-1).mean().item()
        return {'preference_loss':dpo_loss(p,ref,y,.5).item(),'win_rate':(logps[:,0]>logps[:,1]).float().mean().item(),'reference_to_policy_kl':kl}

def train_policy(model,reference,train,validation,steps):
    x,y=tensors(train)
    with torch.no_grad():ref=reference(x,y)
    optimizer=torch.optim.Adam([model.adapter_a,model.adapter_b],lr=.04)
    history=[];best=None;best_score=float('inf')
    for step in range(steps):
        optimizer.zero_grad(set_to_none=True)
        loss=dpo_loss(model(x,y),ref,y,.5)
        loss.backward();optimizer.step()
        score=pair_metrics(model,reference,validation)['preference_loss']
        if score<best_score:
            best_score=score;best=copy.deepcopy(model.state_dict());best_step=step+1
        if step%20==0 or step==steps-1:
            history.append({'step':step+1,'loss':loss.item(),'validation_loss':score})
    model.load_state_dict(best)
    return best_step,history

def run_experiment(seed=7,steps=100,test_override=None):
    check_steps(steps);data=make_data(seed)
    data['test']=override_rows(data['test'],test_override)
    rng=torch.get_rng_state()
    try:torch.manual_seed(seed);reference=PreferencePolicy()
    finally:torch.set_rng_state(rng)
    reference.adapter_a.requires_grad_(False);reference.adapter_b.requires_grad_(False)
    base_optimizer=torch.optim.Adam([*reference.embedding.parameters(),*reference.base.parameters()],lr=.03)
    x,y=tensors(data['train'])
    for warmup in range(20):
        base_optimizer.zero_grad(set_to_none=True)
        logits=reference(x,y)[:,0]
        loss=F.cross_entropy(logits.reshape(-1,3),y[:,0].reshape(-1))
        loss.backward();base_optimizer.step()
    for p in reference.parameters():
        p.requires_grad_(False)
        p.grad=None
    model=copy.deepcopy(reference)
    model.adapter_a.requires_grad_(True);model.adapter_b.requires_grad_(True)
    frozen_before=state_hash({k:v for k,v in model.state_dict().items() if not k.startswith('adapter_')})
    initial=copy.deepcopy(model.state_dict());baseline=pair_metrics(model,reference,data['train'])
    best_step,history=train_policy(model,reference,data['train'],data['validation'],steps)
    frozen_after=state_hash({k:v for k,v in model.state_dict().items() if not k.startswith('adapter_')})
    fit_hash=data_hash({k:data[k] for k in ('train','validation')})
    artifact=export_payload({'kind':'local-preference-lora','state':model.state_dict(),'reference':reference.state_dict(),'initial':initial,'seed':seed,'fit_hash':fit_hash})
    fresh=PreferencePolicy();fresh.load_state_dict(read_payload(artifact)['state'])
    test_x,test_y=tensors(data['test'])
    with torch.no_grad():reload_error=(fresh(test_x,test_y)-model(test_x,test_y)).abs().max().item()
    report={'artifact':artifact,'artifact_bytes':len(artifact),'initial_train':baseline,'train':pair_metrics(model,reference,data['train']),'validation':pair_metrics(model,reference,data['validation']),'test':pair_metrics(model,reference,data['test']),'best_step':best_step,'history':history,'frozen_before':frozen_before,'frozen_after':frozen_after,'trainable_parameters':sum(p.numel() for p in model.parameters() if p.requires_grad),'total_parameters':sum(p.numel() for p in model.parameters()),'reload_max_error':reload_error,'fit_hash':fit_hash,'data_hash':data_hash(data),'ids':{k:[r['id'] for r in rows] for k,rows in data.items()},'steps':steps,'base_provenance':'locally initialized and supervised-trained on synthetic training rows; not pretrained language-model weights'}
    return report

if __name__=='__main__':
    result=run_experiment()
    with open('preference-lora.pt','wb') as f:f.write(result['artifact'])
    with open('preference-report.json','w') as f:json.dump({k:v for k,v in result.items() if k!='artifact'},f,indent=2)
    print('Saved preference-lora.pt and preference-report.json')

About this algorithm

How Project: train and reload a local DPO policy with LoRA ownership works

The idea

Direct Preference Optimization (DPO) trains a model from pairs of responses in which one was chosen and the other rejected, with no separate reward model. For each pair it asks how much more the policy prefers the chosen response than a frozen reference model does, measured in summed token log-probabilities, and raises that margin through the loss softplus(-beta * margin). Measuring against the reference ties the policy to its starting behaviour.

LoRA decides which weights may move. The base layer stays frozen, and the product of two small matrices, adapter_b and adapter_a, is added to its output. run_experiment trains a small local base model, freezes it as the reference, clones it, and lets only the two rank-two adapters learn.

How it works

  1. Build the synthetic prompts with make_data(seed) and train the base for 20 steps of cross-entropy on the chosen training responses.
  2. Freeze every parameter of that model as the reference, deep-copy it, and enable gradients only for adapter_a and adapter_b.
  3. At each step, compute dpo_loss on the training pairs with beta=.5 and update the adapters with Adam.
  4. Score the validation pairs after every step and keep a copy of the state with the lowest preference loss.
  5. Restore that state and hash the non-adapter weights before and after training to show the base did not change.
  6. Save the model, reference and initial state with torch.save, reload into a fresh PreferencePolicy, and report loss, win rate and KL on test prompts.

What it costs

Trainable
18 of 51 parameters
Per step
full batch of 96 pairs
Reference
forward only, no grads

Only the 18 adapter weights receive gradients, the reference is run without gradients, and each optimizer step uses the whole training set of 96 preference pairs.

Where it is used

  • Hugging Face's TRL library includes a DPOTrainer for fine-tuning language models on chosen and rejected response pairs.
  • The Zephyr-7B-beta chat model was produced by applying DPO to Mistral-7B with the UltraFeedback preference dataset.
  • The Hugging Face PEFT library implements LoRA, and image model fine-tunes are widely shared as small LoRA weight files.

Watch out for

  • Leaving the base or the reference trainable, so the loss falls by moving weights that should stay fixed and the frozen hashes stop matching.
  • Initialising both adapter matrices to zero, which gives zero gradients. Here adapter_b starts at zero and adapter_a is small and random.
  • Selecting the checkpoint on test pairs: the Lab reverses the test preferences and checks that the fitted weights do not change.