Upload scripts/mk_explain.py with huggingface_hub
Browse files- scripts/mk_explain.py +65 -0
scripts/mk_explain.py
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""One prompt, three scales. Why the same weights score 40 or 92."""
|
| 3 |
+
import matplotlib; matplotlib.use("Agg")
|
| 4 |
+
import matplotlib.pyplot as plt
|
| 5 |
+
from matplotlib.patches import FancyArrowPatch, Rectangle
|
| 6 |
+
S={"surface":"#fcfcfb","ink":"#0b0b0b","ink2":"#52514e","grid":"#e4e4e1"}
|
| 7 |
+
RED,AMB,BLU="#b3243b","#eb6834","#12508f"
|
| 8 |
+
fig,(ax,bx)=plt.subplots(2,1,figsize=(12.6,8.6),facecolor=S["surface"],
|
| 9 |
+
gridspec_kw={"height_ratios":[1.15,1],"hspace":.42})
|
| 10 |
+
|
| 11 |
+
# ---- top: the squeeze -------------------------------------------------------
|
| 12 |
+
ax.set_facecolor(S["surface"]); ax.axis("off")
|
| 13 |
+
ax.set_xlim(0,100); ax.set_ylim(0,100)
|
| 14 |
+
ax.text(0,96,"ONE prompt of 16,360 tokens. Same model file. Only YARN_SCALE changes.",
|
| 15 |
+
fontsize=12.5,color=S["ink"],weight="bold",va="top")
|
| 16 |
+
# the familiar band
|
| 17 |
+
for y,(s,pos,sc,col) in zip((66,40,14),((4,4090,40,RED),(8,2045,67,AMB),(16,1022,92,BLU))):
|
| 18 |
+
ax.add_patch(Rectangle((14,y-4),62,8,fc="#f0f0ee",ec=S["grid"]))
|
| 19 |
+
ax.text(13,y,f"x{s}",fontsize=11,color=col,ha="right",va="center",weight="bold")
|
| 20 |
+
# familiar / rare split at 4096 -> the bar spans 0..4096 perceived
|
| 21 |
+
frac=pos/4096.0
|
| 22 |
+
ax.add_patch(Rectangle((14,y-4),62*frac,8,fc=col,alpha=.30,ec="none"))
|
| 23 |
+
ax.plot([14+62*frac],[y],"o",color=col,ms=11)
|
| 24 |
+
if s == 4:
|
| 25 |
+
ax.text(14+62*frac-2,y-7.0,f"last token perceived\nat position {pos:,}",
|
| 26 |
+
fontsize=8.4,color=col,ha="right",va="top")
|
| 27 |
+
else:
|
| 28 |
+
ax.text(14+62*frac,y+7.0,f"last token perceived\nat position {pos:,}",
|
| 29 |
+
fontsize=8.4,color=col,ha="center",va="bottom")
|
| 30 |
+
ax.text(78,y,f"scores {sc}",fontsize=11,color="white",va="center",
|
| 31 |
+
bbox=dict(fc=col,ec="none",pad=3.4))
|
| 32 |
+
ax.plot([76,76],[8,72],color=S["ink2"],lw=1.2,ls="--")
|
| 33 |
+
ax.text(78.5,80,"position 4,096\nedge of pretraining",fontsize=8.4,color=S["ink2"],ha="left")
|
| 34 |
+
ax.text(14,4,"position 0",fontsize=8.4,color=S["ink2"],ha="center")
|
| 35 |
+
ax.text(45,-1,"the 4,096-position range the model was pretrained on",
|
| 36 |
+
fontsize=9,color=S["ink2"],ha="center",style="italic")
|
| 37 |
+
|
| 38 |
+
# ---- bottom: where the pretraining data actually was ------------------------
|
| 39 |
+
bx.set_facecolor(S["surface"])
|
| 40 |
+
for s in bx.spines.values(): s.set_color(S["grid"])
|
| 41 |
+
bx.grid(True,color=S["grid"],lw=.7,alpha=.9); bx.tick_params(colors=S["ink2"],labelsize=9)
|
| 42 |
+
import math
|
| 43 |
+
xs=list(range(0,4097,32))
|
| 44 |
+
# schematic doc-length survival curve: p90 = 2,721
|
| 45 |
+
dens=[100*math.exp(-(x/2721.0)**1.7) for x in xs]
|
| 46 |
+
bx.fill_between(xs,dens,color="#9dc3ea",alpha=.55)
|
| 47 |
+
bx.plot(xs,dens,color=BLU,lw=2)
|
| 48 |
+
for pos,sc,col,lab,ha,dx in ((4090,40,RED,"x4","right",-60),(2045,67,AMB,"x8","center",0),
|
| 49 |
+
(1022,92,BLU,"x16","center",0)):
|
| 50 |
+
bx.axvline(pos,color=col,lw=2,ls="--")
|
| 51 |
+
bx.text(pos+dx,95,lab,color=col,fontsize=10.5,ha=ha,weight="bold",va="top")
|
| 52 |
+
bx.text(pos+dx,86,f"scores {sc}",color="white",fontsize=9,ha=ha,va="top",
|
| 53 |
+
bbox=dict(fc=col,ec="none",pad=2.2))
|
| 54 |
+
bx.axvline(2721,color=S["ink2"],lw=1.2)
|
| 55 |
+
bx.text(2721,52," p90 document\n length 2,721",fontsize=8.4,color=S["ink2"])
|
| 56 |
+
bx.set_xlim(0,4096); bx.set_ylim(0,100)
|
| 57 |
+
bx.set_xlabel("perceived position",color=S["ink2"],fontsize=10)
|
| 58 |
+
bx.set_ylabel("how often pretraining\nreached this position (%)",color=S["ink2"],fontsize=10)
|
| 59 |
+
bx.set_title("Why: past ~2,700 the model has almost no experience",
|
| 60 |
+
color=S["ink"],fontsize=11.5,loc="left",pad=9)
|
| 61 |
+
fig.text(.005,.005,"Verified numerically: token 16,360 at x16 and token 1,022 unscaled differ by 1.4e-4 radians "
|
| 62 |
+
"in the interpolated dims -- indistinguishable to the model. Document-length curve is schematic, anchored on the measured p90 of 2,721.",
|
| 63 |
+
fontsize=7.8,color=S["ink2"])
|
| 64 |
+
fig.savefig("results_2026-09-16/why_scale_works.png",dpi=150,bbox_inches="tight")
|
| 65 |
+
print(" wrote results_2026-09-16/why_scale_works.png")
|