SmallMath / scripts /verify.py
SmallMath dataset builder
Build SmallMath Harbor evaluation dataset
de0a028
Raw History Blame Contribute Delete
2.23 kB
#!/usr/bin/env python3
"""Run every oracle and sample rejection checks. Requires bash, python3 and timeout."""
import concurrent.futures, json, os, pathlib, subprocess, tempfile
ROOT=pathlib.Path(__file__).resolve().parents[1]
tasks=sorted(p for p in (ROOT/'tasks').iterdir() if p.is_dir())
assert len(tasks)==600
def run(task, case='oracle'):
with tempfile.TemporaryDirectory(prefix='smallmath-check-') as tmp:
p=pathlib.Path(tmp); answer=p/'answer.txt'; out=p/'verifier'; out.mkdir(); temp=p/'temp'; temp.mkdir()
env={**os.environ,'ANSWER':str(answer),'VERIFIER_DIR':str(out),'TMPDIR':str(temp),'PYTHONDONTWRITEBYTECODE':'1'}
if case=='oracle': subprocess.run(['bash',str(task/'solution/solve.sh')],env=env,check=True,capture_output=True)
elif case=='wrong': answer.write_text(str(int(json.loads((task/'tests/reference.json').read_text())['answer'])+1))
elif case=='empty': answer.touch()
elif case=='symlink':
target=p/'target'; target.write_text(json.loads((task/'tests/reference.json').read_text())['answer']); answer.symlink_to(target)
elif case=='oversized': answer.write_text('0'*10001)
elif case=='fifo': os.mkfifo(answer)
elif case!='missing': raise ValueError(case)
result=subprocess.run(['bash',str(task/'tests/test.sh')],env=env,check=True,capture_output=True,text=True,timeout=180)
reward=(out/'reward.txt').read_text().strip(); expected='1' if case=='oracle' else '0'
return task.name,case,reward,expected,result.stdout.strip(),(out/'grade.json').read_text()
if __name__=='__main__':
failures=[]
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as pool:
for i,r in enumerate(pool.map(run,tasks),1):
if r[2]!=r[3]: failures.append(r); print('FAIL',r,flush=True)
if i%50==0: print('oracle',i,'/',len(tasks),'failures',len(failures),flush=True)
sample=[next(t for t in tasks if f'level = {i}\n' in (t/'task.toml').read_text()) for i in range(1,7)]
results=[run(t,c) for t in sample for c in ['wrong','empty','missing','symlink','oversized','fifo']]
failures.extend(r for r in results if r[2]!=r[3]); print('negative',len(results)-sum(r[2]!=r[3] for r in results),'/',len(results),flush=True)
print('failures',json.dumps(failures),flush=True)
assert not failures