"""Independent existing-LoRA arithmetic, head decomposition and runtime checks.""" import json,struct,tempfile,unittest from pathlib import Path import numpy as np from tests.test_dominance import fixture from dominance.core import InfluenceLM from finetune_compare.lora import install,EffectiveWeight,ExplicitLoRA,BASE_REVISION from functional.model import StreamingLM def adapter(directory,cfg,lm): rng=np.random.default_rng(772);arrays={} for layer,kind in enumerate(cfg['layer_types']): if kind!='linear_attention':continue for role in ('in_proj_qkv','in_proj_a','in_proj_b','out_proj'): shape=lm.tensors[f'model.language_model.layers.{layer}.linear_attn.{role}.weight'].shape prefix=f'base_model.model.model.layers.{layer}.linear_attn.{role}.' arrays[prefix+'lora_A.default.weight']=rng.standard_normal((2,shape[1]),dtype=np.float32)*.1 arrays[prefix+'lora_B.default.weight']=rng.standard_normal((shape[0],2),dtype=np.float32)*.1 header={};body=b'' for name,x in arrays.items(): raw=x.tobytes();header[name]={'shape':list(x.shape),'dtype':'F32','data_offsets':[len(body),len(body)+len(raw)]};body+=raw h=json.dumps(header).encode();Path(directory,'adapter.safetensors').write_bytes(struct.pack('