-
Notifications
You must be signed in to change notification settings - Fork 6
/
run_lib.py
310 lines (256 loc) · 12.9 KB
/
run_lib.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import os
import torch
import numpy as np
import random
import logging
import time
from absl import flags
from torch.utils import tensorboard
from torch_geometric.loader import DataLoader, DenseDataLoader
import pickle
from models import pgsn
import losses
import sampling
from models import utils as mutils
from models.ema import ExponentialMovingAverage
import datasets
from evaluation import get_stats_eval, get_nn_eval
import sde_lib
import visualize
from utils import *
FLAGS = flags.FLAGS
def set_random_seed(config):
seed = config.seed
os.environ['PYTHONHASHSEED'] = str(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
def sde_train(config, workdir):
"""Runs the training pipeline.
Args:
config: Configuration to use.
workdir: Working directory for checkpoints and TF summaries.
If this contains checkpoint training will be resumed from the latest checkpoint.
"""
# Create directories for experimental logs
sample_dir = os.path.join(workdir, "samples")
if not os.path.exists(sample_dir):
os.makedirs(sample_dir)
tb_dir = os.path.join(workdir, "tensorboard")
if not os.path.exists(tb_dir):
os.makedirs(tb_dir)
writer = tensorboard.SummaryWriter(tb_dir)
# Initialize model.
score_model = mutils.create_model(config)
ema = ExponentialMovingAverage(score_model.parameters(), decay=config.model.ema_rate)
optimizer = losses.get_optimizer(config, score_model.parameters())
state = dict(optimizer=optimizer, model=score_model, ema=ema, step=0)
# Create checkpoints directly
checkpoint_dir = os.path.join(workdir, "checkpoints")
# Intermediate checkpoints to resume training
checkpoint_meta_dir = os.path.join(workdir, "checkpoints-meta", "checkpoint.pth")
if not os.path.exists(checkpoint_dir):
os.makedirs(checkpoint_dir)
if not os.path.exists(os.path.dirname(checkpoint_meta_dir)):
os.makedirs(os.path.dirname(checkpoint_meta_dir))
# Resume training when intermediate checkpoints are detected
state = restore_checkpoint(checkpoint_meta_dir, state, config.device)
initial_step = int(state['step'])
# Build dataloader and iterators
train_ds, eval_ds, test_ds, n_node_pmf = datasets.get_dataset(config)
train_loader = DataLoader(train_ds, batch_size=config.training.batch_size, shuffle=True)
eval_loader = DataLoader(eval_ds, batch_size=config.training.batch_size, shuffle=False)
test_loader = DataLoader(test_ds, batch_size=config.training.batch_size, shuffle=False)
n_node_pmf = torch.from_numpy(n_node_pmf).to(config.device)
train_iter = iter(train_loader)
# create data normalizer and its inverse
scaler = datasets.get_data_scaler(config)
inverse_scaler = datasets.get_data_inverse_scaler(config)
# Setup SDEs
if config.training.sde.lower() == 'vpsde':
sde = sde_lib.VPSDE(beta_min=config.model.beta_min, beta_max=config.model.beta_max, N=config.model.num_scales)
sampling_eps = 1e-3
else:
raise NotImplementedError(f"SDE {config.training.sde} unknown.")
# Build one-step training and evaluation functions
optimize_fn = losses.optimization_manager(config)
continuous = config.training.continuous
reduce_mean = config.training.reduce_mean
likelihood_weighting = config.training.likelihood_weighting
train_step_fn = losses.get_step_fn(sde, train=True, optimize_fn=optimize_fn,
reduce_mean=reduce_mean, continuous=continuous,
likelihood_weighting=likelihood_weighting)
eval_step_fn = losses.get_step_fn(sde, train=False, optimize_fn=optimize_fn,
reduce_mean=reduce_mean, continuous=continuous,
likelihood_weighting=likelihood_weighting)
# Build sampling functions
if config.training.snapshot_sampling:
sampling_shape = (config.training.eval_batch_size, config.data.num_channels,
config.data.max_node, config.data.max_node)
sampling_fn = sampling.get_sampling_fn(config, sde, sampling_shape, inverse_scaler, sampling_eps)
num_train_steps = config.training.n_iters
logging.info("Starting training loop at step %d." % (initial_step,))
for step in range(initial_step, num_train_steps + 1):
try:
graphs = next(train_iter)
except StopIteration:
train_iter = train_loader.__iter__()
graphs = next(train_iter)
adj, mask = dense_adj(graphs, config.data.max_node, scaler, config.data.dequantization)
batch = (adj, mask)
# Execute one training step
loss = train_step_fn(state, batch)
if step % config.training.log_freq == 0:
logging.info("step: %d, training_loss: %.5e" % (step, loss.item()))
writer.add_scalar("training_loss", loss, step)
# Save a temporary checkpoint to resume training after pre-emption periodically
if step != 0 and step % config.training.snapshot_freq_for_preemption == 0:
save_checkpoint(checkpoint_meta_dir, state)
# Report the loss on evaluation dataset periodically
if step % config.training.eval_freq == 0:
for eval_graphs in eval_loader:
eval_adj, eval_mask = dense_adj(eval_graphs, config.data.max_node, scaler)
eval_batch = (eval_adj, eval_mask)
eval_loss = eval_step_fn(state, eval_batch)
logging.info("step: %d, eval_loss: %.5e" % (step, eval_loss.item()))
writer.add_scalar("eval_loss", eval_loss.item(), step)
for test_graphs in test_loader:
test_adj, test_mask = dense_adj(test_graphs, config.data.max_node, scaler)
test_batch = (test_adj, test_mask)
test_loss = eval_step_fn(state, test_batch)
logging.info("step: %d, test_loss: %.5e" % (step, test_loss.item()))
writer.add_scalar("test_loss", test_loss.item(), step)
# Save a checkpoint periodically and generate samples
if step != 0 and step % config.training.snapshot_freq == 0 or step == num_train_steps:
# Save the checkpoint.
save_step = step // config.training.snapshot_freq
save_checkpoint(os.path.join(checkpoint_dir, f'checkpoint_{save_step}.pth'), state)
# Generate and save samples
if config.training.snapshot_sampling:
ema.store(score_model.parameters())
ema.copy_to(score_model.parameters())
sample, sample_steps, sample_nodes = sampling_fn(score_model, n_node_pmf)
sample_list = adj2graph(sample, sample_nodes)
ema.restore(score_model.parameters())
this_sample_dir = os.path.join(sample_dir, "iter_{}".format(step))
if not os.path.exists(this_sample_dir):
os.makedirs(this_sample_dir)
# graph visualization and save figs
visualize.visualize_graphs(sample_list, this_sample_dir, config)
def sde_evaluate(config, workdir, eval_folder="eval"):
"""Evaluate trained models.
Args:
config: Configuration to use.
workdir: Working directory for checkpoints.
eval_folder: The subfolder for storing evaluation results. Default to "eval".
"""
# Create directory to eval_folder
eval_dir = os.path.join(workdir, eval_folder)
if not os.path.exists(eval_dir):
os.makedirs(eval_dir)
# Build data pipeline
train_ds, _, test_ds, n_node_pmf = datasets.get_dataset(config)
n_node_pmf = torch.from_numpy(n_node_pmf).to(config.device)
# Creat data normalizer and its inverse
scaler = datasets.get_data_scaler(config)
inverse_scaler = datasets.get_data_inverse_scaler(config)
# Initialize model
score_model = mutils.create_model(config)
optimizer = losses.get_optimizer(config, score_model.parameters())
ema = ExponentialMovingAverage(score_model.parameters(), decay=config.model.ema_rate)
state = dict(optimizer=optimizer, model=score_model, ema=ema, step=0)
checkpoint_dir = os.path.join(workdir, "checkpoints")
# Setup SDEs
if config.training.sde.lower() == 'vpsde':
sde = sde_lib.VPSDE(beta_min=config.model.beta_min, beta_max=config.model.beta_max, N=config.model.num_scales)
sampling_eps = 1e-3
elif config.training.sde.lower() == 'subvpsde':
sde = sde_lib.subVPSDE(beta_min=config.model.beta_min, beta_max=config.model.beta_max,
N=config.model.num_scales)
sampling_eps = 1e-3
elif config.training.sde.lower() == 'vesde':
sde = sde_lib.VESDE(sigma_min=config.model.sigma_min, sigma_max=config.model.sigma_max,
N=config.model.num_scales)
sampling_eps = 1e-5
else:
raise NotImplementedError(f"SDE {config.training.sde} unknown.")
# Build the sampling function when sampling is enabled
if config.eval.enable_sampling:
sampling_shape = (config.eval.batch_size,
config.data.num_channels,
config.data.max_node, config.data.max_node)
sampling_fn = sampling.get_sampling_fn(config, sde, sampling_shape, inverse_scaler, sampling_eps)
eval_stats_fn = get_stats_eval(config)
nn_eval_fn = get_nn_eval(config)
# Begin evaluation
begin_ckpt = config.eval.begin_ckpt
logging.info("begin checkpoint: %d" % (begin_ckpt,))
for ckpt in range(begin_ckpt, config.eval.end_ckpt + 1):
# Wait if the target checkpoint doesn't exist yet
waiting_message_printed = False
ckpt_filename = os.path.join(checkpoint_dir, "checkpoint_{}.pth".format(ckpt))
while not os.path.exists(ckpt_filename):
if not waiting_message_printed:
logging.warning("Waiting for the arrival of checkpoint_%d" % (ckpt,))
waiting_message_printed = True
time.sleep(60)
# Wait for 2 additional mins in case the file exists but is not ready for reading
ckpt_path = os.path.join(checkpoint_dir, f'checkpoint_{ckpt}.pth')
try:
state = restore_checkpoint(ckpt_path, state, device=config.device)
except:
time.sleep(60)
try:
state = restore_checkpoint(ckpt_path, state, device=config.device)
except:
time.sleep(120)
state = restore_checkpoint(ckpt_path, state, device=config.device)
ema.copy_to(score_model.parameters())
# Generate samples and compute MMD stats
if config.eval.enable_sampling:
num_sampling_rounds = int(np.ceil(config.eval.num_samples / config.eval.batch_size))
all_samples = []
for r in range(num_sampling_rounds):
logging.info("sampling -- ckpt: %d, round: %d" % (ckpt, r))
sample, sample_steps, sample_nodes = sampling_fn(score_model, n_node_pmf)
logging.info("sample steps: %d" % sample_steps)
sample_list = adj2graph(sample, sample_nodes)
all_samples += sample_list
all_samples = all_samples[:config.eval.num_samples]
# save the graphs
sampler_name = config.sampling.method
if config.eval.save_graph:
graph_file = os.path.join(eval_dir, sampler_name + "_ckpt_{}.pkl".format(ckpt))
with open(graph_file, "wb") as f:
pickle.dump(all_samples, f)
# evaluate
eval_results = eval_stats_fn(test_ds, all_samples)
all_res = []
for key, values in eval_results.items():
all_res.append(values)
logging.info("sampling -- ckpt: {}, {}: {:.6f}".format(ckpt, key, values))
logging.info("sampling -- ckpt: {}, {}: {:.6f}".format(ckpt, "mean", np.mean(all_res)))
# Draw and save the graph visualize figs
this_sample_dir = os.path.join(eval_dir, sampler_name + "_ckpt_{}".format(ckpt))
if not os.path.exists(this_sample_dir):
os.makedirs(this_sample_dir)
visualize.visualize_graphs(all_samples[:32], this_sample_dir, config, remove=False)
# NN-based metric
nn_eval_results = nn_eval_fn(test_ds, all_samples)
for key, values in nn_eval_results.items():
logging.info("sampling -- ckpt: {}, {} mean: {:.6f} std: {:.6f}".
format(ckpt, key, values[0], values[1]))
run_train_dict = {
'sde': sde_train
}
run_eval_dict = {
'sde': sde_evaluate
}
def train(config, workdir):
run_train_dict[config.model_type](config, workdir)
def evaluate(config, workdir, eval_folder='eval'):
run_eval_dict[config.model_type](config, workdir, eval_folder)