"""Independent algebra, mixed-layer canonical, and joint-mask intervention checks.""" import json,struct,tempfile,unittest from pathlib import Path from unittest.mock import patch import numpy as np from dominance.core import InfluenceLM,concentration,masks_from_influences,js_rows from dominance.run import distances,link from functional.model import StreamingLM,delta_rule,attention from crossqkv.core import QKVModel def fixture(path,kinds): rng=np.random.default_rng(922);h=16;nh=4;dim=4 c={'rms_norm_eps':1e-6,'hidden_size':h,'head_dim':dim,'num_attention_heads':nh,'num_key_value_heads':2,'rope_parameters':{'rope_theta':10000000.,'partial_rotary_factor':.5},'linear_num_key_heads':2,'linear_num_value_heads':4,'linear_key_head_dim':4,'linear_value_head_dim':4,'layer_types':kinds} a={} def add(name,shape):a['model.language_model.'+name]=(rng.standard_normal(shape)*.15).astype(np.float32) add('embed_tokens.weight',(19,h));add('norm.weight',(h,)) for l,kind in enumerate(kinds): p=f'layers.{l}.' for k in ('input_layernorm','post_attention_layernorm'):add(p+k+'.weight',(h,)) for k,shape in [('gate_proj',(24,h)),('up_proj',(24,h)),('down_proj',(h,24))]:add(p+'mlp.'+k+'.weight',shape) if kind=='full_attention': for k,shape in [('q_proj',(32,h)),('k_proj',(8,h)),('v_proj',(8,h)),('o_proj',(h,16)),('q_norm',(4,)),('k_norm',(4,))]:add(p+'self_attn.'+k+'.weight',shape) else: for k,shape in [('in_proj_qkv',(32,h)),('in_proj_z',(16,h)),('in_proj_a',(4,h)),('in_proj_b',(4,h)),('out_proj',(h,16)),('conv1d',(32,1,4)),('norm',(4,))]:add(p+'linear_attn.'+k+'.weight',shape) add(p+'linear_attn.A_log',(4,));add(p+'linear_attn.dt_bias',(4,)) header={};body=b'' for k,v in a.items(): raw=v.tobytes();header[k]={'shape':list(v.shape),'dtype':'F32','data_offsets':[len(body),len(body)+len(raw)]};body+=raw j=json.dumps(header).encode();Path(path,'model.safetensors').write_bytes(struct.pack('