-
Notifications
You must be signed in to change notification settings - Fork 3.1k
[wip][BREAKING][recipe, ckpt]add checkpoint engine for one step off policy #4601
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
x1314aq
wants to merge
3
commits into
volcengine:main
Choose a base branch
from
x1314aq:ckpt
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,140 @@ | ||
| # Copyright 2025 Bytedance Ltd. and/or its affiliates | ||
| # Copyright 2025 Meituan Ltd. and/or its affiliates | ||
| # Copyright 2025 Huawei 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 httpx | ||
| import torch | ||
| import torch.distributed | ||
| from checkpoint_engine.ps import ParameterServer, request_inference_to_update | ||
| from omegaconf import DictConfig, OmegaConf | ||
|
|
||
| from verl.single_controller.base import Worker | ||
| from verl.single_controller.base.decorator import Dispatch, register | ||
| from verl.utils.device import ( | ||
| get_device_name, | ||
| ) | ||
|
|
||
| logger = logging.getLogger(__file__) | ||
| logger.setLevel(os.getenv("VERL_LOGGING_LEVEL", "WARN")) | ||
|
|
||
| device_name = get_device_name() | ||
|
|
||
|
|
||
| class CkptEngineWorker(Worker): | ||
| def __init__(self, rank_offset, ps_world_size, inference_parallel_size, rollout_name): | ||
| super().__init__() | ||
| rank = self.rank + rank_offset | ||
| self.ps_rank = rank | ||
| self.ps_rank_offset = rank_offset | ||
| self.ps_world_size = ps_world_size | ||
| self.inference_parallel_size = inference_parallel_size | ||
| self.rollout_name = rollout_name | ||
| self.ps = ParameterServer(rank=rank, world_size=ps_world_size) | ||
| self.index = 0 | ||
|
|
||
| def _init_process_group(self): | ||
| os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] = "61020" | ||
| self.ps.init_process_group(device_index=0, master_port=60010) | ||
| del os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] | ||
|
|
||
| def check_vllm_ready(self, uds: str | None = None): | ||
| if self.ps_rank != self.ps_rank // self.inference_parallel_size * self.inference_parallel_size: | ||
| return | ||
| retry_num = 0 | ||
| transport = None | ||
| if uds is not None: | ||
| transport = httpx.HTTPTransport(uds=uds) | ||
| while True: | ||
| try: | ||
| response = httpx.Client(transport=transport).get(f"{self.endpoint}/health", timeout=10) | ||
| response.raise_for_status() | ||
| break | ||
| except (httpx.ConnectError, httpx.HTTPStatusError) as e: | ||
| retry_num += 1 | ||
| logger.warning(f"fail to check vllm ready, retry {retry_num} times, error: {e}") | ||
| time.sleep(5) | ||
|
|
||
| def check_sglang_ready(self, uds: str | None = None): | ||
| if self.ps_rank != self.ps_rank // self.inference_parallel_size * self.inference_parallel_size: | ||
| return | ||
| retry_num = 0 | ||
| transport = None | ||
| if uds is not None: | ||
| transport = httpx.HTTPTransport(uds=uds) | ||
| with httpx.Client(transport=transport) as client: | ||
| while True: | ||
| try: | ||
| response = client.get(f"{self.endpoint}/ping", timeout=10) | ||
| response.raise_for_status() | ||
| break | ||
| except (httpx.ConnectError, httpx.HTTPStatusError) as e: | ||
| if retry_num % 10 == 0: | ||
| logger.warning( | ||
| f"fail to check sglang ready, retry {retry_num} times, error: {e}" | ||
| ) | ||
| retry_num += 1 | ||
| time.sleep(0.1) | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) | ||
| def set_server_addresses(self, server_addresses: list[str]): | ||
| # todo support multiple api server | ||
| self.endpoint = f"http://{server_addresses[0]}" | ||
| if self.rollout_name == "sglang": | ||
| self.check_sglang_ready() | ||
| elif self.rollout_name == "vllm": | ||
| self.check_vllm_ready() | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) | ||
| def sync_rollout_weights_by_ckpt_engine(self): | ||
| rank = self.rank | ||
| src = rank // self.inference_parallel_size * self.inference_parallel_size | ||
|
|
||
| def vllm_req_func(socket_paths: list[tuple[str, str]]) -> None: | ||
| if rank == src: | ||
| request_inference_to_update( | ||
| url=f"{self.endpoint}/collective_rpc", | ||
| socket_paths=dict(socket_paths), | ||
| ) | ||
|
|
||
| def sglang_req_func(socket_paths: list[tuple[str, str]]) -> None: | ||
| if rank == src: | ||
| with httpx.Client(transport=httpx.HTTPTransport()) as client: | ||
| resp = client.post( | ||
| f"{self.endpoint}/update_weights_from_ipc", | ||
| json={ | ||
| "zmq_handles": dict(socket_paths), | ||
| "flush_cache": True, | ||
| "weight_version": None, | ||
| }, | ||
| timeout=300.0, | ||
| ) | ||
| resp.raise_for_status() | ||
| pass | ||
|
|
||
| if self.rollout_name == "sglang": | ||
| req_func = sglang_req_func | ||
| elif self.rollout_name == "vllm": | ||
| req_func = vllm_req_func | ||
|
|
||
| self._init_process_group() | ||
| checkpoint_name = f"sync_{self.index}" | ||
| self.ps.register_checkpoint(checkpoint_name=checkpoint_name) | ||
| self.ps.gather_metas(checkpoint_name) | ||
| self.ps.update(checkpoint_name, req_func, ranks=list(range(self.ps_rank_offset, self.ps_world_size))) | ||
| self.index += 1 | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,11 +18,11 @@ | |
|
|
||
| import torch | ||
| import torch.distributed | ||
| from checkpoint_engine.ps import ParameterServer | ||
| from omegaconf import DictConfig | ||
| from ray.util.collective import collective | ||
| from torch.distributed.fsdp import FullyShardedDataParallel as FSDP | ||
|
|
||
| from recipe.one_step_off_policy.distributed_util import vllm_stateless_init_process_group | ||
| from verl.single_controller.base.decorator import Dispatch, register | ||
| from verl.utils.device import ( | ||
| get_device_name, | ||
|
|
@@ -53,17 +53,6 @@ class DetachSync(AsyncActorRolloutRefWorker): | |
| def _get_actor_params(self): | ||
| pass | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) | ||
| def create_weight_sync_group(self, master_address, master_port, rank_offset, world_size): | ||
| rank = torch.distributed.get_rank() + rank_offset | ||
| self._weight_sync_group = vllm_stateless_init_process_group( | ||
| master_address, | ||
| master_port, | ||
| rank, | ||
| world_size, | ||
| get_torch_device().current_device(), | ||
| ) | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) | ||
| def sync_rollout_weights(self): | ||
| assert (self._is_actor or self._is_rollout) and not self.config.hybrid_engine | ||
|
|
@@ -127,6 +116,59 @@ async def update_weights(self, inference_engine, params): | |
|
|
||
|
|
||
| class DetachActorWorker(DetachSync): | ||
| def __init__(self, config: DictConfig, role: str, **kwargs): | ||
| ActorRolloutRefWorker.__init__(self, config, role) | ||
|
|
||
| if role == "actor": | ||
| self.ps_rank_offset = kwargs.get("rank_offset", self.rank) | ||
| self.ps_world_size = kwargs.get("ps_world_size", self.world_size) | ||
| self.ps = ParameterServer(rank=self.rank, world_size=self.ps_world_size) | ||
| self.index = 0 | ||
|
|
||
| def init_process_group(self): | ||
| os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] = "61020" | ||
| self.ps.init_process_group(device_index=0, master_port=60010) | ||
|
Comment on lines
+129
to
+130
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| del os.environ["HCCL_NPU_SOCKET_PORT_RANGE"] | ||
|
|
||
| def split_tensors(self) -> dict[str, torch.Tensor]: | ||
| assert self._is_actor 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() | ||
|
|
||
| named_tensors = {} | ||
|
|
||
| world_size = self.world_size | ||
| rank = self.rank | ||
|
|
||
| weights_per_rank = (len(self._weights_info) + world_size - 1) // world_size | ||
| for index, (key, _, _) in enumerate(self._weights_info): | ||
| assert key in params | ||
| tensor = params[key].full_tensor() | ||
| if index >= rank * weights_per_rank and index < (rank + 1) * weights_per_rank: | ||
| named_tensors[key] = tensor.to("cpu", non_blocking=True) | ||
|
|
||
| get_torch_device().synchronize() | ||
|
|
||
| return named_tensors | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL, blocking=False) | ||
| def sync_rollout_weights_by_ckpt_engine(self): | ||
| def req_func(socket_paths: list[tuple[str, str]]): | ||
| return | ||
|
|
||
| self.init_process_group() | ||
| named_tensors = self.split_tensors() | ||
| checkpoint_name = f"sync_{self.index}" | ||
|
|
||
| self.ps.register_checkpoint(checkpoint_name=checkpoint_name, named_tensors=named_tensors) | ||
| self.ps.gather_metas(checkpoint_name) | ||
| self.ps.update(checkpoint_name, req_func, ranks=list(range(self.ps_rank_offset, self.ps_world_size))) | ||
|
|
||
| self.index += 1 | ||
|
|
||
| def _get_actor_params(self): | ||
| assert self._is_actor | ||
| params = self.actor_module_fsdp.state_dict() | ||
|
|
@@ -159,8 +201,7 @@ def get_actor_weights_info(self): | |
|
|
||
|
|
||
| class DetachAsyncRolloutWorker(DetachSync): | ||
| def __init__(self, config: DictConfig, role: str): | ||
| print(f"[DetachAsyncRolloutWorker] {DetachAsyncRolloutWorker.__mro__}") | ||
| def __init__(self, config: DictConfig, role: str, **kwargs): | ||
| ActorRolloutRefWorker.__init__(self, config, role) | ||
|
|
||
| @register(dispatch_mode=Dispatch.ONE_TO_ALL) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
while Trueloop for checking vLLM readiness can run indefinitely if the server fails to start, causing the worker to hang. It's much safer to implement a timeout mechanism with a maximum number of retries. This ensures that the worker will eventually fail with a clear error message instead of getting stuck. I've also moved thehttpx.Clientinstantiation out of the loop for efficiency.