|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | + |
| 4 | +# unit test for `examples/offline_inference/torchrun_example.py` |
| 5 | +import os |
| 6 | +import random |
| 7 | + |
| 8 | +import torch.distributed as dist |
| 9 | + |
| 10 | +from vllm import LLM, SamplingParams |
| 11 | +from vllm.distributed.parallel_state import get_tp_group, get_world_group |
| 12 | + |
| 13 | +dist.init_process_group(backend="gloo") |
| 14 | + |
| 15 | +# Create prompts |
| 16 | +prompts = [ |
| 17 | + "Hello, my name is", |
| 18 | + "The president of the United States is", |
| 19 | + "The capital of France is", |
| 20 | + "The future of AI is", |
| 21 | +] * 10 |
| 22 | +dp_size = int(os.getenv("DP_SIZE", "1")) |
| 23 | +dp_rank = int(os.getenv("DP_RANK", "0")) |
| 24 | + |
| 25 | +if dp_size > 1: |
| 26 | + # distribute the prompts across the data parallel ranks |
| 27 | + prompts = [ |
| 28 | + prompt for idx, prompt in enumerate(prompts) |
| 29 | + if idx % dp_size == dp_rank |
| 30 | + ] |
| 31 | + |
| 32 | +sampling_params = SamplingParams(temperature=0.8, top_p=0.95) |
| 33 | + |
| 34 | +# set different `gpu_memory_utilization` and `swap_space` for different ranks, |
| 35 | +# to test if all ranks agree on the same kv cache configuration. |
| 36 | +llm = LLM(model="microsoft/Phi-mini-MoE-instruct", |
| 37 | + tensor_parallel_size=int(os.getenv("TP_SIZE", "1")), |
| 38 | + pipeline_parallel_size=int(os.getenv("PP_SIZE", "1")), |
| 39 | + enable_expert_parallel=int(os.getenv("ENABLE_EP", "0")) == 1, |
| 40 | + distributed_executor_backend="external_launcher", |
| 41 | + gpu_memory_utilization=random.uniform(0.7, 0.9), |
| 42 | + swap_space=random.randint(1, 4), |
| 43 | + seed=0) |
| 44 | + |
| 45 | +outputs = llm.generate(prompts, sampling_params) |
| 46 | + |
| 47 | +group = get_world_group() if dp_size == 1 else get_tp_group() |
| 48 | +cpu_group = group.cpu_group |
| 49 | +group_rank = dist.get_rank(group=cpu_group) |
| 50 | + |
| 51 | + |
| 52 | +def test_consistent_across_ranks(obj): |
| 53 | + if group_rank == 0: |
| 54 | + dist.broadcast_object_list([obj], src=group.ranks[0], group=cpu_group) |
| 55 | + else: |
| 56 | + container = [None] |
| 57 | + dist.broadcast_object_list(container, |
| 58 | + src=group.ranks[0], |
| 59 | + group=cpu_group) |
| 60 | + assert container[0] == obj |
| 61 | + |
| 62 | + |
| 63 | +test_consistent_across_ranks( |
| 64 | + llm.llm_engine.vllm_config.cache_config.num_cpu_blocks) |
| 65 | +test_consistent_across_ranks( |
| 66 | + llm.llm_engine.vllm_config.cache_config.num_gpu_blocks) |
| 67 | + |
| 68 | +# make sure we can access the model parameters from the calling process |
| 69 | +# of the `LLM` instance. |
| 70 | +params = list(llm.llm_engine.model_executor.driver_worker.worker.model_runner. |
| 71 | + model.parameters()) |
| 72 | +test_consistent_across_ranks(len(params)) |
| 73 | + |
| 74 | +# all ranks should have the same outputs |
| 75 | +for output in outputs: |
| 76 | + prompt = output.prompt |
| 77 | + generated_text = output.outputs[0].text |
| 78 | + test_consistent_across_ranks(prompt) |
| 79 | + test_consistent_across_ranks(generated_text) |
| 80 | + print(f"Rank {group_rank}, Prompt: {prompt!r}, " |
| 81 | + f"Generated text: {generated_text!r}") |
0 commit comments