Skip to content

feat(moe): EP experts forward收纳到kernel底下实现多impls管理,并且添加 NPU grouped matmul的forward分支。#252

Open
werwrewe wants to merge 1 commit into
modelscope:mainfrom
werwrewe:ep-npu-gmm
Open

feat(moe): EP experts forward收纳到kernel底下实现多impls管理,并且添加 NPU grouped matmul的forward分支。#252
werwrewe wants to merge 1 commit into
modelscope:mainfrom
werwrewe:ep-npu-gmm

Conversation

@werwrewe

@werwrewe werwrewe commented Jul 22, 2026

Copy link
Copy Markdown

kernelize 的 NPU patch 无法生效,导致NPU forward被ep forward覆盖。考虑在ep forward中单独给NPU提供forward。ep forward原实现逐 expert Python 循环,在 EP 闭包内部复用现有 NPU 组件(GmmFunction + npu_swiglu + _get_cached_expert_weights)替换循环实现并行化。每次前向做资格检查,不满足条件自动回退原循环并打印一次 WARNING 说明原因。

端到端验证: loss 曲线与原实现一致,使用ep_fsdp2_lora_qwen3_5_moe.sh 设备2*910B 模型qwen3.5-35B-A3B 每 12 step 约 200s -> 60s。

二编:
改动代码结构,原有EP experts forward也收纳到kernel底下ops里面并且实现多impls管理。测试两种ep forward分支(NPU和逐expert循环)都能跑通,使用ep_fsdp2_lora_qwen3_5_moe.sh 设备2*910B batch8 模型qwen3.5-35B-A3B 16steps NPU时间185s,逐expert循环393s。

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Caution

The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased.

# observed in practice as garbage split sizes. Force a device sync before
# reading gathered results back on the host.
if num_local_tokens_per_expert.device.type == 'npu':
torch.npu.synchronize()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最好使用平台无关的封装:
from twinkle import torch_util

torch_util.synchronize()

# all-to-all, causing EP collective order divergence.
return permuted_tokens

if _ep_experts_can_use_npu_gmm(self):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

更好的做法是把gmm定义成一个接口,有不同的impl,然后在expert_parallel.py中直接调用接口,隐藏impl细节。

@werwrewe werwrewe changed the title feat(moe): NPU平台 EP experts forward计算改用 NPU grouped matmul feat(moe): EP experts forward收纳到kernel底下实现多impls管理,并且添加 NPU grouped matmul的forward分支。 Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants