main
py 277 lines 9.38 KB
Raw
1 #!/usr/bin/env python3
2 # SPDX-License-Identifier: Apache-2.0
3 # SPDX-FileCopyrightText: Copyright contributors to the vLLM project
4 """Benchmark comparing old vs new default fused MoE configs.
5
6 Runs the triton fused_moe kernel with three configurations for each scenario:
7 1. Tuned config (from JSON file, if available) — the target to match
8 2. Old default (the hardcoded defaults before this change)
9 3. New default (the improved defaults)
10
11 Usage:
12 python benchmarks/kernels/benchmark_moe_defaults.py
13
14 Produces a table showing kernel time (us) and speedup of new vs old defaults.
15 """
16
17 import torch
18
19 from vllm.model_executor.layers.fused_moe import fused_topk, override_config
20 from vllm.model_executor.layers.fused_moe.config import FusedMoEQuantConfig
21 from vllm.model_executor.layers.fused_moe.fused_moe import (
22 fused_experts,
23 get_default_config,
24 get_moe_configs,
25 )
26 from vllm.platforms import current_platform
27 from vllm.triton_utils import triton
28 from vllm.utils.torch_utils import set_random_seed
29
30 FP8_DTYPE = current_platform.fp8_dtype()
31
32
33 def old_default_config(M, E, N, K, topk, dtype=None, block_shape=None):
34 """The original defaults before https://github.com/vllm-project/vllm/pull/34846,
35 for comparison."""
36 if dtype == "fp8_w8a8" and block_shape is not None:
37 return {
38 "BLOCK_SIZE_M": 64,
39 "BLOCK_SIZE_N": block_shape[0],
40 "BLOCK_SIZE_K": block_shape[1],
41 "GROUP_SIZE_M": 32,
42 "SPLIT_K": 1,
43 "num_warps": 4,
44 "num_stages": 3 if not current_platform.is_rocm() else 2,
45 }
46 elif M <= E:
47 return {
48 "BLOCK_SIZE_M": 16,
49 "BLOCK_SIZE_N": 32,
50 "BLOCK_SIZE_K": 64,
51 "GROUP_SIZE_M": 1,
52 "SPLIT_K": 1,
53 }
54 else:
55 return {
56 "BLOCK_SIZE_M": 64,
57 "BLOCK_SIZE_N": 64,
58 "BLOCK_SIZE_K": 32,
59 "GROUP_SIZE_M": 8,
60 "SPLIT_K": 1,
61 }
62
63
64 def benchmark_config(
65 config,
66 M,
67 E,
68 N,
69 K,
70 topk,
71 dtype,
72 use_fp8=False,
73 block_shape=None,
74 num_iters=100,
75 ):
76 """Time a single kernel config. Returns kernel time in microseconds."""
77 init_dtype = torch.float16 if use_fp8 else dtype
78
79 a = torch.randn(M, K, device="cuda", dtype=init_dtype) / 10
80 w1 = torch.randn(E, 2 * N, K, device="cuda", dtype=init_dtype) / 10
81 w2 = torch.randn(E, K, N, device="cuda", dtype=init_dtype) / 10
82
83 w1_scale = None
84 w2_scale = None
85 a1_scale = None
86 a2_scale = None
87 if use_fp8:
88 if block_shape is not None:
89 bsn, bsk = block_shape
90 n_tiles_w1 = triton.cdiv(2 * N, bsn)
91 k_tiles_w1 = triton.cdiv(K, bsk)
92 n_tiles_w2 = triton.cdiv(K, bsn)
93 k_tiles_w2 = triton.cdiv(N, bsk)
94 w1_scale = torch.rand(
95 E, n_tiles_w1, k_tiles_w1, device="cuda", dtype=torch.float32
96 )
97 w2_scale = torch.rand(
98 E, n_tiles_w2, k_tiles_w2, device="cuda", dtype=torch.float32
99 )
100 else:
101 w1_scale = torch.rand(E, device="cuda", dtype=torch.float32)
102 w2_scale = torch.rand(E, device="cuda", dtype=torch.float32)
103 a1_scale = torch.rand(1, device="cuda", dtype=torch.float32)
104 a2_scale = torch.rand(1, device="cuda", dtype=torch.float32)
105 # Only weights are stored in fp8; activations stay in bf16/fp16
106 # and get dynamically quantized inside the kernel.
107 w1 = w1.to(FP8_DTYPE)
108 w2 = w2.to(FP8_DTYPE)
109
110 quant_config = FusedMoEQuantConfig.make(
111 quant_dtype=torch.float8_e4m3fn if use_fp8 else None,
112 w1_scale=w1_scale,
113 w2_scale=w2_scale,
114 a1_scale=a1_scale,
115 a2_scale=a2_scale,
116 block_shape=block_shape,
117 )
118
119 gating = torch.randn(M, E, device="cuda", dtype=torch.float32)
120
121 # Warmup
122 for _ in range(20):
123 with override_config(config):
124 topk_weights, topk_ids, _ = fused_topk(a, gating, topk, renormalize=True)
125 fused_experts(
126 a,
127 w1,
128 w2,
129 topk_weights,
130 topk_ids,
131 quant_config=quant_config,
132 )
133 torch.accelerator.synchronize()
134
135 # Benchmark
136 start = torch.cuda.Event(enable_timing=True)
137 end = torch.cuda.Event(enable_timing=True)
138 start.record()
139 for _ in range(num_iters):
140 with override_config(config):
141 topk_weights, topk_ids, _ = fused_topk(a, gating, topk, renormalize=True)
142 fused_experts(
143 a,
144 w1,
145 w2,
146 topk_weights,
147 topk_ids,
148 quant_config=quant_config,
149 )
150 end.record()
151 torch.accelerator.synchronize()
152 return start.elapsed_time(end) / num_iters * 1000 # ms -> us
153
154
155 # Model configurations: (name, E, N, K, topk, dtype_str, use_fp8, block_shape)
156 # N = moe_intermediate_size // tp_size (the value used in config file lookup)
157 MODELS = [
158 # --- Few experts ---
159 ("Mixtral bf16", 8, 7168, 4096, 2, None, False, None),
160 ("Mixtral fp8", 8, 7168, 4096, 2, "fp8_w8a8", True, None),
161 # --- Many experts: real model shapes at tp=1 ---
162 # Qwen2-MoE-57B: E=60, topk=4, N=1408, K=2048
163 ("Qwen2-MoE bf16", 60, 1408, 2048, 4, None, False, None),
164 # DeepSeek-V2: E=64, topk=6, N=1407, K=4096
165 # (use 1408 to avoid odd alignment; real model is 1407)
166 ("DeepSeek-V2 bf16", 64, 1408, 4096, 6, None, False, None),
167 # OLMoE-7B: E=64, topk=8, N=2048, K=2048
168 ("OLMoE bf16", 64, 2048, 2048, 8, None, False, None),
169 # GLM-4-100B-A10B: E=128, topk=8, N=1408, K=4096
170 ("GLM-4-MoE bf16", 128, 1408, 4096, 8, None, False, None),
171 # Qwen3-30B-A3B: E=128, topk=8, N=768, K=2048
172 ("Qwen3-MoE bf16", 128, 768, 2048, 8, None, False, None),
173 # DeepSeek-V3 / MiMo-V2-Flash: E=256, topk=8, N=2048, K=7168
174 ("DeepSeek-V3 bf16", 256, 2048, 7168, 8, None, False, None),
175 # Qwen3.5-70B-A22B (Qwen3-Next): E=512, topk=10, N=512, K=2048
176 ("Qwen3-Next bf16", 512, 512, 2048, 10, None, False, None),
177 # E=128 N=1856 bf16
178 ("E128 N1856 bf16", 128, 1856, 4096, 8, None, False, None),
179 # E=256 N=512 bf16 (DS-V3 tp=4)
180 ("DS-V3 tp4 bf16", 256, 512, 7168, 8, None, False, None),
181 # E=512 N=512 bf16 (Qwen3-Next tp=1)
182 ("Qwen3-Next bf16", 512, 512, 2048, 10, None, False, None),
183 # E=512 N=256 bf16 (Qwen3-Next tp=2)
184 ("Qwen3-Next tp2", 512, 256, 2048, 10, None, False, None),
185 # --- FP8 block quant (many experts) ---
186 # DS-V3 tp=4: E=256, N=512, fp8 block
187 ("DS-V3 tp4 fp8blk", 256, 512, 7168, 8, "fp8_w8a8", True, [128, 128]),
188 # DS-V3 tp=8: E=256, N=256, fp8 block
189 ("DS-V3 tp8 fp8blk", 256, 256, 7168, 8, "fp8_w8a8", True, [128, 128]),
190 # Qwen3-Next tp=2 fp8 block
191 ("Qwen3-Next tp2 fp8blk", 512, 256, 2048, 10, "fp8_w8a8", True, [128, 128]),
192 ]
193
194 BATCH_SIZES = [1, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096]
195
196
197 def main():
198 set_random_seed(0)
199 torch.set_default_device("cuda")
200 dtype = torch.bfloat16
201
202 for name, E, N, K, topk, dtype_str, use_fp8, block_shape in MODELS:
203 print(f"\n{'=' * 90}")
204 print(f" {name} (E={E}, N={N}, K={K}, topk={topk})")
205 print(f"{'=' * 90}")
206
207 # Try to load tuned config
208 block_n = block_shape[0] if block_shape else None
209 block_k = block_shape[1] if block_shape else None
210 tuned = get_moe_configs(E, N, dtype_str, block_n, block_k)
211 has_tuned = tuned is not None
212 print(f" Tuned config available: {has_tuned}")
213
214 hdr = (
215 f"{'Batch':>6} | {'Tuned (us)':>11} | {'Old (us)':>11} | "
216 f"{'New (us)':>11} | {'New/Old':>8} | {'New/Tuned':>10}"
217 )
218 print(f" {hdr}")
219 print(f" {'-' * len(hdr)}")
220
221 for M in BATCH_SIZES:
222 old_cfg = old_default_config(M, E, N, K, topk, dtype_str, block_shape)
223 new_cfg = get_default_config(M, E, N, K, topk, dtype_str, block_shape)
224
225 if has_tuned:
226 tuned_cfg = tuned[min(tuned.keys(), key=lambda x: abs(x - M))]
227 t_tuned = benchmark_config(
228 tuned_cfg,
229 M,
230 E,
231 N,
232 K,
233 topk,
234 dtype,
235 use_fp8=use_fp8,
236 block_shape=block_shape,
237 )
238 else:
239 t_tuned = None
240
241 t_old = benchmark_config(
242 old_cfg,
243 M,
244 E,
245 N,
246 K,
247 topk,
248 dtype,
249 use_fp8=use_fp8,
250 block_shape=block_shape,
251 )
252 t_new = benchmark_config(
253 new_cfg,
254 M,
255 E,
256 N,
257 K,
258 topk,
259 dtype,
260 use_fp8=use_fp8,
261 block_shape=block_shape,
262 )
263
264 ratio_new_old = t_new / t_old
265 tuned_str = f"{t_tuned:11.2f}" if t_tuned else f"{'N/A':>11}"
266 ratio_tuned = f"{t_new / t_tuned:10.2f}x" if t_tuned else f"{'N/A':>10}"
267 # flag regressions where new default is >5% slower than old
268 marker = " <--" if ratio_new_old > 1.05 else ""
269
270 print(
271 f" {M:>6} | {tuned_str} | {t_old:11.2f} | {t_new:11.2f} "
272 f"| {ratio_new_old:7.2f}x | {ratio_tuned}{marker}"
273 )
274
275
276 if __name__ == "__main__":
277 main()