-
Notifications
You must be signed in to change notification settings - Fork 2
/
shared_storage.py
76 lines (58 loc) · 2.22 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
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
import copy
import os
import ray
import torch
from typing import Dict
from base_config import BaseConfig
@ray.remote
class SharedStorage:
"""
Class which run in a dedicated process to store the network weights and some information about played games.
"""
def __init__(self, checkpoint: Dict, config: BaseConfig):
self.config = config
self.current_checkpoint = copy.deepcopy(checkpoint)
# Variables for evaluation mode
self.evaluate_list = [] # Stores flowrates which should be evaluated
self.evaluation_results = []
self.evaluation_mode = False
def save_checkpoint(self, filename: str):
os.makedirs(self.config.results_path, exist_ok=True)
path = os.path.join(self.config.results_path, filename)
torch.save(self.current_checkpoint, path)
def set_checkpoint(self, checkpoint: Dict):
self.current_checkpoint = copy.deepcopy(checkpoint)
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
def in_evaluation_mode(self):
return self.evaluation_mode
def set_evaluation_mode(self, value: bool):
self.evaluation_mode = value
def get_to_evaluate(self):
if len(self.evaluate_list) > 0:
item = self.evaluate_list.pop()
return copy.deepcopy(item)
else:
return None
def set_to_evaluate(self, evaluate_list):
self.evaluate_list = evaluate_list
def push_evaluation_result(self, eval_result):
self.evaluation_results.append(eval_result)
def fetch_evaluation_results(self):
results = self.evaluation_results
self.evaluation_results = []
return results