(self, lr_kwargs)
| 3586 | clip_grad_norm_(parameters=self.module.parameters(), max_norm=self.gradient_clipping(), mpu=self.mpu) |
| 3587 | |
| 3588 | def _take_model_step(self, lr_kwargs): |
| 3589 | if self.gradient_clipping() > 0.0: |
| 3590 | if self.torch_autocast_z0_gradscaler: |
| 3591 | # Unscale for gradient clipping |
| 3592 | self.torch_autocast_z0_gradscaler.unscale_(self.optimizer) |
| 3593 | if not (self.fp16_enabled() or self.bfloat16_enabled() or self.zero_optimization()): |
| 3594 | self.clip_fp32_gradients() |
| 3595 | if self.torch_autocast_z0_gradscaler: |
| 3596 | self.torch_autocast_z0_gradscaler.step(self.optimizer) |
| 3597 | self.torch_autocast_z0_gradscaler.update() |
| 3598 | else: |
| 3599 | self.optimizer.step() |
| 3600 | |
| 3601 | if hasattr(self.optimizer, '_global_grad_norm'): |
| 3602 | self._global_grad_norm = self.optimizer._global_grad_norm |
| 3603 | |
| 3604 | # zero grad in basic optimizer could be unreliable and may not exhibit |
| 3605 | # the behavior that we want |
| 3606 | if self.bfloat16_enabled(): |
| 3607 | # TODO: Temporary until bf16_optimizer and zero_optimizer are integrated |
| 3608 | if hasattr(self.optimizer, "zero_grad"): |
| 3609 | self.optimizer.zero_grad() |
| 3610 | else: |
| 3611 | self.zero_grad() |
| 3612 | elif self.zero_optimization() or self.fp16_enabled(): |
| 3613 | self.optimizer.zero_grad() |
| 3614 | else: |
| 3615 | self.zero_grad() |
| 3616 | |
| 3617 | # Check overflow here since in DS fp16 optimizer, the overflow is updated in above step() function. |
| 3618 | overflow = False |
| 3619 | if hasattr(self.optimizer, "overflow"): |
| 3620 | overflow = self.optimizer.overflow |
| 3621 | self._step_applied = not overflow |
| 3622 | |
| 3623 | if overflow: |
| 3624 | self.skipped_steps += 1 |
| 3625 | else: |
| 3626 | if self.lr_scheduler is not None: |
| 3627 | try: |
| 3628 | self.lr_scheduler.step(**(lr_kwargs or {})) |
| 3629 | except TypeError: |
| 3630 | # XXX Hack to work with Megatron 2.0 and DeepSpeed pipelines. |
| 3631 | # We don't currently have a way to specify lr_kwargs from |
| 3632 | # pipe_engine.train_batch() |
| 3633 | self.lr_scheduler.step(self.train_batch_size()) |
| 3634 | |
| 3635 | if self.steps_per_print() is not None: |
| 3636 | report_progress = self.global_rank == 0 if self.global_rank else True |
| 3637 | if report_progress and (self.global_steps + 1) % self.steps_per_print() == 0: |
| 3638 | self._report_progress(self.global_steps + 1) |
| 3639 | |
| 3640 | self.losses = None |
| 3641 | self.global_steps += 1 |
| 3642 | if not self.managed_gradient_accumulation(): |
| 3643 | # Caller owns the boundary: count actual micro-batches since last step(), not the fixed train_batch_size(). |
| 3644 | samples_per_micro_batch = self.train_batch_size() // self.gradient_accumulation_steps() |
| 3645 | self.global_samples += samples_per_micro_batch * self._unmanaged_backward_count |
no test coverage detected