Files
leleka/scripts/e2e/decode/accept-knobs-suffix-sim.py
T

45 lines
2.1 KiB
Python

# Stage 6d: offline simulation of the suffix drafter (trigram index, K candidates) on a capture: usage accept-knobs-suffix-sim.py <capture dir> <requests>. 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 p<cur]
occ[key].append(cur)
else: cand=[]
for K in Ks:
res[K][0]+=1
best_len=0;best_end=-1
for p in (cand[::-1] if K=='all' else cand[::-1][:K]):
L=0
while L<max_match and L<=p and H[p-L]==H[cur-L]: L+=1
if L>best_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)))