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

Đang mở Phù hợp với người mới
#9,190 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 2 ngày

@its-amann đang làm issue này rồi.

Từ ngày 29/11/2026.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
85/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python, pytorch
Lĩnh vực
machine-learning

Hướng nghiên cứu

Bắt đầu trong deepspeed/runtime/engine.py tại DeepSpeedEngine.step() và is_gradient_accumulation_boundary(), sử dụng bản tái hiện độc lập trong issue để kích hoạt ngoại lệ về các hàng theo giai đoạn. Thêm quy trình dọn dẹp an toàn khi xảy ra ngoại lệ cho _running_engine_step, sau đó xác minh cờ là false và ranh giới trả về False sau khi thất bại. Issue chỉ ra tệp và các phương thức bị ảnh hưởng, nhưng không có tệp kiểm thử hồi quy hiện có nào được nêu.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
Python
Star
43.3k
Fork
5.1k
Merge trung bình
5 ngày 10 giờ
Pull request đã merge (30 ngày)
85

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của deepspeedai/DeepSpeed

Tất cả issue của deepspeedai/DeepSpeed

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.