|
| 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 | + |
| 15 | +import gc |
| 16 | + |
| 17 | +import torch |
| 18 | +import torch.distributed |
| 19 | +import torch.distributed as dist |
| 20 | +from omegaconf import OmegaConf |
| 21 | +from transformers import AutoConfig, AutoTokenizer |
| 22 | + |
| 23 | +from verl import DataProto |
| 24 | +from verl.utils.distributed import initialize_global_process_group |
| 25 | +from verl.utils.model import compute_position_id_with_mask |
| 26 | +from verl.workers.rollout.vllm_rollout.vllm_rollout_spmd import vLLMRollout |
| 27 | + |
| 28 | + |
| 29 | +def test_vllm_rollout_with_yarn_position_embeddings(): |
| 30 | + """ |
| 31 | + Test the vLLM rollout with yarn position embeddings. |
| 32 | + """ |
| 33 | + |
| 34 | + local_rank, rank, world_size = initialize_global_process_group() |
| 35 | + config = OmegaConf.create( |
| 36 | + { |
| 37 | + "model_path": "OldKingMeister/Qwen2.5-1.5B-Instruct-YaRN", |
| 38 | + "prompt_length": 35000, |
| 39 | + "response_length": 512, |
| 40 | + "dtype": "bfloat16", |
| 41 | + "enforce_eager": True, |
| 42 | + "gpu_memory_utilization": 0.4, |
| 43 | + "enable_chunked_prefill": False, |
| 44 | + "free_cache_engine": False, |
| 45 | + "disable_log_stats": True, |
| 46 | + "max_model_len": 35000 + 512, |
| 47 | + "load_format": "auto", |
| 48 | + "val_kwargs": { |
| 49 | + "top_k": -1, |
| 50 | + "top_p": 1.0, |
| 51 | + "temperature": 0, |
| 52 | + "n": 1, |
| 53 | + "do_sample": False, |
| 54 | + }, |
| 55 | + "tensor_model_parallel_size": 4, |
| 56 | + "trust_remote_code": True, |
| 57 | + "calculate_log_probs": False, |
| 58 | + "do_sample": False, |
| 59 | + "temperature": 0.0, |
| 60 | + "max_num_batched_tokens": 35000 + 512, |
| 61 | + } |
| 62 | + ) |
| 63 | + |
| 64 | + tokenizer = AutoTokenizer.from_pretrained(config.model_path, trust_remote_code=True, padding_side="left") |
| 65 | + tokenizer.pad_token = tokenizer.eos_token |
| 66 | + model_hf_config = AutoConfig.from_pretrained(config.model_path) |
| 67 | + |
| 68 | + # do_sample=False for temperate=0 deterministic |
| 69 | + input_dataproto = prepare_input_dataproto(tokenizer, config, validate=True, do_sample=False) |
| 70 | + |
| 71 | + vllm_rollout = vLLMRollout( |
| 72 | + model_path=config.model_path, |
| 73 | + config=config, |
| 74 | + tokenizer=tokenizer, |
| 75 | + model_hf_config=model_hf_config, |
| 76 | + ) |
| 77 | + # rollout |
| 78 | + rollout_response = vllm_rollout.generate_sequences( |
| 79 | + prompts=input_dataproto, |
| 80 | + ) |
| 81 | + if rank == 0: |
| 82 | + print("VLLM Rollout Outputs:") |
| 83 | + print(tokenizer.batch_decode(rollout_response.batch["responses"][:], skip_special_tokens=False)) |
| 84 | + for response in rollout_response.batch["responses"]: |
| 85 | + assert "<|im_end|>" in tokenizer.decode(response, skip_special_tokens=False), ( |
| 86 | + "Response should contain <|im_end|> token" |
| 87 | + ) |
| 88 | + print("Checks passed.") |
| 89 | + |
| 90 | + del vllm_rollout |
| 91 | + gc.collect() |
| 92 | + torch.cuda.empty_cache() |
| 93 | + torch.cuda.ipc_collect() |
| 94 | + dist.barrier() |
| 95 | + torch.distributed.destroy_process_group() |
| 96 | + |
| 97 | + |
| 98 | +def prepare_input_dataproto(tokenizer, config, validate, do_sample=False): |
| 99 | + base_phrase = "Roses are red, sky is blue. " * 4096 |
| 100 | + preencode_prompts = [ |
| 101 | + # 32810 tokens > 32768 tokens |
| 102 | + [{"role": "user", "content": base_phrase + "Who won the Champions League in 2019?"}], |
| 103 | + [{"role": "user", "content": base_phrase + "The founder of Apple is"}], |
| 104 | + [{"role": "user", "content": base_phrase + "What's your name"}], |
| 105 | + ] |
| 106 | + formatted_prompts = [ |
| 107 | + tokenizer.apply_chat_template(conversation, tokenize=False, add_generation_prompt=True) |
| 108 | + for conversation in preencode_prompts |
| 109 | + ] |
| 110 | + prompts = tokenizer(formatted_prompts, return_tensors="pt", padding="max_length", max_length=config.prompt_length) |
| 111 | + input_dataproto = DataProto.from_dict( |
| 112 | + { |
| 113 | + "input_ids": prompts["input_ids"], |
| 114 | + "attention_mask": prompts["attention_mask"], |
| 115 | + "position_ids": compute_position_id_with_mask(prompts["attention_mask"]), |
| 116 | + }, |
| 117 | + meta_info={ |
| 118 | + "bos_token_id": tokenizer.bos_token_id, |
| 119 | + "eos_token_id": tokenizer.eos_token_id, |
| 120 | + "pad_token_id": tokenizer.pad_token_id, |
| 121 | + "validate": validate, |
| 122 | + "do_sample": do_sample, |
| 123 | + "response_length": config.response_length, |
| 124 | + "temperature": config.temperature, |
| 125 | + }, |
| 126 | + ) |
| 127 | + return input_dataproto |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + test_vllm_rollout_with_yarn_position_embeddings() |
0 commit comments