Skip to content

Catch wrapped OOM to trigger the mem. snapshot - #4580

Merged
tianyu-l merged 2 commits into
pytorch:mainfrom
francesco-bertolotti:f14-OOM-snapshot-pp
Sep 11, 2026
Merged

tianyu-l merged 2 commits into
pytorch:mainfrom
francesco-bertolotti:f14-OOM-snapshot-pp

Conversation

@francesco-bertolotti

Copy link
Copy Markdown
Collaborator

Dump the memory snapshot when an OOM is wrapped by another exception

With pipeline parallelism enabled, OOMs can be wrapped in a RuntimeError by stage_backward, so Profiler.__exit__ no longer recognizes the exception as a torch.OutOfMemoryError and 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 a seen set to guard against cycles. This preserves existing behavior while also covering wrapped OOMs. Reproduced with pp=2, dp_shard=32, ep=4

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Sep 10, 2026
Comment thread torchtitan/tools/profiler.py Outdated
return self

@staticmethod
def _caused_by_oom(exc: BaseException | None) -> bool:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can this be an inline embedded function rather than a static method?

@tianyu-l
tianyu-l merged commit 773e16e into pytorch:main Sep 11, 2026
14 checks passed
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants