Prhub

#47122 [XPU] [MoE] add quant input when prepare for fusedmoe

原始 PR 作者 zufangzhu 合并时间 2026-07-20 15:47 文件变更 3 提交数 14 评论 1 代码增减 +37 / -4

执行摘要

为 XPU MoE 层添加激活量化输入支持

引用PR body:基于 https://github.com/vllm-project/vllm/pull/47256 和 https://github.com/vllm-project/vllm-xpu-kernels/pull/450,旨在支持在fused MoE kernel中传递量化后的激活输入,从而提升XPU上MoE推理的精度和性能。

值得相关开发人员阅读,特别是关注Intel GPU后端的团队。该PR展示了如何在fused MoE层中增加量化激活输入的完整流程,包括架构校验、形状推导、量化函数分发等环节。建议合入后补充单元测试和集成测试。

讨论亮点

该PR没有收到实质性讨论或设计争议。mergify[bot]自动提示合并冲突,claude[bot]自动评论(被禁用)。jikunshang审核后直接批准。无未解决疑虑。

实现拆解

  1. 架构检测与属性调整:在 XPUExperts.__init__ 中增加Intel Xe2/Xe3架构检测(torch.ops._xpu_C.is_xe2_arch() 或 is_xe3_arch()),仅该架构支持量化激活;同时将 expects_unquantized_inputs 从静态True改为动态判断,使后续逻辑能区分是否需要预量化。

  2. 新增 workspace_shapes 方法:在 xpu_moe.py 中实现该方法,根据 expects_unquantized_inputs 计算输出logical hidden size:当激活已量化(mxfp4 packed)时 logical_K = 2 * K,否则 logical_K = K。框架调用该方法确定intermediate buffer形状。

  3. 传递量化缩放参数:在 XPUExperts.apply 中调用 self.fused_moe_impl.apply 时新增 a1q_scale 参数,允许kernel接收激活量化缩放因子。

  4. 量化函数XPU分支:在 utils.py 的 _mxfp4_quantize 和 _mxfp8_e4m3_quantize 中增加 if current_platform.is_xpu() 分支,直接调用 xpu_mxfp4_quantize / xpu_mxfp8_quantize,绕过通用路径的断言和模拟逻辑。

  5. 放行量化模式:在 moe_kernel_quantize_input 中,对 mxfp4 和 mxfp8 量化类型放宽条件,允许XPU直接使用量化路径而不触发 NotImplementedError。

  6. 拼写修复:在 topk_weight_and_reduce.py 中修复字符串错误(used_expert_output → fused_expert_output),提升调试信息准确性。

文件 模块 状态 重要度
vllm/model_executor/layers/fused_moe/experts/xpu_moe.py MoE 专家核 modified 7.44
vllm/model_executor/layers/fused_moe/utils.py 量化工具 modified 5.95
vllm/model_executor/layers/fused_moe/topk_weight_and_reduce.py 权重归约 modified 4.08

关键符号

XPUExperts.__init__ XPUExperts.workspace_shapes XPUExperts.apply _mxfp4_quantize _mxfp8_e4m3_quantize moe_kernel_quantize_input TopKWeightAndReduceNoOP.apply

关键源码片段

vllm/model_executor/layers/fused_moe/experts/xpu_moe.py data-contract

核心变更文件:添加 Xe2/Xe3 架构检测、new workspace_shapes 方法、修改 apply 传递 a1q_scale、扩展量化方案支持。

class XPUExperts(mk.FusedMoEExpertsModular):
    def __init__(self, moe_config, quant_config, max_num_tokens=None, num_dispatchers=None):
        super().__init__(moe_config, quant_config, max_num_tokens, num_dispatchers)
        self.gemm1_clamp_limit = quant_config.gemm1_clamp_limit
        self.fused_moe_impl: XpuFusedMoe | None = None
        # 仅 Xe2/Xe3 架构支持量化激活输入
        is_xe2_or_xe3 = torch.ops._xpu_C.is_xe2_arch() or torch.ops._xpu_C.is_xe3_arch()
        if not is_xe2_or_xe3:
            raise NotImplementedError("XPUExperts is only supported on Intel Xe2/Xe3 GPUs")
        self._expects_unquantized_inputs = is_xe2_or_xe3
​
    @property
    def expects_unquantized_inputs(self) -> bool:
        # 与架构相关,决定后续是否需要预量化
        return self._expects_unquantized_inputs
​
    def workspace_shapes(self, M, N, K, topk, global_num_experts, local_num_experts,
                         expert_tokens_meta, activation):
        # 当激活已量化(mxfp4 packed)时,K 是打包后的 hidden_size,
        # 输出为逻辑 hidden_size (2*K);否则 K 就是逻辑 hidden_size
        logical_K = K if self.expects_unquantized_inputs else 2 * K
        return (0,), (0,), (M, logical_K)
​
    def apply(self, ...):
        # ... 初始化 fused_moe_impl 部分省略 ...
        self.fused_moe_impl.apply(
            output=output,
            hidden_states=hidden_states,
            topk_weights=topk_weights,
            topk_ids=topk_ids,
            a1q_scale=a1q_scale, # 新增:传递激活量化缩放因子
        )
vllm/model_executor/layers/fused_moe/utils.py data-contract

在量化函数中增加 XPU 分支,放行量化模式,是量化输入通路的重要组成部分。

def _mxfp4_quantize(A, A_scale, per_act_token_quant, block_shape=None):
    if current_platform.is_xpu():
        # XPU 使用自定义量化函数,避免通用路径的模拟逻辑
        return xpu_mxfp4_quantize(A)
    # 通用路径:mxfp4 模拟量化
    assert block_shape is None
    A = quant_dequant_mxfp4(A)
    return A, None
​
​
def _mxfp8_e4m3_quantize(A, A_scale, per_act_token_quant, block_shape=None,
                           is_sf_swizzled_layout=False, mx_alignment=0):
    if current_platform.is_xpu():
        # XPU 使用自定义量化函数
        return xpu_mxfp8_quantize(A)
    # 通用路径
    assert A_scale is None
    assert not per_act_token_quant
    assert block_shape is None or block_shape == [1, 32]
    return mxfp8_e4m3_quantize(A, is_sf_swizzled_layout, mx_alignment)

评论区精华

没有提炼出高价值讨论线程

当前评论区没有形成足够清晰的争议点或结论,后续有更多讨论时会体现在这里。

风险与影响

  • 回归风险低:所有改动仅在 current_platform.is_xpu() 条件下生效,不影响NVIDIA/AMD等平台。
  • 功能完整性风险:新增的 workspace_shapes 方法和 a1q_scale 参数依赖下游XPU kernel正确实现,若 vllm-xpu-kernels 接口变化可能导致运行时错误。
  • 测试覆盖不足:本次提交未包含任何测试文件,对新量化组合的验证依赖外部依赖或后续补充测试。
  • 兼容性风险:expects_unquantized_inputs 从True改为动态值,若其他模块假设它恒为True可能触发意外行为(但仅限XPU代码路径,影响面小)。
  • 对用户:Intel GPU用户现在可以使用更多量化模式(如mxfp4动态量化)运行MoE模型,可能获得性能或精度收益。
  • 对系统:改动了MoE量化流程中的核心函数,但通过平台判断隔离,不影响其他硬件后端。
  • 对团队:明确了XPU MoE的量化扩展方向,后续可基于此模式增加更多量化方案。
缺少测试覆盖 仅 XPU 平台 核心 MoE 路径变更

关联 Issue

未识别关联 Issue

当前没有检测到明确关联的 Issue 链接,后续同步到相关引用后会出现在这里。

完整报告

参与讨论