MCPcopy Create free account
hub / github.com/cientgu/VQ-Diffusion / save

Method save

image_synthesis/engine/solver.py:300–348  ·  view source on GitHub ↗
(self, force=False)

Source from the content-addressed store, hash-verified

298 return loss
299
300 def save(self, force=False):
301 if is_primary():
302 # save with the epoch specified name
303 if self.save_iterations > 0:
304 if (self.last_iter + 1) % self.save_iterations == 0:
305 save = True
306 else:
307 save = False
308 else:
309 if isinstance(self.save_epochs, int):
310 save = (self.last_epoch + 1) % self.save_epochs == 0
311 else:
312 save = (self.last_epoch + 1) in self.save_epochs
313
314 if save or force:
315 state_dict = {
316 'last_epoch': self.last_epoch,
317 'last_iter': self.last_iter,
318 'model': self.model.module.state_dict() if isinstance(self.model, torch.nn.parallel.DistributedDataParallel) else self.model.state_dict()
319 }
320 if self.ema is not None:
321 state_dict['ema'] = self.ema.state_dict()
322 if self.clip_grad_norm is not None:
323 state_dict['clip_grad_norm'] = self.clip_grad_norm.state_dict()
324
325 # add optimizers and schedulers
326 optimizer_and_scheduler = {}
327 for op_sc_n, op_sc in self.optimizer_and_scheduler.items():
328 state_ = {}
329 for k in op_sc:
330 if k in ['optimizer', 'scheduler']:
331 op_or_sc = {kk: vv for kk, vv in op_sc[k].items() if kk != 'module'}
332 op_or_sc['module'] = op_sc[k]['module'].state_dict()
333 state_[k] = op_or_sc
334 else:
335 state_[k] = op_sc[k]
336 optimizer_and_scheduler[op_sc_n] = state_
337
338 state_dict['optimizer_and_scheduler'] = optimizer_and_scheduler
339
340 if save:
341 save_path = os.path.join(self.ckpt_dir, '{}e_{}iter.pth'.format(str(self.last_epoch).zfill(6), self.last_iter))
342 torch.save(state_dict, save_path)
343 self.logger.log_info('saved in {}'.format(save_path))
344
345 # save with the last name
346 save_path = os.path.join(self.ckpt_dir, 'last.pth')
347 torch.save(state_dict, save_path)
348 self.logger.log_info('saved in {}'.format(save_path))
349
350 def resume(self,
351 path=None, # The path of last.pth

Callers 4

trainMethod · 0.95
sampleMethod · 0.80

Calls 3

is_primaryFunction · 0.90
log_infoMethod · 0.80
state_dictMethod · 0.45

Tested by

no test coverage detected