File size: 2,479 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
import tempfile,unittest,json,inspect
from pathlib import Path
import numpy as np
import torch
from robottt_pi05.assets import ProcessedSequenceSource,SavedSchedule
from robottt_pi05.eval_io import sha

class AssetsTests(unittest.TestCase):
    def test_explicit_allowlist_and_source_index(self):
        with tempfile.TemporaryDirectory() as tmp:
            d=Path(tmp);episode=d/'ep';episode.mkdir()
            rows=list(range(0,180,10));fields={'state':'state.npy','tokenized_prompt':'prompt.npy'}
            np.save(episode/'state.npy',np.arange(18*32,dtype=np.float32).reshape(18,32))
            np.save(episode/'prompt.npy',np.zeros((18,4),np.int64))
            target=np.arange(18*10*32,dtype=np.float32).reshape(18,10,32);np.save(episode/'targets.npy',target)
            source=ProcessedSequenceSource({'1':{'folder':'ep','manifest':{'rows':rows,'fields':fields}}},root=d,allowed_episodes=[1])
            train=source.training(1,20);gen=source.generation(1,20)
            self.assertEqual(tuple(train['actions'].shape),(1,16,10,32))
            self.assertTrue(np.array_equal(train['actions'][0].numpy(),target[2:18]))
            self.assertNotIn('actions',gen);self.assertNotIn('target',gen)
            self.assertEqual(set(gen['observations'][0]),set(fields))
            with self.assertRaises(PermissionError):source.training(2,0)
            with self.assertRaises(ValueError):source.training(1,30)
            view=source.training(1,20,T=4);self.assertTrue(torch.equal(view['actions'],train['actions'][:,:4]))
    def test_saved_schedule_does_not_sample(self):
        with tempfile.TemporaryDirectory() as tmp:
            d=Path(tmp);sp=d/'schedule.json';bp=d/'bank.pt'
            sp.write_text(json.dumps([{'index':0,'episode':1,'split':'train','start':0,'source_rows':list(range(0,160,10))}]))
            eps=torch.arange(5120,dtype=torch.float32).reshape(1,1,16,10,32)/5120
            time=torch.arange(16,dtype=torch.float32).reshape(1,1,16,1,1)/16
            torch.save({'training_epsilon':eps,'training_time':time},bp);before=torch.get_rng_state().clone()
            bank=SavedSchedule(sp,bp,schedule_sha256=sha(sp),bank_sha256=sha(bp),train_ids=[1])
            self.assertTrue(torch.equal(bank[0][1],eps[0]));self.assertTrue(torch.equal(torch.get_rng_state(),before))
            with self.assertRaises(PermissionError):SavedSchedule(sp,bp,schedule_sha256=sha(sp),bank_sha256=sha(bp),train_ids=[2])

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