Repository navigation
[train][torchft][doc] Document fault-tolerant training with torchft - #68464
JasonLi1909 wants to merge 13 commits into
Conversation
Add a user guide for fault-tolerant training with torchft, make TorchftConfig a public alpha API exported from ray.train.torch, add it to the PyTorch API reference, and cross-link it from the fault tolerance guide. Signed-off-by: JasonLi1909 <[email protected]>
There was a problem hiding this comment.
Code Review
This pull request integrates torchft into Ray Train to support per-step fault-tolerant PyTorch training, introducing TorchftConfig, user guides, and a quickstart example. The review feedback highlights critical corrections in the example script: initializing the loss variable to prevent a potential NameError if the training loop is skipped, and setting min_replica_size to 1 instead of min_replicas since each replica group in this DDP setup contains exactly one worker. Corresponding documentation updates and configuration cleanups are also recommended.
| model = nn.Linear(1, 1) | ||
| optimizer = torch.optim.SGD(model.parameters(), lr=1e-2) | ||
| loss_fn = nn.MSELoss() |
There was a problem hiding this comment.
If a replacement worker joins late (e.g., when the step count has already reached num_steps), the while manager.current_step() < num_steps: loop will be skipped entirely. In this case, the local variable loss will not be defined, causing a NameError when calling ray.train.report({"loss": loss.item()}) on line 130.
To prevent this, initialize loss to a default value (such as a NaN tensor) before the training loop.
| model = nn.Linear(1, 1) | |
| optimizer = torch.optim.SGD(model.parameters(), lr=1e-2) | |
| loss_fn = nn.MSELoss() | |
| model = nn.Linear(1, 1) | |
| optimizer = torch.optim.SGD(model.parameters(), lr=1e-2) | |
| loss_fn = nn.MSELoss() | |
| loss = torch.tensor(float("nan")) |
There was a problem hiding this comment.
This can't happen. A new Manager starts with self._step = 0 (manager.py at v0.2.0), and a replacement only heals inside the quorum that optimizer.zero_grad() starts in the loop body. So the loop always runs at least once when num_steps > 0, and loss is defined.
| manager = Manager( | ||
| pg=pg, | ||
| min_replica_size=min_replicas, | ||
| load_state_dict=load_state_dict, | ||
| state_dict=state_dict, | ||
| world_size=1, | ||
| rank=0, | ||
| replica_id=f"train_ddp_{world_rank}", | ||
| timeout=timedelta(seconds=60), | ||
| checkpoint_transport=transport, | ||
| ) |
There was a problem hiding this comment.
In torchft, min_replica_size is the minimum number of processes within a single replica group. Since each Ray Train worker is its own replica group of size 1 (DDP only), the replica group size is always 1. Therefore, min_replica_size must be set to 1.
Setting it to min_replicas (which is the number of replica groups, e.g., 2) is incorrect and will cause the manager to hang or fail when min_replicas > 1.
| manager = Manager( | |
| pg=pg, | |
| min_replica_size=min_replicas, | |
| load_state_dict=load_state_dict, | |
| state_dict=state_dict, | |
| world_size=1, | |
| rank=0, | |
| replica_id=f"train_ddp_{world_rank}", | |
| timeout=timedelta(seconds=60), | |
| checkpoint_transport=transport, | |
| ) | |
| manager = Manager( | |
| pg=pg, | |
| min_replica_size=1, | |
| load_state_dict=load_state_dict, | |
| state_dict=state_dict, | |
| world_size=1, | |
| rank=0, | |
| replica_id=f"train_ddp_{world_rank}", | |
| timeout=timedelta(seconds=60), | |
| checkpoint_transport=transport, | |
| ) |
There was a problem hiding this comment.
Not changing this. In torchft, min_replica_size counts replica groups, not processes within one group. In manager.py at v0.2.0, should_commit checks self.num_participants() >= self._min_replica_size, and num_participants() is documented as "the number of replicas participating in the current step" (set from the quorum's replica_world_size). Setting it to 1 would let a step commit with fewer replica groups than min_replicas. The CI-tested torchft_linear_example.py also passes the min_replicas value here. I reworded the guide in 260d09e to make this explicit.
| def train_func(config): | ||
| num_steps = config.get("num_steps", 100) | ||
| min_replicas = config.get("min_replicas", 1) |
There was a problem hiding this comment.
Since min_replica_size should always be 1 and doesn't depend on min_replicas, we can remove min_replicas from the training function configuration.
| def train_func(config): | |
| num_steps = config.get("num_steps", 100) | |
| min_replicas = config.get("min_replicas", 1) | |
| def train_func(config): | |
| num_steps = config.get("num_steps", 100) |
There was a problem hiding this comment.
Keeping min_replicas in the config, since min_replica_size should match it. See the reply on the Manager comment.
|
|
||
| trainer = TorchTrainer( | ||
| train_func, | ||
| train_loop_config={"num_steps": 100, "min_replicas": 1}, |
There was a problem hiding this comment.
Same as above: min_replicas is still used by the training function.
| - `bind`: The address the Lighthouse listens on. Ray Train defaults this to `"[::]:0"`, which picks a free port. | ||
| - `join_timeout_ms`, `quorum_tick_ms`, `heartbeat_timeout_ms`: Quorum timing parameters. See the [torchft documentation](https://github.com/meta-pytorch/torchft) for details. | ||
|
|
||
| Also pass `min_replica_size` with the same value to the `Manager` in your training function. |
There was a problem hiding this comment.
Correct the explanation for min_replica_size. In this DDP setup, each replica group contains exactly one worker, so min_replica_size must always be 1, regardless of the min_replicas setting in the Lighthouse.
| Also pass `min_replica_size` with the same value to the `Manager` in your training function. | |
| Also pass `min_replica_size=1` to the `Manager` in your training function, since each replica group contains exactly one worker. |
There was a problem hiding this comment.
min_replica_size counts replica groups, so it matches min_replicas rather than being 1. See the reply on the Manager comment. I reworded this sentence in 260d09e to say both settings count replica groups.
dstrodtman
left a comment
There was a problem hiding this comment.
Hi @JasonLi1909, thanks for writing this up. A torchft guide that reads this cleanly on first pass is a nice addition to the Train docs.
This is a docs-team style review from Douglas Strodtman (Anyscale docs). Claude Code assisted with it; I've read every comment and stand behind each one. The scope is style, grammar, and Ray docs conventions only, checked against Ray's writing-style guide. Final approval on anything technical, including the TorchftConfig API change, belongs to the Ray Train maintainers.
The guide is in good shape on structure: sentence-case headings, front matter description, labeled {ref} targets, a tested literalinclude, and the how-it-works section in prose. The inline suggestions are mostly colons used mid-sentence, a few imperative and active-voice fixes, a table lead-in, and two undefined acronyms. Each is click-acceptable and independent.
Questions for the Ray Train maintainers
These are technical, so I'm raising them rather than suggesting text:
- Lighthouse placement wording. The guide (L28, L126) says the Lighthouse runs on the node that runs the Ray Train controller, "usually the head node," which matches the
NodeAffinitySchedulingStrategypinned toray.get_runtime_context().get_node_id()in_maybe_create_lighthouse_actor. The newTorchftConfigdocstring says "on the head node" in two places, including thebinddescription. Should the docstring match the guide's more precise wording, since the API reference and the guide describe the same behavior? min_replica_size(L100 and the example). gemini-code-assist has an open thread arguingmin_replica_sizeshould be 1 rather than matchingmin_replicas. My suggestions don't touch that sentence or the example. A maintainer familiar with torchft'sManagersemantics should settle that thread before merge, because the guide states the current pairing as a rule.
From the docs side, no objection to merging once the maintainers are satisfied on the technical content.
Apply docs style suggestions to the torchft guide, clarify that min_replicas and min_replica_size both count replica groups, and match the TorchftConfig docstring's Lighthouse placement wording to the guide. Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
… start Signed-off-by: JasonLi1909 <[email protected]>
…d number the steps Number each torchft change in the example script with a Step comment that matches a subsection after the script, highlight the changed lines with emphasize-lines, and reword the quick start intro. Signed-off-by: JasonLi1909 <[email protected]>
…ents Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
Show the head node, Lighthouse, and replica groups during normal training, and the four steps of recovering from a worker failure. Signed-off-by: JasonLi1909 <[email protected]>
… recovery figures Place each figure after the text it illustrates, use Excalidraw's Excalifont like the other docs diagrams, show the Manager and model copy as their own boxes, and drop the in-diagram headings. Signed-off-by: JasonLi1909 <[email protected]>
Signed-off-by: JasonLi1909 <[email protected]>
Summary
Ray Train's torchft integration (#61156, #61475, #62651, first usable in Ray 2.56.0) has no user-facing docs, and
TorchftConfigisn't public: the only way to use the feature is to import fromray.train.v2.torch.torchft_config. This PR adds a user guide for fault-tolerant training with torchft and makesTorchftConfiga documented public alpha API. It supersedes the stale draft #63563, with the Ray Data content dropped because the Ray Data integration is not planned.Changes
doc/source/train/user-guides/torchft-fault-tolerance.md: concepts, requirements, quick start with walkthrough, Lighthouse configuration, behavior on failure (from thetest_torchft_linear_replica_failurecases), and alpha limitations.doc/source/train/doc_code/torchft_fault_tolerance.py, included withliteralinclude. It mirrors the CI-testedtorchft_linear_example.py. Excluded fromdoc_code_trainindoc/BUILD.bazel: the doctest image has no torchft, and the torchft CI step only runs//python/ray/train/...targets.TorchftConfig: add@PublicAPI(stability="alpha"), expand the docstring, and export fromray.train.torchunderis_v2_enabled(), mirroring the v2-only exports inray.train.__init__. torchft stays a lazy import, so importing the module doesn't require it.TorchftConfigtodoc/source/train/api/torch.mdunder "Trainer and configs".torchft_linear_example.pynow importsTorchftConfigfrom the public path.Tests
from ray.train.torch import TorchftConfigworks, instantiation works, and withRAY_TRAIN_V2_ENABLED=0the module still imports without exporting the symbol.pre-commit runon all changed files passes.pytest python/ray/train/v2/tests/test_torch_trainer.py -k torchft(torchft publishes Linux-only wheels, and this was prepared on macOS), and the full Sphinx build. The example mirrors the CI-tested script; please check the readthedocs preview for the rendered pages and theTorchftConfigautosummary page.torchft_config.py, thetest_torchft_linear*tests, and the torchft v0.2.0 sources (LighthouseServer signature,Optimizer.zero_grad/stepsemantics).Not a duplicate: no open PR covers torchft docs; the prior draft #63563 is closed and this supersedes it. AI assistance was used for this PR.