Unverified Commit f0148240 authored by bnellnm's avatar bnellnm Committed by GitHub
Browse files

[MoE Refactor][Test] FusedMoE layer test (#24675)


Signed-off-by: default avatarBill Nell <bnell@redhat.com>
Co-authored-by: default avatarRobert Shaw <114415538+robertgshaw2-redhat@users.noreply.github.com>
parent bfdc0a3a
...@@ -180,3 +180,21 @@ steps: ...@@ -180,3 +180,21 @@ steps:
- pytest -v -s kernels/moe/test_flashinfer_moe.py - pytest -v -s kernels/moe/test_flashinfer_moe.py
- pytest -v -s kernels/moe/test_nvfp4_moe.py - pytest -v -s kernels/moe/test_nvfp4_moe.py
- pytest -v -s kernels/moe/test_ocp_mx_moe.py - pytest -v -s kernels/moe/test_ocp_mx_moe.py
- label: Kernels FusedMoE Layer Test (2 H100s)
timeout_in_minutes: 90
device: h100
num_devices: 2
optional: true
commands:
- pytest -v -s kernels/moe/test_moe_layer.py
- label: Kernels FusedMoE Layer Test (2 B200s)
timeout_in_minutes: 90
device: b200
num_devices: 2
optional: true
commands:
- pytest -v -s kernels/moe/test_moe_layer.py
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright contributors to the vLLM project
import pytest
def pytest_addoption(parser):
parser.addoption(
"--subtests", action="store", type=str, default=None, help="subtest ids"
)
@pytest.fixture
def subtests(request):
return request.config.getoption("--subtests")
...@@ -11,7 +11,11 @@ from torch.multiprocessing import spawn # pyright: ignore[reportPrivateImportUs ...@@ -11,7 +11,11 @@ from torch.multiprocessing import spawn # pyright: ignore[reportPrivateImportUs
from typing_extensions import ParamSpec from typing_extensions import ParamSpec
from vllm.config import VllmConfig, set_current_vllm_config from vllm.config import VllmConfig, set_current_vllm_config
from vllm.distributed import init_distributed_environment, initialize_model_parallel from vllm.distributed import (
cleanup_dist_env_and_memory,
init_distributed_environment,
initialize_model_parallel,
)
from vllm.utils.network_utils import get_open_port from vllm.utils.network_utils import get_open_port
## Parallel Processes Utils ## Parallel Processes Utils
...@@ -36,10 +40,17 @@ def _set_vllm_config( ...@@ -36,10 +40,17 @@ def _set_vllm_config(
temp_file = tempfile.mkstemp()[1] temp_file = tempfile.mkstemp()[1]
# When DP is enabled, processes are organized as:
# rank = dp_rank * tp_pp_world_size + tp_pp_rank
tp_pp_world_size = vllm_config.parallel_config.world_size
vllm_config.parallel_config.data_parallel_rank = rank // tp_pp_world_size
tp_pp_rank = rank % tp_pp_world_size
vllm_config.parallel_config.rank = tp_pp_rank
with set_current_vllm_config(vllm_config): with set_current_vllm_config(vllm_config):
init_distributed_environment( init_distributed_environment(
world_size=world_size, world_size=tp_pp_world_size,
rank=rank, rank=tp_pp_rank,
distributed_init_method=f"file://{temp_file}", distributed_init_method=f"file://{temp_file}",
local_rank=local_rank, local_rank=local_rank,
backend="nccl", backend="nccl",
...@@ -59,11 +70,11 @@ def _worker_parallel_launch( ...@@ -59,11 +70,11 @@ def _worker_parallel_launch(
world_local_size: int, world_local_size: int,
node_rank: int, node_rank: int,
init_method: str, init_method: str,
worker: Callable[Concatenate[ProcessGroupInfo, VllmConfig | None, Any, P], None], worker: Callable[..., None],
vllm_config: VllmConfig | None, vllm_config: VllmConfig | None,
env_dict: dict | None, env_dict: dict | None,
*args: P.args, worker_kwargs: dict[str, Any],
**kwargs: P.kwargs, *args: Any,
) -> None: ) -> None:
rank = node_rank * world_local_size + local_rank rank = node_rank * world_local_size + local_rank
torch.accelerator.set_device_index(local_rank) torch.accelerator.set_device_index(local_rank)
...@@ -98,13 +109,16 @@ def _worker_parallel_launch( ...@@ -98,13 +109,16 @@ def _worker_parallel_launch(
vllm_config, vllm_config,
cpu_group, cpu_group,
*args, *args,
**kwargs, **worker_kwargs,
) )
except Exception as ex: except Exception as ex:
print(ex) print(ex)
traceback.print_exc() traceback.print_exc()
raise raise
finally: finally:
if vllm_config is not None:
cleanup_dist_env_and_memory()
else:
torch.distributed.destroy_process_group() torch.distributed.destroy_process_group()
...@@ -116,7 +130,6 @@ def parallel_launch_with_config( ...@@ -116,7 +130,6 @@ def parallel_launch_with_config(
*args: P.args, *args: P.args,
**kwargs: P.kwargs, **kwargs: P.kwargs,
) -> None: ) -> None:
assert not kwargs
spawn( spawn(
_worker_parallel_launch, _worker_parallel_launch,
args=( args=(
...@@ -127,6 +140,7 @@ def parallel_launch_with_config( ...@@ -127,6 +140,7 @@ def parallel_launch_with_config(
worker, worker,
vllm_config, vllm_config,
env_dict, env_dict,
kwargs,
) )
+ args, + args,
nprocs=world_size, nprocs=world_size,
......
This diff is collapsed.
...@@ -248,7 +248,7 @@ def make_quantized_test_activations( ...@@ -248,7 +248,7 @@ def make_quantized_test_activations(
return a, a_q, a_scale return a, a_q, a_scale
def moe_quantize_weights( def moe_quantize_weights_2d(
w: torch.Tensor, w: torch.Tensor,
w_s: torch.Tensor | None, w_s: torch.Tensor | None,
quant_dtype: torch.dtype | str | None, quant_dtype: torch.dtype | str | None,
...@@ -293,31 +293,27 @@ def moe_quantize_weights( ...@@ -293,31 +293,27 @@ def moe_quantize_weights(
return w, w_s, w_gs return w, w_s, w_gs
def make_test_weight( def moe_quantize_weights(
e: int, w: torch.Tensor,
rows: int, w_s: torch.Tensor | None,
cols: int, quant_dtype: torch.dtype | str | None,
in_dtype: torch.dtype = torch.bfloat16, per_token_quant: bool,
quant_dtype: torch.dtype | str | None = None, block_shape: list[int] | None,
block_shape: list[int] | None = None, ) -> tuple[torch.Tensor, torch.Tensor | None, torch.Tensor | None]:
per_out_ch_quant: bool = False, assert w.dim() == 3
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None]: e, rows, cols = w.shape
w_16 = torch.randn((e, rows, cols), device="cuda", dtype=in_dtype) / 15
w_gs = None
if quant_dtype is not None:
w_l = [None] * e w_l = [None] * e
w_s_l = [None] * e w_s_l = [None] * e
w_gs_l = [None] * e w_gs_l = [None] * e
for idx in range(e): for idx in range(e):
w_l[idx], w_s_l[idx], w_gs_l[idx] = moe_quantize_weights( w_l[idx], w_s_l[idx], w_gs_l[idx] = moe_quantize_weights_2d(
w_16[idx], None, quant_dtype, per_out_ch_quant, block_shape w[idx], None, quant_dtype, per_token_quant, block_shape
) )
w = torch.stack(w_l) w = torch.stack(w_l)
w_s = torch.stack(w_s_l) w_s = torch.stack(w_s_l)
if e > 0 and w_gs_l[0] is not None: w_gs = torch.stack(w_gs_l) if e > 0 and w_gs_l[0] is not None else None
w_gs = torch.stack(w_gs_l)
if w_s.ndim == 2: if w_s.ndim == 2:
assert w_s.shape[-1] == 1 assert w_s.shape[-1] == 1
w_s = w_s.view(-1, 1, 1) w_s = w_s.view(-1, 1, 1)
...@@ -327,6 +323,25 @@ def make_test_weight( ...@@ -327,6 +323,25 @@ def make_test_weight(
n_tiles = (rows + block_n - 1) // block_n n_tiles = (rows + block_n - 1) // block_n
k_tiles = (cols + block_k - 1) // block_k k_tiles = (cols + block_k - 1) // block_k
assert w_s.shape == (e, n_tiles, k_tiles) assert w_s.shape == (e, n_tiles, k_tiles)
return w, w_s, w_gs
def make_test_weight(
e: int,
rows: int,
cols: int,
in_dtype: torch.dtype = torch.bfloat16,
quant_dtype: torch.dtype | str | None = None,
block_shape: list[int] | None = None,
per_out_ch_quant: bool = False,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None]:
w_16 = torch.randn((e, rows, cols), device="cuda", dtype=in_dtype) / 15
if quant_dtype is not None:
w, w_s, w_gs = moe_quantize_weights(
w_16, None, quant_dtype, per_out_ch_quant, block_shape
)
else: else:
w = w_16 w = w_16
w_s = None w_s = None
...@@ -454,7 +469,6 @@ def fused_moe( ...@@ -454,7 +469,6 @@ def fused_moe(
) )
# CustomOp?
class BaselineMM(torch.nn.Module): class BaselineMM(torch.nn.Module):
def __init__( def __init__(
self, self,
...@@ -462,13 +476,22 @@ class BaselineMM(torch.nn.Module): ...@@ -462,13 +476,22 @@ class BaselineMM(torch.nn.Module):
out_dtype: torch.dtype, out_dtype: torch.dtype,
): ):
super().__init__() super().__init__()
self.b = b.to(dtype=torch.float32) self.b = torch.nn.Parameter(b.to(dtype=torch.float32))
self.out_dtype = out_dtype self.out_dtype = out_dtype
def forward(self, a: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor | None]: def forward(self, a: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor | None]:
return torch.mm(a.to(dtype=torch.float32), self.b).to(self.out_dtype), None return torch.mm(a.to(dtype=torch.float32), self.b).to(self.out_dtype), None
class BaselineSiluAndMul(torch.nn.Module):
def __init__(self):
super().__init__()
def forward(self, x: torch.Tensor) -> torch.Tensor:
d = x.shape[-1] // 2
return torch.nn.functional.silu(x[..., :d]) * x[..., d:]
class TestMLP(torch.nn.Module): class TestMLP(torch.nn.Module):
def __init__( def __init__(
self, self,
...@@ -479,7 +502,7 @@ class TestMLP(torch.nn.Module): ...@@ -479,7 +502,7 @@ class TestMLP(torch.nn.Module):
super().__init__() super().__init__()
self.gate_up_proj = BaselineMM(w1, out_dtype) self.gate_up_proj = BaselineMM(w1, out_dtype)
self.down_proj = BaselineMM(w2, out_dtype) self.down_proj = BaselineMM(w2, out_dtype)
self.act_fn = SiluAndMul() self.act_fn = BaselineSiluAndMul()
def forward(self, x): def forward(self, x):
x, _ = self.gate_up_proj(x) x, _ = self.gate_up_proj(x)
...@@ -564,35 +587,24 @@ class RealMLP(torch.nn.Module): ...@@ -564,35 +587,24 @@ class RealMLP(torch.nn.Module):
return x return x
def make_shared_experts( def make_shared_experts_with_weights(
N: int, N: int,
K: int, K: int,
in_dtype: torch.dtype = torch.bfloat16, in_dtype: torch.dtype,
w1: torch.Tensor,
w2: torch.Tensor,
w1_s: torch.Tensor | None = None,
w2_s: torch.Tensor | None = None,
quant_dtype: torch.dtype | str | None = None, quant_dtype: torch.dtype | str | None = None,
) -> torch.nn.Module: ) -> torch.nn.Module:
from vllm.model_executor.layers.quantization.fp8 import Fp8Config
(_, w1, w1_s, _), (_, w2, w2_s, _) = make_test_weights(
1,
N,
K,
in_dtype=in_dtype,
quant_dtype=quant_dtype,
)
old_dtype = torch.get_default_dtype() old_dtype = torch.get_default_dtype()
try: try:
torch.set_default_dtype(in_dtype) torch.set_default_dtype(in_dtype)
if quant_dtype == torch.float8_e4m3fn: if quant_dtype == torch.float8_e4m3fn:
w1 = w1[0].transpose(0, 1) from vllm.model_executor.layers.quantization.fp8 import Fp8Config
w2 = w2[0].transpose(0, 1)
w1_s = w1_s[0].transpose(0, 1) if w1_s is not None else None
w2_s = w2_s[0].transpose(0, 1) if w2_s is not None else None
quant_config = Fp8Config(True) quant_config = Fp8Config(True)
else: else:
w1 = w1[0]
w2 = w2[0]
w1_s = None
w2_s = None
quant_config = None quant_config = None
return RealMLP(K, N, w1, w2, "silu", quant_config, w1_s=w1_s, w2_s=w2_s) return RealMLP(K, N, w1, w2, "silu", quant_config, w1_s=w1_s, w2_s=w2_s)
...@@ -614,3 +626,22 @@ def modular_triton_fused_moe( ...@@ -614,3 +626,22 @@ def modular_triton_fused_moe(
TritonExperts(moe_config, quant_config), TritonExperts(moe_config, quant_config),
inplace=False, inplace=False,
) )
def make_shared_experts(
N: int,
K: int,
in_dtype: torch.dtype = torch.bfloat16,
quant_dtype: torch.dtype | str | None = None,
) -> torch.nn.Module:
(_, w1, w1_s, _), (_, w2, w2_s, _) = make_test_weights(
1,
N,
K,
in_dtype=in_dtype,
quant_dtype=quant_dtype,
)
return make_shared_experts_with_weights(
N, K, in_dtype, w1, w2, w1_s=w1_s, w2_s=w2_s, quant_dtype=quant_dtype
)
...@@ -76,7 +76,6 @@ class TrtLlmBf16Experts(mk.FusedMoEExpertsMonolithic): ...@@ -76,7 +76,6 @@ class TrtLlmBf16Experts(mk.FusedMoEExpertsMonolithic):
activation_key: QuantKey | None, activation_key: QuantKey | None,
) -> bool: ) -> bool:
return routing_method in [ return routing_method in [
RoutingMethodType.Default,
RoutingMethodType.DeepSeekV3, RoutingMethodType.DeepSeekV3,
RoutingMethodType.Llama4, RoutingMethodType.Llama4,
RoutingMethodType.Renormalize, RoutingMethodType.Renormalize,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment