Defines the training process for a single epoch with gradient accumulation. Args: epoch (int): The current epoch number.
(
self,
model=None,
optim=None,
scheduler=None,
scaler=None,
dataloader_train=None,
dataloader_val=None,
epoch=None,
**kwargs,
)
| 552 | dist.barrier() |
| 553 | |
| 554 | def train_epoch( |
| 555 | self, |
| 556 | model=None, |
| 557 | optim=None, |
| 558 | scheduler=None, |
| 559 | scaler=None, |
| 560 | dataloader_train=None, |
| 561 | dataloader_val=None, |
| 562 | epoch=None, |
| 563 | **kwargs, |
| 564 | ): |
| 565 | """ |
| 566 | Defines the training process for a single epoch with gradient accumulation. |
| 567 | Args: |
| 568 | epoch (int): The current epoch number. |
| 569 | """ |
| 570 | if self.use_ddp or self.use_fsdp or self.use_deepspeed: |
| 571 | dist.barrier() |
| 572 | logging.info(f"Train epoch: {epoch}, rank: {self.rank}\n") |
| 573 | model.train() |
| 574 | |
| 575 | # Set the number of steps for gradient accumulation |
| 576 | accum_grad = self.accum_grad |
| 577 | # Initialize the gradient accumulation |
| 578 | optim.zero_grad() |
| 579 | speed_stats = {} |
| 580 | |
| 581 | iterator_stop = torch.tensor(0).to(self.device) |
| 582 | |
| 583 | dataloader_train.batch_sampler.set_epoch(epoch) |
| 584 | time_beg = time.perf_counter() |
| 585 | time5 = time_beg |
| 586 | for batch_idx, batch in enumerate(dataloader_train): |
| 587 | self.batch_total += 1 |
| 588 | self.step_in_epoch += 1 |
| 589 | loss_dict = { |
| 590 | "speed_stats": {}, |
| 591 | "epoch": epoch, |
| 592 | "batch_idx": batch_idx, |
| 593 | "data_split_i": kwargs.get("data_split_i", 0), |
| 594 | "data_split_num": kwargs.get("data_split_num", 1), |
| 595 | "log_step": batch_idx + kwargs.get("start_step", 0), |
| 596 | "batch_total": self.batch_total, |
| 597 | "step_in_epoch": self.step_in_epoch, |
| 598 | } |
| 599 | |
| 600 | time1 = time.perf_counter() |
| 601 | loss_dict["speed_stats"]["data_load"] = f"{time1-time_beg:0.3f}" |
| 602 | |
| 603 | batch = to_device(batch, self.device, non_blocking=True) |
| 604 | |
| 605 | my_context = nullcontext |
| 606 | if self.use_ddp or self.use_fsdp: |
| 607 | my_context = model.no_sync if batch_idx % accum_grad != 0 else my_context |
| 608 | with my_context(): |
| 609 | time2 = time.perf_counter() |
| 610 | |
| 611 | self.forward_step(model, batch, loss_dict=loss_dict) |
nothing calls this directly
no test coverage detected