Download code/plot_ouro.py from zigzag9939/jlens-looped-transformers: direct link, hf CLI and curl.
- Browser
- Download file 1.96 kB
-
https://huggingface.co/zigzag9939/jlens-looped-transformers/resolve/main/code/plot_ouro.py
- Command line
-
hf download hf://zigzag9939/jlens-looped-transformers/code/plot_ouro.py
-
curl -L -o plot_ouro.py https://huggingface.co/zigzag9939/jlens-looped-transformers/resolve/main/code/plot_ouro.py
1.96 kB
| import json | |
| import matplotlib | |
| matplotlib.use("Agg") | |
| import matplotlib.pyplot as plt | |
| import torch | |
| res = json.load(open("ouro_analysis.json")) | |
| ll = json.load(open("ouro_probes_ll.json")) | |
| NP, R = 48, 4 | |
| V = NP * R | |
| bounds = [s * NP for s in range(1, R)] | |
| fig, axes = plt.subplots(1, 3, figsize=(16, 4.2)) | |
| ax = axes[0] | |
| ax.plot(res["kl_jlens"], label="Jacobian lens", lw=1.5) | |
| ax.plot(res["kl_logitlens"], label="logit-lens", lw=1.5) | |
| for b in bounds: | |
| ax.axvline(b, color="grey", ls=":", lw=0.8) | |
| ax.set_xlabel("virtual layer (48 layers x 4 loop steps)") | |
| ax.set_ylabel("KL(model || lens) [nats]") | |
| ax.set_title("Lens quality across the unrolled loop (WikiText)") | |
| ax.legend() | |
| ax = axes[1] | |
| ax.plot(ll["top1_agree_jlens"], label="Jacobian lens", lw=1.5) | |
| ax.plot(ll["top1_agree_logitlens"], label="logit-lens", lw=1.5) | |
| for b in bounds: | |
| ax.axvline(b, color="grey", ls=":", lw=0.8) | |
| ax.set_xlabel("virtual layer") | |
| ax.set_ylabel("top-1 agreement with final output") | |
| ax.set_title("Readout agreement by virtual depth") | |
| ax.legend() | |
| ax = axes[2] | |
| norms = [res["J_norm"][str(v)] for v in range(V - 1)] | |
| dist = [res["J_dist_eye"][str(v)] for v in range(V - 1)] | |
| ax.plot(norms, label="||J||/sqrt(d)", lw=1.5) | |
| ax.plot(dist, label="||J - I||/sqrt(d)", lw=1.5) | |
| for b in bounds: | |
| ax.axvline(b, color="grey", ls=":", lw=0.8) | |
| ax.set_xlabel("virtual layer") | |
| ax.set_title("Average Jacobian geometry") | |
| ax.legend() | |
| fig.tight_layout() | |
| fig.savefig("ouro_lens_curves.png", dpi=150) | |
| sim = torch.load("ouro_jsim.pt")["sim"].numpy() | |
| fig2, ax = plt.subplots(figsize=(6.5, 5.5)) | |
| im = ax.imshow(sim, vmin=0, vmax=1, cmap="viridis", origin="lower") | |
| for b in bounds: | |
| ax.axvline(b - 0.5, color="w", ls=":", lw=0.8) | |
| ax.axhline(b - 0.5, color="w", ls=":", lw=0.8) | |
| ax.set_xlabel("virtual layer") | |
| ax.set_ylabel("virtual layer") | |
| ax.set_title("cos similarity of J across virtual layers") | |
| fig2.colorbar(im) | |
| fig2.tight_layout() | |
| fig2.savefig("ouro_jsim_heatmap.png", dpi=150) | |
| print("DONE_PLOTS") | |