#!/usr/bin/env python3 """只读真实题目的来源/任务计数,核查固定2000重采样设计;不读取模型预测。""" import argparse import hashlib import json from pathlib import Path import sys import time ROOT=Path(__file__).resolve().parents[1] sys.path.insert(0,str(ROOT));sys.path.insert(0,str(ROOT/'scripts')) from download_assets import atomic,sha from cgvm.benchmark_data import questions,BT_TASKS,RT_TASKS def audit(cluster_path): import numpy as np started=time.perf_counter();graph=json.loads(cluster_path.read_text());reports=[] allq={benchmark:questions(benchmark) for benchmark in ('ovobench','streamingbench')} tasks_by_benchmark={'ovobench':list(BT_TASKS+RT_TASKS), 'streamingbench':sorted({q['task'] for q in allq['streamingbench']})} specs=[('OVO-BT','ovobench',list(BT_TASKS)),('OVO-RT','ovobench',list(RT_TASKS)), ('OVO-EPM-ASI-diagnostic','ovobench',['EPM','ASI']), ('OVO-all-macro','ovobench',tasks_by_benchmark['ovobench']), ('OVO-all-micro','ovobench',tasks_by_benchmark['ovobench']), ('Streaming-RT','streamingbench',tasks_by_benchmark['streamingbench']), ('Streaming-RT-macro','streamingbench',tasks_by_benchmark['streamingbench'])] for benchmark,tasks in tasks_by_benchmark.items(): specs.extend([(('OVO-' if benchmark=='ovobench' else 'Streaming-')+task,benchmark,[task]) for task in tasks]) for name,benchmark,tasks in specs: qrows=[q for q in allq[benchmark] if q['task'] in tasks];prefix='ovo/' if benchmark=='ovobench' else 'streaming/' for rule in ('labels','released_source_labels'): labels=graph[rule];clusters=sorted({labels[prefix+q['source_video']] for q in qrows}) ci={label:i for i,label in enumerate(clusters)};ti={task:i for i,task in enumerate(tasks)} counts=np.zeros((len(clusters),len(tasks)),dtype=np.float64) for q in qrows:counts[ci[labels[prefix+q['source_video']]],ti[q['task']]]+=1 seed=int.from_bytes(hashlib.sha256(('17:'+name).encode()).digest()[:8],'little') rng=np.random.default_rng(seed) weights=rng.multinomial(len(clusters),np.full(len(clusters),1/len(clusters)),size=2000) denominator=weights@counts reports.append(dict(metric=name,rule=rule,clusters=len(clusters),replicates=2000, zero_task_denominators=int((denominator==0).sum()),minimum_task_resample_count=int(denominator.min()),seed=seed)) partial=graph['partial'];valid=all(row['zero_task_denominators']==0 for row in reports) result=dict(passed=bool(valid and not partial),passed_on_current_graph=valid,partial=partial, scope='actual_question_source_task_counts_only_no_predictions_or_answers_scored', cluster_audit_sha256=sha(cluster_path),statistics_protocol_sha256=sha(ROOT/'configs/statistics.frozen.json'), reports=reports,wall_seconds=time.perf_counter()-started, boundary='count_design_validation_only_not_actual_CI_or_training_result') target=ROOT/'provenance'/('bootstrap_source_design_preflight'+('.partial' if partial else '')+'.json') previous=json.loads(target.read_text()) if target.exists() else None stable=lambda row:{key:value for key,value in row.items() if key!='wall_seconds'} if previous is not None and stable(previous)==stable(result):result=previous else:atomic(target,result) with (ROOT/'logs/bootstrap_design_checks.jsonl').open('a') as log: log.write(json.dumps(dict(timestamp=time.time(),wall_seconds=time.perf_counter()-started, partial=partial,designs=len(reports),valid=valid,proof_sha256=sha(target)))+'\n') print(json.dumps(dict(passed=result['passed'],partial=partial,valid_on_current_graph=valid,designs=len(reports),replicates_per_design=2000)),flush=True) if not valid:raise ValueError('固定bootstrap存在空任务分母,需记录并修复统计设计后再训练') return target,result if __name__=='__main__': parser=argparse.ArgumentParser();parser.add_argument('--allow-partial',action='store_true');args=parser.parse_args() path=ROOT/'provenance'/('eval_source_cluster_audit'+('.partial' if args.allow_partial else '')+'.json') audit(path)