Skip to content

Commit aded916

Browse files
zufangzhumayuyuacemergify[bot]jikunshang
authored andcommitted
[XPU] [MoE] add quant input when prepare for fusedmoe (vllm-project#47122)
Signed-off-by: mayuyuace <qiming1.zhang@intel.com> Signed-off-by: Zhu, Zufang <zufang.zhu@intel.com> Signed-off-by: zofia <110436990+zufangzhu@users.noreply.github.com> Co-authored-by: mayuyuace <qiming1.zhang@intel.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Kunshang Ji <kunshang.ji@intel.com>
1 parent de7f667 commit aded916

3 files changed

Lines changed: 37 additions & 4 deletions

File tree

vllm/model_executor/layers/fused_moe/experts/xpu_moe.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
kFp8StaticTensorSym,
2121
kInt4Static,
2222
kInt4Static32,
23+
kMxfp4Dynamic,
2324
kMxfp4Static,
2425
kMxfp8Dynamic,
2526
kMxfp8Static,
@@ -64,10 +65,16 @@ def __init__(
6465
)
6566
self.gemm1_clamp_limit = quant_config.gemm1_clamp_limit
6667
self.fused_moe_impl: XpuFusedMoe | None = None
68+
is_xe2_or_xe3 = torch.ops._xpu_C.is_xe2_arch() or torch.ops._xpu_C.is_xe3_arch()
69+
if not is_xe2_or_xe3:
70+
raise NotImplementedError(
71+
"XPUExperts is only supported on Intel Xe2/Xe3 GPUs"
72+
)
73+
self._expects_unquantized_inputs = is_xe2_or_xe3
6774

6875
@property
6976
def expects_unquantized_inputs(self) -> bool:
70-
return True
77+
return self._expects_unquantized_inputs
7178

7279
@staticmethod
7380
def activation_format() -> mk.FusedMoEActivationFormat:
@@ -172,6 +179,7 @@ def apply(
172179
hidden_states=hidden_states,
173180
topk_weights=topk_weights,
174181
topk_ids=topk_ids,
182+
a1q_scale=a1q_scale,
175183
)
176184

177185

@@ -309,12 +317,31 @@ def __init__(
309317
num_dispatchers,
310318
)
311319

320+
def workspace_shapes(
321+
self,
322+
M: int,
323+
N: int,
324+
K: int,
325+
topk: int,
326+
global_num_experts: int,
327+
local_num_experts: int,
328+
expert_tokens_meta: mk.ExpertTokensMetadata | None,
329+
activation: MoEActivation,
330+
) -> tuple[tuple[int, ...], tuple[int, ...], tuple[int, ...]]:
331+
# K = a1q.size(-1). When activations are pre-quantized packed mxfp4,
332+
# K is the packed hidden_size (= logical / 2); the kernel output is at
333+
# logical hidden_size (2 * K). When unquantized (bf16), K is already
334+
# the logical size.
335+
logical_K = K if self.expects_unquantized_inputs else 2 * K
336+
return (0,), (0,), (M, logical_K)
337+
312338
@staticmethod
313339
def _supports_quant_scheme(
314340
weight_key: QuantKey | None,
315341
activation_key: QuantKey | None,
316342
) -> bool:
317343
SUPPORTED_W_A = [
318344
(kMxfp4Static, None),
345+
(kMxfp4Static, kMxfp4Dynamic),
319346
]
320347
return (weight_key, activation_key) in SUPPORTED_W_A

vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def apply(
7171
assert output.size() == fused_expert_output.size(), (
7272
"output shape is expected to match the fused_expert_output shape. "
7373
f"But got output={output.size()}, "
74-
f"used_expert_output={fused_expert_output.size()}"
74+
f"fused_expert_output={fused_expert_output.size()}"
7575
)
7676
output.copy_(fused_expert_output, non_blocking=True)
7777
return output

vllm/model_executor/layers/fused_moe/utils.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717
)
1818
from vllm.model_executor.layers.quantization.utils.mxfp4_utils import (
1919
quant_dequant_mxfp4,
20+
xpu_mxfp4_quantize,
2021
)
2122
from vllm.model_executor.layers.quantization.utils.mxfp6_utils import (
2223
quant_dequant_mxfp6,
2324
)
2425
from vllm.model_executor.layers.quantization.utils.mxfp8_utils import (
2526
mxfp8_e4m3_quantize,
27+
xpu_mxfp8_quantize,
2628
)
2729
from vllm.model_executor.layers.quantization.utils.nvfp4_emulation_utils import (
2830
ref_nvfp4_quant_dequant,
@@ -195,6 +197,8 @@ def _mxfp4_quantize(
195197
per_act_token_quant: bool,
196198
block_shape: list[int] | None = None,
197199
) -> tuple[torch.Tensor, None]:
200+
if current_platform.is_xpu():
201+
return xpu_mxfp4_quantize(A)
198202
assert block_shape is None
199203
# TODO: native mxfp4 is currently not integrated in vllm,
200204
# so simulating even on devices supporting this data type natively.
@@ -223,6 +227,8 @@ def _mxfp8_e4m3_quantize(
223227
is_sf_swizzled_layout: bool = False,
224228
mx_alignment: int = 0,
225229
) -> tuple[torch.Tensor, torch.Tensor]:
230+
if current_platform.is_xpu():
231+
return xpu_mxfp8_quantize(A)
226232
assert A_scale is None
227233
assert not per_act_token_quant
228234
assert block_shape is None or block_shape == [1, 32]
@@ -309,7 +315,7 @@ def moe_kernel_quantize_input(
309315
A = ref_nvfp4_quant_dequant(A, A_scale, block_size=16)
310316
return A, None
311317
elif quant_dtype == "mxfp4":
312-
if not quantization_emulation:
318+
if not current_platform.is_xpu() and not quantization_emulation:
313319
raise NotImplementedError(
314320
"moe_kernel_quantize_input should not be used for native"
315321
" quant_dtype='mxfp4' MOE. Please open an issue."
@@ -318,7 +324,7 @@ def moe_kernel_quantize_input(
318324
elif quant_dtype == "mxfp8":
319325
# TODO: `quant_dtype == "mxfp8"` is ambiguous,
320326
# should be fp8_e4m3. OCP MX also defines `fp8_e5m2`.
321-
if quantization_emulation:
327+
if not current_platform.is_xpu() and quantization_emulation:
322328
raise NotImplementedError(
323329
"moe_kernel_quantize_input does not support quant_dtype='mxfp8' MOE "
324330
"quantization emulation. Please open an issue."

0 commit comments

Comments
 (0)