main
py 116 lines 4.23 KB
Raw
1 # SPDX-License-Identifier: Apache-2.0
2 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
3 from contextlib import ExitStack
4
5 import pytest
6
7 from tests.utils import create_new_process_for_each_test
8 from tests.v1.attention.utils import full_cg_backend_configs as backend_configs
9 from vllm import LLM
10 from vllm.config import CompilationConfig, CompilationMode
11 from vllm.platforms import current_platform
12
13 # test attention backend and cudagraph_mode combo
14 # (backend_name, cudagraph_mode, supported)
15 if current_platform.is_rocm():
16 combo_cases_1 = [
17 ("RocmAttn", "FULL", True),
18 ("RocmAttn", "FULL_AND_PIECEWISE", True),
19 ("TritonAttn", "FULL", True),
20 ("TritonAttn", "FULL_AND_PIECEWISE", True),
21 ]
22 else:
23 combo_cases_1 = [
24 ("FA3", "FULL", True),
25 ("FA3", "FULL_AND_PIECEWISE", True),
26 ("FA2", "FULL", True), # Should fallback to FULL_AND_PIECEWISE
27 ("FA2", "FULL_AND_PIECEWISE", True),
28 ("FlashInfer", "FULL", True), # Should fallback to FULL_AND_PIECEWISE
29 ("FlashInfer", "FULL_AND_PIECEWISE", True),
30 ]
31
32
33 @pytest.mark.parametrize("backend_name, cudagraph_mode, supported", combo_cases_1)
34 @create_new_process_for_each_test("spawn")
35 def test_backend_and_cudagraph_mode_combo(backend_name, cudagraph_mode, supported):
36 if backend_name == "FlashInfer":
37 try:
38 import flashinfer # noqa: F401
39 except ImportError:
40 pytest.skip("FlashInfer is not installed")
41 backend_config = backend_configs[backend_name]
42 # Dynamically skip test if GPU capability is not met
43 if (
44 backend_config.specific_gpu_arch
45 and backend_config.specific_gpu_arch != current_platform.get_device_capability()
46 ):
47 pytest.skip("Only Hopper GPUs support FA3 and FlashMLA")
48
49 attention_config = backend_config.attention_config
50
51 with ExitStack() as stack:
52 if not supported:
53 stack.enter_context(pytest.raises(Exception))
54
55 llm = LLM(
56 model="Qwen/Qwen2-1.5B-Instruct",
57 max_num_seqs=16,
58 max_num_batched_tokens=128,
59 trust_remote_code=True,
60 gpu_memory_utilization=0.45,
61 max_model_len=1024,
62 attention_config=attention_config,
63 compilation_config=CompilationConfig(
64 mode=CompilationMode.VLLM_COMPILE,
65 cudagraph_mode=cudagraph_mode,
66 cudagraph_capture_sizes=[1, 2, 4, 8, 16, 64, 128],
67 ),
68 )
69 llm.generate(["Hello, my name is"] * 10)
70
71
72 # test cudagraph_mode with different compilation mode.
73 # (backend_name, cudagraph_mode, compilation_mode, supported)
74 attn_backend = "RocmAttn" if current_platform.is_rocm() else "FA2"
75
76 combo_cases_2 = [
77 (attn_backend, "FULL", CompilationMode.NONE, True),
78 (attn_backend, "PIECEWISE", CompilationMode.NONE, True),
79 (attn_backend, "PIECEWISE", CompilationMode.VLLM_COMPILE, True),
80 (attn_backend, "FULL_AND_PIECEWISE", CompilationMode.NONE, True),
81 (attn_backend, "FULL_DECODE_ONLY", CompilationMode.NONE, True),
82 (attn_backend, "FULL_DECODE_ONLY", CompilationMode.VLLM_COMPILE, True),
83 (attn_backend, "NONE", CompilationMode.NONE, True),
84 (attn_backend, "NONE", CompilationMode.VLLM_COMPILE, True),
85 ]
86
87
88 @pytest.mark.parametrize(
89 "backend_name,cudagraph_mode,compilation_mode,supported", combo_cases_2
90 )
91 @create_new_process_for_each_test("spawn")
92 def test_cudagraph_compilation_combo(
93 backend_name, cudagraph_mode, compilation_mode, supported
94 ):
95 backend_config = backend_configs[backend_name]
96 attention_config = backend_config.attention_config
97
98 with ExitStack() as stack:
99 if not supported:
100 stack.enter_context(pytest.raises(Exception))
101
102 llm = LLM(
103 model="Qwen/Qwen2-1.5B-Instruct",
104 max_num_seqs=16,
105 max_num_batched_tokens=128,
106 trust_remote_code=True,
107 gpu_memory_utilization=0.45,
108 max_model_len=1024,
109 attention_config=attention_config,
110 compilation_config=CompilationConfig(
111 mode=compilation_mode,
112 cudagraph_mode=cudagraph_mode,
113 cudagraph_capture_sizes=[1, 2, 4, 8, 16, 64, 128],
114 ),
115 )
116 llm.generate(["Hello, my name is"] * 10)