Repository navigation
[train][torchft] ray.train.report does not hang across replica group restarts - #62651
Conversation
Signed-off-by: Timothy Seah <[email protected]>
There was a problem hiding this comment.
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]>
pseudo-rnd-thoughts
left a comment
There was a problem hiding this comment.
Overall looks good, I just have a couple of questions
| """ | ||
| execution_context = get_train_context().execution_context | ||
| had_result = False | ||
| while True: |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
Do we only get one checkpoint at the end before report 1 isn't reported due to the worker failure?
There was a problem hiding this comment.
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 | ||
|
|
||
|
|
There was a problem hiding this comment.
How does this interact with validation functions? As they are async then torchft doesn't interact with it?
There was a problem hiding this comment.
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]>
| # 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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
| 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))] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Yeah basically. There are three places where ray.train.report could leave partial state:
SyncActorbarrier- On the
workerbut not pulled to the controller - On the
controller
This handles case 3.
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
Reviewed by Cursor Bugbot for commit 7519276. Configure here.
Signed-off-by: Timothy Seah <[email protected]>
…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]>
…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]>

Summary
Previously, Ray Train + torchft was incompatible with the
ray.train.reportAPI because if a worker died in the middle of aray.train.reportbarrier, it would hang indefinitely.There are two ways to solve this problem:
ray.train.reportstateray.train.reportbarrier.The latter approach is tricky to get right. For example, look at how both workers are stuck in the following scenario:
Therefore, I decided to go with the former approach. In fact,
torchftitself employs a similar approach - whenloss.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 usingmanager.current_step: https://github.com/ray-project/ray/pull/62651/changes#diff-935436cfeb39bc19b0652577dc71350a8352c3a8b797985ab30dab233930847dR173. Replaying the skipped batch is a separate problem;torchftrecommends doing this with dataloader checkpointing, and Ray Data can potentially do this natively.Note that
ray.train.reporthas state in three places:SyncActor:replace_replica_groupnow clear this with the newresetmethod.Worker:replace_replica_groupnow clears the per-worker queues with the newclear_result_queuemethodReportHandler:replace_replica_groupnow clears this with thebefore_replica_group_shutdown/before_replica_group_startcallbacks.Note that
test_report_with_worker_failure_before_synctests case 1 andtest_report_with_worker_failure_before_put_on_worker_queuetests cases 2-3.Testing
Unit tests +
test_torch_trainertests which are basically integration tests that use torchft.