SORILUN-RSI / research /src /resonant_experiments.py
PureOne's picture
Add SORILUN research dataset release
aff9293 verified
Raw History Blame Contribute Delete
4.55 kB
"""Bounded same-output compiler and exact continuous-update experiments."""
from pathlib import Path
import sys,json,time,hashlib
import numpy as np
from resonant import Ring,ProjectedProgram,padded,exact_tail_update
ROOT=Path(__file__).resolve().parents[1]
def ahash(a):return hashlib.sha256(np.asarray(a,dtype='<i8').tobytes()).hexdigest()
def run():
rng=np.random.default_rng(914003);records=[];updates=[];start=time.perf_counter()
for p,n,h in [(5,625,3),(3,243,3),(7,343,2)]:
for rep in range(3):
s=rng.integers(0,p,n+1,dtype=np.int64);s[:2]=[0,1]
delta=rng.integers(0,p,n,dtype=np.int64);delta[:(n+2)//2]=0
program=ProjectedProgram(p,n,h);target=None
while True:
t=time.perf_counter_ns();out,cost=program.evaluate(s,delta);elapsed=time.perf_counter_ns()-t
if target is None:target=out.copy()
if not np.array_equal(out,target):raise AssertionError('Compiler changed output')
records.append({'p':p,'n':n,'h':h,'generation':program.j,'replicate':rep,
'output_length':len(out),'output_sha256':ahash(out),'cost':cost,
'full_input_load_units':2*n+1,'units_including_input_load':cost['units']+2*n+1,
'wall_ns':elapsed,'program':program.certificate()})
try:program=program.successor()
except StopIteration:break
# The same candidate is evaluated under every parent compiler. This is a
# counterfactual measurement of validation cost, not discovery intelligence.
meta=[]
for p,n,h in [(5,625,3),(3,243,3),(7,343,2)]:
rows=[r for r in records if r['p']==p and r['n']==n]
for rep in range(3):
group=[r for r in rows if r['replicate']==rep];child=group[-1]
for parent in group:
meta.append({'p':p,'n':n,'h':h,'replicate':rep,'parent_generation':parent['generation'],
'same_child_sha256':child['program']['sha256'],
'same_output_sha256':child['output_sha256'],
'validation_units':parent['units_including_input_load']+child['units_including_input_load']+100,
'validation_wall_ns':parent['wall_ns']+child['wall_ns']})
# Ongoing environment changes: all requested changes are high-order and
# certification is checked afresh. Exact full output, not just a projection.
p=5;n=125;f=rng.integers(0,p,n,dtype=np.int64);f[:2]=[0,1]
r=(n+2)//2;R=Ring(p);s=R.inverse(f,n);parent_hash=ahash(s)
for epoch in range(24):
delta=np.zeros(n,dtype=np.int64)
if epoch%3==0:delta[r:]=rng.integers(0,p,n-r)
elif epoch%3==1:
support=np.arange(r,n);support=support[(support+1)%5==0]
delta[support]=rng.integers(0,p,len(support))
else:
support=rng.choice(np.arange(r,n),size=4,replace=False)
delta[support]=rng.integers(1,p,len(support))
learner=Ring(p);t=time.perf_counter_ns();new=exact_tail_update(s,delta,n,learner)
learning_ns=time.perf_counter_ns()-t
f=(f+delta)%p;ref=Ring(p);t=time.perf_counter_ns();fresh=ref.inverse(f,n)
fresh_ns=time.perf_counter_ns()-t
if not np.array_equal(new,fresh):raise AssertionError('Continuous update failed')
if not np.array_equal(new[:r],s[:r]):raise AssertionError('Protected prefix changed')
updates.append({'epoch':epoch,'family':['dense_tail','resonant_tail','sparse_tail'][epoch%3],
'parent_sha256':parent_hash,'successor_sha256':ahash(new),'output_sha256':ahash(fresh),
'update_cost':learner.cost.snapshot(),'fresh_cost':ref.cost.snapshot(),
'update_wall_ns':learning_ns,'fresh_wall_ns':fresh_ns,'protected_prefix':r,
'full_inverse_equal':True})
s=new;parent_hash=ahash(new)
result={'records':records,'same_child':meta,'continuous_updates':updates,
'interpretation':'A fixed, proved compiler grammar learns cheaper equivalent update programs. Rules and proofs are supplied. Static Cartier-aware code reaches the same normal form directly.',
'wall_seconds':time.perf_counter()-start,'all_exact_checks_passed':True}
(ROOT/'results/resonant_experiments.json').write_text(json.dumps(result,indent=2))
print(json.dumps({'all_passed':True,'compiler_evaluations':len(records),'continuous_updates':len(updates),
'wall_seconds':result['wall_seconds']},indent=2))
return result
if __name__=='__main__':run()