Download code/src/pilot_core.py from PratikBuilds/modelclinic-experiment-001: direct link, hf CLI and curl.
- Browser
- Download file 2.56 kB
-
https://huggingface.co/datasets/PratikBuilds/modelclinic-experiment-001/resolve/main/code/src/pilot_core.py
- Command line
-
hf download hf://datasets/PratikBuilds/modelclinic-experiment-001/code/src/pilot_core.py
-
curl -L -o pilot_core.py https://huggingface.co/datasets/PratikBuilds/modelclinic-experiment-001/resolve/main/code/src/pilot_core.py
2.56 kB
| """Pure, testable utilities for the paired pilot.""" | |
| import hashlib,json,os,re | |
| from pathlib import Path | |
| from collections import Counter | |
| def read_json(path):return json.loads(Path(path).read_text(encoding='utf-8-sig')) | |
| def read_jsonl(path):return [json.loads(line) for line in Path(path).read_text(encoding='utf-8-sig').splitlines() if line.strip()] | |
| def fingerprint(config, questions): | |
| return hashlib.sha256(json.dumps({'config':config,'questions':questions},sort_keys=True,ensure_ascii=False,separators=(',',':')).encode()).hexdigest() | |
| def validate(config,questions): | |
| assert len({q['source_row'] for q in questions})==len(questions) | |
| assert len({q['problem'].strip() for q in questions})==len(questions) | |
| assert not ({x.strip() for x in config['calibration_prompts']} & {q['problem'].strip() for q in questions}), 'Calibration overlaps evaluation' | |
| assert set(config['conditions'])=={'base_thinking','base_no_thinking','edited','emulation','constant','random'} | |
| assert config['base_revision']=='2fc06364715b967f1860aea9cf38778875588b17' | |
| assert config['edited_revision']=='d1671a178705bcab7644806d23c535517f35723b' | |
| assert config['precision']=='bfloat16' and config['decoding']=={'do_sample':False,'num_beams':1} | |
| assert 0<config['max_new_tokens']<=512 and 0<config['max_time_seconds']<=60 | |
| return True | |
| def thinking(condition):return condition!='base_no_thinking' | |
| def prompt(question,config):return question['problem']+config['prompt_suffix'] | |
| def record_key(row,condition,seed):return (row,condition,seed) | |
| def completed(records,run_hash): | |
| seen=set() | |
| for r in records: | |
| if r['run_hash']!=run_hash: raise ValueError('Run fingerprint mismatch') | |
| k=record_key(r['source_row'],r['condition'],r['seed']) | |
| if k in seen: raise ValueError('Duplicate measurement') | |
| seen.add(k) | |
| return seen | |
| def append_record(path,record): | |
| with open(path,'a',encoding='utf-8',newline='\n') as f: | |
| f.write(json.dumps(record,ensure_ascii=False,allow_nan=False)+'\n');f.flush();os.fsync(f.fileno()) | |
| def loop_metrics(ids): | |
| counts=Counter(tuple(ids[i:i+4]) for i in range(max(0,len(ids)-3))) | |
| most=max(counts.values(),default=0) | |
| return {'loop_detected':len(ids)>=32 and most>=4,'max_fourgram_count':most,'repeated_fourgram_fraction':sum(v-1 for v in counts.values() if v>1)/max(len(ids)-3,1)} | |
| def stopped_on_eos(token_ids, eos_ids): | |
| if isinstance(eos_ids,int): eos_ids=[eos_ids] | |
| return bool(token_ids and eos_ids and token_ids[-1] in eos_ids) | |