aien-train
AIEN training programme: language-model history, GB10 constraints and the route to training on the project engine. Plans and runnable checks are distinguished.
Brought by drakestapleton
aien-train / streams / math-training / extract_e1.py
| 1 | # E1 step 1: freeze SmolLM2-1.7B, extract final-norm features X and next tokens Y once, save to .scratch. |
| 2 | # Splits: fit = GSM8K train examples [0, 6000); val = train [6000, 7473) (for lambda / early stop); test = GSM8K test. |
| 3 | import os, time, torch |
| 4 | from common import * |
| 5 | |
| 6 | CAP = {"fit": 262144, "val": 32768, "test": 65536} |
| 7 | |
| 8 | tok, model = load_model() |
| 9 | train, test = load_split("train"), load_split("test") |
| 10 | parts = {"fit": train[:6000], "val": train[6000:], "test": test} |
| 11 | E = model.model.embed_tokens.weight.detach() # tied LM head, [V, d] |
| 12 | |
| 13 | # Check that last_hidden_state is the post-norm state the LM head reads: logits must match. |
| 14 | ids = encode(tok, parts["test"][:2]) |
| 15 | x, m = next(batches(ids, 2)) |
| 16 | with torch.no_grad(): |
| 17 | ref = model(input_ids=x.to(DEV), attention_mask=m.long().to(DEV)).logits.float() |
| 18 | h = model.model(input_ids=x.to(DEV), attention_mask=m.long().to(DEV)).last_hidden_state |
| 19 | mine = (h @ E.T).float() |
| 20 | md = (ref - mine)[m.to(DEV)].abs().max().item() |
| 21 | print("logit check max abs diff", md) |
| 22 | assert md < 1e-2, md |
| 23 | |
| 24 | info = {"logit_check_max_abs_diff": md} |
| 25 | for name, exs in parts.items(): |
| 26 | t0 = time.time() |
| 27 | X, Y = extract_final(model, encode(tok, exs), CAP[name]) |
| 28 | torch.save({"X": X, "Y": Y}, os.path.join(SCRATCH, f"e1-{name}.pt")) |
| 29 | info[name] = {"examples_available": len(exs), "tokens": int(X.shape[0]), "d": int(X.shape[1]), |
| 30 | "sha256_X": sha_of_tensor(X), "seconds": time.time() - t0} |
| 31 | print(name, info[name]) |
| 32 | torch.save(E.float().cpu(), os.path.join(SCRATCH, "e1-embed.pt")) |
| 33 | info["peak_gpu_bytes"] = torch.cuda.max_memory_allocated() |
| 34 | receipt("e1-extract", info) |