modelclinic-experiment-001 / code /src /analyze_long.py
PratikBuilds's picture
Publish initial ModelClinic Experiment 001 archive
87cc7f2 verified
Raw History Blame Contribute Delete
3.52 kB
"""Summarize saved per-generation vLLM diagnostic files; never impute missing answers."""
import csv,json,statistics
from pathlib import Path
from paired_stats import paired_cluster_summary
ROOT=Path('results/long_diagnostic/collected'); OUT=Path('results/long_diagnostic')
rows=[]
for name in ('vllm_long_v1','vllm_nothink_v1'):
for p in sorted((ROOT/name).glob('*.json')):
if p.name.endswith('.partial.json'):continue
r=json.loads(p.read_text(encoding='utf8'));r['_source']=str(p);rows.append(r)
keys=[(r['source_row'],r['condition']) for r in rows]
if len(keys)!=len(set(keys)):raise ValueError('Duplicate condition/question result')
conditions=['base','edited','constant','base_no_thinking']; group={c:[r for r in rows if r['condition']==c] for c in conditions}; summary={'n_records':len(rows),'conditions':{},'by_question':[]}
for c,rs in group.items():
lengths=[x['tokens'] for x in rs];times=[x['elapsed_seconds'] for x in rs];status={}
for x in rs:status[x['score']['status']]=status.get(x['score']['status'],0)+1
summary['conditions'][c]={'n':len(rs),'scoring':status,'finish':{k:sum(x['finish_reason']==k for x in rs) for k in ('stop','length','wall_time','error','other_stop')},'reasoning_closed':sum(x['reasoning_closed'] for x in rs),'tokens':lengths,'median_tokens':statistics.median(lengths) if lengths else None,'seconds':times,'median_seconds':statistics.median(times) if times else None,'max_eightgram_frequency':[x['loop_heuristic']['max_eightgram_frequency'] for x in rs]}
def paired_outcomes(condition):
result={}
for record in group.get(condition,[]):
if record.get('error'):
continue
question=record['source_row']
replicate=record.get('seed',record.get('settings',{}).get('seed',0))
if replicate in result.setdefault(question,{}):
raise ValueError(f'Duplicate paired question/seed: {question}/{replicate}/{condition}')
# Match the authors' solve-by-cap denominator: a missing or unparseable
# final box receives no credit, while its raw status remains in the report.
result[question][replicate]=record.get('score',{}).get('status')=='correct'
return result
summary['paired']={}
for first,second in (('edited','base'),('edited','constant')):
summary['paired'][f'{first}_minus_{second}']=paired_cluster_summary(
paired_outcomes(first),paired_outcomes(second)
)
for source in sorted({x['source_row'] for x in rows}):
q={'source_row':source}
for c in conditions:
matching=[x for x in rows if x['source_row']==source and x['condition']==c]
q[c]=None if not matching else {k:matching[0][k] for k in ('score','tokens','finish_reason','reasoning_closed','elapsed_seconds','loop_heuristic','error')}
summary['by_question'].append(q)
OUT.mkdir(parents=True,exist_ok=True);(OUT/'analysis.json').write_text(json.dumps(summary,indent=2),encoding='utf8')
with (OUT/'per_question.csv').open('w',newline='',encoding='utf8') as f:
w=csv.writer(f);w.writerow(['source_row','condition','grade_status','extracted_answer','tokens','finish_reason','reasoning_closed','elapsed_seconds','max_eightgram_frequency','estimated_generation_gpu_usd'])
for r in sorted(rows,key=lambda x:(x['source_row'],x['condition'])):
w.writerow([r['source_row'],r['condition'],r['score']['status'],r['score']['answer'],r['tokens'],r['finish_reason'],r['reasoning_closed'],round(r['elapsed_seconds'],2),r['loop_heuristic']['max_eightgram_frequency'],round(r['elapsed_seconds']*1.1/3600,6)])
print(json.dumps(summary['conditions'],indent=2))