[Bug] DeepSpeedEngine.step leaves _running_engine_step=True permanently on exception, corrupting is_gradient_accumulation_boundary()
Summary
In deepspeed/runtime/engine.py:3656-3659,3796-3888 (commit a695f646974c6c92da2a9028bbafde8bb28bad1e), DeepSpeedEngine.step() sets self._running_engine_step = True at line 3796 and only resets self._running_engine_step = False at line 3888 with no try ... finally: guard:
# Unmanaged mode: step() is the accumulation boundary.
self._running_engine_step = True
...
if any(dw_staging.has_staged_rows for dw_staging in getattr(self, "_autoep_dw_stagings", ())):
raise RuntimeError("Expert weight gradients are still staged at an optimizer step...")
...
self._take_model_step(lr_kwargs)
...
self._running_engine_step = False
Meanwhile, DeepSpeedEngine.is_gradient_accumulation_boundary() (lines 3656-3659) returns self._running_engine_step directly when managed_gradient_accumulation() is False:
if not self.managed_gradient_accumulation():
# Unmanaged mode: step() is the accumulation boundary (set only while step() runs).
return self._running_engine_step
If any exception is raised inside step() (e.g., the staged-rows RuntimeError at line 3816, a checkpoint commit failure, an optimizer step error, or a transient OOM) and caught by a caller/recovery loop, self._running_engine_step stays permanently True. Every subsequent backward() query to is_gradient_accumulation_boundary() will falsely report True even though step() is not running, corrupting gradient reduction/partitioning decisions in unmanaged gradient accumulation mode. (Contrast with DeepSpeedEngine.backward(), which wraps _running_engine_backward_count in try ... finally.)
Affected Location
- Repository:
deepspeedai/DeepSpeed(a695f646974c6c92da2a9028bbafde8bb28bad1e, present atHEAD) - File & lines:
deepspeed/runtime/engine.py:3656-3659(is_gradient_accumulation_boundary),3796-3888(step)
Minimal Standalone Reproduction
eng = DeepSpeedEngine.__new__(DeepSpeedEngine)
eng.inside_no_sync_ctxt = False
eng._is_gradient_accumulation_boundary = None
eng._running_engine_step = False
eng.zenflow = False
eng.micro_steps = 0
eng.global_steps = 0
eng.global_rank = 0
eng.enable_backward_allreduce = False
eng.memory_breakdown = lambda: False
eng.flops_profiler_enabled = lambda: False
eng.managed_gradient_accumulation = lambda: False
eng.zero_optimization_partition_gradients = lambda: True
# Minimal collaborator objects triggering line 3816 RuntimeError in upstream step()
eng.engine_timers = type("T", (), {"step_timers": []})()
eng.optimizer = type("Opt", (), {"finalize_gradient_accumulation_boundary": lambda self: None})()
eng.checkpoint_engine = type("Ckpt", (), {"is_decoupled": lambda self: False})()
eng._autoep_dw_stagings = (type("Stage", (), {"has_staged_rows": True})(),)
print("boundary_before_step:", eng.is_gradient_accumulation_boundary())
try:
eng.step()
except RuntimeError as exc:
print("step_error:", exc)
print("running_engine_step_after_exception:", eng._running_engine_step)
print("boundary_after_exception:", eng.is_gradient_accumulation_boundary())
Observed behavior / output:
boundary_before_step: False
step_error: Expert weight gradients are still staged at an optimizer step, which would apply them one step late. deferred_dw_flush_period needs the backward that closes each gradient accumulation window to run through the engine.
running_engine_step_after_exception: True
boundary_after_exception: True
Suggested Fix
Wrap the self._running_engine_step = True .. self._running_engine_step = False region in DeepSpeedEngine.step() inside a try: ... finally: self._running_engine_step = False block so _running_engine_step is always cleared even when step() exits via an exception.
@tohtana can I take this? plan is to wrap the step() region in try/finally so _running_engine_step always gets cleared when step raises, plus a test that forces a failure and checks the next backward still works