Update step. Args: model: Model instance or model name. optim: TODO. scheduler: TODO. scaler: TODO. loss_dict: TODO.
(self, model, optim, scheduler, scaler, loss_dict=None)
| 711 | loss.backward() |
| 712 | |
| 713 | def update_step(self, model, optim, scheduler, scaler, loss_dict=None): |
| 714 | """Update step. |
| 715 | |
| 716 | Args: |
| 717 | model: Model instance or model name. |
| 718 | optim: TODO. |
| 719 | scheduler: TODO. |
| 720 | scaler: TODO. |
| 721 | loss_dict: TODO. |
| 722 | """ |
| 723 | batch_idx = loss_dict["batch_idx"] |
| 724 | if self.use_deepspeed: |
| 725 | model.step() |
| 726 | else: |
| 727 | if (batch_idx + 1) % self.accum_grad == 0: |
| 728 | # Perform gradient clipping if it is set |
| 729 | if self.grad_clip > 0: |
| 730 | grad_norm = torch.nn.utils.clip_grad_norm_( |
| 731 | model.parameters(), |
| 732 | max_norm=self.grad_clip, |
| 733 | norm_type=self.grad_clip_type, |
| 734 | ) |
| 735 | if not torch.isfinite(grad_norm): |
| 736 | logging.warning( |
| 737 | f"The grad norm is {grad_norm}. Skipping updating the model." |
| 738 | ) |
| 739 | optim.zero_grad() # Reset gradients |
| 740 | return |
| 741 | |
| 742 | # Execute an optimization step (update model parameters) |
| 743 | if self.use_ddp or self.use_fsdp: |
| 744 | dist.barrier() |
| 745 | if scaler: |
| 746 | scaler.step(optim) |
| 747 | scaler.update() |
| 748 | else: |
| 749 | optim.step() |
| 750 | scheduler.step() |
| 751 | # Clear gradients for the next accumulation stage |
| 752 | optim.zero_grad(set_to_none=True) |
| 753 | |
| 754 | def validate_epoch( |
| 755 | self, |
no test coverage detected