-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_lib_speedup.py
346 lines (282 loc) · 14.1 KB
/
run_lib_speedup.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import os
import time
import logging
import torch
from torch.utils import tensorboard
import numpy as np
from torch.nn.parallel import DistributedDataParallel as DDP
import torch.distributed as dist
from models import ddpm_cifar, ddpm_celeba, ddpm_imagenet
import tqdm
from models.ema import ExponentialMovingAverage
from models import utils as mutils
from torch_utils.utils import make_dir, get_optimizer_speedup, optimization_manager_speedup, set_seeds
from torch_utils.utils import broadcast_params, reduce_tensor, get_data_inverse_scaler, save_img
from torch_utils.checkpoint import save_checkpoint, restore_checkpoint
from torch_utils.losses import get_step_fn_speedup
from torch_utils.sampling import get_sampling_fn
from torch_utils.diffusion import Diffusion
import torch_utils.evaluation as evaluation
import PIL.Image
def train(config, workdir):
''' Main training script. '''
local_rank = config.local_rank
global_rank = config.global_rank
global_size = config.global_size
if config.mode == 'train':
set_seeds(global_rank, config.seed)
elif config.mode == 'continue':
set_seeds(global_rank, config.seed)# + config.cont_nbr
else:
raise NotImplementedError('Mode %s is unknown.' % config.mode)
torch.cuda.device(local_rank)
config.device = torch.device('cuda:%d' % local_rank)
# Setting up all necessary folders
sample_dir = os.path.join(workdir, 'samples')
tb_dir = os.path.join(workdir, 'tensorboard')
checkpoint_dir = os.path.join(workdir, 'checkpoints')
if config.image_channels == 3:
fid_dir = os.path.join(workdir, 'fid')
if global_rank == 0:
logging.info(config)
if config.mode == 'train':
make_dir(sample_dir)
make_dir(tb_dir)
make_dir(checkpoint_dir)
if config.image_channels == 3:
make_dir(fid_dir)
writer = tensorboard.SummaryWriter(tb_dir)
dist.barrier()
diffusion = Diffusion(config)
# # Creating the score model
whole_model = mutils.create_model(config).to(config.device)
if config.n_discrete_steps > 6:
pretrained_score = torch.load(config.pretrained_model, map_location=torch.device('cpu')) # load target score model
whole_model.target_model.load_state_dict(pretrained_score, strict=True)
whole_model.phinet.load_state_dict(pretrained_score, strict=False)
whole_model.fnet.load_state_dict(pretrained_score, strict=True)
broadcast_params(whole_model.parameters()) # Sync all parameters
whole_model = DDP(whole_model, device_ids=[local_rank], find_unused_parameters=True)
if config.n_discrete_steps ==6:
whole_model.load_state_dict(torch.load(config.pretrained_model, map_location=torch.device('cpu'))['whole_model'])
ema_phinet = ExponentialMovingAverage(
whole_model.module.phinet.parameters(), decay=config.ema_rate)
ema_fnet = ExponentialMovingAverage(
whole_model.module.fnet.parameters(), decay=config.ema_rate)
if global_rank == 0:
model_parameters = filter(
lambda p: p.requires_grad, whole_model.parameters())
n_params = sum([np.prod(p.size()) for p in model_parameters])
logging.info('Number of trainable parameters in model: %d' % n_params)
dist.barrier()
inverse_scaler = get_data_inverse_scaler()
optim_phinet_params = whole_model.module.phinet.parameters()
optimizer_phinet = get_optimizer_speedup(config, optim_phinet_params)
optim_fnet_params = whole_model.module.fnet.parameters()
optimizer_fnet = get_optimizer_speedup(config, optim_fnet_params)
state = dict(optimizer_phinet=optimizer_phinet, optimizer_fnet=optimizer_fnet,
whole_model=whole_model, ema_phinet=ema_phinet, ema_fnet=ema_fnet, step=1)
if config.mode == 'continue':
if config.checkpoint is None:
ckpt_path = os.path.join(checkpoint_dir, 'checkpoint.pth')
else:
ckpt_path = os.path.join(checkpoint_dir, config.checkpoint)
if global_rank == 0:
logging.info('Loading model from path: %s' % ckpt_path)
dist.barrier()
state = restore_checkpoint(ckpt_path, state, device=config.device)
num_total_iter = config.n_train_iters
if global_rank == 0:
logging.info('Number of total iterations: %d' % num_total_iter)
dist.barrier()
phi_optimize_fn = optimization_manager_speedup(config, phi_net = True)
f_optimize_fn = optimization_manager_speedup(config, phi_net = False)
train_step_fn = get_step_fn_speedup(True, phi_optimize_fn, f_optimize_fn, diffusion, config)
training_shape = (config.training_batch_size,
config.image_channels,
config.image_size,
config.image_size
)
sampling_shape = (config.sampling_batch_size,
config.image_channels,
config.image_size,
config.image_size)
sampling_fn = get_sampling_fn(
config, diffusion, sampling_shape)
step = int(state['step'])
if global_rank == 0:
logging.info('Starting training at step %d' % step)
dist.barrier()
if config.mode == 'continue':
config.eval_threshold = max(step + 1, config.eval_threshold)
config.snapshot_threshold = max(step + 1, config.snapshot_threshold)
config.save_threshold = max(step + 1, config.save_threshold)
while step < num_total_iter+1:
start_time = time.time()
# for weight saving.
if step % config.save_freq == 0:
if global_rank == 0:
checkpoint_file = os.path.join(
checkpoint_dir, 'checkpoint_%d.pth' % step)
if not os.path.isfile(checkpoint_file):
save_checkpoint(checkpoint_file, state)
dist.barrier()
# for visualization.
if (step % config.snapshot_freq == 0) and global_rank == 0 and step >= config.snapshot_threshold or step==1:
logging.info('Saving snapshot checkpoint.')
save_checkpoint(os.path.join(
checkpoint_dir, 'snapshot_checkpoint.pth'), state)
ema_phinet.store(whole_model.module.phinet.parameters())
ema_phinet.copy_to(whole_model.module.phinet.parameters())
x = sampling_fn(whole_model)
ema_phinet.restore(whole_model.module.phinet.parameters())
this_sample_dir = os.path.join(sample_dir, 'iter_%d' % step)
os.makedirs(this_sample_dir, exist_ok=True)
x = inverse_scaler(x)
save_img(x, os.path.join(
this_sample_dir, 'sample.png'))
dist.barrier()
# for FID evaluation
if (step % config.fid_freq == 0) and config.eval_fid:
logging.info('FID evaluating with %d samples.'%config.eval_fid_samples)
this_sample_dir = os.path.join(fid_dir, 'step_%d' % step)
if global_rank == 0:
make_dir(this_sample_dir)
dist.barrier()
ema_phinet.store(whole_model.module.phinet.parameters())
ema_phinet.copy_to(whole_model.module.phinet.parameters())
seeds = np.arange(0, config.eval_fid_samples, 1)
num_batches = ((len(seeds) - 1) // (config.sampling_batch_size * evaluation.get_world_size()) + 1) * evaluation.get_world_size()
all_batches = torch.as_tensor(seeds).tensor_split(num_batches)
rank_batches = all_batches[evaluation.get_rank() :: evaluation.get_world_size()]
for batch_seeds in tqdm.tqdm(rank_batches, unit='batch', disable=(evaluation.get_rank() != 0)):
dist.barrier()
batch_size = len(batch_seeds)
if batch_size == 0:
continue
x = sampling_fn(whole_model)
x = inverse_scaler(x)
samples = np.clip(x.permute(0, 2, 3, 1).cpu().numpy()
* 255., 0, 255).astype(np.uint8)
for seed, image_np in zip(batch_seeds, samples):
image_dir = os.path.join(this_sample_dir, f'{seed-seed%1000:06d}')
os.makedirs(image_dir, exist_ok=True)
image_path = os.path.join(image_dir, f'{seed:06d}.png')
if image_np.shape[2] == 1:
PIL.Image.fromarray(image_np, 'L').save(image_path)
else:
PIL.Image.fromarray(image_np, 'RGB').save(image_path)
dist.barrier()
ema_phinet.restore(whole_model.module.phinet.parameters())
if global_rank == 0:
mu, sigma = evaluation.calculate_inception_stats(this_sample_dir)
ref = dict(np.load(config.ref_statistics))
fid = evaluation.calculate_fid_from_inception_stats(
mu, sigma, ref['mu'], ref['sigma'])
logging.info('\tFID: %.6f' % fid)
torch.cuda.empty_cache()
dist.barrier()
# for training.
loss_phi, loss_fnet = train_step_fn(state,training_shape)
# for logging.
if step % config.log_freq == 0 and step >= 1:
loss_phi = reduce_tensor(loss_phi, global_size)
loss_fnet = reduce_tensor(loss_fnet, global_size)
if global_rank == 0:
logging.info('Iter %d/%d Loss_phiNet: %.4f Loss_FNet: %.4f Time: %.3f' % (step + 1,
config.n_train_iters, loss_phi.item(), loss_fnet.item(), time.time() - start_time))
print('Iter %d/%d Loss_phiNet: %.4f Loss_FNet: %.4f Time: %.3f' % (step + 1,
config.n_train_iters, loss_phi.item(), loss_fnet.item(), time.time() - start_time))
writer.add_scalar('training_phinet_loss', loss_phi, step)
writer.add_scalar('training_fnet_loss', loss_fnet, step)
dist.barrier()
step += 1
if step >= num_total_iter+1:
break
if global_rank == 0:
logging.info('Finished after %d iterations.' % config.n_train_iters)
logging.info('Saving final checkpoint.')
save_checkpoint(os.path.join(
checkpoint_dir, 'final_checkpoint.pth'), state)
dist.barrier()
def evaluate(config, workdir):
''' Main evaluation script. '''
local_rank = config.local_rank
global_rank = config.global_rank
set_seeds(global_rank, config.seed)
torch.cuda.device(local_rank)
config.device = torch.device('cuda:%d' % local_rank)
sample_dir = os.path.join(workdir, 'samples')
if config.image_channels == 3:
fid_dir = os.path.join(workdir, 'fid')
if global_rank == 0:
logging.info(config)
make_dir(sample_dir)
if config.image_channels == 3:
make_dir(fid_dir)
dist.barrier()
inverse_scaler = get_data_inverse_scaler()
if config.diffusion == 'ddpm_alpha':
diffusion = Diffusion(config)
else:
raise NotImplementedError('SDE %s is unknown.' % config.diffusion)
if config.is_image:
sampling_shape = (config.sampling_batch_size,
config.image_channels,
config.image_size,
config.image_size)
else:
sampling_shape = (config.sampling_batch_size,
config.data_dim)
sampling_fn = get_sampling_fn(
config, diffusion, sampling_shape)
# whole_model = mutils.create_model(config).to(config.device)
# # Creating the score model
whole_model = mutils.create_model(config).to(config.device)
broadcast_params(whole_model.parameters()) # Sync all parameters
whole_model = DDP(whole_model, device_ids=[local_rank],find_unused_parameters=False)
state = torch.load(config.ckpt_file, map_location=config.device)
if global_rank == 0:
logging.info('Loading model from path: %s' % config.ckpt_file)
dist.barrier()
whole_model.load_state_dict(state['whole_model'], strict=True)
step = int(state['step'])
if global_rank == 0:
logging.info('Evaluating at training step %d' % step)
dist.barrier()
if config.eval_fid:
logging.info('FID evaluating with %d samples.'%config.eval_fid_samples)
this_sample_dir = os.path.join(fid_dir, 'step_%d' % step)
if global_rank == 0:
make_dir(this_sample_dir)
dist.barrier()
seeds = np.arange(0, config.eval_fid_samples, 1)
num_batches = ((len(seeds) - 1) // (config.sampling_batch_size * evaluation.get_world_size()) + 1) * evaluation.get_world_size()
all_batches = torch.as_tensor(seeds).tensor_split(num_batches)
rank_batches = all_batches[evaluation.get_rank() :: evaluation.get_world_size()]
for batch_seeds in tqdm.tqdm(rank_batches, unit='batch', disable=(evaluation.get_rank() != 0)):
dist.barrier()
batch_size = len(batch_seeds)
if batch_size == 0:
continue
x = sampling_fn(whole_model)
x = inverse_scaler(x)
samples = np.clip(x.permute(0, 2, 3, 1).cpu().numpy()
* 255., 0, 255).astype(np.uint8)
for seed, image_np in zip(batch_seeds, samples):
image_dir = os.path.join(this_sample_dir, f'{seed-seed%1000:06d}')
os.makedirs(image_dir, exist_ok=True)
image_path = os.path.join(image_dir, f'{seed:06d}.png')
if image_np.shape[2] == 1:
PIL.Image.fromarray(image_np, 'L').save(image_path)
else:
PIL.Image.fromarray(image_np, 'RGB').save(image_path)
dist.barrier()
if global_rank == 0:
mu, sigma = evaluation.calculate_inception_stats(this_sample_dir)
ref = dict(np.load(config.ref_statistics))
fid = evaluation.calculate_fid_from_inception_stats(
mu, sigma, ref['mu'], ref['sigma'])
logging.info('\tFID: %.6f' % fid)
torch.cuda.empty_cache()
dist.barrier()