|
| 1 | +# Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +"""NCCL collective weight synchronizer for non-colocated deployments. |
| 16 | +
|
| 17 | +Handles weight transfer between policy and generation workers running on |
| 18 | +separate GPU clusters using NCCL collective communication. The policy |
| 19 | +broadcasts its weights, and generation workers receive them via the |
| 20 | +established NCCL process group. |
| 21 | +
|
| 22 | +Lifecycle per sync: |
| 23 | + 1. policy.broadcast_weights_for_collective() -- send via NCCL |
| 24 | + generation.update_weights_from_collective() -- receive via NCCL |
| 25 | + 2. Verify transfer success |
| 26 | +
|
| 27 | +No offload/restore steps are needed since policy and generation run on |
| 28 | +separate GPUs with dedicated memory. |
| 29 | +""" |
| 30 | + |
| 31 | +from contextlib import nullcontext |
| 32 | +from typing import Any, Optional |
| 33 | + |
| 34 | +import ray |
| 35 | + |
| 36 | +from nemo_rl.utils.timer import Timer |
| 37 | +from nemo_rl.weight_sync.interfaces import WeightSynchronizer |
| 38 | + |
| 39 | + |
| 40 | +class CollectiveWeightSynchronizer(WeightSynchronizer): |
| 41 | + """Weight synchronizer using NCCL collectives for non-colocated deployments. |
| 42 | +
|
| 43 | + Policy and generation workers run on separate GPU clusters. Weights are |
| 44 | + synchronized via NCCL broadcast over a pre-established process group. |
| 45 | +
|
| 46 | + Args: |
| 47 | + policy: Policy object implementing ColocatablePolicyInterface. |
| 48 | + generation: Generation object implementing GenerationInterface. |
| 49 | + train_cluster: RayVirtualCluster for the training workers, used to |
| 50 | + obtain the master address/port and world size for collective init. |
| 51 | + inference_cluster: RayVirtualCluster for the inference workers. |
| 52 | + """ |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, |
| 56 | + policy: Any, |
| 57 | + generation: Any, |
| 58 | + train_cluster: Any, |
| 59 | + inference_cluster: Any, |
| 60 | + ): |
| 61 | + self._policy = policy |
| 62 | + self._generation = generation |
| 63 | + self._train_cluster = train_cluster |
| 64 | + self._inference_cluster = inference_cluster |
| 65 | + self._stale = True |
| 66 | + |
| 67 | + def sync_weights( |
| 68 | + self, |
| 69 | + *, |
| 70 | + timer: Optional[Timer] = None, |
| 71 | + kv_scales: Optional[dict[str, float]] = None, |
| 72 | + ) -> None: |
| 73 | + timer_context = ( |
| 74 | + timer.time("prepare_for_generation/transfer_and_update_weights") |
| 75 | + if timer is not None |
| 76 | + else nullcontext() |
| 77 | + ) |
| 78 | + with timer_context: |
| 79 | + futures_train = self._policy.broadcast_weights_for_collective( |
| 80 | + kv_scales=kv_scales |
| 81 | + ) |
| 82 | + futures_inference = ( |
| 83 | + self._generation.update_weights_from_collective() |
| 84 | + ) |
| 85 | + |
| 86 | + ray.get(futures_train) |
| 87 | + results = ray.get(futures_inference) |
| 88 | + update_success = all( |
| 89 | + result for result in results if result is not None |
| 90 | + ) |
| 91 | + |
| 92 | + if not update_success: |
| 93 | + raise RuntimeError( |
| 94 | + "Weight transfer failed during NCCL collective sync. " |
| 95 | + "This often indicates an issue with the NCCL process group " |
| 96 | + "or the generation backend worker." |
| 97 | + ) |
| 98 | + |
| 99 | + self._stale = False |
| 100 | + |
| 101 | + @property |
| 102 | + def is_stale(self) -> bool: |
| 103 | + return self._stale |
| 104 | + |
| 105 | + def mark_stale(self) -> None: |
| 106 | + self._stale = True |
| 107 | + |
| 108 | + def init_communicator(self) -> None: |
| 109 | + # prepare_refit_info is called before init_collective. This matches |
| 110 | + # distillation.py ordering. Neither call depends on the other today, |
| 111 | + # but we document this as the canonical ordering for future reference. |
| 112 | + state_dict_info = self._policy.prepare_refit_info() |
| 113 | + self._generation.prepare_refit_info(state_dict_info) |
| 114 | + |
| 115 | + ip, port = self._train_cluster.get_master_address_and_port() |
| 116 | + train_world_size = self._train_cluster.world_size() |
| 117 | + inference_world_size = self._inference_cluster.world_size() |
| 118 | + world_size = train_world_size + inference_world_size |
| 119 | + |
| 120 | + futures_train = self._policy.init_collective( |
| 121 | + ip, port, world_size, train_world_size=train_world_size |
| 122 | + ) |
| 123 | + futures_inference = self._generation.init_collective( |
| 124 | + ip, port, world_size, train_world_size=train_world_size |
| 125 | + ) |
| 126 | + ray.get(futures_train + futures_inference) |
| 127 | + |
| 128 | + def shutdown(self) -> None: |
| 129 | + # The NCCL process group lifecycle is managed by Ray actor teardown. |
| 130 | + # Explicit destroy_process_group() is not needed here because the |
| 131 | + # workers that own the group are destroyed when the cluster shuts down. |
| 132 | + pass |
0 commit comments