import importlib.util,json,math,sys from pathlib import Path from unittest.mock import patch import torch,torch.nn.functional as F from PIL import Image root=Path(__file__).resolve().parent;torch.set_num_threads(4);sp=importlib.util.spec_from_file_location('_hy4_attn_check',root/'component.py');m=importlib.util.module_from_spec(sp);sys.modules[sp.name]=m;sp.loader.exec_module(m);model=m.load_model(root,dtype=torch.float32);proc=m.load_processor(root);batch=proc(images=Image.open(sys.argv[1]).convert('RGB'),return_tensors='pt');grid=batch['image_grid_thw'];h,w=grid[0].tolist()[1:];orig=F.scaled_dot_product_attention;calls=[0];metrics={} def capture(q,k,v,*args,**kwargs): out=orig(q,k,v,*args,**kwargs);layer=calls[0];calls[0]+=1 if layer not in (0,13,26):return out mask=kwargs.get('attn_mask',args[0] if args else None);assert mask is None and q.shape==(1,16,h*w,72) and q.shape==k.shape==v.shape and not kwargs.get('is_causal',False) scale=kwargs.get('scale') or q.shape[-1]**-.5;p=(q@k.transpose(-2,-1)*scale).softmax(-1);ref=p@v;err=float((ref-out).abs().max());assert torch.allclose(ref,out,rtol=2e-4,atol=2e-5);entropy=float((-(p*p.clamp_min(1e-30).log()).sum(-1).mean()/math.log(h*w)));mass=float((p.sum(-1)-1).abs().max());metrics[str(layer)]={'query_normalized_entropy':entropy,'explicit_vs_sdpa_max_abs_error':err,'max_attention_row_sum_error':mass,'heads':16,'grid':[h,w]};return out with patch.object(F,'scaled_dot_product_attention',side_effect=capture): with torch.inference_mode():y=model(pixel_values=batch['pixel_values'],grid_thw=grid) assert calls[0]==27 and len(metrics)==3 v=json.loads((root/'validation_local.json').read_text());v['attention_check']={'image':Path(sys.argv[1]).name,'layers_zero_based':[0,13,26],'method':'full model native SDPA; explicitly reconstructed softmax(QK^T)×V compared at selected layers; one real image; CPU float32','metrics':metrics};(root/'validation_local.json').write_text(json.dumps(v,indent=2)+'\n');print(json.dumps(v['attention_check'],indent=2))