Skip to content

[train][torchft][doc] Document fault-tolerant training with torchft - #68464

Open
JasonLi1909 wants to merge 13 commits into
ray-project:masterfrom
JasonLi1909:train-torchft-docs
Open

JasonLi1909 wants to merge 13 commits into
ray-project:masterfrom
JasonLi1909:train-torchft-docs

Conversation

@JasonLi1909

@JasonLi1909 JasonLi1909 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Ray Train's torchft integration (#61156, #61475, #62651, first usable in Ray 2.56.0) has no user-facing docs, and TorchftConfig isn't public: the only way to use the feature is to import from ray.train.v2.torch.torchft_config. This PR adds a user guide for fault-tolerant training with torchft and makes TorchftConfig a 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

  • New user guide doc/source/train/user-guides/torchft-fault-tolerance.md: concepts, requirements, quick start with walkthrough, Lighthouse configuration, behavior on failure (from the test_torchft_linear_replica_failure cases), and alpha limitations.
  • New doc/source/train/doc_code/torchft_fault_tolerance.py, included with literalinclude. It mirrors the CI-tested torchft_linear_example.py. Excluded from doc_code_train in doc/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 from ray.train.torch under is_v2_enabled(), mirroring the v2-only exports in ray.train.__init__. torchft stays a lazy import, so importing the module doesn't require it.
  • Add TorchftConfig to doc/source/train/api/torch.md under "Trainer and configs".
  • Cross-link the new guide from the fault tolerance guide's worker fault tolerance section.
  • torchft_linear_example.py now imports TorchftConfig from the public path.

Tests

  • Import check in a clean venv without torchft: from ray.train.torch import TorchftConfig works, instantiation works, and with RAY_TRAIN_V2_ENABLED=0 the module still imports without exporting the symbol.
  • pre-commit run on all changed files passes.
  • Not run locally: the doc example end to end and 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 the TorchftConfig autosummary page.
  • Guide claims were verified against torchft_config.py, the test_torchft_linear* tests, and the torchft v0.2.0 sources (LighthouseServer signature, Optimizer.zero_grad/step semantics).

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.

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]>
@JasonLi1909
JasonLi1909 requested review from a team as code owners November 23, 2026 02:23

@gemini-code-assist gemini-code-assist 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.

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.

Comment on lines +49 to +51
model = nn.Linear(1, 1)
optimizer = torch.optim.SGD(model.parameters(), lr=1e-2)
loss_fn = nn.MSELoss()

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.

high

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.

Suggested change
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"))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +79 to +89
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,
)

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.

high

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.

Suggested change
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,
)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

Comment on lines +41 to +43
def train_func(config):
num_steps = config.get("num_steps", 100)
min_replicas = config.get("min_replicas", 1)

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.

medium

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.

Suggested change
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)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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},

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.

medium

Remove min_replicas from train_loop_config as it is no longer used by the training function.

Suggested change
train_loop_config={"num_steps": 100, "min_replicas": 1},
train_loop_config={"num_steps": 100},

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

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.

medium

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.

Suggested change
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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

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.

@ray-gardener ray-gardener Bot added docs An issue or change related to documentation train Ray Train Related Issue labels Nov 23, 2026

@dstrodtman dstrodtman 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.

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:

  1. 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 NodeAffinitySchedulingStrategy pinned to ray.get_runtime_context().get_node_id() in _maybe_create_lighthouse_actor. The new TorchftConfig docstring says "on the head node" in two places, including the bind description. Should the docstring match the guide's more precise wording, since the API reference and the guide describe the same behavior?
  2. min_replica_size (L100 and the example). gemini-code-assist has an open thread arguing min_replica_size should be 1 rather than matching min_replicas. My suggestions don't touch that sentence or the example. A maintainer familiar with torchft's Manager semantics 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]>
…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]>
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]>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

docs An issue or change related to documentation train Ray Train Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants