"""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 @torch.no_grad() 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]=2 and limit >=1 required') run(a.model,a.weights,a.tasks,a.output,a.groups,a.limit)