File size: 3,229 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
import json,tempfile,unittest
from pathlib import Path
import torch
from robottt_pi05.config import ExperimentConfig
from robottt_pi05.prepare import prepare_protocol
from robottt_pi05.assets import SavedSchedule
from robottt_pi05.eval_io import sha
from robottt_pi05.sequence_trainer import SequenceTrainer,full_masks
from test_sequence import TinyRecurrent

class ProtocolTests(unittest.TestCase):
    def test_t32_s8_masks_scaling_and_detach(self):
        config=ExperimentConfig(sequence_length=32,segment_length=8,observation_stride=1,learn_inner_lr=True,loss_action_dim=7)
        torch.manual_seed(42)
        actions=torch.randn(1,32,10,32,dtype=torch.float64);epsilon=torch.randn_like(actions)
        times=torch.full((1,32,1,1),.5,dtype=torch.float64)
        outputs=[]
        for segment in (8,32):
            adapter=TinyRecurrent();masks=full_masks(1,32,10)
            masks.action_dimension=(torch.arange(32)<7)[None]
            out=SequenceTrainer(adapter).run([None]*32,actions,epsilon,times,masks,segment_length=segment,backward=lambda x:x.backward())
            self.assertEqual(out['valid_scalar_count'],32*10*7)
            expected=(out['prediction'][...,:7]-(epsilon-actions)[...,:7]).square().mean()
            self.assertAlmostEqual(out['loss'],float(expected),places=12)
            self.assertEqual(out['state']['0']['updates'].tolist(),[32])
            self.assertTrue(all(r['denominator']==2240 for r in out['segments']))
            outputs.append((out,adapter.w.grad.clone()))
        torch.testing.assert_close(outputs[0][0]['prediction'],outputs[1][0]['prediction'],rtol=0,atol=0)
        self.assertNotEqual(float(outputs[0][1]),float(outputs[1][1]))
        self.assertEqual(config.expected_trainables,(561540,41))

    def test_new_protocol_roundtrip_and_seed(self):
        with tempfile.TemporaryDirectory() as tmp:
            d=Path(tmp);cp=d/'config.json';index=d/'index.json'
            cp.write_text(json.dumps(dict(sequence_length=32,segment_length=8,observation_stride=1,learn_inner_lr=True,loss_action_dim=7)))
            index.write_text(json.dumps(dict(train_ids=[0],episodes={'0':{'manifest':{'rows':list(range(45))}}})))
            rng=torch.get_rng_state().clone()
            for name in ('a','b'):
                prepare_protocol(config_path=cp,asset_index=index,output=d/name,updates=3,seed=1000)
                bank=SavedSchedule(d/name/'schedule.json',d/name/'bank.pt',schedule_sha256=sha(d/name/'schedule.json'),bank_sha256=sha(d/name/'bank.pt'),train_ids=[0],sequence_length=32,stride=1)
                self.assertEqual(len(bank),3)
                self.assertEqual(bank[0][1].shape,(1,32,10,32))
                self.assertTrue(bool((bank[0][2]>=.001).all() & (bank[0][2]<=1).all()))
            self.assertTrue(torch.equal(rng,torch.get_rng_state()))
            a=torch.load(d/'a/bank.pt',weights_only=True);b=torch.load(d/'b/bank.pt',weights_only=True)
            for k in a:torch.testing.assert_close(a[k],b[k],rtol=0,atol=0)
            self.assertEqual((d/'a/schedule.json').read_text(),(d/'b/schedule.json').read_text())
            with self.assertRaises(FileExistsError):prepare_protocol(config_path=cp,asset_index=index,output=d/'a',updates=3)