| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | """experimental support for tensor-parallel inference with torchrun, |
| 4 | see https://github.com/vllm-project/vllm/issues/11400 for |
| 5 | the motivation and use case for this example. |
| 6 | run the script with `torchrun --nproc-per-node=4 torchrun_example_offline.py`, |
| 7 | the argument `4` should match the product of `tensor_parallel_size` and |
| 8 | `pipeline_parallel_size` below. see `tests/distributed/test_torchrun_example.py` |
| 9 | for the unit test. |
| 10 | """ |
| 11 | |
| 12 | import torch.distributed as dist |
| 13 | |
| 14 | from vllm import LLM, SamplingParams |
| 15 | |
| 16 | # Create prompts, the same across all ranks |
| 17 | prompts = [ |
| 18 | "Hello, my name is", |
| 19 | "The president of the United States is", |
| 20 | "The capital of France is", |
| 21 | "The future of AI is", |
| 22 | ] |
| 23 | |
| 24 | # Create sampling parameters, the same across all ranks |
| 25 | sampling_params = SamplingParams(temperature=0.8, top_p=0.95) |
| 26 | |
| 27 | # Use `distributed_executor_backend="external_launcher"` so that |
| 28 | # this llm engine/instance only creates one worker. |
| 29 | # it is important to set an explicit seed to make sure that |
| 30 | # all ranks have the same random seed, so that sampling can be |
| 31 | # deterministic across ranks. |
| 32 | llm = LLM( |
| 33 | model="meta-llama/Llama-3.1-8B", |
| 34 | tensor_parallel_size=2, |
| 35 | pipeline_parallel_size=2, |
| 36 | distributed_executor_backend="external_launcher", |
| 37 | max_model_len=32768, |
| 38 | seed=1, |
| 39 | ) |
| 40 | |
| 41 | outputs = llm.generate(prompts, sampling_params) |
| 42 | |
| 43 | # all ranks will have the same outputs |
| 44 | if dist.get_rank() == 0: |
| 45 | print("-" * 50) |
| 46 | for output in outputs: |
| 47 | prompt = output.prompt |
| 48 | generated_text = output.outputs[0].text |
| 49 | print(f"Prompt: {prompt!r}\nGenerated text: {generated_text!r}\n") |
| 50 | print("-" * 50) |
| 51 | """ |
| 52 | Further tips: |
| 53 | |
| 54 | 1. to communicate control messages across all ranks, use the cpu group, |
| 55 | a PyTorch ProcessGroup with GLOO backend. |
| 56 | |
| 57 | ```python |
| 58 | from vllm.distributed.parallel_state import get_world_group |
| 59 | cpu_group = get_world_group().cpu_group |
| 60 | torch_rank = dist.get_rank(group=cpu_group) |
| 61 | if torch_rank == 0: |
| 62 | # do something for rank 0, e.g. saving the results to disk. |
| 63 | ``` |
| 64 | |
| 65 | 2. to communicate data across all ranks, use the model's device group, |
| 66 | a PyTorch ProcessGroup with NCCL backend. |
| 67 | ```python |
| 68 | from vllm.distributed.parallel_state import get_world_group |
| 69 | device_group = get_world_group().device_group |
| 70 | ``` |
| 71 | |
| 72 | 3. to access the model directly in every rank, use the following code: |
| 73 | ```python |
| 74 | llm.llm_engine.model_executor.driver_worker.worker.model_runner.model |
| 75 | ``` |
| 76 | """ |