import numpy as np, sys EOT=50256 def stats(path,T,maxw=None): a=np.memmap(path,dtype=np.uint16,mode="r"); nw=(a.size-1)//T if (a.size-1)%T==0 else a.size//T if maxw: nw=min(nw,maxw) ndocs=np.zeros(nw,int); keep=np.zeros(nw); segtok={} edges=[0,512,2048,8192,T//2,T,T+1] hist=np.zeros(len(edges)-1) for w in range(nw): x=np.asarray(a[w*T:(w+1)*T]); e=np.flatnonzero(x==EOT) b=np.concatenate(([0],e[e>0],[T])) if (e.size==0 or e[0]!=0) else np.concatenate((e,[T])) L=np.diff(b); L=L[L>0]; ndocs[w]=L.size keep[w]=(L*(L+1)/2).sum()/(T*(T+1)/2) # surviving causal pairs / dense causal pairs hist+=np.histogram(L,bins=edges,weights=L)[0] hist/=hist.sum() q=np.percentile(ndocs,[50,90,99]) return nw,(ndocs==1).mean(),q,keep.mean(),np.percentile(keep,[10,50,90]),hist,edges for name,p,T in zip(sys.argv[1::3],sys.argv[2::3],map(int,sys.argv[3::3])): nw,single,q,km,kq,h,ed=stats(p,T) lab=["<512","512-2k","2k-8k",f"8k-{T//2048}k",f"{T//2048}k-<{T//1024}k",f"={T//1024}k full"] print(f"{name:22s} T={T//1024}k windows {nw:,} | single-doc windows {100*single:.1f}% | docs/window p50 {q[0]:.0f} p90 {q[1]:.0f} p99 {q[2]:.0f}") print(f"{'':22s} mask keeps {100*km:.1f}% of causal work on average (p10/p50/p90 {100*kq[0]:.0f}/{100*kq[1]:.0f}/{100*kq[2]:.0f}%)") print(f"{'':22s} tokens by in-window segment length: " + " ".join(f"{l} {100*v:.1f}%" for l,v in zip(lab,h)))