wuff-mann commited on
Commit
ffe5130
·
verified ·
1 Parent(s): b0caa68

Archive CIDM-v2 reproducible b2_r11_backward_attribution.py

Browse files
cidm_v2/source/B2_R11_P0/b2_r11_backward_attribution.py ADDED
@@ -0,0 +1,232 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """CIDM-v2 B2-R11/P0: exact frozen 12-step backward attribution, no training.
2
+
3
+ Uses the archived B2-R10 FIX2 objective, model, control heads and normalization.
4
+ Loss uses TRAIN labels only. Attribution does not implement an inference policy.
5
+ Logs (1) each controller invocation's contribution to the full parameter
6
+ gradient, (2) each core operator's VJP contribution to latent adjoint,
7
+ including its normalized local-control basis path, (3) exact replay.
8
+ """
9
+ from __future__ import annotations
10
+ import math, time, json, zipfile, csv, hashlib
11
+ from pathlib import Path
12
+ import torch
13
+ from benchmark import Config,Windows,UnifiedModel,make_synthetic_cache
14
+ from b2_r6_causal_spatial_controller import TASK_LIST,SEED,PARENT_SHA,make_cache_checked,orthogonal_matrix,sha
15
+ from b2_r7_frozen_controller_gain_audit import load_heads,write_csv,write_json
16
+ from b2_r1_flux_control import check_zip
17
+ import b2_r10_gradient_credit_audit as credit
18
+
19
+ PREV_FIX2_SHA='bfc54b95d54714eee6ddd1ef3f1d158418f26f8e00a5fc42faf0b8cc5eeba6bc'
20
+ LEADS=(1,3,9,12)
21
+ BUDGET=.003
22
+
23
+ def l2(v):
24
+ return float(torch.linalg.vector_norm(v.detach().double()))
25
+
26
+ def finite_tensor(t,name):
27
+ if not torch.isfinite(t).all():raise RuntimeError('NONFINITE_TENSOR '+name)
28
+
29
+ def concat_grad(params):
30
+ return torch.cat([torch.zeros_like(p).reshape(-1) if p.grad is None else p.grad.detach().reshape(-1) for p in params])
31
+
32
+ def full_forward(core,head,h,truth,scale,Q=None):
33
+ """R9/R10 exact forward with retained local adjoints and controller outputs."""
34
+ mm=core.model
35
+ one_d=(h.ndim==4)
36
+ hh=h.unsqueeze(-2) if one_d else h
37
+ B,T,C,H,W=hh.shape
38
+ co=mm.coefficients()
39
+ z=mm.encode(hh.reshape(B,T*C,H,W)).detach().requires_grad_(True)
40
+ prev=hh[:,-1].detach().requires_grad_(True)
41
+ initial=prev
42
+ skew=mm.skew-mm.skew.T
43
+ records=[];pred=[]
44
+ for lead in range(1,13):
45
+ z_old=z
46
+ z_old.retain_grad()
47
+ f=co['flux']*mm.flux(z_old)
48
+ local=co['local']*torch.tanh(mm.local(z_old))
49
+ sk=.035*torch.einsum('ij,bjhw->bihw',skew,z_old)
50
+ base=z_old+f+local+sk
51
+ basis,amps=credit.stable_exact_effect_basis(f,local,sk,Q)
52
+ gates=head(base,prev,initial,amps,lead)
53
+ raw=(gates.unsqueeze(2)*basis).sum(dim=1)/math.sqrt(3)
54
+ rr=credit.stable_exact_rms(raw)
55
+ change=BUDGET*credit.stable_exact_rms(base).clamp_min(1e-4)*raw/rr.clamp_min(1e-9)
56
+ change=torch.where(rr>=1e-9,change,torch.zeros_like(change))
57
+ z=base+change
58
+ prev_new=prev+co['out']*torch.tanh(mm.decode(z))
59
+ for elem in (f,local,sk,base,gates,z,prev_new):elem.retain_grad()
60
+ records.append({'lead':lead,'z_old':z_old,'prev_old':prev,'initial':initial,
61
+ 'f':f,'l':local,'s':sk,'base':base,'amps':amps,
62
+ 'gates':gates,'z_new':z,'prev_new':prev_new})
63
+ prev=prev_new
64
+ pred.append(prev.squeeze(-2) if one_d else prev)
65
+ pred=torch.stack(pred,dim=1)
66
+ shape=(1,1,scale.numel())+(1,)*(pred.ndim-3)
67
+ w=scale.reshape(shape)
68
+ losses={j:((pred[:,j-1]-truth[:,j-1])*w.squeeze(1)).square().mean() for j in LEADS}
69
+ loss=.5*(losses[9]+losses[12])+.125*(losses[1]+losses[3])
70
+ return pred,loss,records
71
+
72
+ def analyze_case(task,core,cache,r6zip,device,head_types=('operator','rotated'),max_train_examples=2):
73
+ core.requires_grad_(False).eval()
74
+ ch=int(cache['splits']['train']['h'].shape[2]);d=int(core.model.skew.shape[0])
75
+ head_task= 'swe_2d' if task=='smoke_2d' else ('burgers_1d' if task=='smoke_1d' else task)
76
+ heads=load_heads(r6zip,head_task,ch,device)
77
+ Q=orthogonal_matrix(d,32207,device,torch.float32)
78
+ train=Windows(cache['splits']['train']['h'],cache['splits']['train']['y'],cache['mean'],cache['std'])
79
+ ids=list(range(min(max_train_examples,len(train))))
80
+ arr=[train[i] for i in ids]
81
+ h=torch.stack([t[0] for t in arr]).to(device)
82
+ y=torch.stack([t[1] for t in arr]).to(device)
83
+ scale=train.sd.to(device)
84
+ result=[];steps=[];layers=[]
85
+ for name in head_types:
86
+ head=heads[name].to(device).train()
87
+ for p in head.parameters():p.requires_grad_(True)
88
+ params=tuple(head.parameters())
89
+ head.zero_grad(set_to_none=True)
90
+ rot=Q if name=='rotated' else None
91
+ tic=time.perf_counter()
92
+ out,loss,records=full_forward(core,head,h,y,scale,rot)
93
+ finite_tensor(out,'forward '+task+' '+name)
94
+ with torch.no_grad():
95
+ parity=float((out.detach()-credit.original_frozen_rollout(core,head,h,rotation=rot)).abs().max())
96
+ if parity>2e-6:raise RuntimeError(f'REPLAY_MISMATCH {task} {name} {parity}')
97
+ finite_tensor(loss,'loss '+task+' '+name)
98
+ loss.backward()
99
+ full=concat_grad(params).double()
100
+ finite_tensor(full,'full_gradient '+task+' '+name)
101
+ fullnorm=l2(full)
102
+ pieces=[]
103
+ state_u=[]
104
+ for r in records:
105
+ lead=r['lead']
106
+ gg=r['gates'].grad
107
+ if gg is None:raise RuntimeError(f'MISSING_GATE_ADJOINT {task} {name} {lead}')
108
+ finite_tensor(gg,f'gate_adjoint_{lead}')
109
+ # Replay frozen input to isolate direct per-call contribution to head theta.
110
+ g_replay=head(r['base'].detach(),r['prev_old'].detach(),
111
+ r['initial'].detach(),r['amps'].detach(),lead)
112
+ if not torch.allclose(g_replay.detach(),r['gates'].detach(),atol=1e-6,rtol=1e-6):
113
+ raise RuntimeError(f'GATE_REPLAY_MISMATCH_{lead}')
114
+ grads=torch.autograd.grad(g_replay,params,grad_outputs=gg.detach(),allow_unused=True)
115
+ part=torch.cat([torch.zeros_like(p).flatten() if x is None else x.detach().flatten() for p,x in zip(params,grads)]).double()
116
+ finite_tensor(part,f'head_gradient_part_{lead}')
117
+ pieces.append(part)
118
+ state_u.append(l2(r['z_old'].grad))
119
+ steps.append({'task':task,'controller':name,'lead':lead,
120
+ 'gate_adjoint_l2':l2(gg),'parameter_part_l2':l2(part),
121
+ 'parameter_part_signed_projection':float(torch.dot(part,full)/max(fullnorm,1e-30)),
122
+ 'parameter_part_cosine_full':float(torch.dot(part,full)/max(l2(part)*fullnorm,1e-30)),
123
+ 'latent_state_adjoint_l2':l2(r['z_old'].grad),
124
+ 'base_adjoint_l2':l2(r['base'].grad),
125
+ 'flux_effect_adjoint_l2':l2(r['f'].grad),
126
+ 'local_effect_adjoint_l2':l2(r['l'].grad),
127
+ 'skew_effect_adjoint_l2':l2(r['s'].grad),
128
+ 'forward_parity_max':parity})
129
+ recon=torch.stack(pieces).sum(dim=0)
130
+ reconstruction_error=l2(recon-full)/max(fullnorm,1e-30)
131
+ if reconstruction_error>1e-4:raise RuntimeError(f'HEAD_PARTITION_GRADIENT_FAILED {task} {name} {reconstruction_error}')
132
+ for r in records:
133
+ z=r['z_old'].detach().requires_grad_(True)
134
+ co=core.model.coefficients();mm=core.model;skew=mm.skew-mm.skew.T
135
+ f=co['flux']*mm.flux(z)
136
+ local=co['local']*torch.tanh(mm.local(z))
137
+ sk=.035*torch.einsum('ij,bjhw->bihw',skew,z)
138
+ gf,gl,gs=(r[k].grad.detach() for k in ('f','l','s'))
139
+ gbase=r['base'].grad.detach()
140
+ vjp_f,=torch.autograd.grad(f,z,gf,retain_graph=True)
141
+ vjp_l,=torch.autograd.grad(local,z,gl,retain_graph=True)
142
+ vjp_s,=torch.autograd.grad(sk,z,gs,retain_graph=True)
143
+ components=[gbase.double(),vjp_f.double(),vjp_l.double(),vjp_s.double()]
144
+ recovered=sum(components)
145
+ # Critical: z_old participates in the *previous* step's decoder branch,
146
+ # so z_old.grad of the original full trajectory includes additional
147
+ # graph shortcuts. It is NOT the local transition VJP reference.
148
+ # Use an independently evaluated local VJP with the correct
149
+ # f/l/sk direct-basis adjoints minus their already counted base path.
150
+ other=torch.autograd.grad(
151
+ (z+f+local+sk,f,local,sk),z,
152
+ grad_outputs=(gbase,gf-gbase,gl-gbase,gs-gbase),allow_unused=False)[0]
153
+ actual=other.detach().double()
154
+ relative=l2(recovered-actual)/max(sum(l2(t) for t in components),1e-30)
155
+ if relative>1e-3:raise RuntimeError(f'OPERATOR_VJP_PARTITION_FAILED {task} {name} lead={r["lead"]} relative={relative}')
156
+ layers.append({'task':task,'controller':name,'lead':r['lead'],
157
+ 'direct_identity_vjp_l2':l2(gbase),
158
+ 'flux_vjp_l2':l2(vjp_f),'local_vjp_l2':l2(vjp_l),
159
+ 'skew_vjp_l2':l2(vjp_s),
160
+ 'latent_vjp_sum_l2':l2(recovered),'latent_local_reference_vjp_l2':l2(actual),'original_graph_latent_adjoint_l2':l2(r['z_old'].grad),
161
+ 'local_vjp_partition_error':relative})
162
+ values=[l2(v) for v in pieces]
163
+ key_idx=sorted(range(12),key=lambda i:values[i],reverse=True)
164
+ result.append({'task':task,'controller':name,'examples':len(ids),'TRAIN_indices':ids,
165
+ 'loss':float(loss.detach()),'forward_parity_max':parity,
166
+ 'full_gradient_l2_fp64':fullnorm,
167
+ 'head_step_reconstruction_rel_error':reconstruction_error,
168
+ 'top_step_by_norm':int(key_idx[0]+1),'top_step_gradient_l2':values[key_idx[0]],
169
+ 'early_steps_1_3_sum_norm':float(sum(values[:3])),
170
+ 'late_steps_9_12_sum_norm':float(sum(values[8:])),
171
+ 'elapsed_s':time.perf_counter()-tic})
172
+ for p in head.parameters():p.requires_grad_(False)
173
+ return result,steps,layers
174
+
175
+ def run(parent,r6zip,r9zip,fix2zip,work,output,smoke=False,device='cpu'):
176
+ work=Path(work);output=Path(output);work.mkdir(parents=True,exist_ok=True);output.mkdir(parents=True,exist_ok=True)
177
+ if sha(fix2zip)!=PREV_FIX2_SHA:raise RuntimeError('B2R10_FIX2_EXACT_EVIDENCE_SHA_MISMATCH')
178
+ with zipfile.ZipFile(fix2zip) as z:
179
+ if z.testzip():raise RuntimeError('B2R10_FIX2_EVIDENCE_CRC_ERROR')
180
+ rep=json.loads(z.read('report.json'))
181
+ if rep['status']!='FROZEN_GRADIENT_CREDIT_AUDIT_COMPLETED_NO_TRAINING' or rep['rows']!=18:
182
+ raise RuntimeError('B2R10_FIX2_EVIDENCE_NOT_FULL')
183
+ gate=json.loads(z.read('gradient_qualification_gate.json'))
184
+ if gate['numerical_status']!='SEVERE_GRADIENT_AMPLIFICATION_NO_TRAINING':raise RuntimeError('FIX2_EVIDENCE_NOT_MATCHED')
185
+ credit.validate_r6(r6zip);credit.validate_r9(r9zip)
186
+ rows=[];perlead=[];ops=[];cases=[]
187
+ if smoke:
188
+ cfg=Config(profile='smoke')
189
+ a=make_synthetic_cache(cfg);b=make_synthetic_cache(cfg)
190
+ for split in ('train','val','test'):
191
+ a['splits'][split]['h']=a['splits'][split]['h'].mean(dim=-2)
192
+ a['splits'][split]['y']=a['splits'][split]['y'].mean(dim=-2)
193
+ for task,cache in [('smoke_1d',a),('smoke_2d',b)]:
194
+ core=UnifiedModel('V2_Ported_R5',1,8,12,4).to(device)
195
+ res,st,op=analyze_case(task,core,cache,r6zip,device,max_train_examples=1)
196
+ rows.extend(res);perlead.extend(st);ops.extend(op)
197
+ status='PROGRAM_ONLY_SMOKE_NO_SCIENCE'
198
+ else:
199
+ if parent is None or sha(parent)!=PARENT_SHA:raise RuntimeError('PARENT_SHA_MISMATCH')
200
+ with zipfile.ZipFile(parent) as z:
201
+ manifest,contract=check_zip(z);cfg=Config(**manifest['config'])
202
+ for task in TASK_LIST:
203
+ core,cache,replay=make_cache_checked(task,z,cfg,work,device)
204
+ try:
205
+ res,st,op=analyze_case(task,core,cache,r6zip,device)
206
+ except Exception as e:
207
+ write_json(output/'failure_case.json',{'task':task,'exception':repr(e),'already_completed':cases})
208
+ raise
209
+ rows.extend(res);perlead.extend(st);ops.extend(op)
210
+ cases.append({'task':task,'parent_replay':replay,'rows':len(res),'exact_model_frozen':True})
211
+ write_csv(output/'temporal_gradient_contributions.csv',perlead)
212
+ write_csv(output/'operator_local_vjp.csv',ops)
213
+ write_json(output/'progress.json',{'completed':[x['task'] for x in cases]})
214
+ status='FROZEN_BACKWARD_PATH_ATTRIBUTION_COMPLETED_NO_TRAINING'
215
+ if not smoke and (len(rows)!=6 or len(perlead)!=72 or len(ops)!=72):
216
+ raise RuntimeError(f'INCOMPLETE_BACKWARD_PATH_AUDIT_{len(rows)}_{len(perlead)}_{len(ops)}')
217
+ write_csv(output/'temporal_gradient_contributions.csv',perlead)
218
+ write_csv(output/'operator_local_vjp.csv',ops)
219
+ write_csv(output/'case_summary.csv',rows)
220
+ risk=[r for r in rows if r['full_gradient_l2_fp64']>1e8]
221
+ gate={'status':'DIAGNOSTIC_ONLY_NO_TRAINING','completed_groups':len(rows),
222
+ 'gradient_large_cases':risk,'train_allowed':False,
223
+ 'note':'Relative VJP norms and parameter contribution amplitudes are diagnostic, not causal branch ablations or Jacobian spectral norms.'}
224
+ write_json(output/'gradient_path_gate.json',gate)
225
+ report={'experiment':'CIDM_v2_B2_R11_P0_BackwardPathAttribution',
226
+ 'status':status,'cases':cases,'number_of_controllers':len(rows),
227
+ 'temporal_rows':len(perlead),'vjp_rows':len(ops),
228
+ 'original_fix2_sha256':PREV_FIX2_SHA,
229
+ 'training_updates':0,'no_outer_holdout':True,'train_allowed':False,
230
+ 'attribution':'exact decomposition of the frozen head gradient and local VJPs; no weights changed'}
231
+ write_json(output/'report.json',report)
232
+ return report