(self, lr_kwargs)
| 3663 | clip_grad_norm_(parameters=self.module.parameters(), max_norm=self.gradient_clipping(), mpu=self.mpu) |
| 3664 | |
| 3665 | def _take_model_step(self, lr_kwargs): |
| 3666 | if self.gradient_clipping() > 0.0: |
| 3667 | if self.torch_autocast_z0_gradscaler: |
| 3668 | # Unscale for gradient clipping |
| 3669 | self.torch_autocast_z0_gradscaler.unscale_(self.optimizer) |
| 3670 | if not (self.fp16_enabled() or self.bfloat16_enabled() or self.zero_optimization()): |
| 3671 | self.clip_fp32_gradients() |
| 3672 | if self.torch_autocast_z0_gradscaler: |
| 3673 | self.torch_autocast_z0_gradscaler.step(self.optimizer) |
| 3674 | self.torch_autocast_z0_gradscaler.update() |
| 3675 | else: |
| 3676 | self.optimizer.step() |
| 3677 | |
| 3678 | if hasattr(self.optimizer, '_global_grad_norm'): |
| 3679 | self._global_grad_norm = self.optimizer._global_grad_norm |
| 3680 | |
| 3681 | # zero grad in basic optimizer could be unreliable and may not exhibit |
| 3682 | # the behavior that we want |
| 3683 | if self.bfloat16_enabled(): |
| 3684 | # TODO: Temporary until bf16_optimizer and zero_optimizer are integrated |
| 3685 | if hasattr(self.optimizer, "zero_grad"): |
| 3686 | self.optimizer.zero_grad() |
| 3687 | else: |
| 3688 | self.zero_grad() |
| 3689 | elif self.zero_optimization() or self.fp16_enabled(): |
| 3690 | self.optimizer.zero_grad() |
| 3691 | else: |
| 3692 | self.zero_grad() |
| 3693 | |
| 3694 | # Check overflow here since in DS fp16 optimizer, the overflow is updated in above step() function. |
| 3695 | overflow = False |
| 3696 | if hasattr(self.optimizer, "overflow"): |
| 3697 | overflow = self.optimizer.overflow |
| 3698 | self._step_applied = not overflow |
| 3699 | |
| 3700 | if overflow: |
| 3701 | self.skipped_steps += 1 |
| 3702 | else: |
| 3703 | if self.lr_scheduler is not None: |
| 3704 | try: |
| 3705 | self.lr_scheduler.step(**(lr_kwargs or {})) |
| 3706 | except TypeError: |
| 3707 | # XXX Hack to work with Megatron 2.0 and DeepSpeed pipelines. |
| 3708 | # We don't currently have a way to specify lr_kwargs from |
| 3709 | # pipe_engine.train_batch() |
| 3710 | self.lr_scheduler.step(self.train_batch_size()) |
| 3711 | |
| 3712 | if self.steps_per_print() is not None: |
| 3713 | report_progress = self.global_rank == 0 if self.global_rank else True |
| 3714 | if report_progress and (self.global_steps + 1) % self.steps_per_print() == 0: |
| 3715 | self._report_progress(self.global_steps + 1) |
| 3716 | |
| 3717 | self.losses = None |
| 3718 | self.global_steps += 1 |
| 3719 | if not self.managed_gradient_accumulation(): |
| 3720 | # Caller owns the boundary: count actual micro-batches since last step(), not the fixed train_batch_size(). |
| 3721 | samples_per_micro_batch = self.train_batch_size() // self.gradient_accumulation_steps() |
| 3722 | self.global_samples += samples_per_micro_batch * self._unmanaged_backward_count |
no test coverage detected