forked from TransferQueue/verl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfsdp_workers.py
More file actions
254 lines (208 loc) · 10.2 KB
/
fsdp_workers.py
File metadata and controls
254 lines (208 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# Copyright 2025 Bytedance Ltd. and/or its affiliates
# Copyright 2025 Meituan Ltd. and/or its affiliates
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
import time
import torch
import torch.distributed
from omegaconf import DictConfig
from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
from verl.experimental.fully_async_policy.fsdp2_utils import fsdp2_sharded_load_from_cpu, fsdp2_sharded_save_to_cpu
from verl.single_controller.base.decorator import Dispatch, register
from verl.utils.device import (
get_device_name,
get_torch_device,
)
from verl.utils.fsdp_utils import (
fsdp_version,
load_fsdp_model_to_gpu,
offload_fsdp_model_to_cpu,
)
from verl.workers.fsdp_workers import ActorRolloutRefWorker, AsyncActorRolloutRefWorker, CriticWorker
from .checkpoint_engine import CheckpointEngine
logger = logging.getLogger(__file__)
logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN"))
device_name = get_device_name()
__all__ = ["DetachActorWorker", "DetachAsyncRolloutWorker", "CriticWorker"]
def get_inference_model(rollout):
"""
get models according to different types of inference_engine
Args:
rollout: rollout object
Returns:
model: model object
"""
inference_engine = rollout.inference_engine
if hasattr(inference_engine, "llm_engine"):
inference_model = inference_engine.llm_engine.model_executor.driver_worker.worker.model_runner.model
elif hasattr(inference_engine, "worker"):
inference_model = inference_engine.worker.model_runner.model
else:
raise AttributeError(
f"Unsupported inference_engine type: {type(inference_engine)}. "
f"Expected LLM (with llm_engine attribute) or WorkerWrapperBase (with worker attribute)."
)
return inference_model
class DetachNcclSync(AsyncActorRolloutRefWorker):
@register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False)
def init_checkpoint_engine(self, rank_offset: int, actor_num: int, rollout_num: int):
current_rank = torch.distributed.get_rank() + rank_offset
actor_ranks = list(range(actor_num))
rollout_ranks = [rank + actor_num for rank in range(rollout_num)]
assert rank_offset == 0 or rank_offset == actor_num
self.checkpoint_engine = CheckpointEngine(
current_rank, actor_ranks, rollout_ranks, self.config.checkpoint_engine.device_buffer_size_M
)
def _get_actor_params(self):
pass
@register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False)
def sync_rollout_weights(self, sync_group_name="actor_rollout"):
assert (self._is_actor or self._is_rollout) and not self.config.hybrid_engine
assert hasattr(self, "_weights_info") and self._weights_info is not None
if self._is_actor and self._is_offload_param:
load_fsdp_model_to_gpu(self.actor_module_fsdp)
params = self._get_actor_params() if self._is_actor else None
if self._is_rollout:
inference_model = get_inference_model(self.rollout)
from verl.utils.vllm.patch import patch_vllm_moe_model_weight_loader
patch_vllm_moe_model_weight_loader(inference_model)
for key, shape, dtype in self._weights_info:
tensor = torch.empty(shape, dtype=dtype, device=get_torch_device().current_device())
if self._is_actor:
assert key in params
origin_data = params[key]
if hasattr(origin_data, "full_tensor"):
origin_data = origin_data.full_tensor()
if torch.distributed.get_rank() == 0:
tensor.copy_(origin_data)
from ray.util.collective import collective
collective.broadcast(tensor, src_rank=0, group_name=sync_group_name)
if self._is_rollout:
inference_model.load_weights([(key, tensor)])
if self._is_actor and self._is_offload_param:
offload_fsdp_model_to_cpu(self.actor_module_fsdp)
get_torch_device().empty_cache()
def cache_actor_weights_to_cpu(self):
self.cpu_named_params = {}
if self._is_actor:
params = self._get_actor_params()
local_rank = torch.distributed.get_rank()
world_size = torch.distributed.get_world_size()
for tensor_idx, (key, _, _) in enumerate(self._weights_info):
origin_data = params[key]
if hasattr(origin_data, "full_tensor"):
origin_data = origin_data.full_tensor()
if tensor_idx % world_size == local_rank:
self.cpu_named_params[key] = origin_data.to("cpu", non_blocking=True)
get_torch_device().synchronize()
@register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False)
def sync_rollout_weights_by_checkpoint(self, sync_group_name="actor_rollout"):
assert (self._is_actor or self._is_rollout) and not self.config.hybrid_engine
assert hasattr(self, "_weights_info") and self._weights_info is not None
# Load model to GPU
load_start_time = time.time()
if self._is_actor and self._is_offload_param:
load_fsdp_model_to_gpu(self.actor_module_fsdp)
load_duration = time.time() - load_start_time
from ray.util.collective import collective
# Cache actor weights to CPU and measure the time taken
cache_start_time = time.time()
self.cache_actor_weights_to_cpu()
cache_end_time = time.time()
cache_duration = cache_end_time - cache_start_time
# Register the cached weights into the checkpoint engine
self.checkpoint_engine.register_checkpoint(self._weights_info, self.cpu_named_params)
register_end_time = time.time()
register_duration = register_end_time - cache_end_time
self.cpu_named_params = {}
collective.barrier(group_name=sync_group_name)
update_start_time = time.time()
inference_model = None
if self._is_rollout:
inference_model = get_inference_model(self.rollout)
from verl.utils.vllm.patch import patch_vllm_moe_model_weight_loader
patch_vllm_moe_model_weight_loader(inference_model)
# Update the checkpoint with the inference model and broadcast weights
self.checkpoint_engine.update_checkpoint(
inference_model=inference_model,
group_name=sync_group_name,
overlap_broadcast_and_consume=self.config.checkpoint_engine.overlap_broadcast_and_consume,
)
update_end_time = time.time()
update_duration = update_end_time - update_start_time
offload_start_time = time.time()
if self._is_actor and self._is_offload_param:
offload_fsdp_model_to_cpu(self.actor_module_fsdp)
offload_duration = time.time() - offload_start_time
print(
f"sync_rollout_weights_by_checkpoint finish!, rank:{torch.distributed.get_rank()},"
f" is_actor:{self._is_actor}, is_rollout:{self._is_rollout},"
f" total cost:{update_end_time - cache_start_time} seconds, while cache cost {cache_duration} seconds, "
f" register cost {register_duration} seconds, update cost {update_duration} seconds"
)
if self._is_actor and self._is_offload_param:
print(
f"sync_rollout_weights_by_checkpoint load model to gpu cost {load_duration} seconds,"
f" offload model to cpu cost {offload_duration} seconds"
)
class DetachActorWorker(DetachNcclSync):
def _get_actor_params(self):
assert self._is_actor
params = self.actor_module_fsdp.state_dict()
from verl.utils.model import convert_weight_keys
params = convert_weight_keys(
params, getattr(self.actor_module_fsdp, "_fsdp_wrapped_module", self.actor_module_fsdp)
)
return params
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def get_actor_weights_info(self):
assert self._is_actor
if hasattr(self, "_weights_info"):
return self._weights_info
if fsdp_version(self.actor_module_fsdp) == 1:
from torch.distributed.fsdp.api import ShardedStateDictConfig, StateDictType
FSDP.set_state_dict_type(
self.actor_module_fsdp,
state_dict_type=StateDictType.SHARDED_STATE_DICT,
state_dict_config=ShardedStateDictConfig(),
)
params = self._get_actor_params()
ret = []
for key, tensor in params.items():
ret.append((key, tensor.size(), tensor.dtype))
self._weights_info = ret
return ret
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def save_model_to_cpu(self, n):
if not hasattr(self, "cpu_saved_models"):
self.cpu_saved_models = {}
self.cpu_saved_models[n] = fsdp2_sharded_save_to_cpu(self.actor_module_fsdp)
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def restore_model_from_cpu(self, n):
if n in self.cpu_saved_models:
cpu_sharded_state, global_spec = self.cpu_saved_models[n]
fsdp2_sharded_load_from_cpu(self.actor_module_fsdp, cpu_sharded_state, global_spec)
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def clear_cpu_model(self, n):
if n in self.cpu_saved_models:
del self.cpu_saved_models[n]
class DetachAsyncRolloutWorker(DetachNcclSync):
def __init__(self, config: DictConfig, role: str):
print(f"[DetachAsyncRolloutWorker] {DetachAsyncRolloutWorker.__mro__}")
ActorRolloutRefWorker.__init__(self, config, role)
@register(dispatch_mode=Dispatch.ONE_TO_ALL)
def set_actor_weights_info(self, weights_info):
assert self._is_rollout
self._weights_info = weights_info