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)
| 3765 | self.global_samples += self.train_batch_size() |
| 3766 | |
| 3767 | def step(self, lr_kwargs=None): |
| 3768 | r"""Execute the weight update step after forward and backward propagation |
| 3769 | on effective_train_batch. |
| 3770 | |
| 3771 | In managed gradient accumulation (default), the optimizer update is applied only |
| 3772 | on the accumulation boundary tracked by the internal micro-step counter. With |
| 3773 | ``managed_gradient_accumulation=false``, every ``step()`` is the accumulation |
| 3774 | boundary: it finalizes the locally accumulated gradients and applies an update. |
| 3775 | """ |
| 3776 | assert not self.inside_no_sync_ctxt, \ |
| 3777 | "It is illegal to call Engine.step() inside no_sync context manager" |
| 3778 | |
| 3779 | see_memory_usage("Engine before step", force=self.memory_breakdown()) |
| 3780 | |
| 3781 | # Check early because self.global_steps is incremented at some point here. |
| 3782 | # TODO: Delay self.global_steps increment until very end of this function. |
| 3783 | flops_profiler_active = self.flops_profiler_enabled( |
| 3784 | ) and self.global_steps == self.flops_profiler_profile_step() and self.global_rank == 0 |
| 3785 | |
| 3786 | self._start_timers(self.engine_timers.step_timers) |
| 3787 | |
| 3788 | assert self.optimizer is not None and not isinstance(self.optimizer, DummyOptim), \ |
| 3789 | "must provide optimizer during init in order to use step" |
| 3790 | |
| 3791 | report_progress = False |
| 3792 | |
| 3793 | self._step_applied = False # assume False, will flip to True |
| 3794 | |
| 3795 | # Unmanaged mode: step() is the accumulation boundary. |
| 3796 | self._running_engine_step = True |
| 3797 | |
| 3798 | # Unmanaged boundary: stage 2/3 already reduced/partitioned per backward so only finalize (incl. offload); stage 0/1/DDP reduce here. |
| 3799 | if not self.managed_gradient_accumulation(): |
| 3800 | if self.zero_optimization_partition_gradients(): |
| 3801 | self.optimizer.finalize_gradient_accumulation_boundary() |
| 3802 | elif self.enable_backward_allreduce and not self.inside_no_sync_ctxt: |
| 3803 | self.allreduce_gradients() |
| 3804 | |
| 3805 | if self.zenflow: |
| 3806 | self.optimizer._sync_selective_optimizer_lr() |
| 3807 | if self.auto_update: |
| 3808 | self.update_interval += 1 |
| 3809 | |
| 3810 | # Update the model when we reach gradient accumulation boundaries |
| 3811 | if self.is_gradient_accumulation_boundary(): |
| 3812 | if self.checkpoint_engine.is_decoupled(): |
| 3813 | self._commit_decoupled_checkpoint() |
| 3814 | |
| 3815 | if any(dw_staging.has_staged_rows for dw_staging in getattr(self, "_autoep_dw_stagings", ())): |
| 3816 | raise RuntimeError("Expert weight gradients are still staged at an optimizer step, which would " |
| 3817 | "apply them one step late. deferred_dw_flush_period needs the backward that " |
| 3818 | "closes each gradient accumulation window to run through the engine.") |
| 3819 | |
| 3820 | # Written before _take_model_step() resets self.losses and advances global_samples |
| 3821 | if self.monitor.enabled and self.global_rank == 0 and self.losses is not None: |
| 3822 | self.monitor.write_events([("Train/Samples/train_loss", self.losses.item(), self.global_samples)]) |
| 3823 | |
| 3824 | self._take_model_step(lr_kwargs) |
no test coverage detected