Skip to content

[train][torchft] ray.train.report does not hang across replica group restarts - #62651

Merged
justinvyu merged 9 commits into
ray-project:masterfrom
TimothySeah:tseah/torchft-compatible-report
May 6, 2026
Merged

justinvyu merged 9 commits into
ray-project:masterfrom
TimothySeah:tseah/torchft-compatible-report

Conversation

@TimothySeah

@TimothySeah TimothySeah commented Apr 16, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Previously, Ray Train + torchft was incompatible with the ray.train.report API because if a worker died in the middle of a ray.train.report barrier, it would hang indefinitely.

There are two ways to solve this problem:

  1. Clearing the ray.train.report state
  2. Making the replacement worker rejoin the ray.train.report barrier.

The latter approach is tricky to get right. For example, look at how both workers are stuck in the following scenario:

for batch in batches:
	loss = ...
	loss.backward()
	optimizer.step()
	if manager.current_step() % 10:
		ray.train.report()

Worker 0 Worker 1
loss.backward() barrier loss.backward() barrier
ray.train.report() barrier dies
  loss.backward() barrier

Therefore, I decided to go with the former approach. In fact, torchft itself employs a similar approach - when loss.backward() fails because a worker dies, it effectively skips over the batch without advancing the step count: https://github.com/meta-pytorch/torchft/blob/7eb7087885f7ffaf51148a9df3289d73b8709fbd/torchft/manager.py#L928. Furthermore, if we want to "replay" the report, we can do so using manager.current_step: https://github.com/ray-project/ray/pull/62651/changes#diff-935436cfeb39bc19b0652577dc71350a8352c3a8b797985ab30dab233930847dR173. Replaying the skipped batch is a separate problem; torchft recommends doing this with dataloader checkpointing, and Ray Data can potentially do this natively.

Note that ray.train.report has state in three places:

  1. SyncActor: replace_replica_group now clear this with the new reset method.
  2. Worker: replace_replica_group now clears the per-worker queues with the new clear_result_queue method
  3. ReportHandler: replace_replica_group now clears this with the before_replica_group_shutdown/before_replica_group_start callbacks.

Note that test_report_with_worker_failure_before_sync tests case 1 and test_report_with_worker_failure_before_put_on_worker_queue tests cases 2-3.

Testing

Unit tests + test_torch_trainer tests which are basically integration tests that use torchft.

@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 introduces a robust mechanism for handling worker failures during the synchronization barrier in Ray Train V2. Key changes include the addition of a reset method to the SynchronizationActor to unblock surviving workers, the introduction of a specific SynchronizationBarrierResetError, and updates to the ReportCallbackHandler to dynamically manage worker counts through new replica group callbacks. The PR also implements logic to drain result queues on surviving workers during replica replacement to prevent mixed-generation results. Feedback provided addresses potential TypeError risks during state initialization, concerns regarding worker count consistency during transient replacement states, the need for more granular exception handling when catching barrier resets, and performance optimizations for remote calls within the worker group controller.

Signed-off-by: Timothy Seah <[email protected]>
@TimothySeah
TimothySeah marked this pull request as ready for review April 16, 2026 02:46
@TimothySeah
TimothySeah requested a review from a team as a code owner April 16, 2026 02:46
@ray-gardener ray-gardener Bot added the train Ray Train Related Issue label Apr 16, 2026

@pseudo-rnd-thoughts pseudo-rnd-thoughts left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Overall looks good, I just have a couple of questions

"""
execution_context = get_train_context().execution_context
had_result = False
while True:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it not possible to use a for loop for this or while not execution_context.result_queue.empty(). While true seems a tad excessive

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.

I agree it looks weird, but apparently Queue.empty isn't reliable in a concurrent context: https://docs.python.org/3/library/queue.html#queue.Queue.empty

result = trainer.fit()
assert result.error is None
assert result.metrics["iter"] == 2
assert len(result.best_checkpoints) == 1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do we only get one checkpoint at the end before report 1 isn't reported due to the worker failure?

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.

Yeah. Report 1 was stuck in the barrier, the worker died, the controller cleared out the barrier, and then we hit the report 2 barrier.

assert result.metrics["iter"] == 2
assert len(result.best_checkpoints) == 1


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How does this interact with validation functions? As they are async then torchft doesn't interact with it?

@TimothySeah TimothySeah Apr 16, 2026 •

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.

Validation basically has 2 steps: 1) Workers form a report barrier to "commit" the checkpoint to the controller 2) Controller manages validation for the committed checkpoint. Torchft affects the first part, which is why this PR is clearing out the barrier. It doesn't affect the second part because the controller manages the validation task independently of the worker group.

…oing its own brittle accounting

Signed-off-by: Timothy Seah <[email protected]>
@TimothySeah TimothySeah added the go add ONLY when ready to merge, run all tests label Apr 16, 2026

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

Looks good overall!

Comment on lines +23 to +25
# We set the worker group after it has been started and remove it after it
# has been shut down.
self._worker_group: Optional[WorkerGroup] = None

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.

why do we need to change from tracking num_workers to the actual worker group? we don't use the worker group at all and only use len(worker_group) anyways

@TimothySeah TimothySeah Apr 30, 2026 •

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 isn't needed for the "fixed worker group" case that we are currently optimizing for, but if we want to support torchft-compatible elastic training, we need a way to update num_workers when replica groups join/leave while avoiding incorrect state updates from retries: #62651 (comment)

Comment on lines +124 to +129
def after_replica_group_start(self, replica_group: ReplicaGroup) -> None:
"""Handle replica group start. Initialize internal states."""
self._assert_initialized()
# TODO: it might be possible to reuse existing queues.
# For example, if 3/4 ddp workers reported a checkpoint, that checkpoint is usable.
self._training_report_queues = [deque() for _ in range(len(self._worker_group))]

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.

Does this handle the case where one of the replica groups goes down after the barrier but before the report? In which case the controller is left with a partial report where some of these report queues aren't empty?

@TimothySeah TimothySeah Apr 30, 2026 •

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.

Yeah basically. There are three places where ray.train.report could leave partial state:

  1. SyncActor barrier
  2. On the worker but not pulled to the controller
  3. On the controller

This handles case 3.

Signed-off-by: Timothy Seah <[email protected]>

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit 7519276. Configure here.

@justinvyu
justinvyu merged commit 7d321b3 into ray-project:master May 6, 2026
3 checks passed
Lucas61000 pushed a commit to Lucas61000/ray that referenced this pull request May 15, 2026
…restarts (ray-project#62651)

Previously, Ray Train + torchft was incompatible with the
`ray.train.report` API because if a worker died in the middle of a
`ray.train.report` barrier, it would hang indefinitely.
Solve this by clearing the `ray.train.report` state if a replica group fails and rejoins.

---------

Signed-off-by: Timothy Seah <[email protected]>
alexandrplashchinsky pushed a commit to alexandrplashchinsky/ray-alex that referenced this pull request May 29, 2026
…restarts (ray-project#62651)

Previously, Ray Train + torchft was incompatible with the
`ray.train.report` API because if a worker died in the middle of a
`ray.train.report` barrier, it would hang indefinitely.
Solve this by clearing the `ray.train.report` state if a replica group fails and rejoins.

---------

Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Alexandr Plashchinsky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

go add ONLY when ready to merge, run all tests train Ray Train Related Issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants