Skip to content

feat(moe): NPU平台 EP experts forward计算改用 NPU grouped matmul#252

Open
werwrewe wants to merge 4 commits into
modelscope:mainfrom
werwrewe:ep-npu-gmm
Open

feat(moe): NPU平台 EP experts forward计算改用 NPU grouped matmul#252
werwrewe wants to merge 4 commits 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。

werwrewe added 2 commits July 22, 2026 09:38
两个预存 bug 导致 EP 训练在 torch_npu 2.10 + CANN 9.0.0 上无法运行:

1. preprocess():HCCL all_gather 在独立 stream 上执行,紧随其后的
   host 读(.tolist())与通信完成无序;且 non_blocking=True 的 D2H
   拷贝未落地就被 host 消费,产生垃圾 split size(实测报错
   "Trying to create tensor with negative dimension")。
   修复:集合通信后增加 torch.npu.synchronize()(仅 NPU),
   两处 D2H 拷贝改为同步。

2. permute():index_select 的反向走 aclnnIndexAdd,在本环境对任意
   dtype/shape 均失败(error 161001)。改为数学等价的高级索引
   x[idx](反向为 index_put,可正常累加重复下标的梯度)。
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 曲线与原实现一致,2*910B qwen3.5-35B-A3B 每 12 step 约 200s -> 60s。
@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 added 2 commits July 23, 2026 15:35
替换写死的 torch.npu.synchronize(),GPU/NPU 自动分发。
新增 kernel/ops/:EpExpertsGmm 基类 + 注册表 + 分发器,NPU 实现迁移为
NpuEpExpertsGmm;expert_parallel.py 只调接口,GMM 命中日志改为只打印一次。
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