Record ten-part results and fix bounded pilot timing and AMP handling

This commit is contained in:
Frank14f committed 2026-11-15 03:02:29 +08:00
1 parent f8a6ed3f81
commit cdb52988c3
42 files changed
+31677 -77

No files matched your search

+28 -1
View File
@@ -2,7 +2,7 @@ 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
from s2_pilot_train import part_weights,train_part,component_score,select_capacity,SkipPolicy,common_epoch_limit
class TinyQueryModel(nn.Module):
@@ -12,6 +12,33 @@ class TinyQueryModel(nn.Module):
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]]