Loading codebase...
Complete Source Code
Every single line of code behind Build Atlas, organized across all 9 parts. From tensor math, autograd, and byte-level BPE to TinyGPT training, LoRA chat tuning, hybrid RAG, autonomous agents, and continuous-batch serving.
Supervised fine-tuning (SFT) pipeline with prompt templates, loss masking on user turns, and checkpoint loading.
"""Part 5 · Make it helpful (1/2): supervised fine-tuning with a chat template.
A base model continues text. To make it answer, we show it conversations in a fixed format and train only on
the assistant's tokens (the loss mask): it should learn to *answer*, not to predict the user's question.
"""
from __future__ import annotations
import random
import torch
USER, ASSISTANT, END = "<|user|>", "<|assistant|>", "<|end|>"
def render_prompt(question: str, context: str | None = None) -> str:
"""Everything up to where the assistant starts writing (the generation cue)."""
ctx = f"Context:\n{context}\n\n" if context else ""
return f"{USER}\n{ctx}{question}\n{ASSISTANT}\n"
def build_example(tok, question: str, answer: str, block: int) -> tuple:
"""Token ids for one conversation, and a mask that is 1 only on the answer (+ the end marker)."""
p = tok.encode(render_prompt(question))
a = tok.encode(answer + END)
ids = (p + a)[: block + 1]
mask = ([0] * len(p) + [1] * len(a))[: block + 1]
return ids, mask
def make_batch(examples: list, idx: list, block: int) -> tuple:
"""Pad to a fixed length; padding positions are masked out of the loss."""
x = torch.zeros(len(idx), block, dtype=torch.long)
y = torch.zeros(len(idx), block, dtype=torch.long)
m = torch.zeros(len(idx), block, dtype=torch.long)
for r, i in enumerate(idx):
ids, mask = examples[i]
n = len(ids) - 1
x[r, :n] = torch.tensor(ids[:-1])
y[r, :n] = torch.tensor(ids[1:])
m[r, :n] = torch.tensor(mask[1:]) # mask aligned with the *targets*
return x, y, m
def split_qa(qa: list, n_val: int = 16, seed: int = 0) -> tuple:
pairs = list(qa)
random.Random(seed).shuffle(pairs)
return pairs[n_val:], pairs[:n_val]
@torch.no_grad()
def answer_loss(model, tok, pairs: list) -> float:
"""Mean cross-entropy on the answer tokens only: lower means the model finds the right answers more likely."""
model.eval()
ex = [build_example(tok, q, a, model.config.block_size) for q, a in pairs]
x, y, m = make_batch(ex, list(range(len(ex))), model.config.block_size)
_, loss = model(x, y, loss_mask=m)
model.train()
return float(loss)
def train_sft(model, tok, pairs: list, steps: int = 300, lr: float = 1e-3, batch: int = 16, seed: int = 0, log=print) -> None:
gen = torch.Generator().manual_seed(seed)
block = model.config.block_size
examples = [build_example(tok, q, a, block) for q, a in pairs]
params = [p for p in model.parameters() if p.requires_grad]
opt = torch.optim.AdamW(params, lr=lr, weight_decay=0.0)
model.train()
for s in range(steps):
idx = torch.randint(0, len(examples), (batch,), generator=gen).tolist()
x, y, m = make_batch(examples, idx, block)
_, loss = model(x, y, loss_mask=m)
opt.zero_grad(set_to_none=True)
loss.backward()
torch.nn.utils.clip_grad_norm_(params, 1.0)
opt.step()
if s % 100 == 0 or s == steps - 1:
log(f"sft step {s:4d} answer loss {loss.item():.3f}")
def chat(model, tok, question: str, max_new_tokens: int = 60, temperature: float = 0.0, context: str | None = None) -> str:
ids = torch.tensor([tok.encode(render_prompt(question, context))])
out = model.generate(ids, max_new_tokens, temperature=temperature, top_k=None)[0, ids.shape[1]:].tolist()
text = tok.decode(out)
# The end marker spans several tokens and a small model may only half-produce it: cut at any fragment.
for stop in (END, "<|", "|end", "|>"):
text = text.split(stop)[0]
return text.strip()