|
| 1 | +# Copyright 2024 Bytedance Ltd. and/or its affiliates |
| 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 | +import os |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import pytest |
| 18 | +import ray |
| 19 | +from omegaconf import DictConfig |
| 20 | + |
| 21 | +from verl.checkpoint_engine import CheckpointEngineManager |
| 22 | +from verl.experimental.agent_loop.agent_loop import AgentLoopManager |
| 23 | +from verl.protocol import DataProto |
| 24 | +from verl.single_controller.ray import ( |
| 25 | + RayClassWithInitArgs, |
| 26 | + RayResourcePool, |
| 27 | + RayWorkerGroup, |
| 28 | +) |
| 29 | +from verl.single_controller.ray.base import create_colocated_worker_cls |
| 30 | +from verl.utils.device import get_device_name |
| 31 | +from verl.utils.tokenizer import hf_tokenizer |
| 32 | +from verl.workers.engine_workers import ActorRolloutRefWorker |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def init_config() -> DictConfig: |
| 37 | + from hydra import compose, initialize_config_dir |
| 38 | + |
| 39 | + with initialize_config_dir(config_dir=os.path.abspath("verl/trainer/config")): |
| 40 | + config = compose(config_name="ppo_trainer") |
| 41 | + |
| 42 | + config.trainer.n_gpus_per_node = 8 |
| 43 | + config.trainer.nnodes = 1 |
| 44 | + config.actor_rollout_ref.actor.use_dynamic_bsz = True |
| 45 | + config.actor_rollout_ref.model.path = os.path.expanduser("~/models/Qwen/Qwen3-VL-2B-Instruct") |
| 46 | + config.actor_rollout_ref.rollout.name = os.environ["ROLLOUT_NAME"] |
| 47 | + config.actor_rollout_ref.rollout.skip_tokenizer_init = False |
| 48 | + config.actor_rollout_ref.rollout.max_num_seqs = 256 |
| 49 | + config.actor_rollout_ref.rollout.gpu_memory_utilization = 0.8 |
| 50 | + config.actor_rollout_ref.rollout.agent.num_workers = 2 |
| 51 | + config.actor_rollout_ref.rollout.checkpoint_engine.backend = "naive" |
| 52 | + config.actor_rollout_ref.rollout.checkpoint_engine.update_weights_bucket_megabytes = 256 |
| 53 | + config.actor_rollout_ref.rollout.enforce_eager = True |
| 54 | + |
| 55 | + return config |
| 56 | + |
| 57 | + |
| 58 | +@pytest.mark.skip(reason="This test costs too much to run in CI.") |
| 59 | +@pytest.mark.asyncio |
| 60 | +def test_server_adapter_colocated_weight_update(init_config): |
| 61 | + ray.init( |
| 62 | + runtime_env={ |
| 63 | + "env_vars": { |
| 64 | + "TOKENIZERS_PARALLELISM": "true", |
| 65 | + "NCCL_DEBUG": "WARN", |
| 66 | + "VLLM_LOGGING_LEVEL": "INFO", |
| 67 | + "VLLM_USE_V1": "1", |
| 68 | + "VLLM_DISABLE_COMPILE_CACHE": "1", |
| 69 | + "HCCL_HOST_SOCKET_PORT_RANGE": "60000-60050", |
| 70 | + "HCCL_NPU_SOCKET_PORT_RANGE": "61000-61050", |
| 71 | + } |
| 72 | + } |
| 73 | + ) |
| 74 | + |
| 75 | + # 0. init actor rollout worker group |
| 76 | + resource_pool = RayResourcePool( |
| 77 | + process_on_nodes=[init_config.trainer.n_gpus_per_node] * init_config.trainer.nnodes, max_colocate_count=3 |
| 78 | + ) |
| 79 | + actor_rollout_cls = ray.remote(ActorRolloutRefWorker) |
| 80 | + cls_dict = { |
| 81 | + "actor_rollout": RayClassWithInitArgs( |
| 82 | + cls=actor_rollout_cls, config=init_config.actor_rollout_ref, role="actor_rollout" |
| 83 | + ) |
| 84 | + } |
| 85 | + ray_cls_with_init = create_colocated_worker_cls(cls_dict) |
| 86 | + wg_dict = RayWorkerGroup( |
| 87 | + resource_pool=resource_pool, ray_cls_with_init=ray_cls_with_init, device_name=get_device_name() |
| 88 | + ) |
| 89 | + spawn_wg = wg_dict.spawn(prefix_set=cls_dict.keys()) |
| 90 | + actor_rollout_wg = spawn_wg["actor_rollout"] |
| 91 | + actor_rollout_wg.init_model() |
| 92 | + |
| 93 | + # 1. create AgentLoopManager |
| 94 | + agent_loop_manager = AgentLoopManager( |
| 95 | + config=init_config, |
| 96 | + worker_group=actor_rollout_wg, |
| 97 | + rollout_resource_pool=resource_pool, |
| 98 | + ) |
| 99 | + |
| 100 | + # 2. create CheckpointEngineManager |
| 101 | + checkpoint_manager = CheckpointEngineManager( |
| 102 | + backend=init_config.actor_rollout_ref.rollout.checkpoint_engine.backend, |
| 103 | + trainer=actor_rollout_wg, |
| 104 | + replicas=agent_loop_manager.rollout_replicas, |
| 105 | + ) |
| 106 | + checkpoint_manager.sleep_replicas() |
| 107 | + |
| 108 | + # 3. generate prompts |
| 109 | + raw_prompts = [ |
| 110 | + [ |
| 111 | + { |
| 112 | + "role": "user", |
| 113 | + "content": "This is a test for weight update. If the weight has been correctly " |
| 114 | + 'updated and you understand my meaning, please respond with "Test Passed".', |
| 115 | + } |
| 116 | + ], |
| 117 | + [ |
| 118 | + { |
| 119 | + "role": "user", |
| 120 | + "content": "This is a test for weight update. If the weight has been correctly " |
| 121 | + 'updated and you understand my meaning, please respond with "Test Passed".', |
| 122 | + } |
| 123 | + ], |
| 124 | + ] |
| 125 | + batch = DataProto( |
| 126 | + non_tensor_batch={ |
| 127 | + "raw_prompt": np.array(raw_prompts), |
| 128 | + "agent_name": np.array(["single_turn_agent"] * len(raw_prompts)), |
| 129 | + "data_source": np.array(["openai/gsm8k"] * len(raw_prompts)), |
| 130 | + "reward_model": np.array([{"style": "rule", "ground_truth": "1.0"}] * len(raw_prompts)), |
| 131 | + }, |
| 132 | + ) |
| 133 | + |
| 134 | + # 4. update weights and generate sequences, check if the responses are correct |
| 135 | + for _ in range(3): |
| 136 | + checkpoint_manager.update_weights() |
| 137 | + result = agent_loop_manager.generate_sequences(batch) |
| 138 | + checkpoint_manager.sleep_replicas() |
| 139 | + |
| 140 | + # Check response |
| 141 | + tokenizer = hf_tokenizer(init_config.actor_rollout_ref.model.path) |
| 142 | + responses = result.batch["responses"] |
| 143 | + response_mask = result.batch["response_mask"] |
| 144 | + |
| 145 | + for i in range(len(responses)): |
| 146 | + valid_tokens = responses[i][response_mask[i].bool()] |
| 147 | + response = tokenizer.decode(valid_tokens) |
| 148 | + assert "test passed" in response.lower(), f"Response does not contain 'test passed': {response}" |
| 149 | + |
| 150 | + print("=========================") |
| 151 | + print("[OUTPUT]:", response) |
| 152 | + print("---") |
| 153 | + |
| 154 | + ray.shutdown() |
0 commit comments