[CPU] Migrate unquantized MoE to the modular-kernel experts structure (#50133)
Signed-off-by: jiang1.li <jiang1.li@intel.com> Signed-off-by: Fadi Arafeh <fadi.arafeh@arm.com> Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com> Co-authored-by: Fadi Arafeh <fadi.arafeh@arm.com>
This commit is contained in:
co-authored by
Claude Opus 5
Fadi Arafeh
parent
b3f97dae24
commit
0a6446005d
@@ -1482,85 +1482,6 @@ def test_moe_sum_pad_aware(topk: int, dtype: torch.dtype, topk_ids_dtype: torch.
|
||||
opcheck(torch.ops._moe_C.moe_sum, (input, actual, topk_ids, expert_map))
|
||||
|
||||
|
||||
@pytest.mark.usefixtures("default_vllm_config")
|
||||
@pytest.mark.parametrize("m", [1, 33])
|
||||
@pytest.mark.parametrize("n,k", [(128, 128)])
|
||||
@pytest.mark.parametrize("e", [8])
|
||||
@pytest.mark.parametrize("topk", [2])
|
||||
@pytest.mark.parametrize("dtype", [torch.float32, torch.bfloat16])
|
||||
@pytest.mark.parametrize("with_bias", [False, True])
|
||||
@pytest.mark.parametrize("activation", [MoEActivation.SILU])
|
||||
@pytest.mark.skipif(not current_platform.is_cpu(), reason="CPU only test")
|
||||
def test_cpu_fused_moe_basic(
|
||||
m: int,
|
||||
n: int,
|
||||
k: int,
|
||||
e: int,
|
||||
topk: int,
|
||||
dtype: torch.dtype,
|
||||
with_bias: bool,
|
||||
activation: MoEActivation,
|
||||
):
|
||||
from vllm.model_executor.layers.fused_moe.cpu_fused_moe import CPUFusedMOE
|
||||
|
||||
device = "cpu"
|
||||
set_random_seed(7)
|
||||
|
||||
a = torch.randn((m, k), device=device, dtype=dtype) / 10
|
||||
w13 = torch.randn((e, 2 * n, k), device=device, dtype=dtype) / 10
|
||||
w2 = torch.randn((e, k, n), device=device, dtype=dtype) / 10
|
||||
router_logits = torch.randn((m, e), device=device, dtype=dtype)
|
||||
|
||||
b1 = b2 = None
|
||||
if with_bias:
|
||||
b1 = torch.randn((e, 2 * n), device=device, dtype=dtype) / 10
|
||||
b2 = torch.randn((e, k), device=device, dtype=dtype) / 10
|
||||
|
||||
ref = (
|
||||
torch_moe(a, w13, w2, router_logits, topk, b1, b2)
|
||||
if with_bias
|
||||
else torch_moe(a, w13, w2, router_logits, topk)
|
||||
)
|
||||
|
||||
class _Dummy(torch.nn.Module):
|
||||
def __init__(self, w13, w2, b1=None, b2=None):
|
||||
super().__init__()
|
||||
self.w13_weight = torch.nn.Parameter(w13, requires_grad=False)
|
||||
self.w2_weight = torch.nn.Parameter(w2, requires_grad=False)
|
||||
if b1 is not None:
|
||||
self.w13_bias = torch.nn.Parameter(b1, requires_grad=False)
|
||||
if b2 is not None:
|
||||
self.w2_bias = torch.nn.Parameter(b2, requires_grad=False)
|
||||
|
||||
layer = _Dummy(w13, w2, b1, b2).to(dtype)
|
||||
fused = CPUFusedMOE(layer)
|
||||
out = fused(
|
||||
layer=layer,
|
||||
x=a,
|
||||
use_grouped_topk=False,
|
||||
top_k=topk,
|
||||
router_logits=router_logits,
|
||||
renormalize=False,
|
||||
global_num_experts=e,
|
||||
expert_map=None,
|
||||
custom_routing_function=None,
|
||||
scoring_func="softmax",
|
||||
routed_scaling_factor=1.0,
|
||||
e_score_correction_bias=None,
|
||||
apply_router_weight_on_input=False,
|
||||
activation=activation,
|
||||
)
|
||||
|
||||
# Tolerances: fp32 tight; bf16 looser (esp. with bias)
|
||||
if dtype == torch.float32:
|
||||
atol = 1e-3
|
||||
elif with_bias:
|
||||
atol = 8e-2
|
||||
else:
|
||||
atol = 5e-2
|
||||
torch.testing.assert_close(out, ref, atol=atol, rtol=0)
|
||||
|
||||
|
||||
def _batched_fused_marlin_moe_cases() -> list[Any]:
|
||||
cases = [
|
||||
pytest.param(
|
||||
|
||||
Reference in New Issue
Block a user