Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/aien-train/blob/main/streams/own-fp4/outlier_study.py # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `aien-train / streams / own-fp4 / outlier_study.py` 57 lines ยท 3447 bytes Raw Blame History 1 # outlier_study.py (L284 own-fp4 Part 2b, ORACLE): on real Wgrad operands dumped from the BF16 baseline (X and dY of four 2 # layers at three points of training), how much error does each outlier treatment leave in the weight gradient 3 # dW = dY^T X when both operands are quantized to NVFP4 (1x16 along the token dimension, X nearest, dY stochastic)? 4 # Metric: relative Frobenius error of dW against the FP32 product (the GEMM output, not per-element error). 5 # ~/.venv/bin/python -I outlier_study.py DUMP_DIR OUT.json 6 import glob, json, os, sys, torch 7 sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) 8 import fp4sim as Q 9 10 dev = "cuda" if torch.cuda.is_available() else "cpu" 11 gen = torch.Generator(device=dev); gen.manual_seed(5) 12 sgn = torch.where(torch.rand(16, generator=torch.Generator().manual_seed(11)) < 0.5, -1.0, 1.0).to(dev) 13 H16 = Q.hadamard(16, dev); R16 = H16 * sgn[None, :] 14 sgn128 = torch.where(torch.rand(128, generator=torch.Generator().manual_seed(12)) < 0.5, -1.0, 1.0).to(dev) 15 R128 = Q.hadamard(128, dev) * sgn128[None, :] 16 17 def diag(x, dy): # exact per-token balancing with power-of-two factors (dW unchanged in exact arithmetic) 18 s = (dy.norm(dim=1) / x.norm(dim=1).clamp(min=1e-30)).clamp(min=1e-30).sqrt() 19 s = torch.exp2(torch.round(torch.log2(s))) 20 return x * s[:, None], dy / s[:, None] 21 22 T = { 23 "none": lambda x, d: (x, d), 24 "signs_only": lambda x, d: (x * sgn.repeat(x.shape[0] // 16)[:, None], d * sgn.repeat(d.shape[0] // 16)[:, None]), 25 "h16_fixed": lambda x, d: (Q.rotate_tokens(x, H16), Q.rotate_tokens(d, H16)), 26 "rht16": lambda x, d: (Q.rotate_tokens(x, R16), Q.rotate_tokens(d, R16)), 27 "rht128": lambda x, d: (Q.rotate_tokens(x, R128), Q.rotate_tokens(d, R128)), 28 "diag_pow2": diag, 29 "diag+rht16": lambda x, d: T["rht16"](*diag(x, d)), 30 } 31 # cost per operand element on the chip (multiply-adds), for the table 32 COST = {"none": 0, "signs_only": 0, "h16_fixed": 16, "rht16": 16, "rht128": 128, "diag_pow2": 1, "diag+rht16": 17} 33 34 def run(dump_dir, out): 35 rows = [] 36 for f in sorted(glob.glob(f"{dump_dir}/dump_step*.pt")): 37 step = int(f.split("dump_step")[1].split(".")[0]); d = torch.load(f) 38 for tag in sorted({k.rsplit(".", 1)[0] for k in d}): 39 x = d[tag + ".x"].float().to(dev); dy = d[tag + ".dy"].float().to(dev) 40 ref = dy.T @ x 41 for name, fn in T.items(): 42 for fmt in ("nv", "mx"): 43 xr, dr = fn(x, dy) 44 if fmt == "nv": xq, g1 = Q.quant_nv(xr, 0); dq, g2 = Q.quant_nv(dr, 0, True, gen) 45 else: xq, g1 = Q.quant_mx(xr, 0); dq, g2 = Q.quant_mx(dr, 0, True, gen) 46 dw = (dq.T @ xq) * (g1 * g2) 47 rows.append(dict(step=step, layer=tag, transform=name, fmt=fmt, dw_rel_err=Q.rel_err(dw, ref), 48 x_rel_err=Q.rel_err(xq * g1, xr), dy_rel_err=Q.rel_err(dq * g2, dr), cost_mac_per_elem=COST[name])) 49 json.dump(rows, open(out, "w"), indent=0) 50 # summary: mean dW error per transform and format over all layers and steps 51 summ = {} 52 for r in rows: summ.setdefault((r["fmt"], r["transform"]), []).append(r["dw_rel_err"]) 53 for (fmt, name), v in sorted(summ.items()): 54 print(f"{fmt} {name:12s} mean dW rel err {sum(v)/len(v):.4f} max {max(v):.4f} n {len(v)} cost {COST[name]} MAC/elem") 55 56 if __name__ == "__main__": 57 run(sys.argv[1], sys.argv[2])