import hashlib,importlib.util,json,math,sys,time from pathlib import Path import numpy as np,torch from PIL import Image root=Path(__file__).resolve().parent;torch.set_num_threads(4);start=time.monotonic();spec=importlib.util.spec_from_file_location('_hy4_extracted',root/'component.py');mod=importlib.util.module_from_spec(spec);sys.modules[spec.name]=mod;spec.loader.exec_module(mod);ex=json.loads((root/'extraction.json').read_text());mapping=json.loads((root/'tensor_mapping.json').read_text());assert len(mapping)==ex['tensor_count']==441 and all(x.startswith('vit.') for x in mapping);targets=[mod.convert_name(x) for x in mapping];assert len(set(targets))==441 print('LOAD model',flush=True);model=mod.load_model(root,dtype=torch.float32);proc=mod.load_processor(root);assert len(model.layers)==27 and all(torch.isfinite(x).all() for x in model.state_dict().values());print('MODEL READY',flush=True) assert len(sys.argv)==3, 'Usage: python validate_local_reference.py IMAGE1 IMAGE2' inputs=[Path(x) for x in sys.argv[1:]];rows=[] for path in inputs: im=Image.open(path).convert('RGB');batch=proc(images=im,return_tensors='pt');grid=batch['image_grid_thw'];assert grid.shape==(1,3);t,h,w=grid[0].tolist();assert t==1 and h%2==w%2==0 and batch['pixel_values'].shape==(h*w,3*16*16) with torch.inference_mode():out=model(pixel_values=batch['pixel_values'],grid_thw=grid);out2=model(pixel_values=batch['pixel_values'],grid_thw=grid) a=out.last_hidden_state;b=out.pooler_output;assert a.shape==(1,h*w,1152) and b.shape==(1,(h//2)*(w//2+1),3072), (a.shape,b.shape,grid);assert torch.isfinite(a).all() and torch.isfinite(b).all();repeat=float((b-out2.pooler_output).abs().max());assert repeat<1e-6;rows.append({'image':path.name,'sha256':hashlib.sha256(path.read_bytes()).hexdigest(),'grid_thw':grid.tolist()[0],'patch_features_shape':list(a.shape),'projected_features_shape':list(b.shape),'output_rms':float(b.square().mean().sqrt()),'repeat_max_abs_error':repeat});print('VALIDATED',path.name,flush=True) # Same spatial token geometry and different images must produce different outputs. a=proc(images=Image.open(inputs[0]).convert('RGB'),return_tensors='pt');b=proc(images=Image.open(inputs[1]).convert('RGB'),return_tensors='pt');assert a['image_grid_thw'].tolist()==b['image_grid_thw'].tolist() with torch.inference_mode():x=model(pixel_values=a['pixel_values'],grid_thw=a['image_grid_thw']).pooler_output;y=model(pixel_values=b['pixel_values'],grid_thw=b['image_grid_thw']).pooler_output delta=float((x-y).abs().mean());assert delta>1e-5 v={'passed':True,'source_revision':ex['source_revision'],'model_sha256':ex['sha256'],'num_tensors':441,'strict_load':True,'native_model_layers':27,'dtype_for_test':'float32 CPU; artifact retains source dtype','runtime':'Local Transformers version '+__import__('transformers').__version__,'weight_key_mapping':'matches vision renames in PR conversion_mapping.py; original file keys unchanged','input_preprocessing':'upstream HYV4FlashImageProcessor from source processor_config.json','images':rows,'different_image_mean_abs_difference':delta,'notes':'Two existing real COCO128 images, deterministic repeat and input sensitivity checks; not a downstream task accuracy or complete multimodal pipeline test.','seconds':round(time.monotonic()-start,1)};(root/'validation_local.json').write_text(json.dumps(v,indent=2)+'\n');print('VALIDATION PASSED',json.dumps(v),flush=True)