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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""CIDM-v2 B0-R1 PDEBench three-seed convergence-aware v1 Complete vs v2 restored Flux port.
|
| 2 |
+
Fairness: identical official files, trajectory ids, histories, normalization, budgets,
|
| 3 |
+
validation rules, and paired random seeds. V2 is channel-generalized adapter,
|
| 4 |
+
NOT bitwise original trained R5 checkpoint. Synthetic mode tests PROGRAM only.
|
| 5 |
+
"""
|
| 6 |
+
from __future__ import annotations
|
| 7 |
+
import os,sys,time,json,hashlib,random,zipfile,math,traceback,copy
|
| 8 |
+
from pathlib import Path
|
| 9 |
+
from dataclasses import dataclass,asdict
|
| 10 |
+
from collections import defaultdict
|
| 11 |
+
import numpy as np
|
| 12 |
+
import torch
|
| 13 |
+
from torch import nn
|
| 14 |
+
import torch.nn.functional as F
|
| 15 |
+
from torch.utils.data import Dataset,DataLoader
|
| 16 |
+
from cidm_complete_frozen import CIDMConfig,CIDM_BER
|
| 17 |
+
|
| 18 |
+
ARCHIVE_SHA='f9b1cf4ff7630f5540bb3adde20e4c4b81fff3218d9325ea3105bf28b1d97665'
|
| 19 |
+
TASKS={
|
| 20 |
+
'advection_1d':('1D_Advection_Sols_beta0.4.hdf5','255674','d595bbfd2c659df995a93cd40d6ea568',4),
|
| 21 |
+
'burgers_1d':('1D_Burgers_Sols_Nu0.01.hdf5','281363','e6d9a4f62baf9a29121a816b919e2770',4),
|
| 22 |
+
'diff_sorp_1d':('1D_diff-sorp_NA_NA.h5','133020','9d466d1213065619d087319e16d9a938',4),
|
| 23 |
+
'reacdiff_1d':('ReacDiff_Nu1.0_Rho2.0.hdf5','133183','112c01a76447162bd67c8c1073f58ca2',4),
|
| 24 |
+
'cfd_1d':('1D_CFD_Rand_Eta0.01_Zeta0.01_periodic_Train.hdf5','164672','2fdd00138f1d7abc794fb953021a9f43',4),
|
| 25 |
+
'reacdiff_2d':('2D_diff-react_NA_NA.h5','133017','b8d0b86064193195ddc30c33be5dc949',1),
|
| 26 |
+
'swe_2d':('2D_rdb_NA_NA.h5','133021','75d838c47aa410694bdc912ea7f22282',1),
|
| 27 |
+
'cfd_2d':('2D_CFD_Rand_M0.1_Eta0.01_Zeta0.01_periodic_128_Train.hdf5','164687','5b21dcccaef4d2145ca579a71153c580',2),
|
| 28 |
+
}
|
| 29 |
+
PILOT=('burgers_1d','reacdiff_2d','swe_2d')
|
| 30 |
+
CANONICAL_TASKS=tuple(TASKS)
|
| 31 |
+
|
| 32 |
+
@dataclass
|
| 33 |
+
class Config:
|
| 34 |
+
root:str='/content/CIDM_v2_B0_FairPDE'
|
| 35 |
+
profile:str='pilot_r1' # smoke | pilot_r1 | formal8 | canonical8
|
| 36 |
+
data_root:str=''
|
| 37 |
+
seed:int=20261012
|
| 38 |
+
match_target:int=500_000
|
| 39 |
+
match_tolerance:float=.05
|
| 40 |
+
history:int=4
|
| 41 |
+
train_horizon:int=6
|
| 42 |
+
eval_horizon:int=12
|
| 43 |
+
pilot_updates:int=600
|
| 44 |
+
formal_updates:int=600
|
| 45 |
+
canonical_updates:int=1000
|
| 46 |
+
train_traj:int=64
|
| 47 |
+
val_traj:int=16
|
| 48 |
+
test_traj:int=16
|
| 49 |
+
windows_per_traj:int=2
|
| 50 |
+
eval_every:int=60
|
| 51 |
+
batch_1d:int=8
|
| 52 |
+
batch_2d:int=2
|
| 53 |
+
lr:float=.001
|
| 54 |
+
grad_clip:float=1.0
|
| 55 |
+
resume:bool=True
|
| 56 |
+
num_workers:int=0
|
| 57 |
+
def tasks(self):return ('smoke_2d',) if self.profile=='smoke' else PILOT if self.profile in ('pilot','pilot_r1') else CANONICAL_TASKS
|
| 58 |
+
def seeds(self):return (17,) if self.profile in ('smoke','pilot') else (17,41,73) if self.profile in ('pilot_r1','formal8') else (17,41,73,101,137)
|
| 59 |
+
def updates(self):return 2 if self.profile=='smoke' else self.pilot_updates if self.profile in ('pilot','pilot_r1') else self.formal_updates if self.profile=='formal8' else self.canonical_updates
|
| 60 |
+
|
| 61 |
+
def jsonsave(p,v):
|
| 62 |
+
p=Path(p);p.parent.mkdir(parents=True,exist_ok=True)
|
| 63 |
+
tmp=p.with_name(p.name+'.tmp');tmp.write_text(json.dumps(v,ensure_ascii=False,indent=2,default=str),encoding='utf8');tmp.replace(p)
|
| 64 |
+
|
| 65 |
+
def sha(p):
|
| 66 |
+
h=hashlib.sha256()
|
| 67 |
+
with open(p,'rb') as f:
|
| 68 |
+
for a in iter(lambda:f.read(1<<22),b''):h.update(a)
|
| 69 |
+
return h.hexdigest()
|
| 70 |
+
|
| 71 |
+
def md5(p):
|
| 72 |
+
h=hashlib.md5()
|
| 73 |
+
with open(p,'rb') as f:
|
| 74 |
+
for a in iter(lambda:f.read(1<<22),b''):h.update(a)
|
| 75 |
+
return h.hexdigest()
|
| 76 |
+
|
| 77 |
+
def seed_all(s):
|
| 78 |
+
random.seed(s);np.random.seed(s);torch.manual_seed(s)
|
| 79 |
+
if torch.cuda.is_available():torch.cuda.manual_seed_all(s)
|
| 80 |
+
|
| 81 |
+
def params(model):return sum(p.numel() for p in model.parameters() if p.requires_grad)
|
| 82 |
+
|
| 83 |
+
# Copy of R5-R1 RawEdgeFlux, with architecture frozen; no simulator coefficients or task IDs.
|
| 84 |
+
class RawEdgeFlux(nn.Module):
|
| 85 |
+
def __init__(self,d,h):
|
| 86 |
+
super().__init__();self.net=nn.Sequential(nn.Conv2d(2*d,h,1),nn.SiLU(),nn.Conv2d(h,d,1))
|
| 87 |
+
def forward(self,z):
|
| 88 |
+
out=torch.zeros_like(z)
|
| 89 |
+
for dim in (-1,-2):
|
| 90 |
+
q=z.roll(-1,dims=dim)
|
| 91 |
+
edge=self.net(torch.cat((z,q),1))-self.net(torch.cat((q,z),1))
|
| 92 |
+
out=out+edge-edge.roll(1,dims=dim)
|
| 93 |
+
return out
|
| 94 |
+
|
| 95 |
+
class PortedRestoredCIDM(nn.Module):
|
| 96 |
+
"""Exact structural R5-R1 port to arbitrary input channel count and lifted 1D.
|
| 97 |
+
Differences vs the original two-channel R5-R1: encoder in channels L*C,
|
| 98 |
+
decoder out channels C, no unused train-only scale head, support singleton H=1.
|
| 99 |
+
Training here is from scratch; original R5 weights are NOT load-compatible.
|
| 100 |
+
"""
|
| 101 |
+
def __init__(self,channels=1,d=24,hidden=40,hist=4,sync_interval=6):
|
| 102 |
+
super().__init__();assert d%4==0
|
| 103 |
+
self.channels=channels;self.hist=hist;self.sync_interval=sync_interval
|
| 104 |
+
self.encode=nn.Sequential(nn.Conv2d(hist*channels,hidden,3,padding=1),nn.SiLU(),nn.Conv2d(hidden,d,3,padding=1),nn.GroupNorm(4,d))
|
| 105 |
+
self.flux=RawEdgeFlux(d,hidden)
|
| 106 |
+
self.local=nn.Sequential(nn.Conv2d(d,hidden,3,padding=1),nn.SiLU(),nn.Conv2d(hidden,d,1))
|
| 107 |
+
self.skew=nn.Parameter(torch.randn(d,d)*.01)
|
| 108 |
+
self.decode=nn.Sequential(nn.Conv2d(d,hidden,3,padding=1),nn.SiLU(),nn.Conv2d(hidden,channels,1))
|
| 109 |
+
# R5-R1 scale_readout was auxiliary train-only; B0 has no paired-scale labels.
|
| 110 |
+
# Omit it instead of counting untrained, disconnected parameters in a 500K match.
|
| 111 |
+
self.flux_logit=nn.Parameter(torch.tensor(.0))
|
| 112 |
+
self.local_logit=nn.Parameter(torch.tensor(-.5))
|
| 113 |
+
self.out_logit=nn.Parameter(torch.tensor(-.4))
|
| 114 |
+
self.sync_logit=nn.Parameter(torch.tensor(-.6))
|
| 115 |
+
def coefficients(self):
|
| 116 |
+
return {'flux':.30*self.flux_logit.sigmoid(),'local':.15*self.local_logit.sigmoid(),
|
| 117 |
+
'out':.22*self.out_logit.sigmoid(),'sync':.8*self.sync_logit.sigmoid()}
|
| 118 |
+
def forward(self,h,steps=6,mode='off',use_flux=True,freeze_latent=False):
|
| 119 |
+
assert mode in ('off','soft','hard')
|
| 120 |
+
B,L,C,H,W=h.shape
|
| 121 |
+
assert L==self.hist and C==self.channels and (H==1 or H%4==0) and W%4==0
|
| 122 |
+
coeff=self.coefficients();buf=list(h.unbind(1))
|
| 123 |
+
z=self.encode(h.reshape(B,L*C,H,W));sk=self.skew-self.skew.T;ps=[]
|
| 124 |
+
for k in range(steps):
|
| 125 |
+
if not freeze_latent:
|
| 126 |
+
f=self.flux(z) if use_flux else torch.zeros_like(z)
|
| 127 |
+
z=z+coeff['flux']*f+coeff['local']*torch.tanh(self.local(z))+.035*torch.einsum('ij,bjhw->bihw',sk,z)
|
| 128 |
+
xn=buf[-1]+coeff['out']*torch.tanh(self.decode(z))
|
| 129 |
+
ps.append(xn);buf.append(xn)
|
| 130 |
+
if mode!='off' and (k+1)%self.sync_interval==0 and k+1<steps:
|
| 131 |
+
ref=self.encode(torch.stack(buf[-L:],1).reshape(B,L*C,H,W))
|
| 132 |
+
z=ref if mode=='hard' else z+coeff['sync']*(ref-z)
|
| 133 |
+
return torch.stack(ps,1)
|
| 134 |
+
|
| 135 |
+
class V1Archived(nn.Module):
|
| 136 |
+
def __init__(self,channels,d,hidden,hist=4):
|
| 137 |
+
super().__init__()
|
| 138 |
+
conf=CIDMConfig(channels=channels,hist_len=hist,horizon=6,latent_dim=d,hidden=hidden,
|
| 139 |
+
soc_modes1=8,soc_modes2=8,port_channels=1,ber_enabled=True,soc_enabled=True)
|
| 140 |
+
self.model=CIDM_BER(conf)
|
| 141 |
+
def forward(self,h,steps=6):return self.model(h,horizon=steps,port=None)['pred']
|
| 142 |
+
|
| 143 |
+
class UnifiedModel(nn.Module):
|
| 144 |
+
def __init__(self,kind,channels,d,hidden,hist=4):
|
| 145 |
+
super().__init__();self.kind=kind
|
| 146 |
+
self.model=V1Archived(channels,d,hidden,hist) if kind=='V1_CIDM_Complete' else PortedRestoredCIDM(channels,d,hidden,hist)
|
| 147 |
+
def forward(self,h,steps,mode='off'):
|
| 148 |
+
dim1=h.ndim==4
|
| 149 |
+
hh=h.unsqueeze(-2) if dim1 else h
|
| 150 |
+
y=self.model(hh,steps) if self.kind=='V1_CIDM_Complete' else self.model(hh,steps,mode=mode)
|
| 151 |
+
return y.squeeze(-2) if dim1 else y
|
| 152 |
+
|
| 153 |
+
def choose_match(kind,channels,cfg,dev='cpu'):
|
| 154 |
+
if cfg.profile=='smoke':return {'d':8,'hidden':12,'params':params(UnifiedModel(kind,channels,8,12,cfg.history)),'relative_error':None}
|
| 155 |
+
ds=(8,12,16,20,24,28,32,40,48,56,64,72,80,96,112,128)
|
| 156 |
+
hs=(12,16,20,24,32,40,48,56,64,72,80,96,112,128,144,160,192,224)
|
| 157 |
+
candidates=[]
|
| 158 |
+
for d in ds:
|
| 159 |
+
for hidden in hs:
|
| 160 |
+
model=UnifiedModel(kind,channels,d,hidden,cfg.history)
|
| 161 |
+
p=params(model)
|
| 162 |
+
candidates.append((abs(p-cfg.match_target),d,hidden,p))
|
| 163 |
+
del model
|
| 164 |
+
score,d,hidden,p=min(candidates)
|
| 165 |
+
if score/cfg.match_target>cfg.match_tolerance:
|
| 166 |
+
raise RuntimeError(f'NO PARAMETER MATCH {kind} channels={channels} best d={d},h={hidden},params={p},target={cfg.match_target}')
|
| 167 |
+
return {'d':d,'hidden':hidden,'params':p,'relative_error':score/cfg.match_target}
|
| 168 |
+
|
| 169 |
+
class Windows(Dataset):
|
| 170 |
+
def __init__(self,h,y,mean,std):
|
| 171 |
+
self.h=h.float();self.y=y.float();shape=[1,1,-1]+[1]*(h.ndim-3)
|
| 172 |
+
self.mn=mean.view(*shape);self.sd=std.view(*shape)
|
| 173 |
+
def __len__(self):return len(self.h)
|
| 174 |
+
def __getitem__(self,i):return ((self.h[i]-self.mn[0])/self.sd[0],(self.y[i]-self.mn[0])/self.sd[0])
|
| 175 |
+
|
| 176 |
+
def fetch_official(root,task):
|
| 177 |
+
import requests
|
| 178 |
+
name,id,expected,stride=TASKS[task]
|
| 179 |
+
root=Path(root);root.mkdir(parents=True,exist_ok=True)
|
| 180 |
+
dest=root/name
|
| 181 |
+
if dest.exists() and md5(dest)==expected:return dest
|
| 182 |
+
if dest.exists():raise RuntimeError('Existing file MD5 mismatch: '+str(dest)+'; delete or replace manually')
|
| 183 |
+
part=Path(str(dest)+'.part'); url=f'https://darus.uni-stuttgart.de/api/access/datafile/{id}'
|
| 184 |
+
for retry in range(3):
|
| 185 |
+
pos=part.stat().st_size if part.exists() else 0
|
| 186 |
+
headers={'Range':f'bytes={pos}-'} if pos else {}
|
| 187 |
+
try:
|
| 188 |
+
with requests.get(url,stream=True,timeout=(20,120),headers=headers) as r:
|
| 189 |
+
r.raise_for_status()
|
| 190 |
+
if pos and r.status_code!=206:part.unlink(missing_ok=True);pos=0
|
| 191 |
+
with open(part,'ab' if pos else 'wb') as f:
|
| 192 |
+
for ch in r.iter_content(chunk_size=1<<22):
|
| 193 |
+
if ch:f.write(ch)
|
| 194 |
+
break
|
| 195 |
+
except (requests.RequestException,OSError) as exc:
|
| 196 |
+
if retry==2:raise RuntimeError(f'{task}: download failed. Place verified official file manually into {root}. {exc}') from exc
|
| 197 |
+
time.sleep(2*(retry+1))
|
| 198 |
+
if md5(part)!=expected:raise RuntimeError(f'MD5 mismatch for {task}: {md5(part)}; expected {expected}. Do not train.')
|
| 199 |
+
part.rename(dest)
|
| 200 |
+
return dest
|
| 201 |
+
|
| 202 |
+
def schema(path):
|
| 203 |
+
import h5py
|
| 204 |
+
with h5py.File(path,'r') as f:
|
| 205 |
+
if 'tensor' in f:return ('tensor',int(f['tensor'].shape[0]))
|
| 206 |
+
if all(k in f for k in ('density','pressure','Vx')):return ('cfd',int(f['density'].shape[0]))
|
| 207 |
+
keys=[k for k in sorted(f) if isinstance(f[k],h5py.Group) and 'data' in f[k]]
|
| 208 |
+
if keys:return ('grouped',len(keys))
|
| 209 |
+
raise RuntimeError(f'Unsupported data schema {list(f.keys())[:12]}')
|
| 210 |
+
|
| 211 |
+
def load_traj(task,path,idx):
|
| 212 |
+
import h5py
|
| 213 |
+
with h5py.File(path,'r') as f:
|
| 214 |
+
if 'tensor' in f:
|
| 215 |
+
a=np.asarray(f['tensor'][idx],dtype=np.float32)
|
| 216 |
+
if a.ndim==2:a=a[:,None,:]
|
| 217 |
+
elif a.ndim==3:
|
| 218 |
+
if task.endswith('_2d'):a=a[:,None,:,:]
|
| 219 |
+
elif a.shape[-1]<=8:a=np.moveaxis(a,-1,1)
|
| 220 |
+
else:raise RuntimeError(f'ambiguous tensor {task} {a.shape}')
|
| 221 |
+
elif a.ndim==4:
|
| 222 |
+
if a.shape[-1]<=8:a=np.moveaxis(a,-1,1)
|
| 223 |
+
elif a.shape[1]<=8:pass
|
| 224 |
+
else:raise RuntimeError((task,a.shape))
|
| 225 |
+
else:raise RuntimeError((task,a.shape))
|
| 226 |
+
elif all(k in f for k in ('density','pressure','Vx')):
|
| 227 |
+
arr=[np.asarray(f[k][idx],dtype=np.float32) for k in ('density','pressure','Vx')]
|
| 228 |
+
if 'Vy' in f:arr.append(np.asarray(f['Vy'][idx],dtype=np.float32))
|
| 229 |
+
a=np.stack(arr,axis=1)
|
| 230 |
+
else:
|
| 231 |
+
keys=[k for k in sorted(f) if isinstance(f[k],h5py.Group) and 'data' in f[k]]
|
| 232 |
+
a=np.asarray(f[keys[idx]]['data'],dtype=np.float32)
|
| 233 |
+
if a.ndim==4:a=np.moveaxis(a,-1,1)
|
| 234 |
+
elif a.ndim==3:a=a[:,None,:,:]
|
| 235 |
+
elif a.ndim==2:a=a[:,None,:]
|
| 236 |
+
else:raise RuntimeError((task,a.shape))
|
| 237 |
+
stride=TASKS[task][3]
|
| 238 |
+
if stride>1:a=a[...,::stride] if a.ndim==3 else a[...,::stride,::stride]
|
| 239 |
+
if not np.isfinite(a).all():raise RuntimeError(f'{task}: nonfinite trajectory {idx}')
|
| 240 |
+
if a.ndim not in (3,4):raise RuntimeError(a.shape)
|
| 241 |
+
# Fail closed rather than fake-convert impossible sizes.
|
| 242 |
+
if (a.shape[-1]%4)!=0 or (a.ndim==4 and a.shape[-2]%4)!=0:
|
| 243 |
+
raise RuntimeError(f'{task}: grid {a.shape[2:]} must be multiple of 4 for ported v2; no silent resize')
|
| 244 |
+
return torch.from_numpy(np.ascontiguousarray(a))
|
| 245 |
+
|
| 246 |
+
def splits(N,cfg):
|
| 247 |
+
ntest=max(1,int(.1*N));nval=max(1,int(.1*N))
|
| 248 |
+
pools={'test':np.arange(0,ntest),'val':np.arange(N-nval,N),'train':np.arange(ntest,N-nval)}
|
| 249 |
+
cap={'train':cfg.train_traj,'val':cfg.val_traj,'test':cfg.test_traj}
|
| 250 |
+
return {k:np.asarray(v,dtype=int)[np.unique(np.linspace(0,len(v)-1,min(cap[k],len(v)),dtype=int))].tolist() for k,v in pools.items()}
|
| 251 |
+
|
| 252 |
+
def make_cache(task,path,cfg,out):
|
| 253 |
+
assert cfg.history==4 and cfg.train_horizon==6 and cfg.eval_horizon>=6
|
| 254 |
+
signature={'task':task,'source_md5':TASKS[task][2],'history':cfg.history,'eval_horizon':cfg.eval_horizon,
|
| 255 |
+
'counts':[cfg.train_traj,cfg.val_traj,cfg.test_traj,cfg.windows_per_traj],'stride':TASKS[task][3]}
|
| 256 |
+
token=hashlib.sha256(json.dumps(signature,sort_keys=True).encode()).hexdigest()[:12]
|
| 257 |
+
fp=Path(out)/f'{task}_{token}.pt'
|
| 258 |
+
if fp.exists():
|
| 259 |
+
obj=torch.load(fp,map_location='cpu',weights_only=True)
|
| 260 |
+
if obj['signature']==signature:return obj
|
| 261 |
+
_,N=schema(path);ids=splits(N,cfg)
|
| 262 |
+
all_splits={}; meta={}
|
| 263 |
+
for sp,inds in ids.items():
|
| 264 |
+
hs=[];ys=[];windows=[]
|
| 265 |
+
for idx in inds:
|
| 266 |
+
a=load_traj(task,path,int(idx))
|
| 267 |
+
mx=a.shape[0]-cfg.history-cfg.eval_horizon
|
| 268 |
+
if mx<0:raise RuntimeError(f'{task} traj {idx} only has {a.shape[0]} time steps, need {cfg.history+cfg.eval_horizon}. Try a smaller eval_horizon.')
|
| 269 |
+
starts=np.unique(np.linspace(0,mx,cfg.windows_per_traj,dtype=int))
|
| 270 |
+
for start in starts:
|
| 271 |
+
hs.append(a[start:start+cfg.history]);ys.append(a[start+cfg.history:start+cfg.history+cfg.eval_horizon]);windows.append([int(idx),int(start)])
|
| 272 |
+
all_splits[sp]={'h':torch.stack(hs),'y':torch.stack(ys)};meta[sp]=windows
|
| 273 |
+
# Training-only moments, no val/test leakage. Same for both models.
|
| 274 |
+
tensors=[all_splits['train']['h'].double(),all_splits['train']['y'].double()]
|
| 275 |
+
cc=tensors[0].shape[2];ss=torch.zeros(cc,dtype=torch.float64);sq=torch.zeros_like(ss);count=0
|
| 276 |
+
for a in tensors:
|
| 277 |
+
rd=(0,1)+tuple(range(3,a.ndim));ss+=a.sum(rd);sq+=(a*a).sum(rd)
|
| 278 |
+
count+=math.prod(a.shape[x] for x in rd)
|
| 279 |
+
mu=ss/count;std=(sq/count-mu.square()).clamp_min(1e-12).sqrt()
|
| 280 |
+
obj={'signature':signature,'splits':all_splits,'ids':ids,'windows':meta,'mean':mu.float(),'std':std.float()}
|
| 281 |
+
fp.parent.mkdir(parents=True,exist_ok=True);torch.save(obj,fp)
|
| 282 |
+
return obj
|
| 283 |
+
|
| 284 |
+
def make_synthetic_cache(cfg):
|
| 285 |
+
# Synthetic preflight is NEVER an official PDEBench performance result.
|
| 286 |
+
gen=torch.Generator().manual_seed(1000)
|
| 287 |
+
spl={}
|
| 288 |
+
for sp,n in [('train',8),('val',4),('test',4)]:
|
| 289 |
+
a=torch.randn(n,cfg.history+cfg.eval_horizon,1,8,16,generator=gen)*.15
|
| 290 |
+
for t in range(1,a.shape[1]):a[:,t]=.95*a[:,t-1]+.05*a[:,t]
|
| 291 |
+
spl[sp]={'h':a[:,:cfg.history],'y':a[:,cfg.history:]}
|
| 292 |
+
return {'signature':{'task':'smoke_2d','synthetic':True},'splits':spl,'ids':{},'windows':{},'mean':torch.zeros(1),'std':torch.ones(1)}
|
| 293 |
+
|
| 294 |
+
def load_data(cache,split,batch,seed,shuffle):
|
| 295 |
+
n=Windows(cache['splits'][split]['h'],cache['splits'][split]['y'],cache['mean'],cache['std'])
|
| 296 |
+
return DataLoader(n,batch_size=batch,shuffle=shuffle,drop_last=shuffle and len(n)>=batch,
|
| 297 |
+
generator=torch.Generator().manual_seed(seed),num_workers=0,pin_memory=torch.cuda.is_available())
|
| 298 |
+
|
| 299 |
+
@torch.no_grad()
|
| 300 |
+
def assess(model,loader,dev,cfg,mode='off'):
|
| 301 |
+
model.eval();tot={}; cnt={}; baselines={}
|
| 302 |
+
for h,y in loader:
|
| 303 |
+
h=h.to(dev);y=y.to(dev);K=min(cfg.eval_horizon,y.shape[1]);p=model(h,K,mode)
|
| 304 |
+
baseline=h[:,-1:].expand_as(y[:,:K])
|
| 305 |
+
kvec=torch.arange(1,K+1,device=dev,dtype=y.dtype).view(1,K,*([1]*(h.ndim-2)))
|
| 306 |
+
lin=h[:,-1:]+kvec*(h[:,-1:]-h[:,-2:-1])
|
| 307 |
+
for k in sorted(set((cfg.train_horizon,K))):
|
| 308 |
+
if k>K:continue
|
| 309 |
+
for name,q in [('model',p),('persistence',baseline),('trend',lin)]:
|
| 310 |
+
err=(q[:,:k]-y[:,:k]).square()
|
| 311 |
+
# Per element, denormalized physical-unit RMSE; normalize separately per channel only at dataset stage
|
| 312 |
+
std=loader.dataset.std if hasattr(loader.dataset,'std') else loader.dataset.sd
|
| 313 |
+
# std shape [1,1,C,...]; broadcast to tensor.
|
| 314 |
+
ss=(err*std.to(dev).square()).sum().item()
|
| 315 |
+
key=(name,k);tot[key]=tot.get(key,0.)+ss;cnt[key]=cnt.get(key,0)+err.numel()
|
| 316 |
+
return {name+'_'+str(k):math.sqrt(tot[(name,k)]/cnt[(name,k)]) for name,k in sorted(tot)}
|
| 317 |
+
|
| 318 |
+
@torch.no_grad()
|
| 319 |
+
def per_lead_assess(model,loader,dev,cfg):
|
| 320 |
+
"""Per-target-step physically denormalized RMSE; strictly test-only after checkpoint freeze."""
|
| 321 |
+
model.eval();K=cfg.eval_horizon; ss=np.zeros(K,dtype=np.float64); nncounts=np.zeros(K,dtype=np.float64)
|
| 322 |
+
for h,y in loader:
|
| 323 |
+
h=h.to(dev); y=y.to(dev);p=model(h,K,'off')
|
| 324 |
+
sd=loader.dataset.sd.to(dev)
|
| 325 |
+
for k in range(K):
|
| 326 |
+
e=((p[:,k:k+1]-y[:,k:k+1])*sd).square()
|
| 327 |
+
ss[k]+=e.sum().item();nncounts[k]+=e.numel()
|
| 328 |
+
return [float(np.sqrt(ss[k]/max(nncounts[k],1))) for k in range(K)]
|
| 329 |
+
|
| 330 |
+
@torch.no_grad()
|
| 331 |
+
def measure_latency(model,loader,dev,K):
|
| 332 |
+
model.eval(); x=next(iter(loader))[0].to(dev)
|
| 333 |
+
if x.shape[0]>1:x=x[:1]
|
| 334 |
+
for _ in range(2):model(x,K,'off')
|
| 335 |
+
if dev.type=='cuda':torch.cuda.synchronize();torch.cuda.reset_peak_memory_stats()
|
| 336 |
+
t0=time.perf_counter()
|
| 337 |
+
for _ in range(4):model(x,K,'off')
|
| 338 |
+
if dev.type=='cuda':torch.cuda.synchronize()
|
| 339 |
+
elapsed=(time.perf_counter()-t0)/4
|
| 340 |
+
peak=int(torch.cuda.max_memory_allocated()) if dev.type=='cuda' else None
|
| 341 |
+
return {'latency_ms_per_trajectory':1000*elapsed,'peak_allocated_gpu_bytes':peak,
|
| 342 |
+
'note':'batch1; GPU warmup2, timed4, use as hardware-bound descriptive evidence. It is not equal-FLOP comparison.'}
|
| 343 |
+
|
| 344 |
+
@torch.no_grad()
|
| 345 |
+
def rollout_diagnostics(model,loader,dev,K):
|
| 346 |
+
model.eval(); drifts=[]; flux_rms=[]; output_grows=[]
|
| 347 |
+
for bi,(h,y) in enumerate(loader):
|
| 348 |
+
if bi>=3:break
|
| 349 |
+
h=h.to(dev);p=model(h,K,'off');
|
| 350 |
+
output_grows.append(float((p[:,-1]-p[:,0]).square().mean().sqrt()))
|
| 351 |
+
if model.kind=='V2_Ported_R5':
|
| 352 |
+
mm=model.model
|
| 353 |
+
hh=h.unsqueeze(-2) if h.ndim==4 else h
|
| 354 |
+
b,l,c,hhgt,w=hh.shape; z=mm.encode(hh.reshape(b,l*c,hhgt,w)); f=mm.flux(z)
|
| 355 |
+
drifts.append(float((z.square().mean()).sqrt()))
|
| 356 |
+
flux_rms.append(float((f.square().mean()).sqrt()))
|
| 357 |
+
return {'mean_rollout_field_change_rms':float(np.mean(output_grows)),
|
| 358 |
+
'encoded_latent_rms':float(np.mean(drifts)) if drifts else None,
|
| 359 |
+
'raw_flux_rms':float(np.mean(flux_rms)) if flux_rms else None}
|
| 360 |
+
|
| 361 |
+
def smoke_grad(model,dev,cfg,dim):
|
| 362 |
+
model.train();shape=(2,4,model.model.model.config.channels,8,16) if model.kind=='V1_CIDM_Complete' else (2,4,model.model.channels,8,16)
|
| 363 |
+
if dim==1:shape=shape[:3]+(16,)
|
| 364 |
+
h=torch.randn(*shape,device=dev);p=model(h,3);assert p.shape==(shape[0],3,*shape[2:]) and bool(torch.isfinite(p).all())
|
| 365 |
+
loss=p.square().mean();loss.backward()
|
| 366 |
+
grads={n:float(v.grad.norm()) for n,v in model.named_parameters() if v.grad is not None}
|
| 367 |
+
if model.kind=='V2_Ported_R5':
|
| 368 |
+
flux=next(v for n,v in grads.items() if '.flux.net.' in n)
|
| 369 |
+
assert flux>1e-12,'Flux path not differentiable'
|
| 370 |
+
assert grads and max(grads.values())>0
|
| 371 |
+
model.zero_grad(set_to_none=True);return {'shape':list(p.shape),'grad_nonzero':sum(x>0 for x in grads.values()),'total_param':params(model)}
|
| 372 |
+
|
| 373 |
+
def save_ckpt(path,model,opt,iteration,best,best_state,rng):
|
| 374 |
+
torch.save({'model':model.state_dict(),'optimizer':opt.state_dict(),'iteration':iteration,'best':best,
|
| 375 |
+
'best_model':best_state,'cpu_rng':torch.get_rng_state(),'numpy_rng':np.random.get_state(),
|
| 376 |
+
'python_rng':random.getstate(),'cuda_rng':torch.cuda.get_rng_state_all() if torch.cuda.is_available() else None},path)
|
| 377 |
+
|
| 378 |
+
def fit(model,train,val,cfg,dev,runid,root,seed=17):
|
| 379 |
+
# Single fixed LR across models, no differential tuning budget.
|
| 380 |
+
target=cfg.updates();opt=torch.optim.AdamW(model.parameters(),lr=cfg.lr,weight_decay=1e-4)
|
| 381 |
+
checkpoint=Path(root)/'checkpoints'/f'{runid}.pt';checkpoint.parent.mkdir(parents=True,exist_ok=True)
|
| 382 |
+
hist=[];best=float('inf');best_state=None;start=0
|
| 383 |
+
if cfg.resume and checkpoint.exists():
|
| 384 |
+
chk=torch.load(checkpoint,map_location='cpu',weights_only=False)
|
| 385 |
+
model.load_state_dict(chk['model'],strict=True);opt.load_state_dict(chk['optimizer']);start=int(chk['iteration'])
|
| 386 |
+
best=chk['best'];best_state=chk['best_model'];torch.set_rng_state(chk['cpu_rng'])
|
| 387 |
+
np.random.set_state(chk['numpy_rng']);random.setstate(chk['python_rng'])
|
| 388 |
+
if torch.cuda.is_available() and chk['cuda_rng'] is not None:torch.cuda.set_rng_state_all(chk['cuda_rng'])
|
| 389 |
+
print('[resumed]',runid,'from',start)
|
| 390 |
+
losses=[];t0=time.time()
|
| 391 |
+
for step in range(start,target):
|
| 392 |
+
model.train()
|
| 393 |
+
# SAME per-seed per-step indices for BOTH architectures. Exact on resume.
|
| 394 |
+
g=torch.Generator().manual_seed(int(seed)*1000003+step)
|
| 395 |
+
ids=torch.randint(0,len(train.dataset),(train.batch_size,),generator=g).tolist()
|
| 396 |
+
xs=[train.dataset[i] for i in ids]
|
| 397 |
+
h=torch.stack([x[0] for x in xs]).to(dev)
|
| 398 |
+
y=torch.stack([x[1] for x in xs]).to(dev)
|
| 399 |
+
opt.zero_grad(set_to_none=True)
|
| 400 |
+
p=model(h,cfg.train_horizon,mode='off')
|
| 401 |
+
loss=F.mse_loss(p,y[:,:cfg.train_horizon]);
|
| 402 |
+
if not torch.isfinite(loss):raise FloatingPointError('Non-finite train loss')
|
| 403 |
+
loss.backward();nn.utils.clip_grad_norm_(model.parameters(),cfg.grad_clip);opt.step()
|
| 404 |
+
losses.append(float(loss.detach()))
|
| 405 |
+
if (step+1)%cfg.eval_every==0 or step+1==target:
|
| 406 |
+
eva=assess(model,val,dev,cfg,'off')
|
| 407 |
+
score=eva['model_'+str(cfg.eval_horizon)] # pre-registered 12-step VAL selection for BOTH models
|
| 408 |
+
if score<best:best=score;best_state={k:v.detach().cpu().clone() for k,v in model.state_dict().items()}
|
| 409 |
+
row={'iter':step+1,'train_mse':float(np.mean(losses[-cfg.eval_every:])),
|
| 410 |
+
'val_6_rmse':eva['model_'+str(cfg.train_horizon)],'val_12_rmse':eva['model_'+str(cfg.eval_horizon)],'val_rmse':score,'best_val_rmse':best,'wall_seconds':time.time()-t0}
|
| 411 |
+
hist.append(row);print('[train]',runid,row,flush=True)
|
| 412 |
+
save_ckpt(checkpoint,model,opt,step+1,best,best_state,None)
|
| 413 |
+
if best_state is not None:model.load_state_dict(best_state,strict=True)
|
| 414 |
+
return {'best_val_rmse':best,'validation_selection_horizon':cfg.eval_horizon,'history':hist,'elapsed_seconds':time.time()-t0, 'best_at_last_validation':bool(hist and hist[-1]['val_rmse'] <= best+1e-12)}
|
| 415 |
+
|
| 416 |
+
def run(cfg=None):
|
| 417 |
+
cfg=cfg or Config();out=Path(cfg.root)/cfg.profile;out.mkdir(parents=True,exist_ok=True)
|
| 418 |
+
torch.set_num_threads(min(4,max(1,torch.get_num_threads())))
|
| 419 |
+
dev=torch.device('cuda' if torch.cuda.is_available() else 'cpu')
|
| 420 |
+
frozen=Path(__file__).resolve().parent/'cidm_complete_frozen.py'
|
| 421 |
+
if sha(frozen)!=ARCHIVE_SHA:raise RuntimeError('CIDM-v1 source SHA256 mismatch; STOP')
|
| 422 |
+
if cfg.profile!='smoke' and dev.type!='cuda':raise RuntimeError('Real-data paired training requires GPU; use smoke for program validation.')
|
| 423 |
+
print('PROFILE',cfg.profile,'device',dev,'tasks',cfg.tasks(),'seeds',cfg.seeds())
|
| 424 |
+
if cfg.profile=='smoke':caches={'smoke_2d':make_synthetic_cache(cfg)}
|
| 425 |
+
else:
|
| 426 |
+
data_dir=Path(cfg.data_root) if cfg.data_root else Path(cfg.root)/'official_data'
|
| 427 |
+
caches={}
|
| 428 |
+
for task in cfg.tasks():
|
| 429 |
+
path=fetch_official(data_dir,task)
|
| 430 |
+
caches[task]=make_cache(task,path,cfg,Path(cfg.root)/'window_cache')
|
| 431 |
+
manifests=[];results=[];failures=[]
|
| 432 |
+
for task in cfg.tasks():
|
| 433 |
+
cache=caches[task];sp=cache['splits']['train']['h'].shape
|
| 434 |
+
channel=sp[2];dim=len(sp)-3
|
| 435 |
+
spec={'task':task,'channels':channel,'spatial_dim':dim,'spatial_shape':list(sp[3:]),'windows':{k:len(v['h']) for k,v in cache['splits'].items()},'ids':cache.get('ids',{}),'source_md5':TASKS[task][2] if task!='smoke_2d' else 'synthetic'}
|
| 436 |
+
match={}
|
| 437 |
+
for kind in ('V1_CIDM_Complete','V2_Ported_R5'):
|
| 438 |
+
match[kind]=choose_match(kind,channel,cfg)
|
| 439 |
+
model=UnifiedModel(kind,channel,match[kind]['d'],match[kind]['hidden'],cfg.history).to(dev)
|
| 440 |
+
g=smoke_grad(model,dev,cfg,dim);match[kind]['preflight']=g
|
| 441 |
+
del model
|
| 442 |
+
spec['matched']=match;manifests.append(spec)
|
| 443 |
+
print('[preflight]',task,match,flush=True)
|
| 444 |
+
jsonsave(out/'data_and_architecture_contract.json',{'config':asdict(cfg),'source_sha':ARCHIVE_SHA,'tasks':manifests,
|
| 445 |
+
'warning':'v2 is a documented channel-port from two-channel synthetic architecture, not same checkpoint as R5-R1',
|
| 446 |
+
'data_policy':'official DaRUS MD5 checked, shared windows, train-only norm, no future inference controls'})
|
| 447 |
+
for seed in cfg.seeds():
|
| 448 |
+
for task in cfg.tasks():
|
| 449 |
+
cache=caches[task];sp=cache['splits']['train']['h'].shape;dim=len(sp)-3;channel=sp[2]
|
| 450 |
+
batch=cfg.batch_1d if dim==1 else cfg.batch_2d
|
| 451 |
+
tr=load_data(cache,'train',batch,seed,True)
|
| 452 |
+
va=load_data(cache,'val',batch,seed,False)
|
| 453 |
+
te=load_data(cache,'test',batch,seed,False)
|
| 454 |
+
spec=next(x for x in manifests if x['task']==task)
|
| 455 |
+
for kind in ('V1_CIDM_Complete','V2_Ported_R5'):
|
| 456 |
+
runid=f'{task}__{kind}__seed{seed}';fp=out/(runid+'.json')
|
| 457 |
+
if fp.exists():
|
| 458 |
+
try:
|
| 459 |
+
prior=json.loads(fp.read_text())
|
| 460 |
+
if prior.get('status')=='COMPLETED' and prior.get('total_steps')==cfg.updates():
|
| 461 |
+
results.append(prior);print('[skip completed]',runid);continue
|
| 462 |
+
except:pass
|
| 463 |
+
seed_all(seed)
|
| 464 |
+
conf=spec['matched'][kind]
|
| 465 |
+
model=UnifiedModel(kind,channel,conf['d'],conf['hidden'],cfg.history).to(dev)
|
| 466 |
+
try:
|
| 467 |
+
trained=fit(model,tr,va,cfg,dev,runid,out,seed=seed)
|
| 468 |
+
metrics=assess(model,te,dev,cfg,'off')
|
| 469 |
+
lead_rmse=per_lead_assess(model,te,dev,cfg)
|
| 470 |
+
timing=measure_latency(model,te,dev,cfg.eval_horizon)
|
| 471 |
+
mechanism=rollout_diagnostics(model,te,dev,cfg.eval_horizon)
|
| 472 |
+
best_model_path=out/'best_models'/f'{runid}.pt';best_model_path.parent.mkdir(parents=True,exist_ok=True)
|
| 473 |
+
torch.save({'model':{k:v.detach().cpu() for k,v in model.state_dict().items()},
|
| 474 |
+
'model_kind':kind,'config':conf,'data_signature':cache['signature'],'seed':seed,'source_sha':ARCHIVE_SHA},best_model_path)
|
| 475 |
+
noflux=None
|
| 476 |
+
if kind=='V2_Ported_R5':
|
| 477 |
+
coefficients={k:float(v.detach()) for k,v in model.model.coefficients().items()}
|
| 478 |
+
# Mechanism dependence on same learned model (not trained ablation).
|
| 479 |
+
model.model.flux.forward_saved=model.model.flux.forward
|
| 480 |
+
model.model.flux.forward=lambda z:torch.zeros_like(z)
|
| 481 |
+
nf=assess(model,te,dev,cfg,'off');noflux=nf['model_'+str(cfg.eval_horizon)]
|
| 482 |
+
del model.model.flux.forward;coefficients['no_flux_test_rmse']=noflux
|
| 483 |
+
else:coefficients={}
|
| 484 |
+
row={'status':'COMPLETED','seed':seed,'task':task,'model':kind,'parameter_count':params(model),
|
| 485 |
+
'model_config':conf,'total_steps':cfg.updates(),'train':trained,'test':metrics,'test_per_lead_rmse':lead_rmse,
|
| 486 |
+
'latency':timing,'mechanism_diagnostic':mechanism,'best_model_path':str(best_model_path),
|
| 487 |
+
'coefficients':coefficients}
|
| 488 |
+
jsonsave(fp,row);results.append(row)
|
| 489 |
+
except Exception as e:
|
| 490 |
+
row={'status':'FAILED','seed':seed,'task':task,'model':kind,'error':repr(e),'traceback':traceback.format_exc()[-2400:]}
|
| 491 |
+
jsonsave(fp,row);failures.append(row);print('[FAILED]',runid,row['error'],flush=True)
|
| 492 |
+
finally:
|
| 493 |
+
del model
|
| 494 |
+
if torch.cuda.is_available():torch.cuda.empty_cache()
|
| 495 |
+
summary=[]
|
| 496 |
+
for task in cfg.tasks():
|
| 497 |
+
for lead in (cfg.train_horizon,cfg.eval_horizon):
|
| 498 |
+
paired=[]
|
| 499 |
+
for seed in cfg.seeds():
|
| 500 |
+
v1=next((r for r in results if r['task']==task and r['seed']==seed and r['model']=='V1_CIDM_Complete' and r['status']=='COMPLETED'),None)
|
| 501 |
+
v2=next((r for r in results if r['task']==task and r['seed']==seed and r['model']=='V2_Ported_R5' and r['status']=='COMPLETED'),None)
|
| 502 |
+
if v1 is not None and v2 is not None:
|
| 503 |
+
a=v1['test']['model_'+str(lead)];b=v2['test']['model_'+str(lead)];naive=min(v2['test']['persistence_'+str(lead)],v2['test']['trend_'+str(lead)])
|
| 504 |
+
paired.append({'seed':seed,'v1_rmse':a,'v2_rmse':b,'v2_improvement_pct':100*(1-b/a),'best_naive_rmse':naive})
|
| 505 |
+
if paired:
|
| 506 |
+
summary.append({'task':task,'horizon':lead,'n_paired_seeds':len(paired),
|
| 507 |
+
'v1_mean_rmse':float(np.mean([x['v1_rmse'] for x in paired])),
|
| 508 |
+
'v2_mean_rmse':float(np.mean([x['v2_rmse'] for x in paired])),
|
| 509 |
+
'paired_improvement_pct_mean':float(np.mean([x['v2_improvement_pct'] for x in paired])),
|
| 510 |
+
'paired_results':paired,
|
| 511 |
+
'all_v2_beat_v1':all(x['v2_rmse']<x['v1_rmse'] for x in paired),
|
| 512 |
+
'all_v2_beat_strong_naive':all(x['v2_rmse']<x['best_naive_rmse'] for x in paired)})
|
| 513 |
+
# Gate only evaluates PROGRAM/SIGNAL quality; pilot cannot qualify architecture maturity.
|
| 514 |
+
expected=len(cfg.tasks())*len(cfg.seeds())*2
|
| 515 |
+
complete=len([r for r in results if r.get('status')=='COMPLETED'])
|
| 516 |
+
gate={'source_sha_pass':True,'completed_runs':complete,'expected_runs':expected,'all_complete':complete==expected,
|
| 517 |
+
'all_config_matched':all((cfg.profile=='smoke' or s['matched'][k]['relative_error']<=cfg.match_tolerance) for s in manifests for k in ('V1_CIDM_Complete','V2_Ported_R5')),
|
| 518 |
+
'all_v2_better_v1':bool(summary) and all(x['all_v2_beat_v1'] for x in summary),
|
| 519 |
+
'all_v2_better_strong_naive':bool(summary) and all(x['all_v2_beat_strong_naive'] for x in summary),
|
| 520 |
+
'enough_independent_seeds':len(cfg.seeds())>=3,
|
| 521 |
+
'all_eight_pde_families':len(cfg.tasks())==8,
|
| 522 |
+
'official_data':cfg.profile!='smoke',
|
| 523 |
+
'state':'DIAGNOSTIC_ONLY' if cfg.profile in ('smoke','pilot','pilot_r1') else 'FORMAL_COMPARISON_UNQUALIFIED',
|
| 524 |
+
'convergence_proven':False,'no_claim_all_pde_superiority':True}
|
| 525 |
+
if cfg.profile in ('formal8','canonical8') and all([gate['all_complete'],gate['all_config_matched'],gate['all_v2_better_v1'],gate['all_v2_better_strong_naive'],gate['enough_independent_seeds'],gate['all_eight_pde_families']]):
|
| 526 |
+
gate['state']='EIGHT_PDE_V1_SUPERIORITY_PRELIMINARY_PASS_EXTERNAL_MODELS_PENDING'
|
| 527 |
+
jsonsave(out/'B0_REPORT.json',{'gate':gate,'config':asdict(cfg),'summary':summary,'failures':failures,'manifest':manifests})
|
| 528 |
+
# Descriptive multi-seed confidence summary and frozen mechanism diagnostics.
|
| 529 |
+
paired_task_summary=[]
|
| 530 |
+
for row in summary:
|
| 531 |
+
diffs=[x['v2_improvement_pct'] for x in row['paired_results']]
|
| 532 |
+
paired_task_summary.append({'task':row['task'],'lead':row['horizon'],
|
| 533 |
+
'n_seeds':len(diffs),'mean_improvement_pct':float(np.mean(diffs)),
|
| 534 |
+
'median_improvement_pct':float(np.median(diffs)),
|
| 535 |
+
'min_improvement_pct':float(np.min(diffs)),
|
| 536 |
+
'max_improvement_pct':float(np.max(diffs)),
|
| 537 |
+
'n_v2_wins':sum(x>0 for x in diffs),
|
| 538 |
+
'remark':'three seeds are not sufficient to assert statistical superiority'})
|
| 539 |
+
jsonsave(out/'B0_R1_PAIRED_DIAGNOSTICS.json',{'paired_task_summary':paired_task_summary,
|
| 540 |
+
'warning':'12-step validation selection used for BOTH models, not comparable by itself to old 6-step selected B0 pilot; insufficient converged for v2 final claim',
|
| 541 |
+
'sync_mode':'OFF for all v2 runs; not a state synchronization evaluation',
|
| 542 |
+
'extra':'No SOC in v2 but v1 SOC and BER structure retained as archival baseline; interpretation is architecture-level comparison'})
|
| 543 |
+
# Plot - failure plot should still be generated.
|
| 544 |
+
try:
|
| 545 |
+
import matplotlib.pyplot as plt
|
| 546 |
+
xx=[r for r in summary if r['horizon']==cfg.eval_horizon]
|
| 547 |
+
if xx:
|
| 548 |
+
fig,ax=plt.subplots(figsize=(max(7,1.3*len(xx)),4));ax.bar([r['task'] for r in xx],[r['paired_improvement_pct_mean'] for r in xx]);ax.axhline(0,color='gray',lw=.8)
|
| 549 |
+
ax.set_ylabel('v2 versus exact v1: paired RMSE improvement (%)');ax.tick_params(axis='x',labelrotation=25);fig.tight_layout();fig.savefig(out/'paired_comparison.png',dpi=180);plt.close(fig)
|
| 550 |
+
except Exception as e:print('[plot skipped]',repr(e))
|
| 551 |
+
archive=out/'CIDM_v2_B0_PDE_FAIR_REPORTS.zip'
|
| 552 |
+
with zipfile.ZipFile(archive,'w',compression=zipfile.ZIP_DEFLATED) as z:
|
| 553 |
+
for p in sorted(out.glob('*.json')):z.write(p,p.name)
|
| 554 |
+
if (out/'paired_comparison.png').exists():z.write(out/'paired_comparison.png','paired_comparison.png')
|
| 555 |
+
if cfg.profile in ('smoke','pilot','pilot_r1') or os.getenv('CIDM_B0_ZIP_WEIGHTS','0')=='1':
|
| 556 |
+
for p in sorted((out/'best_models').glob('*.pt')):z.write(p,'best_models/'+p.name)
|
| 557 |
+
print('[FINAL]',gate, '\nReport ZIP:',archive,flush=True)
|
| 558 |
+
return {'gate':gate,'summary':summary,'archive':str(archive),'out':str(out)}
|
| 559 |
+
|
| 560 |
+
if __name__=='__main__':
|
| 561 |
+
import argparse
|
| 562 |
+
parser=argparse.ArgumentParser();parser.add_argument('--profile',default=os.getenv('CIDM_B0_PROFILE','smoke'),choices=['smoke','pilot','pilot_r1','formal8','canonical8'])
|
| 563 |
+
parser.add_argument('--root',default=os.getenv('CIDM_B0_ROOT','/mnt/data/CIDM_v2_B0_R1_demo'))
|
| 564 |
+
parser.add_argument('--data-root',default=os.getenv('CIDM_B0_DATA_ROOT',''))
|
| 565 |
+
args=parser.parse_args();run(Config(profile=args.profile,root=args.root,data_root=args.data_root))
|