Repository navigation
[train][torchft] Ray Train configures torchft environment - #61156
Conversation
Signed-off-by: Timothy Seah <[email protected]>
There was a problem hiding this comment.
Code Review
The pull request integrates Ray Train with torchft for fault-tolerant training. It introduces a new on_controller_start method in the Backend class and a TorchftConfig for torchft-specific configurations. The changes also include modifications to environment variable setup for torchft and new methods in TrainContext to handle replica groups. A new example torchft_linear_example.py and corresponding tests have been added. The changes are well-structured and address the stated goal of integrating torchft.
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
|
Warning You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again! |
justinvyu
left a comment
There was a problem hiding this comment.
Very nice! Only a few nits
| def before_worker_group_start(self, backend_config: TorchftConfig): | ||
| if self.lighthouse_actor is not None: |
There was a problem hiding this comment.
can we just do this on the first on_start to avoid needing to check for that Mixin class
There was a problem hiding this comment.
Done. Only callout is that I have to set the lighthouse address on the workers manually.
Signed-off-by: Timothy Seah <[email protected]>
Signed-off-by: Timothy Seah <[email protected]>
This PR implements all the torchft environment setup in [this design](https://docs.google.com/document/d/1phjJanIPv5JSD-_B-iPp4Afa4cRV_sEBLeW6q_Z26Xc/edit?tab=t.0#heading=h.mm9jf5hqeinp) by productionizing [this prototype](#60820). --------- Signed-off-by: Timothy Seah <[email protected]> Signed-off-by: Kamil Kaczmarek <[email protected]>
# Summary This PR follows up on #61156 by handling torchft worker group failure recovery. Here are some of the design decisions: * `ReplicaGroup.shutdown` is similar to `WorkerGroup.shutdown` (they both shut down workers and clear state) but doesn't do some other stuff (e.g. callbacks and placementgroup cleanup). * `WorkerGroup.replace_replica_group` is similar to `WorkerGroup._start_impl` so I refactored their shared functionality accordingly. The main difference is that the former runs fewer callbacks. This PR also changes the semantics of get_world_rank and get_local_rank/get_node_rank: * get_world_rank/get_world_size still apply to all workers (across replica groups). This is because we often need one global rank 0 e.g. DDP rank 0 worker uploads checkpoint. Since we are doing DDP only, this is equivalent to get_replica_group_id. * get_local_rank/get_local_size/get_node_rank now apply to (replica group, node) pairs. One common use case for get_local_rank is to only download data to a node once - but since every replica group gets a different shard of data, every "local rank 0" needs to download data anyway. Also, creating a local_rank across all replica groups on the same node might not be feasible; if there is a node failure that results in a replica group getting scheduled on an existing node with another replica group, we would need to re-sort all the local ranks. Fortunately, every torchft replica group is effectively its own torchrun group (communication between torchrun groups is handled by the `Manager`), so this is consistent with their model. For this reason we don't need to worry about `CUDA_VISIBLE_DEVICES` either. * The order of workers in `WorkerGroupState.workers` is equivalent to `get_world_rank`. This invariant still holds. I also went through every single `WorkerGroupCallback` method and determined whether or not they are relevant for `ReplicaGroups`. In many cases, the behavior is the same for `WorkerGroups` and `ReplicaGroups`, so I also defined corresponding `ExecutionGroupCallback` methods that get called in both cases by default. `before_init_train_context`: Always the same between `WorkerGroup` and `ReplicaGroup` so I moved this behavior up to `ExecutionGroupCallback`. * `AcceleratorSetupCallback`: well handled and tested in this PR * `CheckpointManagerCallback` and `ValidationManagerCallback`: same behavior. Will test this more thoroughly in the followup `ray.train.report` PR. * `DatasetSetupCallback`: will test this more in the followup `data integration` PR. `before_worker_group_shutdown`: sometimes same (in which case the user can override `before_execution_group_shutdown`) sometimes different (in which case the user can override `before_worker_group_shutdown` or `before_replica_group_shutdown`). * `BackendSetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `StateManagerCallback`: only implemented with `before_worker_group_shutdown` because replica groups are not reflected in train run state * `ReportCallbackHandler`: will be fixed in followup `ray.train.report` PR. The main idea is that `before_replica_group_shutdown` can also clear report states. `after_worker_group_start`: same as `before_worker_group_shutdown` * `BackendSetupCallback` and `WorkingDirectorySetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `DatasetSetupCallback` and `ReportCallbackHandler` will be handled in the aforementioned future PR's * `StateManager` and `PlacementGroupCleanerCallback` only apply to worker groups. `after_worker_group_shutdown` and `after_worker_group_abort` are only used by `DatasetsCallback`. They should only apply to worker groups; when a replica group goes down, rather than shutting down any state, we simply send the state to the replacement worker. Of course, the aforementioned future data integration PR will test this better. All other `WorkerGroupCallback` methods are irrelevant: * `on_worker_group-start/on_worker_group_shutdown` are just for timing the worker group. * `before_worker_group_start` and `before_worker_group_abort` are only used by `StateManagerCallback` which is irrelevant as explained earlier. * `after_worker_group_training_start` is never used. * `after_worker_group_poll_status` is irrelevant because it operates on all the workers in the worker group, while replica groups are just thin wrappers around the workers. # Testing I'm open to more unit test suggestions. I basically tried to unit test different layers of the stack as follows: * `test_torch_trainer`: e2e test. I also verified that it works as expected. It's still disabled until I add torchft dependencies to the train CI. * `test_controller`: tests that we correctly decide when to do a replica group restart or a full worker group restart. Note that this also tests elastic training. * `test_worker_group`: tests that when we `replace_replica_group` we correctly update the relevant state (`WorkerGroupState`, replica groups, polling state). I added `mark.parametrize` to other unit tests to verify other behavior works as expected with both worker group and replica group restarts e.g. callbacks and worker initialization. Successfully ran a driver script in a workspace with simulated node failures (killed the raylet) and confirmed that it worked. Driver script: https://gist.github.com/TimothySeah/35aa96b81b2d98d77c23b10c0baa71c9 Logs Failure detected: https://gist.github.com/TimothySeah/923ce69987de98b774d8de214e6845ae Training stops due to unmet quorum ``` �[36m(LighthouseServerActor pid=24407)�[0m 2026-03-11T13:02:09.398 [INFO] [torchft::lighthouse] - Quorum status: New quorum not ready, only have 2 participants, need min_replicas 3 [2/2 participants healthy][2 heartbeating][shrink_only=false] �[33m(raylet)�[0m The node with node id: 0b015b238c8d9b950c92fe08a188001514a61a624cebdfe0e2aab715 and address: 10.0.82.255 and node name: 10.0.82.255 has been marked dead because the detector has missed too many heartbeats from it. This can happen when a (1) raylet crashes unexpectedly (OOM, etc.) (2) raylet has lagging heartbeats due to slow network or busy workload ``` replace_replica_group fails because node is unschedulable: https://gist.github.com/TimothySeah/12030fe7814a72e1b21fd75c25710635 Autoscaling completes and training continues: https://gist.github.com/TimothySeah/98b0776199ff18a97ce64246ab7894c9 Eventually we reach the finished state. --------- Signed-off-by: Timothy Seah <[email protected]>
…t#61475) # Summary This PR follows up on ray-project#61156 by handling torchft worker group failure recovery. Here are some of the design decisions: * `ReplicaGroup.shutdown` is similar to `WorkerGroup.shutdown` (they both shut down workers and clear state) but doesn't do some other stuff (e.g. callbacks and placementgroup cleanup). * `WorkerGroup.replace_replica_group` is similar to `WorkerGroup._start_impl` so I refactored their shared functionality accordingly. The main difference is that the former runs fewer callbacks. This PR also changes the semantics of get_world_rank and get_local_rank/get_node_rank: * get_world_rank/get_world_size still apply to all workers (across replica groups). This is because we often need one global rank 0 e.g. DDP rank 0 worker uploads checkpoint. Since we are doing DDP only, this is equivalent to get_replica_group_id. * get_local_rank/get_local_size/get_node_rank now apply to (replica group, node) pairs. One common use case for get_local_rank is to only download data to a node once - but since every replica group gets a different shard of data, every "local rank 0" needs to download data anyway. Also, creating a local_rank across all replica groups on the same node might not be feasible; if there is a node failure that results in a replica group getting scheduled on an existing node with another replica group, we would need to re-sort all the local ranks. Fortunately, every torchft replica group is effectively its own torchrun group (communication between torchrun groups is handled by the `Manager`), so this is consistent with their model. For this reason we don't need to worry about `CUDA_VISIBLE_DEVICES` either. * The order of workers in `WorkerGroupState.workers` is equivalent to `get_world_rank`. This invariant still holds. I also went through every single `WorkerGroupCallback` method and determined whether or not they are relevant for `ReplicaGroups`. In many cases, the behavior is the same for `WorkerGroups` and `ReplicaGroups`, so I also defined corresponding `ExecutionGroupCallback` methods that get called in both cases by default. `before_init_train_context`: Always the same between `WorkerGroup` and `ReplicaGroup` so I moved this behavior up to `ExecutionGroupCallback`. * `AcceleratorSetupCallback`: well handled and tested in this PR * `CheckpointManagerCallback` and `ValidationManagerCallback`: same behavior. Will test this more thoroughly in the followup `ray.train.report` PR. * `DatasetSetupCallback`: will test this more in the followup `data integration` PR. `before_worker_group_shutdown`: sometimes same (in which case the user can override `before_execution_group_shutdown`) sometimes different (in which case the user can override `before_worker_group_shutdown` or `before_replica_group_shutdown`). * `BackendSetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `StateManagerCallback`: only implemented with `before_worker_group_shutdown` because replica groups are not reflected in train run state * `ReportCallbackHandler`: will be fixed in followup `ray.train.report` PR. The main idea is that `before_replica_group_shutdown` can also clear report states. `after_worker_group_start`: same as `before_worker_group_shutdown` * `BackendSetupCallback` and `WorkingDirectorySetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `DatasetSetupCallback` and `ReportCallbackHandler` will be handled in the aforementioned future PR's * `StateManager` and `PlacementGroupCleanerCallback` only apply to worker groups. `after_worker_group_shutdown` and `after_worker_group_abort` are only used by `DatasetsCallback`. They should only apply to worker groups; when a replica group goes down, rather than shutting down any state, we simply send the state to the replacement worker. Of course, the aforementioned future data integration PR will test this better. All other `WorkerGroupCallback` methods are irrelevant: * `on_worker_group-start/on_worker_group_shutdown` are just for timing the worker group. * `before_worker_group_start` and `before_worker_group_abort` are only used by `StateManagerCallback` which is irrelevant as explained earlier. * `after_worker_group_training_start` is never used. * `after_worker_group_poll_status` is irrelevant because it operates on all the workers in the worker group, while replica groups are just thin wrappers around the workers. # Testing I'm open to more unit test suggestions. I basically tried to unit test different layers of the stack as follows: * `test_torch_trainer`: e2e test. I also verified that it works as expected. It's still disabled until I add torchft dependencies to the train CI. * `test_controller`: tests that we correctly decide when to do a replica group restart or a full worker group restart. Note that this also tests elastic training. * `test_worker_group`: tests that when we `replace_replica_group` we correctly update the relevant state (`WorkerGroupState`, replica groups, polling state). I added `mark.parametrize` to other unit tests to verify other behavior works as expected with both worker group and replica group restarts e.g. callbacks and worker initialization. Successfully ran a driver script in a workspace with simulated node failures (killed the raylet) and confirmed that it worked. Driver script: https://gist.github.com/TimothySeah/35aa96b81b2d98d77c23b10c0baa71c9 Logs Failure detected: https://gist.github.com/TimothySeah/923ce69987de98b774d8de214e6845ae Training stops due to unmet quorum ``` �[36m(LighthouseServerActor pid=24407)�[0m 2026-03-11T13:02:09.398 [INFO] [torchft::lighthouse] - Quorum status: New quorum not ready, only have 2 participants, need min_replicas 3 [2/2 participants healthy][2 heartbeating][shrink_only=false] �[33m(raylet)�[0m The node with node id: 0b015b238c8d9b950c92fe08a188001514a61a624cebdfe0e2aab715 and address: 10.0.82.255 and node name: 10.0.82.255 has been marked dead because the detector has missed too many heartbeats from it. This can happen when a (1) raylet crashes unexpectedly (OOM, etc.) (2) raylet has lagging heartbeats due to slow network or busy workload ``` replace_replica_group fails because node is unschedulable: https://gist.github.com/TimothySeah/12030fe7814a72e1b21fd75c25710635 Autoscaling completes and training continues: https://gist.github.com/TimothySeah/98b0776199ff18a97ce64246ab7894c9 Eventually we reach the finished state. --------- Signed-off-by: Timothy Seah <[email protected]>
…t#61475) # Summary This PR follows up on ray-project#61156 by handling torchft worker group failure recovery. Here are some of the design decisions: * `ReplicaGroup.shutdown` is similar to `WorkerGroup.shutdown` (they both shut down workers and clear state) but doesn't do some other stuff (e.g. callbacks and placementgroup cleanup). * `WorkerGroup.replace_replica_group` is similar to `WorkerGroup._start_impl` so I refactored their shared functionality accordingly. The main difference is that the former runs fewer callbacks. This PR also changes the semantics of get_world_rank and get_local_rank/get_node_rank: * get_world_rank/get_world_size still apply to all workers (across replica groups). This is because we often need one global rank 0 e.g. DDP rank 0 worker uploads checkpoint. Since we are doing DDP only, this is equivalent to get_replica_group_id. * get_local_rank/get_local_size/get_node_rank now apply to (replica group, node) pairs. One common use case for get_local_rank is to only download data to a node once - but since every replica group gets a different shard of data, every "local rank 0" needs to download data anyway. Also, creating a local_rank across all replica groups on the same node might not be feasible; if there is a node failure that results in a replica group getting scheduled on an existing node with another replica group, we would need to re-sort all the local ranks. Fortunately, every torchft replica group is effectively its own torchrun group (communication between torchrun groups is handled by the `Manager`), so this is consistent with their model. For this reason we don't need to worry about `CUDA_VISIBLE_DEVICES` either. * The order of workers in `WorkerGroupState.workers` is equivalent to `get_world_rank`. This invariant still holds. I also went through every single `WorkerGroupCallback` method and determined whether or not they are relevant for `ReplicaGroups`. In many cases, the behavior is the same for `WorkerGroups` and `ReplicaGroups`, so I also defined corresponding `ExecutionGroupCallback` methods that get called in both cases by default. `before_init_train_context`: Always the same between `WorkerGroup` and `ReplicaGroup` so I moved this behavior up to `ExecutionGroupCallback`. * `AcceleratorSetupCallback`: well handled and tested in this PR * `CheckpointManagerCallback` and `ValidationManagerCallback`: same behavior. Will test this more thoroughly in the followup `ray.train.report` PR. * `DatasetSetupCallback`: will test this more in the followup `data integration` PR. `before_worker_group_shutdown`: sometimes same (in which case the user can override `before_execution_group_shutdown`) sometimes different (in which case the user can override `before_worker_group_shutdown` or `before_replica_group_shutdown`). * `BackendSetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `StateManagerCallback`: only implemented with `before_worker_group_shutdown` because replica groups are not reflected in train run state * `ReportCallbackHandler`: will be fixed in followup `ray.train.report` PR. The main idea is that `before_replica_group_shutdown` can also clear report states. `after_worker_group_start`: same as `before_worker_group_shutdown` * `BackendSetupCallback` and `WorkingDirectorySetupCallback`: implemented in `before_execution_group_shutdown`. Well handled and tested in this PR * `DatasetSetupCallback` and `ReportCallbackHandler` will be handled in the aforementioned future PR's * `StateManager` and `PlacementGroupCleanerCallback` only apply to worker groups. `after_worker_group_shutdown` and `after_worker_group_abort` are only used by `DatasetsCallback`. They should only apply to worker groups; when a replica group goes down, rather than shutting down any state, we simply send the state to the replacement worker. Of course, the aforementioned future data integration PR will test this better. All other `WorkerGroupCallback` methods are irrelevant: * `on_worker_group-start/on_worker_group_shutdown` are just for timing the worker group. * `before_worker_group_start` and `before_worker_group_abort` are only used by `StateManagerCallback` which is irrelevant as explained earlier. * `after_worker_group_training_start` is never used. * `after_worker_group_poll_status` is irrelevant because it operates on all the workers in the worker group, while replica groups are just thin wrappers around the workers. # Testing I'm open to more unit test suggestions. I basically tried to unit test different layers of the stack as follows: * `test_torch_trainer`: e2e test. I also verified that it works as expected. It's still disabled until I add torchft dependencies to the train CI. * `test_controller`: tests that we correctly decide when to do a replica group restart or a full worker group restart. Note that this also tests elastic training. * `test_worker_group`: tests that when we `replace_replica_group` we correctly update the relevant state (`WorkerGroupState`, replica groups, polling state). I added `mark.parametrize` to other unit tests to verify other behavior works as expected with both worker group and replica group restarts e.g. callbacks and worker initialization. Successfully ran a driver script in a workspace with simulated node failures (killed the raylet) and confirmed that it worked. Driver script: https://gist.github.com/TimothySeah/35aa96b81b2d98d77c23b10c0baa71c9 Logs Failure detected: https://gist.github.com/TimothySeah/923ce69987de98b774d8de214e6845ae Training stops due to unmet quorum ``` �[36m(LighthouseServerActor pid=24407)�[0m 2026-03-11T13:02:09.398 [INFO] [torchft::lighthouse] - Quorum status: New quorum not ready, only have 2 participants, need min_replicas 3 [2/2 participants healthy][2 heartbeating][shrink_only=false] �[33m(raylet)�[0m The node with node id: 0b015b238c8d9b950c92fe08a188001514a61a624cebdfe0e2aab715 and address: 10.0.82.255 and node name: 10.0.82.255 has been marked dead because the detector has missed too many heartbeats from it. This can happen when a (1) raylet crashes unexpectedly (OOM, etc.) (2) raylet has lagging heartbeats due to slow network or busy workload ``` replace_replica_group fails because node is unschedulable: https://gist.github.com/TimothySeah/12030fe7814a72e1b21fd75c25710635 Autoscaling completes and training continues: https://gist.github.com/TimothySeah/98b0776199ff18a97ce64246ab7894c9 Eventually we reach the finished state. --------- Signed-off-by: Timothy Seah <[email protected]>
Summary
This PR implements all the torchft environment setup in this design by productionizing this prototype. I will handle torchft worker group failure recovery in a separate PR.
I would like to call out the following about this part:
TorchftConfig(v2 only) which subclassesTorchConfig(v1 or v2) which subclassesBackend(v1 or v2). Note thatTorchftConfig.on_training_startandTorchftConfig.on_shutdownare the same as theirTorchConfigcounterparts. OnlyTorchftConfig.on_startis different because we need toinit_process_grouponce per replica group. To simplify this, I also created a newReplicaGroupconstruct so we can restart and initialize replica groups easily without doing rank math. TheWorkerGroupwill storeReplicaGroups if itsBackendhas replica groups; eachReplicaGroupis a light wrapper around the relevant subset of theWorkerGroup's workers.assign_worker_rankssuch that every replica group has a different global view.BackendSetupCallbackbecause the lighthouse is lightweight (won't slow down the head node) and can be restarted (the controller can manage the restarts). To achieve this, I added a newBeforeWorkerGroupStartMixinand madeBackendSetupCallbackcall itsbefore_worker_group_startmethod. I think this makes sense because some backends (so far just torchft) may need to do some work before starting a worker group.Aside: TrainContext
Here are 4 rank-related values that torchft cares about and how they are set in DDP training:
For model parallel training:
Since we will start with DDP and all 4 values are easily deducible from the existing
TrainContext, we will hold off on modifyingTrainContextfor now. After we implement native support for model parallel training in Ray Train, we will ask users to use the newTrainContextmethods instead, which should be simple because replica group id's are the same as DDP ranks.Testing
I modeled
torchft_linear_example.pyoff oftorch_linear_example.pyand confirmed that it runs e2e without any issues. The newtest_torchft_lineartest case also verifies that we are actually doing data parallel training - when I created a "split brain" by removing the manager code, the test failed as expectedI considered adding
mark.parametrizeto a bunch oftest_torch_trainerandtest_torch_gpumethods but decided to only do these whereTorchftConfigis expected to behave differently fromTorchConfig.In order to properly unit test torchft + Ray Train in premerge checks, we need to upgrade some dependencies, which I will do in a separate PR. In the meantime, I've disabled my newly added torchft + Ray Train unit tests but confirmed that they work on my workstation.