Skip to content

Record entry-point failures to the torchelastic error file - #6336

Merged
d4l3k merged 1 commit into
mainfrom
d4l3k/titan-record
Nov 24, 2026
Merged

d4l3k merged 1 commit into
mainfrom
d4l3k/titan-record

Conversation

@d4l3k

@d4l3k d4l3k commented Nov 23, 2026 •

Copy link
Copy Markdown
Member

Config loading in train.py main() ran outside the @record-decorated Trainer methods, so config errors produced error_file: <N/A> and no traceback in the torchrun summary (e.g. https://github.com/pytorch/torchtitan/actions/runs/44839672553/job/136644722893).

Fix:

  • @record on the torchrun entry points: train.main, flux inference main, rl generate.
  • Drop the now-redundant inner @record on Trainer.__init__/train (core and torchft). Custom torchrun entry points that build Trainer directly need their own @record.
  • If trainer.close() fails during error cleanup, log it and re-raise the original exception. The outer @record rewrites the error file, so a cleanup failure would otherwise replace the root cause.

Notes:

  • argparse errors (SystemExit) are still not recorded; @record only records exceptions.
  • Flux main() and rl generate are decorator-only moves with no new tests (generate needs vLLM).

Lines: +17/-10 implementation, +67 tests.

Test plan:

  • pytest tests/unit_tests/cpu/test_train_main.py: a config error and a train error with a failing close() both land in TORCHELASTIC_ERROR_FILE. Both tests fail without the fix.
  • pytest tests/unit_tests/cpu/models/flux/test_flux_inference.py

@d4l3k

d4l3k commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@claude review this

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 23, 2026
@claude

claude Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @d4l3k's task in 2m 43s —— View job


Approve -- please address the items below before merging (delete the now-redundant inner @record decorators on the Trainer methods; fix the test path in the description). Review

@claude claude Bot left a comment

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.

Verdict: Approve -- please address the items below before merging. The fix is at the right layer (the torchrun entry point), and the close() guard is needed exactly because of the new outer @record. A maintainer still needs to review, since this touches train.py.

What this PR does. It moves torchelastic error recording up to the process entry points (train.main, flux main, rl generate). Before this, ConfigLoader().load(), init_structured_logger, and the seed-checkpoint ValueError all ran outside any @record. It also keeps a failing trainer.close() during error cleanup from replacing the original exception. Without that guard, the outer @record would write the close() error into the error file instead of the real failure.

Example.

# main: torchrun -m torchtitan.train --module ... --config bad_name
ConfigLoader().load() -> ValueError       # not inside any @record
torchrun summary: error_file: <N/A>, no traceback

# PR: same command
@record main -> TORCHELASTIC_ERROR_FILE = {"message": "ValueError: ..."}

# PR: trainer.train() raises RuntimeError, then trainer.close() raises OSError
main:  inner @record on Trainer.train writes RuntimeError (no outer record)
PR without the try/except: outer @record would overwrite it with OSError
PR:    close() error is logged, RuntimeError is re-raised and recorded

Must fix

  1. torchtitan/train.py:22-24 -- the inner @record on Trainer.__init__/Trainer.train (and the torchft copies) is now redundant and should be deleted, the same way flux inference() already was (see the inline comment).
  2. PR description, test plan: tests/unit_tests/cpu/test_flux_inference.py doesn't exist. The file is tests/unit_tests/cpu/models/flux/test_flux_inference.py.

Should consider

  • torchtitan/train.py:22-23 -- the comment restates what the decorator does. Say why it has to be on main() instead (see inline).
  • torchtitan/experiments/graph_trainer/precompile_main.py:337 -- this is another torchrun entry point that loads config with no @record. It is an experiment, so leaving it out of this PR is fine. The description's "the torchrun entry points" is just slightly broader than what the PR covers.
Details
  • Nested @record behaves like this: each wrapper calls error_handler.record_exception(e), which rewrites the error file, and then re-raises. With main() decorated, the inner records on Trainer.__init__/train (trainer.py:154,498) and torchft (experiments/torchft/trainer.py:171,510) only repeat the same write. All in-repo launches of these trainers go through torchtitan.train (run_train.sh ENTRYPOINT, torchft README and integration tests). Flux inference now constructs FluxTrainer under the new @record main. Removing the inner records is roughly -8 lines.
  • torchtitan/rl/train.py is launched with python3 -m, not torchrun, so it correctly gets no @record.
  • Tests: both tests in tests/unit_tests/cpu/test_train_main.py fail on main for the stated reasons. The config error is not recorded, and the OSError would propagate and be recorded. They guard an invariant that would otherwise break silently (losing the root cause in the torchrun summary), and they are CPU-only and cheap.
  • SystemExit (argparse) is still not recorded. That is a limitation of record and is correctly called out in the description.

Not verified: an actual torchrun failure summary. This review relies on reading the code and on torchelastic record semantics.

Comment thread torchtitan/train.py Outdated
Comment on lines +22 to +24
# Record exceptions (including config errors) to the torchelastic error file:
# https://pytorch.org/docs/stable/elastic/errors.html
@record

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.

Now that main() has @record, the inner decorators on Trainer.__init__ (torchtitan/trainer.py:153-154) and Trainer.train (torchtitan/trainer.py:498) are redundant. The same goes for torchtitan/experiments/torchft/trainer.py:171,510, since torchft also launches through run_train.sh -> torchtitan.train. Each one writes the error file and re-raises, and then this outer @record overwrites the file with the same exception. You already did this cleanup for flux: @record moved off inference() onto main(). Doing the same here keeps one rule, "@record lives on the torchrun entry point," and makes the PR smaller instead of larger. Fix: delete those four decorators, their comment, and the now-unused record imports in both trainer files.

Comment thread torchtitan/train.py Outdated
Comment on lines +22 to +23
# Record exceptions (including config errors) to the torchelastic error file:
# https://pytorch.org/docs/stable/elastic/errors.html

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: @record is a standard torchelastic decorator, so this comment mostly restates what the decorator does. If you keep a comment, make it say the non-obvious part: the decorator has to sit on main() and not on Trainer methods, because config loading and the seed-checkpoint ValueError run outside Trainer.

Config loading in train.py main() ran outside the @record-decorated Trainer methods, so torchrun reported error_file: <N/A> and no traceback for config errors (e.g. invalid expert_parallel_degree). Decorate the torchrun entry points (train.main, flux inference main, rl generate) with @record, and keep the original exception as the recorded root cause if trainer.close() fails during error cleanup.
@d4l3k
d4l3k force-pushed the d4l3k/titan-record branch from 6ed5a2a to c05812e Compare November 23, 2026 19:57
@d4l3k

d4l3k commented Nov 23, 2026

Copy link
Copy Markdown
Member Author

@claude review this

@claude

claude Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @d4l3k's task in 1m 5s —— View job


Approve: the earlier review items are all fixed and I found no new problems. Review

@claude claude Bot left a comment

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.

Verdict: Approve -- every item from the earlier review is fixed at HEAD, and I found no new problems. A maintainer still needs to review, since this touches train.py and trainer.py.

What this PR does. It moves torchelastic @record from the Trainer.__init__/train methods up to the torchrun entry points (train.main, flux inference main, rl generate). Config loading, structured-logger init, and the seed-checkpoint ValueError now reach the error file. It also stops a failing trainer.close() during error cleanup from replacing the original exception.

Example.

# torchrun -m torchtitan.train ... with a bad config
main: ConfigLoader().load() raises outside any @record -> error_file: <N/A>
PR:   @record main -> TORCHELASTIC_ERROR_FILE has "ValueError: ..."

# trainer.train() raises RuntimeError, then close() raises OSError
PR:   OSError is logged, RuntimeError is re-raised and recorded

Earlier review items, now resolved:

  • Inner @record on Trainer.__init__/train (core and torchft) and their imports: deleted.
  • train.py:22-23 comment: now says why the decorator sits on main().
  • Test path in the description: corrected to tests/unit_tests/cpu/models/flux/test_flux_inference.py.
Details
  • Callsite audit for the removed inner records. After this PR, the only @record uses in the repo are the three entry points. The only code that constructs a trainer directly is flux inference(), now under @record main, and a graph_trainer unit test. graph_trainer/precompile_main.py never builds a Trainer, so it loses nothing. rl/train.py is launched with python -m, not torchrun.
  • rl/generate.py. Its docstring launches it with torchrun -m torchtitan.rl.generate, so @record belongs there. The import sits after os.environ["VLLM_WORKER_MULTIPROC_METHOD"] is set, and torch is already imported by then through apply_env_defaults/_env, so the import order does not matter.
  • Success path. If trainer.close() fails after a successful run, the exception still propagates and is recorded, which is correct.
  • Tests. The two CPU tests are cheap, and each one fails on main for the reason it states. They guard an invariant that would otherwise break silently: losing the root cause in the torchrun summary.

Not verified: an actual torchrun failure summary. This review relies on reading the code and on torchelastic record semantics.

@d4l3k
d4l3k marked this pull request as ready for review November 23, 2026 23:29
@pytorch-bot pytorch-bot Bot added the ciflow/fake-pg Run 1-GPU Fake PG integration tests label Nov 23, 2026
@d4l3k
d4l3k merged commit 34ac322 into main Nov 24, 2026
35 checks passed
acisseJZhong added a commit that referenced this pull request Nov 24, 2026
#6336 removed `@record` from `Trainer.train`, so `Trainer.train.__wrapped__`
no longer exists and `test_train_passes_failure_to_profiler_close` (added in
#5679) fails with `AttributeError: 'function' object has no attribute
'__wrapped__'`, breaking CPU Unit Test on main. The test only used
`__wrapped__` to bypass `@record`; call `Trainer.train` directly.

Test plan:
```
pytest tests/unit_tests/cpu/test_trainer.py tests/unit_tests/cpu/test_train_main.py
```
Before: 3 failed. After: 59 passed.

This PR was authored with an AI assistant.
acisseJZhong added a commit that referenced this pull request Nov 24, 2026
## Summary (human-written)
<!-- Write this yourself, not with an AI tool. 2-5 sentences: the
problem, why
     this approach, and what you checked yourself. -->
fix CI failing test

## Agent details (AI-generated, to be reviewed by @acisseJZhong)

> AI-generated by an AI agent on behalf of @acisseJZhong.
>
> **Why.** [CPU Unit
Test](https://github.com/pytorch/torchtitan/actions/runs/45367705910)
fails on main since #6336:
> ```
> FAILED
tests/unit_tests/cpu/test_trainer.py::test_train_passes_failure_to_profiler_close[none|oom|wrapped_oom]
>   AttributeError: 'function' object has no attribute '__wrapped__'
> ```
> #6336 moved `@record` from `Trainer.train` to `train.main`, so
`Trainer.train.__wrapped__` no longer exists. The test (from #5679) only
used `__wrapped__` to bypass `@record`.
>
> **What changed.** Test-only: call `Trainer.train(trainer)` instead of
`Trainer.train.__wrapped__(trainer)`.
>
> **Testing.** `pytest tests/unit_tests/cpu/test_trainer.py
tests/unit_tests/cpu/test_train_main.py`: 3 failed before, 59 passed
after (torch 2.16.0.dev20261124+cpu).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ciflow/fake-pg Run 1-GPU Fake PG integration tests ciflow/rl 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