Buckets:
| """One native-score beam over source-derived coherent component rewrites. | |
| This search receives executable source graphs and a native scalar oracle. It | |
| does not receive task identities, component stage scores or predicted value. | |
| """ | |
| import json,math,time | |
| from pathlib import Path | |
| class GraphSearch: | |
| def __init__(self,family,oracle,*,budget,width=4,journal=None,batch_size=None,known_originals=None): | |
| if budget<0 or width<1 or batch_size is not None and batch_size<1:raise ValueError('invalid search budget/width') | |
| self.family=family;self.oracle=oracle;self.budget=budget;self.width=width | |
| self.rows=[];self.cache={};self.journal=Path(journal)if journal else None;self.batch_size=batch_size or 2*width;self.queries=0 | |
| if self.journal: | |
| self.journal.parent.mkdir(parents=True,exist_ok=True) | |
| if self.journal.exists():raise FileExistsError('fresh search journal required') | |
| # Callers must verify source hash and evaluation signature before | |
| # supplying these records. Reusing a rollout score costs no native call. | |
| for source,value in (known_originals or{}).items(): | |
| c=family.initial(source);key=family.identity(c);self.record(c,key,value,'original_source',0.,reused=True) | |
| def record(self,c,key,value,phase,elapsed,reused=False): | |
| value=dict(value);loss=value.get('loss');valid=bool(value.get('valid'))and loss is not None and math.isfinite(loss) | |
| row=dict(id=key,config=self.family.canonical(c),valid=valid,loss=float(loss)if valid else None,phase=phase,evaluation_seconds=elapsed,reused_native_result=reused,evidence=value) | |
| self.cache[key]=row;self.rows.append(row) | |
| if self.journal: | |
| with self.journal.open('a')as f:f.write(json.dumps(row,allow_nan=False)+'\n') | |
| return row | |
| def evaluate(self,c,phase): | |
| key=self.family.identity(c) | |
| if key in self.cache:return self.cache[key] | |
| if self.queries>=self.budget:return None | |
| start=time.monotonic();value=dict(self.oracle(c,key));self.queries+=1 | |
| return self.record(c,key,value,phase,time.monotonic()-start) | |
| def rank(row):return (not row['valid'],row['loss']if row['valid']else math.inf,row['id']) | |
| def correct(self,row,allowance): | |
| """Joint black-box correction on the CURRENT source-defined face.""" | |
| if not row['config'].get('controls')or allowance<3:return row | |
| import copy,numpy as np | |
| from scipy.optimize import minimize | |
| domains=self.family.sources[row['config']['root']].controls;names=list(row['config']['controls']);sizes=[len(row['config']['controls'][n])for n in names];offset=np.cumsum([0]+sizes);initial=np.concatenate([row['config']['controls'][n]for n in names]);start=self.queries;best=row;scale=1. | |
| class Stop(Exception):pass | |
| def objective(a): | |
| nonlocal best | |
| if self.queries>=self.budget or self.queries-start>=allowance:raise Stop | |
| config=copy.deepcopy(row['config']);config['controls']={} | |
| for i,name in enumerate(names): | |
| w=np.maximum(np.asarray(a[offset[i]:offset[i+1]],float),0.) | |
| if w.sum()<=0:return 1e9 | |
| if np.count_nonzero(w>1e-8)>domains[name].get('max_active',len(w)):return 1e9 | |
| config['controls'][name]=(w/w.sum()).tolist() | |
| result=self.evaluate(config,'joint_source_control_correction') | |
| if result is None:raise Stop | |
| if self.rank(result)<self.rank(best):best=result | |
| return result['loss']*scale if result['valid']else 1e9 | |
| try: | |
| # Calibrate numerical units from feasible source-space probes. | |
| probes=[] | |
| for i in range(min(len(initial),max(1,allowance//3))): | |
| a=initial.copy();a[i]+=1e-3;probes.append(objective(a)) | |
| if probes: | |
| sensitivity=max(abs(v-row['loss'])for v in probes)/1e-3 | |
| if sensitivity>1e-12:scale=1./sensitivity | |
| constraints=[dict(type='eq',fun=lambda a,i=i:float(np.sum(a[offset[i]:offset[i+1]])-1.))for i in range(len(names))] | |
| result=minimize(objective,initial,method='SLSQP',bounds=[(0.,1.)]*len(initial),constraints=constraints,options=dict(maxiter=12,eps=1e-3,ftol=1e-9));objective(result.x) | |
| except Stop:pass | |
| return best | |
| def batch(self,configs,phase): | |
| if not hasattr(self.oracle,'evaluate_many'):return [r for c in configs if(r:=self.evaluate(c,phase))is not None] | |
| unique={} | |
| for c in configs: | |
| key=self.family.identity(c) | |
| if key not in self.cache and len(unique)<self.budget-self.queries:unique[key]=c | |
| started=time.monotonic();values=self.oracle.evaluate_many(list(unique.values()),list(unique)) | |
| output=[] | |
| for (key,c),value in zip(unique.items(),values): | |
| self.queries+=1;row=self.record(c,key,value,phase,value.get('seconds',time.monotonic()-started));output.append(row) | |
| return output | |
| def run(self): | |
| initials=[self.family.initial(s)for s in self.family.ids];self.batch(initials,'original_source');admitted=[self.cache[k]for c in initials if(k:=self.family.identity(c))in self.cache] | |
| beam=sorted((r for r in admitted if r['valid']),key=self.rank)[:self.width] | |
| expanded=set();rounds=0;iterators={} | |
| has_controls=any(r['config'].get('controls')for r in beam);structural_limit=max(self.queries,min(self.budget,4*self.width))if has_controls else self.budget | |
| while beam and self.queries<structural_limit: | |
| rounds+=1;requests=[] | |
| # Round-robin across retained contexts prevents one large helper | |
| # library from consuming every query before other callers are tried. | |
| front=[(r,iterators.setdefault(r['id'],iter(self.family.proposals(r['config']))))for r in beam if r['id']not in expanded] | |
| if not front:break | |
| while front and len(requests)<min(self.batch_size,structural_limit-self.queries): | |
| next_front=[] | |
| for parent,it in front: | |
| try:c=next(it) | |
| except StopIteration:expanded.add(parent['id']);continue | |
| if self.family.identity(c)not in self.cache:requests.append(c) | |
| next_front.append((parent,it)) | |
| if len(requests)>=min(self.batch_size,structural_limit-self.queries):break | |
| front=next_front | |
| candidates=self.batch(requests,'coherent_source_rewrite') | |
| unique={r['id']:r for r in beam+candidates if r['valid']} | |
| beam=sorted(unique.values(),key=self.rank)[:self.width] | |
| if has_controls: | |
| for i,row in enumerate(beam): | |
| # Fully correct the best measured complete context before | |
| # spending what remains on the other retained contexts. | |
| remaining=self.budget-self.queries;self.correct(row,remaining) | |
| valid=[r for r in self.rows if r['valid']] | |
| original=[r for r in valid if r['phase']=='original_source'] | |
| best=min(valid,key=self.rank)if valid else None | |
| return dict(best=best,best_original=min(original,key=self.rank)if original else None,evaluations=self.queries,recorded_programs=len(self.rows),reused_originals=sum(r['reused_native_result']for r in self.rows),rounds=rounds,retained=[r['id']for r in beam],width=self.width,component_boundaries_from_source=True,caller_preserved=True,task_specific_search_rules=False) | |
Xet Storage Details
- Size:
- 7.52 kB
- Xet hash:
- 13d9d753f17b1bbe1aea5c4f46f9d65c1c03aa47a8e24bc851a22bb8a90a72c3
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.