| | |
| | | self.device = next(model.parameters()).device |
| | | self.avg_nbest_model = kwargs.get("avg_nbest_model", 5) |
| | | self.kwargs = kwargs |
| | | self.log_interval = kwargs.get("log_interval", 50) |
| | | |
| | | |
| | | try: |
| | |
| | | my_context = self.model.no_sync if batch_idx % accum_grad != 0 else nullcontext |
| | | with my_context(): |
| | | time2 = time.perf_counter() |
| | | # print("before, GPU, memory: {:.3f} GB, " |
| | | # "{:.3f} GB, " |
| | | # "{:.3f} GB, " |
| | | # "{:.3f} GB".format(torch.cuda.memory_allocated()/1024/1024/1024, |
| | | # torch.cuda.max_memory_allocated()/1024/1024/1024, |
| | | # torch.cuda.memory_reserved()/1024/1024/1024, |
| | | # torch.cuda.max_memory_reserved()/1024/1024/1024, |
| | | # )) |
| | | |
| | | retval = self.model(**batch) |
| | | torch.cuda.empty_cache() |
| | | # print("after, GPU, memory: {:.3f} GB, " |
| | | # "{:.3f} GB, " |
| | | # "{:.3f} GB, " |
| | | # "{:.3f} GB".format(torch.cuda.memory_allocated()/1024/1024/1024, |
| | | # torch.cuda.max_memory_allocated()/1024/1024/1024, |
| | | # torch.cuda.memory_reserved()/1024/1024/1024, |
| | | # torch.cuda.max_memory_reserved()/1024/1024/1024, |
| | | # )) |
| | | time3 = time.perf_counter() |
| | | speed_stats["forward_time"] = f"{time3 - time2:0.3f}" |
| | | loss, stats, weight = retval |
| | |
| | | speed_stats["total_time"] = total_time |
| | | |
| | | |
| | | pbar.update(1) |
| | | if self.local_rank == 0: |
| | | |
| | | if batch_idx % self.log_interval == 0 or batch_idx == len(self.dataloader_train) - 1: |
| | | pbar.update(self.log_interval) |
| | | gpu_info = "GPU, memory: {:.3f} GB, " \ |
| | | "{:.3f} GB, "\ |
| | | "{:.3f} GB, "\ |
| | | "{:.3f} GB".format(torch.cuda.memory_allocated()/1024/1024/1024, |
| | | torch.cuda.max_memory_allocated()/1024/1024/1024, |
| | | torch.cuda.memory_reserved()/1024/1024/1024, |
| | | torch.cuda.max_memory_reserved()/1024/1024/1024, |
| | | ) |
| | | description = ( |
| | | f"rank: {self.local_rank}, " |
| | | f"Train epoch: {epoch}/{self.max_epoch}, " |
| | | f"step {batch_idx}/{len(self.dataloader_train)}, " |
| | | f"{speed_stats}, " |
| | | f"(loss: {loss.detach().cpu().item():.3f}), " |
| | | f"{[(k, round(v.cpu().item(), 3)) for k, v in stats.items()]}" |
| | | f"{gpu_info}" |
| | | ) |
| | | pbar.set_description(description) |
| | | if self.writer: |
| | | self.writer.add_scalar('Loss/train', loss.item(), |
| | | self.writer.add_scalar(f'rank{self.local_rank}, Loss/train', loss.item(), |
| | | epoch*len(self.dataloader_train) + batch_idx) |
| | | for key, var in stats.items(): |
| | | self.writer.add_scalar(f'{key}/train', var.item(), |
| | | self.writer.add_scalar(f'rank{self.local_rank}, {key}/train', var.item(), |
| | | epoch * len(self.dataloader_train) + batch_idx) |
| | | for key, var in speed_stats.items(): |
| | | self.writer.add_scalar(f'{key}/train', eval(var), |
| | | self.writer.add_scalar(f'rank{self.local_rank}, {key}/train', eval(var), |
| | | epoch * len(self.dataloader_train) + batch_idx) |
| | | |
| | | # if batch_idx == 2: |
| | |
| | | loss = loss |
| | | time4 = time.perf_counter() |
| | | |
| | | pbar.update(1) |
| | | if self.local_rank == 0: |
| | | |
| | | if batch_idx % self.log_interval == 0 or batch_idx == len(self.dataloader_train) - 1: |
| | | pbar.update(self.log_interval) |
| | | description = ( |
| | | f"rank: {self.local_rank}, " |
| | | f"validation epoch: {epoch}/{self.max_epoch}, " |
| | | f"step {batch_idx}/{len(self.dataloader_train)}, " |
| | | f"{speed_stats}, " |
| | | f"(loss: {loss.detach().cpu().item():.3f}), " |
| | | f"{[(k, round(v.cpu().item(), 3)) for k, v in stats.items()]}" |
| | | f"rank: {self.local_rank}" |
| | | ) |
| | | pbar.set_description(description) |
| | | if self.writer: |
| | | self.writer.add_scalar('Loss/val', loss.item(), |
| | | self.writer.add_scalar(f"rank{self.local_rank}, Loss/val", loss.item(), |
| | | epoch*len(self.dataloader_train) + batch_idx) |
| | | for key, var in stats.items(): |
| | | self.writer.add_scalar(f'{key}/val', var.item(), |
| | | self.writer.add_scalar(f'rank{self.local_rank}, {key}/val', var.item(), |
| | | epoch * len(self.dataloader_train) + batch_idx) |
| | | for key, var in speed_stats.items(): |
| | | self.writer.add_scalar(f'{key}/val', eval(var), |
| | | self.writer.add_scalar(f'rank{self.local_rank}, {key}/val', eval(var), |
| | | epoch * len(self.dataloader_train) + batch_idx) |