|
| 1 | +# SPDX-License-Identifier: Apache-2.0 |
| 2 | +# SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | +from dataclasses import dataclass |
| 4 | + |
| 5 | +import pytest |
| 6 | +import torch |
| 7 | + |
| 8 | +from vllm.config import ParallelConfig, VllmConfig, set_current_vllm_config |
| 9 | +from vllm.model_executor.layers.fused_moe.fused_moe import fused_experts |
| 10 | +from vllm.model_executor.layers.fused_moe.layer import FusedMoE |
| 11 | +from vllm.model_executor.layers.quantization.utils.flashinfer_utils import ( |
| 12 | + apply_flashinfer_per_tensor_scale_fp8, flashinfer_cutlass_moe_fp8, |
| 13 | + register_moe_scaling_factors, rotate_flashinfer_fp8_moe_weights, |
| 14 | + swap_w13_to_w31) |
| 15 | +from vllm.model_executor.layers.quantization.utils.fp8_utils import ( |
| 16 | + input_to_float8) |
| 17 | +from vllm.model_executor.models.llama4 import Llama4MoE |
| 18 | +from vllm.platforms import current_platform |
| 19 | +from vllm.utils.flashinfer import has_flashinfer_cutlass_fused_moe |
| 20 | + |
| 21 | +if not has_flashinfer_cutlass_fused_moe( |
| 22 | +) or not current_platform.has_device_capability(100): |
| 23 | + pytest.skip("Requires flashinfer_cutlass_fused_moe and nvfp4 support", |
| 24 | + allow_module_level=True) |
| 25 | + |
| 26 | +NUM_EXPERTS = [16] |
| 27 | +TOP_KS = [1] |
| 28 | + |
| 29 | +MNK_FACTORS = [ |
| 30 | + (256, 8192, 5120), |
| 31 | + (256, 4096, 5120), |
| 32 | + (127, 8192, 5120), |
| 33 | + (127, 4096, 5120), |
| 34 | + (10, 8192, 5120), |
| 35 | + (10, 4096, 5120), |
| 36 | + (1, 8192, 5120), |
| 37 | + (1, 4096, 5120), |
| 38 | +] |
| 39 | + |
| 40 | +vllm_config = VllmConfig(parallel_config=ParallelConfig( |
| 41 | + pipeline_parallel_size=1)) |
| 42 | +vllm_config.scheduler_config.max_num_seqs = 128 |
| 43 | +vllm_config.scheduler_config.max_model_len = 8192 |
| 44 | + |
| 45 | + |
| 46 | +def quant_fp8_per_tensor_batches(a): |
| 47 | + num_batches = a.size(0) |
| 48 | + a_quant = [] |
| 49 | + a_scales = [] |
| 50 | + |
| 51 | + for i in range(num_batches): |
| 52 | + a_fp8, a_global_sf = input_to_float8(a[i]) |
| 53 | + a_global_sf = 1.0 / a_global_sf |
| 54 | + a_quant.append(a_fp8) |
| 55 | + a_scales.append(a_global_sf) |
| 56 | + |
| 57 | + result_a_quant = torch.stack(a_quant) |
| 58 | + result_a_scales = torch.stack(a_scales) |
| 59 | + |
| 60 | + return result_a_quant, result_a_scales |
| 61 | + |
| 62 | + |
| 63 | +@dataclass |
| 64 | +class TestData: |
| 65 | + hidden_states: torch.Tensor |
| 66 | + w13_quantized: torch.Tensor |
| 67 | + w2_quantized: torch.Tensor |
| 68 | + a1_scale: torch.Tensor |
| 69 | + a2_scale: torch.Tensor |
| 70 | + w13_weight_scale: torch.Tensor |
| 71 | + w2_weight_scale: torch.Tensor |
| 72 | + layer: torch.nn.Module |
| 73 | + |
| 74 | + @staticmethod |
| 75 | + def make_moe_tensors_8bit(m: int, k: int, n: int, e: int, |
| 76 | + reorder: bool) -> "TestData": |
| 77 | + hidden_states = torch.randn( |
| 78 | + (m, k), device="cuda", dtype=torch.bfloat16) / 10 |
| 79 | + w13 = torch.randn((e, 2 * n, k), device="cuda", dtype=torch.bfloat16) |
| 80 | + w2 = torch.randn((e, k, n), device="cuda", dtype=torch.bfloat16) |
| 81 | + |
| 82 | + # Scale to fp8 |
| 83 | + _, a1_scale = input_to_float8(hidden_states) |
| 84 | + a1_scale = 1.0 / a1_scale |
| 85 | + a2_scale = torch.scalar_tensor(1.0).to(device="cuda").to( |
| 86 | + dtype=torch.float32) |
| 87 | + w13_quantized, w13_weight_scale = quant_fp8_per_tensor_batches(w13) |
| 88 | + w2_quantized, w2_weight_scale = quant_fp8_per_tensor_batches(w2) |
| 89 | + |
| 90 | + layer = torch.nn.Module() |
| 91 | + layer.w13_weight = w13_quantized.clone() |
| 92 | + layer.w2_weight = w2_quantized.clone() |
| 93 | + layer.w13_input_scale = a1_scale |
| 94 | + layer.w2_input_scale = a2_scale |
| 95 | + layer.w13_weight_scale = w13_weight_scale |
| 96 | + layer.w2_weight_scale = w2_weight_scale |
| 97 | + |
| 98 | + register_moe_scaling_factors(layer) |
| 99 | + |
| 100 | + # flashinfer expects swapped rows for w13 |
| 101 | + layer.w13_weight.data = swap_w13_to_w31(layer.w13_weight.data) |
| 102 | + if reorder: |
| 103 | + rotate_flashinfer_fp8_moe_weights(layer.w13_weight, |
| 104 | + layer.w2_weight) |
| 105 | + layer.custom_routing_function = Llama4MoE.custom_routing_function |
| 106 | + layer.intermediate_size_per_partition = n |
| 107 | + layer.ep_rank = 0 |
| 108 | + layer.local_num_experts = e |
| 109 | + |
| 110 | + return TestData( |
| 111 | + hidden_states=hidden_states, |
| 112 | + w13_quantized=w13_quantized, |
| 113 | + w2_quantized=w2_quantized, |
| 114 | + a1_scale=a1_scale, |
| 115 | + a2_scale=a2_scale, |
| 116 | + w13_weight_scale=w13_weight_scale, |
| 117 | + w2_weight_scale=w2_weight_scale, |
| 118 | + layer=layer, |
| 119 | + ) |
| 120 | + |
| 121 | + |
| 122 | +@pytest.mark.parametrize("m,n,k", MNK_FACTORS) |
| 123 | +@pytest.mark.parametrize("e", NUM_EXPERTS) |
| 124 | +@pytest.mark.parametrize("topk", TOP_KS) |
| 125 | +def test_flashinfer_per_tensor_moe_fp8_no_graph( |
| 126 | + m: int, |
| 127 | + n: int, |
| 128 | + k: int, |
| 129 | + e: int, |
| 130 | + topk: int, |
| 131 | + monkeypatch, |
| 132 | +): |
| 133 | + current_platform.seed_everything(7) |
| 134 | + monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", "8192") |
| 135 | + with set_current_vllm_config(vllm_config): |
| 136 | + td = TestData.make_moe_tensors_8bit(m, k, n, e, reorder=True) |
| 137 | + |
| 138 | + score = torch.randn((m, e), device="cuda", dtype=torch.bfloat16) |
| 139 | + topk_weights, topk_ids = FusedMoE.select_experts( |
| 140 | + hidden_states=td.hidden_states, |
| 141 | + router_logits=score, |
| 142 | + use_grouped_topk=False, |
| 143 | + top_k=topk, |
| 144 | + renormalize=False, |
| 145 | + custom_routing_function=Llama4MoE.custom_routing_function, |
| 146 | + scoring_func="softmax") |
| 147 | + |
| 148 | + output = fused_experts( |
| 149 | + td.hidden_states, |
| 150 | + td.w13_quantized, |
| 151 | + td.w2_quantized, |
| 152 | + topk_weights=topk_weights, |
| 153 | + topk_ids=topk_ids, |
| 154 | + inplace=False, |
| 155 | + activation="silu", |
| 156 | + use_fp8_w8a8=True, |
| 157 | + per_channel_quant=False, |
| 158 | + global_num_experts=e, |
| 159 | + expert_map=None, |
| 160 | + w1_scale=td.w13_weight_scale, |
| 161 | + w2_scale=td.w2_weight_scale, |
| 162 | + a1_scale=td.a1_scale, |
| 163 | + a2_scale=td.a2_scale, |
| 164 | + apply_router_weight_on_input=True, |
| 165 | + ) |
| 166 | + |
| 167 | + flashinfer_output = apply_flashinfer_per_tensor_scale_fp8( |
| 168 | + layer=td.layer, |
| 169 | + hidden_states=td.hidden_states, |
| 170 | + router_logits=score, |
| 171 | + routing_bias=None, |
| 172 | + global_num_experts=e, |
| 173 | + top_k=topk, |
| 174 | + num_expert_group=None, |
| 175 | + topk_group=None, |
| 176 | + apply_router_weight_on_input=True) |
| 177 | + |
| 178 | + torch.testing.assert_close(output, |
| 179 | + flashinfer_output, |
| 180 | + atol=5.5e-2, |
| 181 | + rtol=1e-2) |
| 182 | + |
| 183 | + |
| 184 | +@pytest.mark.skip( |
| 185 | + "Requires flashinfer version that contains https://github.com/flashinfer-ai/flashinfer/pull/1472" |
| 186 | +) |
| 187 | +@pytest.mark.parametrize("m,n,k", MNK_FACTORS) |
| 188 | +@pytest.mark.parametrize("e", NUM_EXPERTS) |
| 189 | +@pytest.mark.parametrize("topk", TOP_KS) |
| 190 | +def test_flashinfer_cutlass_moe_fp8_no_graph( |
| 191 | + m: int, |
| 192 | + n: int, |
| 193 | + k: int, |
| 194 | + e: int, |
| 195 | + topk: int, |
| 196 | + monkeypatch, |
| 197 | +): |
| 198 | + current_platform.seed_everything(7) |
| 199 | + monkeypatch.setenv("VLLM_FUSED_MOE_CHUNK_SIZE", "8192") |
| 200 | + with set_current_vllm_config(vllm_config): |
| 201 | + td = TestData.make_moe_tensors_8bit(m, k, n, e, reorder=False) |
| 202 | + |
| 203 | + score = torch.randn((m, e), device="cuda", dtype=torch.bfloat16) |
| 204 | + topk_weights, topk_ids = FusedMoE.select_experts( |
| 205 | + hidden_states=td.hidden_states, |
| 206 | + router_logits=score, |
| 207 | + use_grouped_topk=False, |
| 208 | + top_k=topk, |
| 209 | + renormalize=False, |
| 210 | + custom_routing_function=Llama4MoE.custom_routing_function, |
| 211 | + scoring_func="softmax") |
| 212 | + |
| 213 | + output = fused_experts( |
| 214 | + td.hidden_states, |
| 215 | + td.w13_quantized, |
| 216 | + td.w2_quantized, |
| 217 | + topk_weights=topk_weights, |
| 218 | + topk_ids=topk_ids, |
| 219 | + inplace=False, |
| 220 | + activation="silu", |
| 221 | + use_fp8_w8a8=True, |
| 222 | + per_channel_quant=False, |
| 223 | + global_num_experts=e, |
| 224 | + expert_map=None, |
| 225 | + w1_scale=td.w13_weight_scale, |
| 226 | + w2_scale=td.w2_weight_scale, |
| 227 | + a1_scale=td.a1_scale, |
| 228 | + a2_scale=td.a2_scale, |
| 229 | + apply_router_weight_on_input=True, |
| 230 | + ) |
| 231 | + |
| 232 | + td.layer.dp_size = 1 |
| 233 | + |
| 234 | + flashinfer_cutlass_output = flashinfer_cutlass_moe_fp8( |
| 235 | + td.hidden_states, |
| 236 | + td.layer, |
| 237 | + topk_weights, |
| 238 | + topk_ids, |
| 239 | + activation="silu", |
| 240 | + global_num_experts=e, |
| 241 | + expert_map=None, |
| 242 | + apply_router_weight_on_input=True, |
| 243 | + ) |
| 244 | + |
| 245 | + torch.testing.assert_close(output, |
| 246 | + flashinfer_cutlass_output, |
| 247 | + atol=5.5e-2, |
| 248 | + rtol=1e-2) |
0 commit comments