"""Archive 2026-10-15 measurements: per-mix profile, context signal, per-window kernel workload, and aligned-packing feasibility. Writes JSON + NPZ into the given out dir.""" import numpy as np, json, sys, bisect, os EOT=50256 EDGES=[0,4096,16384,32768,65536,131072,1<<40] out=sys.argv[1]; os.makedirs(out,exist_ok=True) R="/localstorage/adhi_l32/data/" MIXES=[("p2c",R+"mix_p2c",32768,False),("p2d",R+"mix_p2d",32768,True), ("p1b_unaligned",R+"mix_p1b_unaligned",16384,False),("p1b",R+"mix_p1b",16384,True)] res={} for name,d,T,aligned in MIXES: a=np.fromfile(d+"/train_000000.bin",dtype=np.uint16); n=(a.size//T)*T; x=a[:n] e=np.flatnonzero(x==EOT); pos=np.arange(n,dtype=np.int64); last=np.zeros(n,dtype=np.int64) last[e]=e; last=np.maximum.accumulate(last); ctx=pos-np.maximum(last,(pos//T)*T) thr=[T//8,T//4,T//2,3*T//4] r={"T":T,"aligned":aligned,"windows":n//T,"steps":(n//T)//128, "ctx_ge":{str(t):float((ctx>=t).mean()) for t in thr}} if not aligned: # document-length profile from whole documents (aligned files split long docs) s=np.flatnonzero(a==EOT); L=np.diff(np.append(s,a.size)) r["bucket_token_share"]=[float(L[(L>=lo)&(L0],[T])) Ls=np.diff(b); Ls=Ls[Ls>0]; nd[w]=Ls.size keep[w]=(Ls*(Ls+1)/2).sum()/(T*(T+1)/2); seg+=np.histogram(Ls,bins=segE,weights=Ls)[0] r["single_doc_windows"]=float((nd==1).mean()); r["docs_per_window_pct"]=dict(zip(["p50","p90","p99"],map(float,np.percentile(nd,[50,90,99])))) r["mask_keeps_mean"]=float(keep.mean()); r["mask_keeps_pct"]=dict(zip(["p10","p50","p90"],map(float,np.percentile(keep,[10,50,90])))) r["segment_edges"]=segE[:-1]; r["segment_token_share"]=list(map(float,seg/seg.sum())) rep=json.load(open(d+"/mix_report.json")); r["mix_report"]=rep np.savez_compressed(f"{out}/windows_{name}.npz",docs_per_window=nd,mask_keeps=keep.astype(np.float32)) res[name]=r; print(name,"done",flush=True) json.dump(res,open(f"{out}/mix_stats.json","w"),indent=1) # aligned-packing feasibility: empty window space after best-fit, by anchor threshold (no float-tails) feas={} for name,d,T in (("p2c",R+"mix_p2c",32768),("p1b_unaligned",R+"mix_p1b_unaligned",16384)): a=np.memmap(d+"/train_000000.bin",dtype=np.uint16,mode="r"); s=np.flatnonzero(np.asarray(a)==EOT); L=np.diff(np.append(s,a.size)) rows=[] for A in (T//4,T//2,T): longs=[int(l) for l in sorted(L[L>=A],reverse=True)]; fills=sorted((int(l) for l in L[L0: bisect.insort(gaps,(T-l,len(W)-1)) else: r_,w=gaps.pop(k); W[w]+=l if r_-l>0: bisect.insort(gaps,(r_-l,w)) G=sum(r_ for r_,_ in gaps); rows.append({"align_min":A,"windows":len(W),"partial":len(gaps),"empty_tokens":int(G),"empty_frac":G/a.size}) feas[name]=rows; print(name,"feasibility done",flush=True) json.dump(feas,open(f"{out}/packing_feasibility.json","w"),indent=1)