wuff-mann commited on
Commit
7247b3d
·
verified ·
1 Parent(s): 25e3d2a

Archive CIDM-v2 reproducible b2_r11_backward_attribution.py

Browse files
cidm_v2/source/B2_R11_P0_FIX1/b2_r11_backward_attribution.py ADDED
@@ -0,0 +1,287 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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, copy
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
+ # Independent FP64 local-head VJP replay. Does not change frozen forward or
105
+ # the FP32 12-step backward adjoints. This is a numerical *diagnostic*.
106
+ head64=copy.deepcopy(head).to(dtype=torch.float64).eval()
107
+ params64=tuple(head64.parameters())
108
+ pieces64=[]
109
+ replay_error64_max=0.0
110
+ for r in records:
111
+ lead=r['lead']
112
+ gg=r['gates'].grad
113
+ if gg is None:raise RuntimeError(f'MISSING_GATE_ADJOINT {task} {name} {lead}')
114
+ finite_tensor(gg,f'gate_adjoint_{lead}')
115
+ # Replay frozen input to isolate direct per-call contribution to head theta.
116
+ g_replay=head(r['base'].detach(),r['prev_old'].detach(),
117
+ r['initial'].detach(),r['amps'].detach(),lead)
118
+ if not torch.allclose(g_replay.detach(),r['gates'].detach(),atol=1e-6,rtol=1e-6):
119
+ raise RuntimeError(f'GATE_REPLAY_MISMATCH_{lead}')
120
+ grads=torch.autograd.grad(g_replay,params,grad_outputs=gg.detach(),allow_unused=True)
121
+ part=torch.cat([torch.zeros_like(p).flatten() if x is None else x.detach().flatten() for p,x in zip(params,grads)]).double()
122
+ finite_tensor(part,f'head_gradient_part_{lead}')
123
+ pieces.append(part)
124
+ # Recompute the head Jacobian with float64 kernel arithmetic while
125
+ # holding the ORIGINAL FP32 gate cotangent fixed. No new labels,
126
+ # trajectory, or optimizer steps; this is NOT a full FP64 gradient.
127
+ g64=head64(r['base'].detach().double(),r['prev_old'].detach().double(),
128
+ r['initial'].detach().double(),r['amps'].detach().double(),lead)
129
+ replay_error64_max=max(replay_error64_max,float((g64.detach()-r['gates'].detach().double()).abs().max()))
130
+ grad64=torch.autograd.grad(g64,params64,grad_outputs=gg.detach().double(),allow_unused=True)
131
+ part64=torch.cat([torch.zeros_like(p).flatten() if x is None else x.detach().flatten()
132
+ for p,x in zip(params64,grad64)])
133
+ finite_tensor(part64,f'head_gradient_part_fp64_{lead}')
134
+ pieces64.append(part64)
135
+ state_u.append(l2(r['z_old'].grad))
136
+ steps.append({'task':task,'controller':name,'lead':lead,
137
+ 'gate_adjoint_l2':l2(gg),'parameter_part_l2':l2(part),
138
+ 'parameter_part_signed_projection':float(torch.dot(part,full)/max(fullnorm,1e-30)),
139
+ 'parameter_part_cosine_full':float(torch.dot(part,full)/max(l2(part)*fullnorm,1e-30)),
140
+ 'latent_state_adjoint_l2':l2(r['z_old'].grad),
141
+ 'base_adjoint_l2':l2(r['base'].grad),
142
+ 'flux_effect_adjoint_l2':l2(r['f'].grad),
143
+ 'local_effect_adjoint_l2':l2(r['l'].grad),
144
+ 'skew_effect_adjoint_l2':l2(r['s'].grad),
145
+ 'forward_parity_max':parity})
146
+ # Three different summation modes, with per-term FP32 and FP64 replay.
147
+ # A very small ||sum(parts)|| compared with sum(||part||) makes the
148
+ # reconstruction ill-conditioned; never mistake that for gate passing.
149
+ recon=torch.stack(pieces).sum(dim=0)
150
+ recon_fp32=torch.stack([p.float() for p in pieces]).sum(dim=0).double()
151
+ recon_head64=torch.stack(pieces64).sum(dim=0)
152
+ reconstruction_error=l2(recon-full)/max(fullnorm,1e-30)
153
+ fp32_sum_rel_error=l2(recon_fp32-full)/max(fullnorm,1e-30)
154
+ fp64_head_rel_error=l2(recon_head64-full)/max(fullnorm,1e-30)
155
+ local_fp32_fp64_rel_error=l2(recon_head64-recon)/max(l2(recon),1e-30)
156
+ sum_piece_norms=sum(l2(p) for p in pieces)
157
+ cancellation_condition=sum_piece_norms/max(l2(recon),1e-30)
158
+ absolute_residual_l2=l2(recon-full)
159
+ head_partition_status=('PASS' if reconstruction_error<=1e-4 else 'FAIL_NUMERIC_UNRESOLVED')
160
+ # The original 1e-4 gate stays intact. We still collect per-step VJP
161
+ # evidence for a blocked task, but NEVER report it as qualified science.
162
+ diagnostic={'task':task,'controller':name,'original_gate_threshold':1e-4,
163
+ 'head_partition_status':head_partition_status,
164
+ 'full_gradient_l2_fp64':fullnorm,
165
+ 'head_step_reconstruction_rel_error':reconstruction_error,
166
+ 'absolute_residual_l2_fp64':absolute_residual_l2,
167
+ 'fp32_reduced_head_sum_rel_error':fp32_sum_rel_error,
168
+ 'fp64_local_head_sum_rel_error':fp64_head_rel_error,
169
+ 'fp32_vs_fp64_local_sum_rel_error':local_fp32_fp64_rel_error,
170
+ 'per_step_norms_sum':sum_piece_norms,
171
+ 'cancellation_condition':cancellation_condition,
172
+ 'head_fp64_forward_replay_max_abs':replay_error64_max,
173
+ 'warning':'Double local-head replay does not fix an unstable upstream FP32 cotangent.'}
174
+ for r in records:
175
+ z=r['z_old'].detach().requires_grad_(True)
176
+ co=core.model.coefficients();mm=core.model;skew=mm.skew-mm.skew.T
177
+ f=co['flux']*mm.flux(z)
178
+ local=co['local']*torch.tanh(mm.local(z))
179
+ sk=.035*torch.einsum('ij,bjhw->bihw',skew,z)
180
+ gf,gl,gs=(r[k].grad.detach() for k in ('f','l','s'))
181
+ gbase=r['base'].grad.detach()
182
+ vjp_f,=torch.autograd.grad(f,z,gf,retain_graph=True)
183
+ vjp_l,=torch.autograd.grad(local,z,gl,retain_graph=True)
184
+ vjp_s,=torch.autograd.grad(sk,z,gs,retain_graph=True)
185
+ components=[gbase.double(),vjp_f.double(),vjp_l.double(),vjp_s.double()]
186
+ recovered=sum(components)
187
+ # Critical: z_old participates in the *previous* step's decoder branch,
188
+ # so z_old.grad of the original full trajectory includes additional
189
+ # graph shortcuts. It is NOT the local transition VJP reference.
190
+ # Use an independently evaluated local VJP with the correct
191
+ # f/l/sk direct-basis adjoints minus their already counted base path.
192
+ other=torch.autograd.grad(
193
+ (z+f+local+sk,f,local,sk),z,
194
+ grad_outputs=(gbase,gf-gbase,gl-gbase,gs-gbase),allow_unused=False)[0]
195
+ actual=other.detach().double()
196
+ relative=l2(recovered-actual)/max(sum(l2(t) for t in components),1e-30)
197
+ if relative>1e-3:raise RuntimeError(f'OPERATOR_VJP_PARTITION_FAILED {task} {name} lead={r["lead"]} relative={relative}')
198
+ layers.append({'task':task,'controller':name,'lead':r['lead'],
199
+ 'direct_identity_vjp_l2':l2(gbase),
200
+ 'flux_vjp_l2':l2(vjp_f),'local_vjp_l2':l2(vjp_l),
201
+ 'skew_vjp_l2':l2(vjp_s),
202
+ 'latent_vjp_sum_l2':l2(recovered),'latent_local_reference_vjp_l2':l2(actual),'original_graph_latent_adjoint_l2':l2(r['z_old'].grad),
203
+ 'local_vjp_partition_error':relative})
204
+ values=[l2(v) for v in pieces]
205
+ key_idx=sorted(range(12),key=lambda i:values[i],reverse=True)
206
+ result.append({'task':task,'controller':name,'examples':len(ids),'TRAIN_indices':ids,
207
+ **diagnostic,
208
+ 'loss':float(loss.detach()),'forward_parity_max':parity,
209
+ 'full_gradient_l2_fp64':fullnorm,
210
+ 'head_step_reconstruction_rel_error':reconstruction_error,
211
+ 'top_step_by_norm':int(key_idx[0]+1),'top_step_gradient_l2':values[key_idx[0]],
212
+ 'early_steps_1_3_sum_norm':float(sum(values[:3])),
213
+ 'late_steps_9_12_sum_norm':float(sum(values[8:])),
214
+ 'elapsed_s':time.perf_counter()-tic})
215
+ for p in head.parameters():p.requires_grad_(False)
216
+ return result,steps,layers
217
+
218
+ def run(parent,r6zip,r9zip,fix2zip,work,output,smoke=False,device='cpu'):
219
+ work=Path(work);output=Path(output);work.mkdir(parents=True,exist_ok=True);output.mkdir(parents=True,exist_ok=True)
220
+ if sha(fix2zip)!=PREV_FIX2_SHA:raise RuntimeError('B2R10_FIX2_EXACT_EVIDENCE_SHA_MISMATCH')
221
+ with zipfile.ZipFile(fix2zip) as z:
222
+ if z.testzip():raise RuntimeError('B2R10_FIX2_EVIDENCE_CRC_ERROR')
223
+ rep=json.loads(z.read('report.json'))
224
+ if rep['status']!='FROZEN_GRADIENT_CREDIT_AUDIT_COMPLETED_NO_TRAINING' or rep['rows']!=18:
225
+ raise RuntimeError('B2R10_FIX2_EVIDENCE_NOT_FULL')
226
+ gate=json.loads(z.read('gradient_qualification_gate.json'))
227
+ if gate['numerical_status']!='SEVERE_GRADIENT_AMPLIFICATION_NO_TRAINING':raise RuntimeError('FIX2_EVIDENCE_NOT_MATCHED')
228
+ credit.validate_r6(r6zip);credit.validate_r9(r9zip)
229
+ rows=[];perlead=[];ops=[];cases=[]
230
+ if smoke:
231
+ cfg=Config(profile='smoke')
232
+ a=make_synthetic_cache(cfg);b=make_synthetic_cache(cfg)
233
+ for split in ('train','val','test'):
234
+ a['splits'][split]['h']=a['splits'][split]['h'].mean(dim=-2)
235
+ a['splits'][split]['y']=a['splits'][split]['y'].mean(dim=-2)
236
+ a['splits'][split]['h']=a['splits'][split]['h'][...,:4]
237
+ a['splits'][split]['y']=a['splits'][split]['y'][...,:4]
238
+ b['splits'][split]['h']=b['splits'][split]['h'][...,:2,:4]
239
+ b['splits'][split]['y']=b['splits'][split]['y'][...,:2,:4]
240
+ for task,cache in [('smoke_1d',a),('smoke_2d',b)]:
241
+ core=UnifiedModel('V2_Ported_R5',1,8,12,4).to(device)
242
+ res,st,op=analyze_case(task,core,cache,r6zip,device,
243
+ head_types=('operator',) if task=='smoke_1d' else ('rotated',),
244
+ max_train_examples=1)
245
+ rows.extend(res);perlead.extend(st);ops.extend(op)
246
+ status='PROGRAM_ONLY_SMOKE_NO_SCIENCE'
247
+ else:
248
+ if parent is None or sha(parent)!=PARENT_SHA:raise RuntimeError('PARENT_SHA_MISMATCH')
249
+ with zipfile.ZipFile(parent) as z:
250
+ manifest,contract=check_zip(z);cfg=Config(**manifest['config'])
251
+ for task in TASK_LIST:
252
+ core,cache,replay=make_cache_checked(task,z,cfg,work,device)
253
+ try:
254
+ res,st,op=analyze_case(task,core,cache,r6zip,device)
255
+ except Exception as e:
256
+ write_json(output/'failure_case.json',{'task':task,'exception':repr(e),'already_completed':cases})
257
+ raise
258
+ rows.extend(res);perlead.extend(st);ops.extend(op)
259
+ cases.append({'task':task,'parent_replay':replay,'rows':len(res),'exact_model_frozen':True})
260
+ write_csv(output/'temporal_gradient_contributions.csv',perlead)
261
+ write_csv(output/'operator_local_vjp.csv',ops)
262
+ write_json(output/'progress.json',{'completed':[x['task'] for x in cases]})
263
+ if any(r['head_partition_status']!='PASS' for r in rows):
264
+ status='FROZEN_ATTRIBUTION_DATA_COLLECTED_NUMERIC_GATE_FAIL_NO_TRAINING'
265
+ else:
266
+ status='FROZEN_BACKWARD_PATH_ATTRIBUTION_COMPLETED_NO_TRAINING'
267
+ if not smoke and (len(rows)!=6 or len(perlead)!=72 or len(ops)!=72):
268
+ raise RuntimeError(f'INCOMPLETE_BACKWARD_PATH_AUDIT_{len(rows)}_{len(perlead)}_{len(ops)}')
269
+ write_csv(output/'temporal_gradient_contributions.csv',perlead)
270
+ write_csv(output/'operator_local_vjp.csv',ops)
271
+ write_csv(output/'case_summary.csv',rows)
272
+ risk=[r for r in rows if r['full_gradient_l2_fp64']>1e8]
273
+ unresolved=[r for r in rows if r['head_partition_status']!='PASS']
274
+ gate={'status':'DIAGNOSTIC_ONLY_NO_TRAINING','completed_groups':len(rows),
275
+ 'gradient_large_cases':risk,'head_partition_unresolved':unresolved,
276
+ 'strict_numerical_gate_pass':len(unresolved)==0,'train_allowed':False,
277
+ 'note':'Relative VJP norms and parameter contribution amplitudes are diagnostic, not causal branch ablations or Jacobian spectral norms.'}
278
+ write_json(output/'gradient_path_gate.json',gate)
279
+ report={'experiment':'CIDM_v2_B2_R11_P0_BackwardPathAttribution',
280
+ 'status':status,'cases':cases,'number_of_controllers':len(rows),
281
+ 'temporal_rows':len(perlead),'vjp_rows':len(ops),
282
+ 'original_fix2_sha256':PREV_FIX2_SHA,
283
+ 'training_updates':0,'no_outer_holdout':True,'train_allowed':False,
284
+ 'strict_numerical_gate_pass':len(unresolved)==0,
285
+ 'attribution':'exact decomposition of the frozen head gradient and local VJPs; no weights changed'}
286
+ write_json(output/'report.json',report)
287
+ return report