# Stage 6d: offline simulation of the suffix drafter (trigram index, K candidates) on a capture: usage accept-knobs-suffix-sim.py . CPU only. import json,glob,sys,collections def run(files,Ks,maxlen=7,min_match=3,max_match=64): res={K:[0,0,0,0] for K in Ks} # positions, proposals, accepted tokens sum, accepted>=maxlen for f in files: j=json.load(open(f)); P=j['prompt_ids']; G=j['generated_ids'] H=P+G n0=len(P) occ=collections.defaultdict(list) # trigram -> end positions (ascending) for e in range(2,n0): # trigram ending at e covers e-2..e occ[(H[e-2],H[e-1],H[e])].append(e) for i in range(n0, n0+len(G)-1): # history = H[:i]; cur = i-1 cur=i-1 # add trigram ending at cur if not present (positions up to cur-1 were added) if cur>=2: key=(H[cur-2],H[cur-1],H[cur]) cand=[p for p in occ.get(key,[]) if pbest_len: best_len=L;best_end=p if best_len>=min_match: res[K][1]+=1 a=0 for t in range(maxlen): q=best_end+1+t if q>cur or i+t>=len(H): break if H[q]==H[i+t]: a+=1 else: break res[K][2]+=a if a>=maxlen: res[K][3]+=1 return res files=sorted(glob.glob(sys.argv[1]+'/req-*.json'))[:int(sys.argv[2])] for ml in (7,11): r=run(files,[4,8,16,'all'],maxlen=ml) print('maxlen',ml) for K,(pos,pr,acc,full) in r.items(): print(' K=%-4s positions %d proposals %.3f of positions, mean accepted per proposal %.2f, accepted tokens per position %.3f, full-length share of proposals %.3f'%(K,pos,pr/pos,acc/max(pr,1),acc/pos,full/max(pr,1)))