Vendor pinned MeZO, AGZO, LOZO and LoREN sources with independent environments. Add direct in-place fix/adjust/node training, derived history-free basis gradients, development-best checkpoints and experiment tooling. Keep models, datasets, caches and credentials outside Git. Validation: 93 project tests passed; 521 vendored author files verified.
51 lines
2.7 KiB
Python
51 lines
2.7 KiB
Python
"""Small numerical checks, not pretrained-model performance evidence."""
|
|
from types import SimpleNamespace
|
|
import copy
|
|
import torch
|
|
from transformers import OPTConfig,OPTForCausalLM
|
|
from experiments_codebase.task import Task,top_basis
|
|
|
|
def tiny():
|
|
torch.manual_seed(42)
|
|
t=Task.__new__(Task);t.device=torch.device('cpu')
|
|
t.args=SimpleNamespace(task='sst2',mu=.001,history_beta=.9,node_queries=2,node_epsilon=.001)
|
|
t.model=OPTForCausalLM(OPTConfig(vocab_size=32,hidden_size=16,ffn_dim=24,num_hidden_layers=3,num_attention_heads=2,max_position_embeddings=32,dropout=0,attention_dropout=0)).eval()
|
|
t.named_parameters=list(t.model.named_parameters());t.parameter_map=dict(t.named_parameters);t.parameters=list(t.parameter_map.values())
|
|
t.linear_map={n+'.weight':m for n,m in t.model.named_modules() if isinstance(m,torch.nn.Linear) and n+'.weight' in t.parameter_map}
|
|
t.label_ids=[5,7];t.counts=dict(training=0,calibration=0,evaluation=0,diagnostic=0)
|
|
t.structured={};t.noise=None;t.handles=[];t.history={}
|
|
inputs=dict(input_ids=torch.tensor([[2,4,6,3],[2,5,9,3]]),attention_mask=torch.ones(2,4,dtype=torch.long),labels=torch.tensor([0,1]))
|
|
return t,inputs
|
|
|
|
@torch.no_grad()
|
|
def test_basis_restoration_and_switch():
|
|
t,x=tiny();t.configure(x);clean=t.call(x,'diagnostic')[0].clone();old=t.dense_snapshot()
|
|
t.shifted(x,t.bases(),t.directions(91),.001,old)
|
|
assert all(torch.equal(t.parameter_map[n],v) for n,v in old.items())
|
|
torch.testing.assert_close(clean,t.call(x,'diagnostic')[0],rtol=0,atol=0)
|
|
t.update(x,33,1e-5);before=t.call(x,'diagnostic')[0].clone()
|
|
t.switch({n:-d for n,d in t.bases().items()})
|
|
torch.testing.assert_close(before,t.call(x,'diagnostic')[0],rtol=0,atol=0)
|
|
t.close()
|
|
|
|
@torch.no_grad()
|
|
def test_same_basis_probe_has_zero_gain_and_five_queries():
|
|
t,x=tiny();t.configure(x);before=t.counts['training'];old=[p.clone() for p in t.parameters]
|
|
event=t.probe(x,x,t.bases(),17,1e-5)
|
|
assert event['gain']==0 and t.counts['training']-before==5
|
|
assert all(torch.equal(p,v) for p,v in zip(t.parameters,old));t.close()
|
|
|
|
@torch.no_grad()
|
|
def test_node_refresh_preserves_function_and_charges_seven_queries():
|
|
t,x=tiny();t.configure(x);t.configure_history();t.update(x,18,1e-5)
|
|
before=t.call(x,'diagnostic')[0].clone();q=t.counts['training'];event=t.node_refresh(x,19)
|
|
assert t.counts['training']-q==7
|
|
assert len(event)==3
|
|
torch.testing.assert_close(before,t.call(x,'diagnostic')[0],rtol=0,atol=0);t.close()
|
|
|
|
def test_pca_order_and_orthogonality():
|
|
x=torch.tensor([[9.,0,0,0],[0,2.,0,0],[0,0,1.,0]])
|
|
d=top_basis(x,2)
|
|
torch.testing.assert_close(d.T@d,torch.eye(2))
|
|
assert abs(float(d[0,0]))>.9999 and abs(float(d[1,1]))>.9999
|