| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import pytest |
| 5 | |
| 6 | from vllm._aiter_ops import is_aiter_found, rocm_aiter_ops |
| 7 | from vllm.config import CompilationConfig, CompilationMode, CUDAGraphMode |
| 8 | from vllm.envs import disable_envs_cache |
| 9 | from vllm.platforms import current_platform |
| 10 | |
| 11 | from ....conftest import VllmRunner |
| 12 | from ....utils import ( |
| 13 | assert_rocm_custom_allreduce_backend_state_on_worker, |
| 14 | multi_gpu_test, |
| 15 | ) |
| 16 | |
| 17 | PROMPTS = ["Hello, my name is", "The capital of France is"] |
| 18 | |
| 19 | |
| 20 | def _run_generation( |
| 21 | vllm_runner: type[VllmRunner], |
| 22 | monkeypatch: pytest.MonkeyPatch, |
| 23 | compilation_config: CompilationConfig, |
| 24 | *, |
| 25 | model: str, |
| 26 | max_tokens: int, |
| 27 | use_aiter_custom_ar: bool, |
| 28 | quick_reduce_quantization: str, |
| 29 | ) -> list[tuple[list[int], str]]: |
| 30 | with monkeypatch.context() as m: |
| 31 | m.setenv("VLLM_ALLOW_INSECURE_SERIALIZATION", "1") |
| 32 | m.setenv("VLLM_ROCM_USE_AITER", "1") |
| 33 | m.setenv( |
| 34 | "VLLM_ROCM_USE_AITER_CUSTOM_AR", |
| 35 | "1" if use_aiter_custom_ar else "0", |
| 36 | ) |
| 37 | m.setenv("VLLM_ROCM_QUICK_REDUCE_QUANTIZATION", quick_reduce_quantization) |
| 38 | disable_envs_cache() |
| 39 | rocm_aiter_ops.refresh_env_variables() |
| 40 | |
| 41 | with vllm_runner( |
| 42 | model, |
| 43 | dtype="half", |
| 44 | tensor_parallel_size=2, |
| 45 | compilation_config=compilation_config, |
| 46 | max_model_len=256, |
| 47 | max_num_seqs=len(PROMPTS), |
| 48 | gpu_memory_utilization=0.7, |
| 49 | ) as llm: |
| 50 | llm.get_llm().collective_rpc( |
| 51 | assert_rocm_custom_allreduce_backend_state_on_worker, |
| 52 | args=(use_aiter_custom_ar, quick_reduce_quantization), |
| 53 | ) |
| 54 | |
| 55 | return llm.generate_greedy(PROMPTS, max_tokens) |
| 56 | |
| 57 | |
| 58 | @pytest.mark.skipif(not current_platform.is_rocm(), reason="ROCm-only") |
| 59 | @pytest.mark.skipif(not is_aiter_found(), reason="AITER is not installed") |
| 60 | @multi_gpu_test(num_gpus=2) |
| 61 | @pytest.mark.parametrize( |
| 62 | "quick_reduce_quantization", |
| 63 | [ |
| 64 | pytest.param("FP", id="quick-reduce-on"), |
| 65 | pytest.param("NONE", id="quick-reduce-off"), |
| 66 | ], |
| 67 | ) |
| 68 | @pytest.mark.parametrize( |
| 69 | "cudagraph_mode", |
| 70 | [ |
| 71 | pytest.param(CUDAGraphMode.NONE, id="cudagraph-none"), |
| 72 | pytest.param(CUDAGraphMode.FULL, id="cudagraph-full"), |
| 73 | ], |
| 74 | ) |
| 75 | @pytest.mark.parametrize( |
| 76 | "model,max_tokens", |
| 77 | [ |
| 78 | pytest.param("facebook/opt-125m", 8, id="opt-125m"), |
| 79 | ], |
| 80 | ) |
| 81 | def test_rocm_aiter_custom_ar_e2e( |
| 82 | vllm_runner: type[VllmRunner], |
| 83 | monkeypatch: pytest.MonkeyPatch, |
| 84 | cudagraph_mode: CUDAGraphMode, |
| 85 | quick_reduce_quantization: str, |
| 86 | model: str, |
| 87 | max_tokens: int, |
| 88 | ): |
| 89 | compilation_mode = ( |
| 90 | CompilationMode.NONE |
| 91 | if cudagraph_mode == CUDAGraphMode.NONE |
| 92 | else CompilationMode.VLLM_COMPILE |
| 93 | ) |
| 94 | compilation_config = CompilationConfig( |
| 95 | mode=compilation_mode, |
| 96 | cudagraph_mode=cudagraph_mode, |
| 97 | ) |
| 98 | |
| 99 | baseline_generations = _run_generation( |
| 100 | vllm_runner, |
| 101 | monkeypatch, |
| 102 | compilation_config, |
| 103 | model=model, |
| 104 | max_tokens=max_tokens, |
| 105 | use_aiter_custom_ar=False, |
| 106 | quick_reduce_quantization=quick_reduce_quantization, |
| 107 | ) |
| 108 | aiter_custom_ar_generations = _run_generation( |
| 109 | vllm_runner, |
| 110 | monkeypatch, |
| 111 | compilation_config, |
| 112 | model=model, |
| 113 | max_tokens=max_tokens, |
| 114 | use_aiter_custom_ar=True, |
| 115 | quick_reduce_quantization=quick_reduce_quantization, |
| 116 | ) |
| 117 | |
| 118 | assert aiter_custom_ar_generations == baseline_generations |