r"""Execute the weight update step after forward and backward propagation on effective_train_batch. In managed gradient accumulation (default), the optimizer update is applied only on the accumulation boundary tracked by the internal micro-step counter. With ``manage
(self, lr_kwargs=None)
| 3648 | self.global_samples += self.train_batch_size() |
| 3649 | |
| 3650 | def step(self, lr_kwargs=None): |
| 3651 | r"""Execute the weight update step after forward and backward propagation |
| 3652 | on effective_train_batch. |
| 3653 | |
| 3654 | In managed gradient accumulation (default), the optimizer update is applied only |
| 3655 | on the accumulation boundary tracked by the internal micro-step counter. With |
| 3656 | ``managed_gradient_accumulation=false``, every ``step()`` is the accumulation |
| 3657 | boundary: it finalizes the locally accumulated gradients and applies an update. |
| 3658 | """ |
| 3659 | assert not self.inside_no_sync_ctxt, \ |
| 3660 | "It is illegal to call Engine.step() inside no_sync context manager" |
| 3661 | |
| 3662 | see_memory_usage("Engine before step", force=self.memory_breakdown()) |
| 3663 | |
| 3664 | # Check early because self.global_steps is incremented at some point here. |
| 3665 | # TODO: Delay self.global_steps increment until very end of this function. |
| 3666 | flops_profiler_active = self.flops_profiler_enabled( |
| 3667 | ) and self.global_steps == self.flops_profiler_profile_step() and self.global_rank == 0 |
| 3668 | |
| 3669 | self._start_timers(self.engine_timers.step_timers) |
| 3670 | |
| 3671 | assert self.optimizer is not None and not isinstance(self.optimizer, DummyOptim), \ |
| 3672 | "must provide optimizer during init in order to use step" |
| 3673 | |
| 3674 | report_progress = False |
| 3675 | |
| 3676 | self._step_applied = False # assume False, will flip to True |
| 3677 | |
| 3678 | # Unmanaged mode: step() is the accumulation boundary. |
| 3679 | self._running_engine_step = True |
| 3680 | |
| 3681 | # Unmanaged boundary: stage 2/3 already reduced/partitioned per backward so only finalize (incl. offload); stage 0/1/DDP reduce here. |
| 3682 | if not self.managed_gradient_accumulation(): |
| 3683 | if self.zero_optimization_partition_gradients(): |
| 3684 | self.optimizer.finalize_gradient_accumulation_boundary() |
| 3685 | elif self.enable_backward_allreduce and not self.inside_no_sync_ctxt: |
| 3686 | self.allreduce_gradients() |
| 3687 | |
| 3688 | if self.zenflow: |
| 3689 | self.optimizer._sync_selective_optimizer_lr() |
| 3690 | if self.auto_update: |
| 3691 | self.update_interval += 1 |
| 3692 | |
| 3693 | # Update the model when we reach gradient accumulation boundaries |
| 3694 | if self.is_gradient_accumulation_boundary(): |
| 3695 | if self.checkpoint_engine.is_decoupled(): |
| 3696 | self._commit_decoupled_checkpoint() |
| 3697 | |
| 3698 | self._take_model_step(lr_kwargs) |
| 3699 | |
| 3700 | report_progress = self.global_rank == 0 if self.global_rank else True |
| 3701 | |
| 3702 | if self.zenflow: |
| 3703 | self._zenflow_step(lr_kwargs) |
| 3704 | |
| 3705 | self.tput_timer.stop(global_step=self.is_gradient_accumulation_boundary(), report_speed=report_progress) |
| 3706 | |
| 3707 | self._stop_timers(self.engine_timers.step_timers) |
no test coverage detected