-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
196 lines (168 loc) · 4.62 KB
/
main.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
import sys
import time
from pathlib import Path
from typing import Optional
from unittest.result import failfast
from test_gen_util import generate_tests
from pprint import pprint
import ray
import torch
import yaml
from git.repo import Repo
from ray import tune
from agent.arguments import get_args, read_params
from logger import get_charts, get_metadata
from trainer import GNNTrainer, ResumeGNNTrainer
from ray.air.config import RunConfig, ScalingConfig
from ray.air.integrations.wandb import WandbLoggerCallback
def _log(
name: str,
repo: Repo,
graphql_endpoint: str,
save_dir: str,
sweep_id: Optional[int] = None,
render: bool = False,
**kwargs
):
sweep = (sweep_id != None)
trainer = GNNTrainer(name,kwargs)
trainer.train(
render=render, save_dir=save_dir, sweep=sweep
)
def resume(
name: str,
config_path: str,
graphql_endpoint: str,
save_dir: str,
resume_from_id:int,
resume_from_name:str,
render: bool = False,
):
params = read_params(config_path)
trainer = ResumeGNNTrainer(
log_name=name,
params=params,
resume_id=resume_from_id,
resume_name=resume_from_name,
)
#Train
trainer.train(
render=render, save_dir=save_dir, sweep=sweep
)
def run(
name: str,
config_path: str,
graphql_endpoint: str,
save_dir: str,
render: bool = False,
):
params = read_params(config_path)
_log(
name=name,
repo=Repo("."),
graphql_endpoint=graphql_endpoint,
save_dir=save_dir,
sweep_id=None,
render=render,
**params,
)
def trainable(config: dict):
print(config)
return _log(**config)
def sweep(
name: str,
config_path: str,
graphql_endpoint: str,
save_dir: str,
random_search: bool = False,
render: bool = False,
):
params = read_params(config_path)
# sweep_id = create_sweep(
# config=params,
# graphql_endpoint=graphql_endpoint,
# log_level="INFO",
# name=name,
# )
hyperparam_names = [
"base",
"ppo",
"return",
"seed",
]
for section in params.keys():
if section in hyperparam_names:
if isinstance(params[section],dict):
params[section] = {
k: (tune.choice(v) if random_search else tune.grid_search(v))
if isinstance(v, list)
else v
for k, v in params[section].items()
}
elif isinstance(params[section],list):
print(params[section])
print(section)
params[section] = [
tune.choice(params[section]) if random_search else tune.grid_search(params[section])
]
print(params['seed'])
pprint(params)
print(params['base']['hidden_size'])
num_cpus = os.cpu_count()
num_gpus = torch.cuda.device_count()
config = {
"name": name,
"repo": Repo("."),
"graphql_endpoint": graphql_endpoint,
"save_dir": save_dir,
"sweep_id": None,
"wandb": {"project": name},
**params,
}
ray.init()
num_cpus = os.cpu_count()
num_gpus = torch.cuda.device_count()
tuner = ray.tune.Tuner(
tune.with_resources(
tune.with_parameters(trainable),
resources={"cpu": int(num_cpus / num_gpus), "gpu": 1}
),
param_space=config,
)
results = tuner.fit()
print(results)
results = tuner.fit()
print(results)
if __name__ == "__main__":
print(sys.argv)
args = get_args()
print("Python started.")
generate_tests( config_path= "params.yaml")
if args.resume:
assert(not args.sweep) # cannot sweep when resuming
assert(args.resume_name is not None and args.resume_id is not None) # verify that we have name and id
resume(
name=args.log_name,
config_path="params.yaml",
graphql_endpoint=os.getenv("GRAPHQL_ENDPOINT"),
save_dir=args.save_dir,
resume_from_id=args.resume_id,
render=args.render,
)
elif args.sweep:
sweep(
name=args.log_name,
config_path="params.yaml",
graphql_endpoint=os.getenv("GRAPHQL_ENDPOINT"),
save_dir=args.save_dir,
render=args.render,
)
else:
run(
name=args.log_name,
config_path="params.yaml",
graphql_endpoint=os.getenv("GRAPHQL_ENDPOINT"),
save_dir=args.save_dir,
render=args.render,
)