Files
MeltMind/tools/test_s2_pilot_train.py
T

75 lines
4.7 KiB
Python

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,SkipPolicy,common_epoch_limit
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_one_forced_amp_overflow_skips_then_continues(self):
torch.manual_seed(0);m=TinyQueryModel();o=torch.optim.AdamW(m.parameters(),lr=1e-3)
scaler=torch.amp.GradScaler('cpu',init_scale=2.)
b=torch.tensor([[1.,2.],[2.,3.]]);ids=torch.tensor([0,1]);g=torch.zeros(2,2);w=torch.tensor([.5,.5]);target=torch.ones(2)
before=m.linear.weight.detach().clone();hook=m.linear.weight.register_hook(lambda grad:torch.full_like(grad,float('inf')))
first={};train_part(m,o,b,ids,g,None,target,w,scaler=scaler,step_info=first);hook.remove()
self.assertTrue(first['step_skipped']);self.assertLess(first['scale_after'],first['scale_before']);self.assertTrue(torch.equal(before,m.linear.weight))
second={};train_part(m,o,b,ids,g,None,target,w,scaler=scaler,step_info=second)
self.assertFalse(second['step_skipped']);self.assertFalse(torch.equal(before,m.linear.weight))
def test_skip_fraction_end_of_epoch_and_consecutive_limit(self):
p=SkipPolicy()
for i in range(100):p.observe(i==0)
p.finish_epoch(1)
p.start_epoch()
for i in range(100):p.observe(i in [0,2])
p.finish_epoch(0)
with self.assertRaises(FloatingPointError):p.finish_epoch(1)
p=SkipPolicy()
for _ in range(20):p.observe(True)
with self.assertRaises(FloatingPointError):p.observe(True)
def test_common_timing_epoch_limit_frozen_for_all_runs(self):
result=common_epoch_limit(.001,.0005,38700,11200)
self.assertEqual(result['E_max'],33);self.assertEqual(result['cosine_T_max'],33)
self.assertAlmostEqual(result['projected_epoch_seconds'],44.3)
self.assertEqual(result['learning_nodes'],38700);self.assertEqual(result['validation_nodes'],11200)
self.assertEqual(common_epoch_limit(.001,.001,387,112)['E_max'],200)
with self.assertRaises(ValueError):common_epoch_limit(10,1,387,112)
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)