[Bug] DeepSpeedEngine.step leaves _running_engine_step=True permanently on exception, corrupting is_gradient_accumulation_boundary()

Aperta Adatta ai principianti
#9,190 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 2 giorni

@its-amann ci sta già lavorando.

Dal 29/11/2026.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
85/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python, pytorch

Direzione di ricerca

Inizia in deepspeed/runtime/engine.py da DeepSpeedEngine.step() e is_gradient_accumulation_boundary(), usando la riproduzione autonoma dell'issue per attivare l'eccezione delle righe a lotti. Aggiungi una pulizia sicura in caso di eccezione per _running_engine_step, quindi verifica che il flag sia false e che il limite restituisca False dopo l'errore. L'issue identifica il file e i metodi interessati, ma non indica alcun file di test di regressione esistente.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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 at HEAD)
  • 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.

Lingua principale
Python
Stelle
43.3k
Fork
5.1k
Merge medio
5g 10h
PR unite (30g)
85

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di deepspeedai/DeepSpeed

Tutte le issue di deepspeedai/DeepSpeed

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.