Repository navigation
Catch wrapped OOM to trigger the mem. snapshot - #4580
Merged
Merged
Conversation
francesco-bertolotti
requested review from
fegin,
tianyu-l,
wconstab and
wwwjn
as code owners
September 10, 2026 16:43
tianyu-l
reviewed
Sep 10, 2026
| return self | ||
|
|
||
| @staticmethod | ||
| def _caused_by_oom(exc: BaseException | None) -> bool: |
Contributor
There was a problem hiding this comment.
nit: can this be an inline embedded function rather than a static method?
francesco-bertolotti
force-pushed
the
f14-OOM-snapshot-pp
branch
from
September 11, 2026 08:13
b445c3e to
3a42119
Compare
francesco-bertolotti
force-pushed
the
f14-OOM-snapshot-pp
branch
from
September 11, 2026 08:16
3a42119 to
ed112e3
Compare
tianyu-l
approved these changes
Sep 11, 2026
shuhuayu
added a commit
that referenced
this pull request
Nov 10, 2026
Trainer.train() closes the profiler through TrainingEngine.close_profiler(), which always called Profiler.__exit__(None, None, None). Since #4773 replaced the profiler `with` block with start_profiler()/close_profiler(), the OOM exit-snapshot branch from #4580 has been unreachable for the main trainer. close_profiler() now takes the exception that ended training, and train() passes it on failure and nothing on normal exit.
shuhuayu
added a commit
that referenced
this pull request
Nov 11, 2026
Independent PR on `main`. ## Summary (human-written) On main, an OOM during training closes the profiler as if shutdown were clean, so the memory profiler never writes its exit snapshot; passing the exception to the profiler fixes that, which is how I debugged several full-scale Kimi K3 OOMs. ## Why #4580 added an OOM branch to `Profiler.__exit__` that writes an exit memory snapshot when the exception (or anything in its cause chain) is an `OutOfMemoryError`. #4773 then replaced the profiler `with` block in `Trainer.train()` with `engine.start_profiler()` plus `engine.close_profiler()` in a `finally`, and `close_profiler()` always calls `Profiler.__exit__(None, None, None)`. Since then the OOM branch is unreachable for the main trainer. The exception itself still propagates; only the snapshot is lost. ## What changed - `TrainingEngine.close_profiler(exc: BaseException | None = None)` passes `exc` to `Profiler.__exit__`. - `Trainer.train()` replaces its `finally` with `except BaseException as e: close_profiler(e); raise` and `else: close_profiler()`. - `engine.close()` still calls `close_profiler()` with no exception. It only reaches an open profiler when `train()` never started it, so nothing is lost. ## Example ```text train_step raises an OOM (possibly wrapped in a RuntimeError by PP) before: close_profiler() -> Profiler.__exit__(None, None, None); no snapshot after: close_profiler(e) -> Profiler.__exit__(type(e), e, tb); snapshot written once the same exception object still propagates to the caller ``` The exception is passed explicitly rather than read from `sys.exc_info()`, which would also return an exception an outer frame is still handling (e.g. a caller that catches an OOM and retries `train()` would get a bogus snapshot). This matches the `close(exc)` shape in #5871. ## Scope - RL is not covered. `rl/trainer/trainer.py` starts the profiler through the shared engine but closes it from its async `close()` endpoint, which has no training exception to pass, so RL OOMs still get no exit snapshot. - #5871 rewrites `observability/profiler.py` and touches `tests/unit_tests/cpu/test_trainer.py`; whichever lands second needs a rebase. - CUDA-graph stream-lane rendering is not part of this PR; #5526 covers it. ## User-facing changes | Before | After | | --- | --- | | An OOM in `Trainer.train()` writes no exit memory snapshot. | The existing OOM branch writes it once. | ## Numerics None. Only profiler shutdown on a training failure changes. ## Testing `test_train_passes_failure_to_profiler_close` runs the real `Trainer.train`, `TrainingEngine.close_profiler`, and `Profiler.__exit__` with the snapshot writer mocked, for three cases: normal exit (no snapshot), a plain OOM, and an OOM wrapped in a RuntimeError as PP does (snapshot once, and the same exception object is re-raised). All 57 tests in `tests/unit_tests/cpu/test_trainer.py` pass; `pre-commit` passes. Each of these breakages fails the test: `train()` not passing the exception, `close_profiler()` ignoring it, a snapshot on every close, and swallowing the exception after closing. Not verified: writing a real GPU OOM snapshot.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Dump the memory snapshot when an OOM is wrapped by another exception
With pipeline parallelism enabled, OOMs can be wrapped in a
RuntimeErrorbystage_backward, soProfiler.__exit__no longer recognizes the exception as atorch.OutOfMemoryErrorand skips the memory snapshot. This can happen with any caller that catches and re-raises an OOM.Walk the exception chain instead of checking only the outermost exception. Check
__cause__first and fall back to__context__, with aseenset to guard against cycles. This preserves existing behavior while also covering wrapped OOMs. Reproduced withpp=2, dp_shard=32, ep=4