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

オープン 初心者向け
#9,190 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 2 日以内に返信

@its-amann がすでに取り組んでいます。

2026年11月29日 から。

評価

難易度
2/5
見積もり時間
1〜3時間
初心者へのやさしさ
85/100
issue の種類
バグ
明瞭さ
明確に書かれている
活発さ
活発
技術スタック
python, pytorch

調査の方向性

deepspeed/runtime/engine.py の DeepSpeedEngine.step() と is_gradient_accumulation_boundary() から始め、issue の単独の再現コードを使用して、行を段階的に処理するときの例外を発生させます。_running_engine_step に例外安全なクリーンアップを追加し、その後、フラグが false で、失敗後に境界が False を返すことを確認します。issue には影響を受けるファイルとメソッドは特定されていますが、既存の回帰テストファイルはありません。

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
Python
スター
43.3k
フォーク
5.1k
平均マージ
5日 10時間
マージ済み PR(30日)
85

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

deepspeedai/DeepSpeed のほかの issue

deepspeedai/DeepSpeed の issue をすべて見る

似ている issue

Python の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。