forked from werner-duvaud/muzero-general
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared_storage.py
41 lines (32 loc) · 1.12 KB
/
shared_storage.py
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
import copy
import os
import ray
import torch
@ray.remote
class SharedStorage:
"""
Class which run in a dedicated thread to store the network weights and some information.
"""
def __init__(self, checkpoint, config):
self.config = config
self.current_checkpoint = copy.deepcopy(checkpoint)
def save_checkpoint(self, path=None):
if not path:
path = os.path.join(self.config.results_path, "model.checkpoint")
torch.save(self.current_checkpoint, path)
def get_checkpoint(self):
return copy.deepcopy(self.current_checkpoint)
def get_info(self, keys):
if isinstance(keys, str):
return self.current_checkpoint[keys]
elif isinstance(keys, list):
return {key: self.current_checkpoint[key] for key in keys}
else:
raise TypeError
def set_info(self, keys, values=None):
if isinstance(keys, str) and values is not None:
self.current_checkpoint[keys] = values
elif isinstance(keys, dict):
self.current_checkpoint.update(keys)
else:
raise TypeError