import os for k in ('OPENBLAS_NUM_THREADS','OMP_NUM_THREADS','MKL_NUM_THREADS'):os.environ[k]='1' import ast,difflib,json,pathlib,types import numpy as np P=pathlib.Path(__file__).parent old=(P/'job1415-bank-diff.py').read_text();src=(P/'corrected_checks.py').read_text();nodes=ast.parse(src) replacements={} for name,target in [('gap_correct','L_gap'),('literal_correct','L_literal')]: node=next(n for n in nodes.body if isinstance(n,ast.FunctionDef) and n.name==name) body='\n'.join(src.splitlines()[node.lineno-1:node.end_lineno])+'\n' body=body.replace('def '+name+'(','def '+target+'(') if name=='gap_correct':body=body.replace(' n=len(R);g=', ' assert 0 < len(R) < 2**30, "index range exceeded"\n n=len(R);g=') replacements[target]=body lines=old.splitlines(True);out=[];last=0 for node in ast.parse(old).body: if isinstance(node,ast.FunctionDef) and node.name in replacements: out+=lines[last:node.lineno-1];out.append(replacements[node.name]);last=node.end_lineno out+=lines[last:];new=''.join(out) needle=' rec["totals"] = ' assert needle in new new=new.replace(needle,' assert all(g["agree"] for g in rec["gates"]), "literal gate failed"\n'+needle) compile(new,'patched-bank-diff','exec') (P/'corrected-instruments.patch').write_text(''.join(difflib.unified_diff(old.splitlines(True),new.splitlines(True),fromfile='a/job1415-bank-diff.py',tofile='b/job1415-bank-diff.py'))) module=types.ModuleType('patched');module.__file__=str(P/'job1415-bank-diff.py');exec(compile(new,module.__file__,'exec'),module.__dict__) checks=json.loads((P/'seam-checks.json').read_text());count=0 for level in (5,7,11,13): R,M=module.tile_crt(level) for row in (r for r in checks['full_rows'] if r['level']==level): assert module.L_gap(R,M,row['p'])==module.L_literal(R,M,row['p'])==row['true'];count+=1 for row in checks['generic_controls']: g=row['gaps'];R=np.array([0]+list(np.cumsum(g[:-1])),dtype=np.int64);M=sum(g) assert module.L_gap(R,M,7)==module.L_literal(R,M,7)==row['true'] (P/'patch-checks.json').write_text(json.dumps({'patched_source_compiles':True,'actual_small_cells':count,'generic_controls':5,'passed':True},indent=2))