| | |
| | | import torch |
| | | import logging |
| | | from tqdm import tqdm |
| | | from datetime import datetime |
| | | import torch.distributed as dist |
| | | from contextlib import nullcontext |
| | | # from torch.utils.tensorboard import SummaryWriter |
| | |
| | | filename = os.path.join(self.output_dir, f'model.pt.ep{epoch}') |
| | | torch.save(state, filename) |
| | | |
| | | print(f'Checkpoint saved to {filename}') |
| | | print(f'\nCheckpoint saved to {filename}\n') |
| | | latest = Path(os.path.join(self.output_dir, f'model.pt')) |
| | | torch.save(state, latest) |
| | | |
| | |
| | | self._resume_checkpoint(self.output_dir) |
| | | |
| | | for epoch in range(self.start_epoch, self.max_epoch + 1): |
| | | |
| | | time1 = time.perf_counter() |
| | | self._train_epoch(epoch) |
| | | |
| | | |
| | |
| | | |
| | | self.scheduler.step() |
| | | |
| | | time2 = time.perf_counter() |
| | | time_escaped = (time2 - time1)/3600.0 |
| | | print(f"\nrank: {self.local_rank}, time_escaped_epoch: {time_escaped:.3f} hours, estimated to finish {self.max_epoch} epoch: {(self.max_epoch-epoch)*time_escaped:.3f}\n") |
| | | |
| | | if self.rank == 0: |
| | | average_checkpoints(self.output_dir, self.avg_nbest_model) |
| | |
| | | torch.cuda.max_memory_reserved()/1024/1024/1024, |
| | | ) |
| | | lr = self.scheduler.get_last_lr()[0] |
| | | time_now = datetime.now() |
| | | time_now = time_now.strftime("%Y-%m-%d %H:%M:%S") |
| | | description = ( |
| | | f"{time_now}, " |
| | | f"rank: {self.local_rank}, " |
| | | f"epoch: {epoch}/{self.max_epoch}, " |
| | | f"step: {batch_idx+1}/{len(self.dataloader_train)}, total: {self.batch_total}, " |
| | |
| | | ) |
| | | pbar.set_description(description) |
| | | if self.writer: |
| | | self.writer.add_scalar(f'rank{self.local_rank}_Loss/train', loss.item(), |
| | | epoch*len(self.dataloader_train) + batch_idx) |
| | | self.writer.add_scalar(f'rank{self.local_rank}_Loss/train', loss.item(), self.batch_total) |
| | | self.writer.add_scalar(f'rank{self.local_rank}_lr/train', lr, self.batch_total) |
| | | for key, var in stats.items(): |
| | | self.writer.add_scalar(f'rank{self.local_rank}_{key}/train', var.item(), |
| | | epoch * len(self.dataloader_train) + batch_idx) |
| | | self.writer.add_scalar(f'rank{self.local_rank}_{key}/train', var.item(), self.batch_total) |
| | | for key, var in speed_stats.items(): |
| | | self.writer.add_scalar(f'rank{self.local_rank}_{key}/train', eval(var), |
| | | epoch * len(self.dataloader_train) + batch_idx) |
| | | |
| | | # if batch_idx == 2: |
| | | # break |
| | | self.writer.add_scalar(f'rank{self.local_rank}_{key}/train', eval(var), self.batch_total) |
| | | |
| | | |
| | | pbar.close() |
| | | |
| | | def _validate_epoch(self, epoch): |
| | |
| | | |
| | | if (batch_idx+1) % self.log_interval == 0 or (batch_idx+1) == len(self.dataloader_val): |
| | | pbar.update(self.log_interval) |
| | | time_now = datetime.now() |
| | | time_now = time_now.strftime("%Y-%m-%d %H:%M:%S") |
| | | description = ( |
| | | f"{time_now}, " |
| | | f"rank: {self.local_rank}, " |
| | | f"validation epoch: {epoch}/{self.max_epoch}, " |
| | | f"step: {batch_idx+1}/{len(self.dataloader_val)}, " |