Add Control v2 incremental experiment results (evidence, visuals, 42 fixed samples)
a64600b verified Download method_v2/evidence/code/suite/probe.py from CharlesZhang-USTC/snf_experiments: direct link, hf CLI and curl.
- Browser
- Download file 5.74 kB
-
https://huggingface.co/CharlesZhang-USTC/snf_experiments/resolve/main/method_v2/evidence/code/suite/probe.py
- Command line
-
hf download hf://CharlesZhang-USTC/snf_experiments/method_v2/evidence/code/suite/probe.py
-
curl -L -o probe.py https://huggingface.co/CharlesZhang-USTC/snf_experiments/resolve/main/method_v2/evidence/code/suite/probe.py
5.74 kB
| """Exploratory fixed-state coupling probe for masked generators. | |
| The score is a nonlinear KL proxy, NOT Gaussian total_I or a conditional | |
| covariance theorem. No production sample/alpha curve is used to fit the score. | |
| Reference generation (when requested) is counted separately from probe NFE. | |
| """ | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| import torch | |
| from .design import ROOT,read_tasks | |
| from .results import atomic_json | |
| def run(model,weights,tasks_file,output,groups=16,limit=8): | |
| from .adapters import LOADERS,target_mask | |
| from huggingface_hub import snapshot_download | |
| cfg=json.loads((ROOT/'suite/models.json').read_text())[model] | |
| if model!='momask': | |
| lock=json.loads((Path(weights)/'weights.lock.json').read_text()) | |
| for k,v in list(cfg.items()): | |
| if k.endswith('repo'):cfg[k]=snapshot_download(v,revision=lock[v],local_files_only=True) | |
| else: | |
| original=torch.load | |
| def load(*a,**kw):kw.setdefault('weights_only',False);return original(*a,**kw) | |
| torch.load=load | |
| adapter=LOADERS[model](cfg,weights);out=Path(output);out.mkdir(parents=True,exist_ok=True);records=[] | |
| for task in [t for t in read_tasks(tasks_file) if model in t.get('models',[]) and t.get('split','test')=='test'][:limit]: | |
| _,ref=adapter.reference(task);ref=ref.reshape(1,-1) | |
| if model=='showo': | |
| mask_id=adapter.model.config.mask_token_id;side=int(np.sqrt(ref.numel()));shape=(side,side) | |
| predict=lambda ids:adapter.predict(task['prompt'],ids) | |
| elif model=='momask': | |
| mask_id=adapter.model.mask_id;shape=(ref.shape[-1],);vector=adapter.model.encode_text([task['prompt']]);padding=torch.zeros_like(ref,dtype=torch.bool) | |
| predict=lambda ids:adapter.model.forward_with_cond_scale(ids,cond_vector=vector,padding_mask=padding,cond_scale=cfg['guidance']).permute(0,2,1) | |
| else: | |
| shape=(int(np.sqrt(ref.numel())),)*2;mask_id=adapter.pipe.scheduler.config.mask_token_id;holder={} | |
| class Captured(Exception):pass | |
| def capture(module,args,kw):holder.update(kw);raise Captured() | |
| handle=adapter.pipe.transformer.register_forward_pre_hook(capture,with_kwargs=True) | |
| try:adapter.pipe(prompt=task['prompt'],height=cfg['resolution'],width=cfg['resolution'],guidance_scale=cfg['guidance'],num_inference_steps=cfg['steps']) | |
| except Captured:pass | |
| finally:handle.remove() | |
| def predict(ids): | |
| kw=dict(holder);x=ids.reshape(1,*shape);kw['hidden_states']=torch.cat([x,x]);kw['timestep']=torch.tensor([cfg['steps']//2],device=x.device) | |
| u,c=adapter.pipe.transformer(**kw).chunk(2);logits=u+cfg['guidance']*(c-u) | |
| return logits.reshape(1,logits.shape[1],-1).permute(0,2,1) | |
| support=target_mask(shape,task).reshape(-1);n=ref.numel();count=min(groups,n) | |
| chunks=[torch.tensor(a,device=ref.device) for a in np.array_split(np.arange(n),count)] | |
| masked=torch.full_like(ref,mask_id);base=predict(masked).float().log_softmax(-1);confidence=base.exp().max(-1).values[0] | |
| coupling=np.zeros((count,count),dtype='float64') | |
| for i,index in enumerate(chunks): | |
| visible=masked.clone();visible[:,index]=ref[:,index];changed=predict(visible).float().log_softmax(-1) | |
| kl=(base.exp()*(base-changed)).sum(-1).clamp_min(0)[0] | |
| for j,targets in enumerate(chunks):coupling[i,j]=float(kl[targets].mean()) | |
| target_fraction=np.array([float(support[c].mean()) for c in chunks]);conf=np.array([float(confidence[c].mean()) for c in chunks]) | |
| random=np.random.default_rng(5678).random(count) | |
| priorities=dict(confidence=-conf,random=random,target_first=-target_fraction+random*.001,target_last=target_fraction+random*.001) | |
| scores={} | |
| for order,priority in priorities.items(): | |
| permutation=np.argsort(priority,kind='stable');rank=np.argsort(permutation) | |
| # Source group committed before target group; weight by target support. | |
| scores[order]=float(sum(coupling[i,j]*target_fraction[j]*(rank[i]<rank[j]) for i in range(count) for j in range(count))) | |
| folder=out/task['id'];folder.mkdir(exist_ok=True);np.save(folder/'coupling.npy',coupling) | |
| import matplotlib;matplotlib.use('Agg') | |
| import matplotlib.pyplot as plt | |
| fig,ax=plt.subplots(figsize=(5,4));im=ax.imshow(coupling);fig.colorbar(im,ax=ax,label='Fixed-state KL influence');ax.set(xlabel='Affected group',ylabel='Revealed reference group');fig.tight_layout();fig.savefig(folder/'coupling.png',dpi=180);plt.close(fig) | |
| records.append(dict(task=task['id'],model=model,score_name='fixed_state_KL_order_proxy',scores=scores,probe_model_evaluations=1+count, | |
| reference_kind=task.get('reference_kind'),conditioning='all MASK, one revealed reference group; Meissonic fixed middle timestep', | |
| caveat='confidence order uses initial confidence only; this is not exact total_I or a measured prior covariance')) | |
| atomic_json(out/'probe.json',records) | |
| if not records:raise ValueError('No matching test tasks') | |
| if __name__=='__main__': | |
| p=argparse.ArgumentParser();p.add_argument('--model',choices=('meissonic','momask','showo'),required=True);p.add_argument('--weights',required=True);p.add_argument('--tasks',required=True);p.add_argument('--output',required=True);p.add_argument('--groups',type=int,default=16);p.add_argument('--limit',type=int,default=8);a=p.parse_args() | |
| if a.groups<2 or a.limit<1:raise ValueError('groups >=2 and limit >=1 required') | |
| run(a.model,a.weights,a.tasks,a.output,a.groups,a.limit) | |