File size: 3,391 Bytes
6bc6dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""CPU smoke test with two 224x224 RGB images and three attention parity checks."""
import hashlib, json, math, sys
from pathlib import Path
import torch
import torch.nn.functional as F
from PIL import Image
from transformers import Qwen2VLImageProcessor
from component import load_vision_model

def sha(path):
 h=hashlib.sha256()
 with open(path,'rb') as f:
  for b in iter(lambda:f.read(8*1024*1024),b''):h.update(b)
 return h.hexdigest()

def main():
 root=Path(__file__).resolve().parent;images=[Path(x) for x in sys.argv[1:]];assert len(images)>=2
 torch.set_num_threads(4);torch.set_grad_enabled(False)
 model=load_vision_model(root);print('STRICT LOAD OK',flush=True)
 p=json.loads((root/'preprocessor_config.json').read_text())
 processor=Qwen2VLImageProcessor(patch_size=p['patch_size'],temporal_patch_size=p['temporal_patch_size'],merge_size=p['merge_size'],image_mean=p['image_mean'],image_std=p['image_std'],size={'shortest_edge':1024,'longest_edge':224*224})
 original=F.scaled_dot_product_attention;rows=[]
 for path in images:
  image=Image.open(path).convert('RGB').resize((224,224));data=processor(images=image,return_tensors='pt');grid=data['image_grid_thw'];assert grid.tolist()==[[1,14,14]],grid
  calls=[0];metrics={}
  def wrapped(q,k,v,*args,**kwargs):
   out=original(q,k,v,*args,**kwargs);layer=calls[0];calls[0]+=1
   if layer in (0,13,26):
    mask=kwargs.get('attn_mask',args[0] if args else None);assert mask is None and not kwargs.get('is_causal',False) and kwargs.get('dropout_p',0.0)==0
    scale=kwargs.get('scale',None);scale=q.shape[-1]**-.5 if scale is None else scale
    assert q.shape==(1,16,196,72),q.shape
    probs=(q@k.transpose(-2,-1)*scale).softmax(-1);calc=probs@v;err=(calc-out).abs().max().item();assert torch.allclose(calc,out,rtol=2e-4,atol=2e-5),err
    metrics[str(layer)]={'sdpa_max_absolute_error':err,'row_sum_max_error':float((probs.sum(-1)-1).abs().max()),'mean_normalized_entropy':float((-(probs*probs.clamp_min(1e-30).log()).sum(-1)).mean()/math.log(196))}
   return out
  try:
   F.scaled_dot_product_attention=wrapped
   with torch.inference_mode():out=model(data['pixel_values'],grid_thw=grid)
  finally:F.scaled_dot_product_attention=original
  assert calls[0]==27 and len(metrics)==3;assert out.last_hidden_state.shape==(196,1152) and out.pooler_output.shape==(49,4096)
  assert torch.isfinite(out.last_hidden_state).all() and torch.isfinite(out.pooler_output).all()
  rows.append({'image':path.name,'image_sha256':sha(path),'grid_thw':grid.tolist(),'last_hidden_state_shape':list(out.last_hidden_state.shape),'pooler_output_shape':list(out.pooler_output.shape),'pooler_mean':float(out.pooler_output.mean()),'attention_metrics':metrics})
  print('TESTED',path.name,flush=True)
 assert abs(rows[0]['pooler_mean']-rows[1]['pooler_mean'])>1e-8
 import transformers
 result={'passed':True,'strict_load':True,'model_sha256':sha(root/'model.safetensors'),'device':'cpu','dtype':'float32','torch':torch.__version__,'transformers':transformers.__version__,'processor':'Qwen2VLImageProcessor with source normalization and patch settings; testing capped at 224x224','attention_method':'Compare explicit softmax(QK^T/sqrt(d))V to native SDPA after rotary embeddings, layers 0,13,26','images':rows}
 (root/'validation.json').write_text(json.dumps(result,indent=2)+'\n');print('VALIDATION PASSED',flush=True)
if __name__=='__main__':main()