import unittest import numpy as np import torch from torch import nn from s2_pilot_train import part_weights,train_part,component_score,select_capacity class TinyQueryModel(nn.Module): arm='N' def __init__(self):super().__init__();self.linear=nn.Linear(2,1) def forward(self,base,ids,gaps,edge):return self.linear(base[ids]).squeeze(-1) class PilotTrainChecks(unittest.TestCase): def test_step_weights_expectation_and_component_balance(self): # Two components with 1 and 2 parts; total global contribution is one. weights=[part_weights([1,3],n,2,3) for n in [1,2,2]] self.assertAlmostEqual(sum(w.sum() for w in weights)/3,1.) self.assertAlmostEqual(weights[0].sum(),weights[1].sum()+weights[2].sum()) def test_synthetic_cpu_update_and_nonfinite_no_update(self): torch.manual_seed(0);m=TinyQueryModel();o=torch.optim.AdamW(m.parameters(),lr=1e-3) b=torch.tensor([[1.,2.],[2.,3.]]);ids=torch.tensor([0,1]);g=torch.zeros(2,2);w=torch.tensor([.25,.75]);target=torch.tensor([1.,2.]) before=m.linear.weight.detach().clone();loss=train_part(m,o,b,ids,g,None,target,w) self.assertTrue(np.isfinite(loss));self.assertFalse(torch.equal(before,m.linear.weight)) before=m.linear.weight.detach().clone() with self.assertRaises(ValueError):train_part(m,o,b,ids,g,None,torch.tensor([float('nan'),2.]),w) self.assertTrue(torch.equal(before,m.linear.weight)) def test_validation_component_weight_and_capacity_tie(self): self.assertEqual(component_score({'a':1.,'b':3.,'c':5.},{'a':'x','b':'y','c':'y'}),2.5) self.assertEqual(select_capacity({('N',128):1.,('E',128):3.,('N',256):2.,('E',256):2.}),128) with self.assertRaises(ValueError):select_capacity({('N',128):1.}) with self.assertRaises(ValueError):component_score({'a':1.},{'a':'x','b':'y'}) def test_actual_four_model_variants_backward_on_synthetic_nodes(self): from s2_models.normalization import fit_base_standardizer from s2_models.network import QueryModel values=np.arange(90,dtype=float).reshape(9,10)/90 stats=fit_base_standardizer({'synthetic':values},learning_part_ids=['synthetic'],fold='synthetic',pool='100') for arm in ['N','E']: for width in [128,256]: torch.manual_seed(0);m=QueryModel(arm,width,standardizer=stats) o=torch.optim.AdamW(m.parameters(),lr=1e-3,weight_decay=1e-5) before=m.decoder[-1].weight.detach().clone() loss=train_part(m,o,torch.tensor(values,dtype=torch.float32),torch.tensor([1,3]),torch.ones(2,2)*.1,torch.ones(2,4)*.1,torch.tensor([.2,.8]),torch.tensor([.5,.5])) self.assertTrue(np.isfinite(loss));self.assertFalse(torch.equal(before,m.decoder[-1].weight)) if __name__=='__main__':torch.set_num_threads(1);unittest.main(verbosity=2)