File size: 6,386 Bytes
add1d5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
"""CPU synthetic end-to-end orchestration, explicitly not a VLA optimizer smoke."""
import json,tempfile,unittest
from contextlib import ExitStack
from pathlib import Path
from unittest.mock import patch
import torch
from robottt_pi05 import runner
from robottt_pi05.config import ExperimentConfig
from robottt_pi05.modeling import extension
from robottt_pi05.checkpoint import capture_rng
from robottt_pi05.eval_io import atomic_tensor,sha

class CPUModel(torch.nn.Module):
    def __init__(self):super().__init__();self.frozen=torch.nn.Parameter(torch.tensor(1.),requires_grad=False)
    def denoise_step(self,state,pm,cache,x,time):return x*.25

class EmptyAdapter:
    def __init__(self,slow):self.ttt=slow.ttt;self.registers=slow.registers;self.history=[];self.call_contracts=[]

class CPURunnerTests(unittest.TestCase):
    def test_generation_orchestration_without_targets_and_completed_reuse(self):
        with tempfile.TemporaryDirectory() as tmp:
            d=Path(tmp);(d/'config.json').write_text(json.dumps(ExperimentConfig().to_dict()))
            (d/'assets.json').write_text(json.dumps({'train_ids':[1],'dev_ids':[],'prefixes':{}}))
            (d/'plan.json').write_text(json.dumps([{'key':'synthetic_original','episode':1,'noise_id':0,'mode':'persistent','timesteps':list(range(16))}]))
            atomic_tensor(d/'bank.pt',{'evaluation_noise':torch.ones(2,1,16,10,32)})
            paths={'output':'eval_output','experiment_config':'config.json','asset_index':'assets.json','bank':'bank.pt','evaluation_plan':'plan.json','openpi_source':'unused','model_checkpoint':'unused','prefix_root':'.'}
            r={'paths':paths,'hashes':{k:sha(d/paths[k]) for k in ['bank','evaluation_plan']},'limits':{'model_load':1,'expert':160,'inner':0,'rollout':16},'original':True,'gpu_uuid':'synthetic'}
            run=d/'eval.json';run.write_text(json.dumps(r))
            class Prefix:
                def __init__(self,*a,**k):pass
                def get(self,*a):return (torch.zeros(1,1,1),torch.ones(1,1,dtype=torch.bool),torch.zeros(1,1),torch.zeros(1,32),())
            class Guard:
                def __call__(self,*a):pass
            with ExitStack() as stack:
                for name,value in [('_gpu_preflight',lambda *_:None),('load_model',lambda **_:CPUModel()),('FrozenPrefixStore',Prefix),('SafetyGuard',Guard)]:stack.enter_context(patch.object(runner,name,value))
                stack.enter_context(patch.object(torch.Tensor,'cuda',lambda self,*a,**k:self))
                with patch.object(runner,'ProcessedSequenceSource',side_effect=AssertionError('generation must not load targets')):
                    result=runner.evaluate(run)
                    self.assertEqual(result['completed']['expert'],160);self.assertEqual(result['completed']['rollout'],16)
                with patch.object(runner,'load_model',side_effect=AssertionError('completed eval must not reload model')):
                    self.assertEqual(result,runner.evaluate(run))
            self.assertFalse(torch.cuda.is_initialized())

    def test_durable_step_and_completed_skip_without_model_reload(self):
        self.assertFalse(torch.cuda.is_initialized())
        with tempfile.TemporaryDirectory() as tmp:
            d=Path(tmp);cfg=ExperimentConfig();slow=extension(cfg)
            (d/'config.json').write_text(json.dumps(cfg.to_dict()))
            (d/'assets.json').write_text(json.dumps({'train_ids':[1],'dev_ids':[],'episodes':{},'prefixes':{}}))
            initial=d/'initial.pt';atomic_tensor(initial,{'slow':slow.state_dict(),'rng':capture_rng(),'parameter_names':[],'source_initial':'synthetic','source_step0':'synthetic','source_step0_sha256':'synthetic','not_smoke':True})
            schedule=[{'index':0,'episode':1,'split':'train','start':0,'key':'synthetic','source_rows':list(range(0,160,10))}]
            (d/'schedule.json').write_text(json.dumps(schedule));bank=d/'bank.pt'
            atomic_tensor(bank,{'training_epsilon':torch.zeros(1,1,16,10,32),'training_time':torch.full((1,1,16,1,1),.5)})
            limits={'model_load':1,'optimizer_update':1,'backward':4,'expert':0,'inner':0,'rollout':0}
            paths={'output':'output','experiment_config':'config.json','asset_index':'assets.json','initial_extension':'initial.pt','schedule':'schedule.json','bank':'bank.pt','openpi_source':'unused','model_checkpoint':'unused','data_root':'.','prefix_root':'.'}
            r={'paths':paths,'hashes':{k:sha(d/paths[k]) for k in ['initial_extension','schedule','bank']},'limits':limits,'updates':1,'gpu_uuid':'synthetic','milestones':[1]}
            run=d/'run.json';run.write_text(json.dumps(r))
            class Prefix:
                def __init__(self,*args,**kwargs):pass
                def sequence(self,*args):return []
            class Data:
                def __init__(self,*args,**kwargs):pass
                def targets(self,*args):return torch.zeros(1,16,10,32)
            class Guard:
                def __call__(self,*args):pass
                def learning_rates(self,*args):pass
            def synthetic_train(adapter,config,prepared,a,e,t,*,backward):
                # Four shared-parameter synthetic segments exercise actual accumulation/save.
                loss=0.
                for _ in range(4):
                    v=sum(p.square().sum() for p in slow.parameters() if p.requires_grad)/4
                    loss+=float(v.detach());backward(v)
                return {'loss':loss}
            with ExitStack() as stack:
                for name,value in [('_gpu_preflight',lambda *_:None),('load_model',lambda **_:CPUModel()),('create_adapter',lambda *a,**k:(EmptyAdapter(slow),slow)),('FrozenPrefixStore',Prefix),('ProcessedSequenceSource',Data),('SafetyGuard',Guard),('train_sequence',synthetic_train)]:stack.enter_context(patch.object(runner,name,value))
                stack.enter_context(patch.object(torch.Tensor,'cuda',lambda self,*a,**k:self))
                result=runner.train(run)
                self.assertEqual(result['completed']['optimizer_update'],1);self.assertEqual(result['completed']['backward'],4)
                with patch.object(runner,'load_model',side_effect=AssertionError('must not reload completed run')):
                    reused=runner.train(run);self.assertEqual(result,reused)
            self.assertFalse(torch.cuda.is_initialized())

if __name__=='__main__':unittest.main()