Repository navigation
Record entry-point failures to the torchelastic error file - #6336
Conversation
|
@claude review this |
There was a problem hiding this comment.
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
torchtitan/train.py:22-24-- the inner@recordonTrainer.__init__/Trainer.train(and the torchft copies) is now redundant and should be deleted, the same way fluxinference()already was (see the inline comment).- PR description, test plan:
tests/unit_tests/cpu/test_flux_inference.pydoesn't exist. The file istests/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 onmain()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
@recordbehaves like this: each wrapper callserror_handler.record_exception(e), which rewrites the error file, and then re-raises. Withmain()decorated, the inner records onTrainer.__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 throughtorchtitan.train(run_train.shENTRYPOINT, torchft README and integration tests). Flux inference now constructsFluxTrainerunder the new@record main. Removing the inner records is roughly -8 lines. torchtitan/rl/train.pyis launched withpython3 -m, not torchrun, so it correctly gets no@record.- Tests: both tests in
tests/unit_tests/cpu/test_train_main.pyfail onmainfor the stated reasons. The config error is not recorded, and theOSErrorwould 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 ofrecordand 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.
| # Record exceptions (including config errors) to the torchelastic error file: | ||
| # https://pytorch.org/docs/stable/elastic/errors.html | ||
| @record |
There was a problem hiding this comment.
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.
| # Record exceptions (including config errors) to the torchelastic error file: | ||
| # https://pytorch.org/docs/stable/elastic/errors.html |
There was a problem hiding this comment.
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.
6ed5a2a to
c05812e
Compare
|
@claude review this |
There was a problem hiding this comment.
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
@recordonTrainer.__init__/train(core and torchft) and their imports: deleted. train.py:22-23comment: now says why the decorator sits onmain().- 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
@recorduses in the repo are the three entry points. The only code that constructs a trainer directly is fluxinference(), now under@record main, and a graph_trainer unit test.graph_trainer/precompile_main.pynever builds aTrainer, so it loses nothing.rl/train.pyis launched withpython -m, not torchrun. rl/generate.py. Its docstring launches it withtorchrun -m torchtitan.rl.generate, so@recordbelongs there. The import sits afteros.environ["VLLM_WORKER_MULTIPROC_METHOD"]is set, and torch is already imported by then throughapply_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
mainfor 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.
#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.
## 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).
Config loading in
train.pymain()ran outside the@record-decoratedTrainermethods, so config errors producederror_file: <N/A>and no traceback in the torchrun summary (e.g. https://github.com/pytorch/torchtitan/actions/runs/44839672553/job/136644722893).Fix:
@recordon the torchrun entry points:train.main, flux inferencemain, rlgenerate.@recordonTrainer.__init__/train(core and torchft). Custom torchrun entry points that buildTrainerdirectly need their own@record.trainer.close()fails during error cleanup, log it and re-raise the original exception. The outer@recordrewrites the error file, so a cleanup failure would otherwise replace the root cause.Notes:
SystemExit) are still not recorded;@recordonly records exceptions.main()and rlgenerateare decorator-only moves with no new tests (generateneeds 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 failingclose()both land inTORCHELASTIC_ERROR_FILE. Both tests fail without the fix.pytest tests/unit_tests/cpu/models/flux/test_flux_inference.py