用户可利用该项目在华为昇腾NPU上实现专家并行通信功能,通过动态冗余专家和在线负载规划实现跨rank完美负载均衡,核心功能已完成昇腾平台功能与数值正确性验证,适配HCCL通信并提供与原库相同API。【此简介由AI生成】
Access rate limit exceeded. Please log in to continue.
MoonEP Ascend Adaptation
MoonEP 是一个专家并行(Expert Parallelism)通信库,通过动态冗余专家 + 在线负载规划实现跨 rank 的完美负载均衡:无论路由偏斜程度,每个 rank 恒定接收 S × K 个 token。
本项目将 MoonEP 的核心算法适配到华为昇腾 NPU(Ascend910),使用 HCCL 替代 CUDA VMM + NVLink,在 Atlas 560T 服务器上完成功能验证与数值正确性验证。
适配范围:Phase 1 功能验证(对应《MoonEP 技术分析与昇腾适配评估》第 13.1 节)。高性能 Phase 2(planner 上 NPU、HCCL Symmetric Memory、zero-copy)为后续工作。
验证结果
功能验证
| 配置 | NPU 数 | S | K | E | H | routing | maxvio | 结果 |
|---|---|---|---|---|---|---|---|---|
| 小规模均衡 | 2 | 32 | 4 | 16 | 64 | balanced | 0.0 | ✅ ALL PASS |
| 中规模偏斜 | 4 | 64 | 4 | 16 | 64 | skewed | 0.8 | ✅ ALL PASS |
| 大规模偏斜 | 8 | 64 | 4 | 32 | 128 | skewed | 0.5 | ✅ ALL PASS |
每个配置验证:负载均衡(recv_per_rank == S×K)、dispatch 正确性、combine 正确性、与标准 MoE 对比。
数值正确性验证
使用已知输入值验证 dispatch→combine round-trip 和 grad_reduce 的数值正确性(2 NPU, S=16, K=4, E=8, H=32, B=2):
| 测试项 | 方法 | max_err | 结果 |
|---|---|---|---|
| Hidden round-trip(无 FFN) | hidden[s] = s*0.1 + rank*10,验证 output[s] ≈ hidden[s] * K |
0.0000 | ✅ 精确 |
| Route weights round-trip | 验证 output_rw[s,k] ≈ rw[s,k] |
0.000000 | ✅ 精确 |
| FFN zero-weights | 所有专家权重=0,验证输出=0 | 0.000000 | ✅ 精确 |
| FFN identity round-trip | gate_w=I, up_w=I, down_w=I/K,与 PyTorch SiLU 参考对比 |
0.3891 | ✅ BF16 精度内 |
| grad_reduce gate/up/down | 已知 slot grads,验证 home expert 累加 | 0.000000 | ✅ 精确 |
| grad_reduce buffer zeroed | 验证 reduce buffer 使用后归零 | 0.000000 | ✅ 精确 |
| combine backward(reuse plan) | dispatch(grad, plan=existing) vs dispatch(grad, plan=None) |
0.000000 | ✅ 精确 |
负载均衡效果
MoonEP 的核心价值——完美负载均衡——在昇腾上完全成立(4 NPU, S=128, K=8, E=32, H=256):
| maxvio | MoonEP recv/rank | 标准 MoE max recv | MoonEP 均衡比 |
|---|---|---|---|
| 0.0 | 1024 | 278 | 完美 |
| 0.2 | 1024 | 432 | 完美 |
| 0.5 | 1024 | 647 | 完美 |
| 0.8 | 1024 | 881 | 完美 |
| 1.0 | 1024 | 1024 | 完美 |
MoonEP 在所有路由偏斜水平下保持 recv_per_rank = S×K = 1024 恒定,而标准 MoE 的 hottest rank 接收量随 maxvio 线性增长。
适配方案
遵循《MoonEP 技术分析与昇腾适配评估》第 13.1 节"第一阶段:功能验证"的建议:
- 保留在线负载规划算法 —— 直接移植 upstream
tests/planning_reference.py(纯 PyTorch 参考实现) - 使用 HCCL AlltoAllV —— 替代 CUDA VMM + NVLink symmetric memory
- 保留中间 buffer —— 不追求 zero-copy(A2 不支持 HCCL Symmetric Memory)
- BF16 数据类型 —— 不处理 K3 的 MXFP4/MXFP8
- 合成路由测试 —— 验证算法正确性,不接入真实模型
后端映射
| MoonEP NVIDIA 实现 | 昇腾适配实现 | 适配状态 |
|---|---|---|
| CUDA VMM 对称地址 | 每 rank 独立 NPU tensor + HCCL collective | ✅ 功能实现 |
| CuTe DSL dispatch kernel | PyTorch permute + HCCL all_to_all_single | ✅ 功能实现 |
| CuTe DSL combine kernel | PyTorch gather + HCCL all_to_all_single + K-sum | ✅ 功能实现 |
| CUTLASS grouped GEMM | torch.bmm 逐专家计算 | ✅ 功能实现 |
| NVLink 权重预取 | HCCL broadcast from home rank | ✅ 功能实现 |
| NVLink 梯度归并 | HCCL all_gather + 本地累加 | ✅ 功能实现 |
| CuTe DSL 在线 planner | 纯 PyTorch planner (CPU-side) | ✅ 功能实现 |
| CUDA Stream/Event async | torch.npu.synchronize (同步) | ⚠️ Phase 2 |
| zero_copy 模式 | 不支持 (返回 clone) | ⚠️ Phase 2 |
| multicast (NVSwitch SHARP) | 不支持 | ⚠️ Phase 2/3 |
代码结构
moonep_ascend/
├── __init__.py # 包导出 (Buffer, MoonEPCommPlan)
├── api.py # Buffer 类 (dispatch/combine/prefetch/reduce_grad)
├── planning.py # 纯 PyTorch 在线 planner + MoonEPCommPlan
├── buffer.py # NPU tensor 分配 (替代 VMM)
├── dispatch.py # HCCL AlltoAllV dispatch
├── combine.py # HCCL AlltoAllV combine + K-sum
├── prefetch.py # HCCL broadcast 权重预取
├── grad_reduce.py # HCCL all_gather 梯度归并
└── expert_ffn.py # 逐专家 FFN (替代 grouped GEMM)
tests_ascend/
├── test_utils.py # 合成路由生成 + 验证工具
├── test_e2e_ascend.py # 端到端测试 (形状/NaN/负载均衡)
└── test_correctness_ascend.py # 数值正确性测试 (round-trip/grad_reduce/combine bwd)
benchmarks_ascend/
├── bench_comm_ascend.py # MoonEP vs 标准 MoE 对比
└── bench_timing.py # planner/comm 时间分解
总计约 2,004 行新增代码(moonep_ascend 1,092 + tests 575 + benchmarks 337),无修改 upstream 文件。
API 用法
moonep_ascend.Buffer 提供与 upstream moonep.Buffer 相同的方法签名:
from moonep_ascend import Buffer
buffer = Buffer(S=128, H=256, K=8, E=32, num_ep_ranks=4, token_padding=32, B=4)
# dispatch fwd
hidden_nvsh, route_weights_nvs, cu_seqlens, plan = buffer.dispatch(
hidden_sh, # [S, H] bf16
route_weights_sk, # [S, K] fp32
topk_experts_sk, # [S, K] int32
tokens_per_expert, # [E] int32
)
# prefetch weights
buffer.prefetch_weight(
plan=plan,
full_gate_weight=full_gate_weight, # [E+B, H, H'] bf16
full_up_weight=full_up_weight,
full_down_weight=full_down_weight,
)
# combine fwd
output_sh, gathered_route_weights_sk, _ = buffer.combine(
plan=plan, hidden_nvsh=hidden_nvsh, route_weights_nvs=route_weights_nvs,
)
# combine bwd (reuse saved plan, skips planning)
grad_expert_output_nvsh, _, _, _ = buffer.dispatch(grad_output_sh, plan=plan)
# reduce grad (training)
buffer.reduce_grad(
plan=plan,
full_gate_grad=full_gate_grad, full_up_grad=full_up_grad, full_down_grad=full_down_grad,
gate_reduce_buffer=gate_reduce_buffer, up_reduce_buffer=up_reduce_buffer,
down_reduce_buffer=down_reduce_buffer,
)
buffer.destroy()
Phase 1 限制:
zero_copy=True不支持(返回 clone 副本)async_finish=True退化为torch.npu.synchronize()(无独立 comm stream)- planner 在 CPU 上运行(~95ms),Phase 2 将重写为 Ascend C kernel
验证环境
- 服务器:Atlas 560T(华为昇腾训练服务器)
- NPU:Ascend910 × 16(8 芯片 × 2 die),每芯片 64GB HBM
- 互联:HCCS(同一主机内全互联,非 SuperPoD)
- 软件栈:CANN 9.1.0 / torch 2.10.0 / torch_npu 2.10.0.post2 / HCCL / Python 3.11.10
快速验证
在昇腾服务器上(需 Docker 容器内含 torch_npu + HCCL):
# 端到端测试
python -m tests_ascend.test_e2e_ascend --nproc 2 --port 29595 \
--S 32 --K 4 --E 16 --H 64 --token_padding 8 --B 2 --routing balanced
python -m tests_ascend.test_e2e_ascend --nproc 4 --port 29598 \
--S 64 --K 4 --E 16 --H 64 --token_padding 16 --B 4 \
--routing skewed --maxvio 0.8
# 数值正确性测试
python -m tests_ascend.test_correctness_ascend --nproc 2 --port 29635 \
--S 16 --K 4 --E 8 --H 32 --token_padding 8 --B 2
# 基准测试
python -m benchmarks_ascend.bench_comm_ascend --nproc 4 --port 29605 \
--S 128 --K 8 --E 32 --H 256 --token_padding 32 --B 4 \
--warmup 3 --iters 10 --maxvios 0.0,0.2,0.5,0.8,1.0 \
--output /tmp/bench_results.json
# 时间分解
python benchmarks_ascend/bench_timing.py 4 29615
注意:
torchrun在容器内因 IPv6 hostname 解析问题失败,使用multiprocessing.spawn+MASTER_ADDR=127.0.0.1替代。
适配过程中的技术问题
| 问题 | 原因 | 解决 |
|---|---|---|
index_fill_ 参数错误 |
昇腾 torch_npu 的 index_fill_ 要求 0 维标量 |
改用 index_copy_ |
| prefetch broadcast 死锁 | unused slot 跳过 broadcast 导致 collective 不匹配 | unused slot 执行 dummy broadcast |
| torchrun rendezvous 失败 | 容器内 hostname 无法解析为 IPv6 | 改用 multiprocessing.spawn + MASTER_ADDR=127.0.0.1 |
性能分析与后续路线
当前性能瓶颈(Phase 1)
| 组件 | 耗时 (ms) | 占比 |
|---|---|---|
| Planner (CPU-side) | 95.72 | 92.0% |
| Comm (AlltoAllV + scatter) | 0.88 | 0.8% |
| Full dispatch (planner + comm) | 103.98 | 100% |
| Combine | 0.65 | — |
Phase 1 的瓶颈是 CPU-side planner(95.72ms),通信本身极快(0.88ms)。upstream MoonEP 的 planner 是 GPU kernel(CuTe DSL),Phase 2 需重写为 Ascend C kernel。
Phase 2 优化路线
| 优化项 | 方法 | 预期收益 |
|---|---|---|
| Planner 上 NPU | Ascend C kernel 重写 planner | 95ms → <1ms |
| HCCL Symmetric Memory | ACL Runtime 预留 VA + HCCL 注册 | 实现 zero-copy |
| Kernel 侧 HCCL | Ascend C kernel 内提交 AlltoAll | 通信计算融合 |
| 精准权重预取 | HIXL 单边通信 或 HCCL send/recv | 减少广播开销 |
| 异步通信 | torch_npu Stream + Event | 通信计算重叠 |
| grouped GEMM | Ascend Cube Matmul / ATB | 替代逐专家 bmm |
Phase 3 路线
目标:K3 原生量化,避免 BF16 转换。
- MXFP4 权重格式解析
- MXFP8 activation dispatch
- 量化 grouped-GEMM
- 精度回归验证
相关文档
- MoonEP 昇腾适配总结 — 完整的适配过程、测试结果、技术问题、性能分析文档
- MoonEP 原始仓库 — upstream NVIDIA 实现
致谢
本项目基于 MoonEP(Moonshot AI)的算法设计,将其适配到华为昇腾 NPU 平台。