[2026春季][T2-1-3] goog00#500
Open
goog00 wants to merge 1 commit into
Open
Conversation
- MoE experts 前向:stacked weights + grouped_gemm + swiglu + TP allreduce (相对朴素 per-expert 循环,bs=1 输出吞吐 +42%) - P0:可选 host group counts 跳过 grouped_gemm 内部 D2H 同步(graph guard) - 正确性脚本:prefill 首步/前缀 token 对齐 HF 两阶段校验 (MetaX TP=2 + NVIDIA TP=4 均 4/4 PASS)+ CORRECTNESS_README - bench/qwen3_moe:无依赖并发吞吐基准(serve + load client + summarize) Signed-off-by: sunteng <steng2009@163.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
T2-1-3 Qwen3-MoE 推理性能优化 — 赛题报告
实现的功能概述
在 InfiniLM/InfiniCore 框架上完成 Qwen3-30B-A3B-Thinking-2507(30B 总参 / 3B 激活,

128 专家、top-8、48 层全 MoE、BF16)的可运行 + 性能优化适配,并给出正确性与性能的实测证据。
使用的技术方案与参考资料
2.1 架构确认(对照 HF)
对照
config.json与 transformersmodeling_qwen3_moe确认:model_type=qwen3_moe,无共享专家、无 aux-free 偏置、per-expert split 权重、48 层全 MoE,与 InfiniLM 现有
qwen3_moe骨架完全兼容,无需新建模型目录、无需扩展路由算子。风险集中在性能优化。2.2 核心方案:
grouped_gemm专家批化问题:原朴素实现对 ntoken × top-8 个 (token, expert) 组合逐个调用单专家 MLP,
每次都是 1×hidden 的小 GEMM,完全失去 batch 性,kernel launch 爆炸。
方案(InfiniCore 新增
grouped_gemm算子 + InfiniLM 重写Qwen3MoeExperts::forward):CPU 端对路由 indices 分桶,得到每专家 token 数
counts与置换perm;embedding按permgather → 三次grouped_gemm(gate/up/down,各一次覆盖全部专家)swiglu;路由权重广播缩放 →
index_add_scatter 回原 token;TP
allreduce。grouped_gemm接收group_sizes,一次调用内对各专家 slab 依次发 GEMM(行主序C=A@Bᵀ,列主序转置实现),CPU/NVIDIA/MetaX 三后端 + CPU 参考实现 + Python 单测。2.3
grouped_gemm的 host-counts 直通接口grouped_gemm额外提供可选的 host 端group_sizes直通参数(默认nullptr,向后兼容),允许调用方在已知各专家 token 数时跳过算子内部的 device→host 同步拷贝。该接口为拷贝-同步
受限平台预留优化位,不影响默认路径行为。性能主线是 §2.2 的 grouped_gemm 批化(见 §3 实测)。
2.4 参考资料
HF 模型与配置:
Qwen/Qwen3-30B-A3B-Thinking-2507(config.json / generation_config / tokenizer)transformers
modeling_qwen3_moe.py(权重命名、路由、chat template 参照)InfiniLM/InfiniCore 框架现有
qwen3_moe骨架、op::topksoftmax、moe_mlp、TP 通信测试脚本:运行方式与运行效果
通用方法(两平台一致):
HF 仅作参照,不进入推理路径。
判读:
prompt_tok ok(chat template/分词一致)+first_tok ==(prefill 首步 argmax 与 HF 一致)prefix 16/16(前 16 个贪心 token 逐一命中)。bench/qwen3_moe/,server + 零依赖并发客户端。out_tok/s= 聚合输出吞吐,total_tok/s= 含 prefill 的聚合总吞吐(单流速率 = 聚合 / 并发数)。3.1 NVIDIA RTX 4090D(TP=4)
3.1.1 正确性:prefill 对齐 HF —— 4/4 PASS
图 3.1.1:RTX 4090D TP=4
check_prefill_logits.py—— 4 条 prompt 首 token + 前 16 token 全命中 HF,OVERALL PASS。3.1.2 并发吞吐
4090D 单卡 24GB,30B 权重(TP=4 每卡 ~15GB)后 KV 空间有限,取代表性子集:
图 3.1.2:RTX 4090D TP=4
load_client.py—— 单流 decode ≈ 34 tok/s;bs=8 聚合 ~178 tok/s;长输入 in=2048 总吞吐达 1605 tok/s。3.1.3 端到端连贯输出
图 3.1.3:RTX 4090D TP=4
test_infer.py --backend cpp—— "How are you" → 正常<think>…</think>思考链 + 连贯英文回答,total_time≈ 7.6 s。3.2 沐曦 MetaX C500(TP=2)
3.2.1 正确性:prefill 对齐 HF —— 4/4 PASS
图 3.2.1:沐曦 C500 TP=2
check_prefill_logits.py—— 4 条 prompt 首 token + 前 16 token 全命中 HF,OVERALL PASS。3.2.2 性能:base vs this(grouped_gemm 的收益)
base=朴素逐 token MoE,this=grouped_gemm,同参数可比:

图 3.2.2:
summarize.py base this—— bs=1 输出吞吐 base 10.98 → this 15.56(+41.7%),bs=8 +4.8%。bs=1:grouped_gemm 输出吞吐 +42%——朴素版每 token 跑 8 个独立小专家 MLP(8× launch/token),
grouped_gemm 批成 3 次 grouped GEMM,launch 大减。
小规模(bs=1)不但不回退,还大幅提升(超额满足赛题「小规模不回退」要求)。
3.2.3 并发 × 长输入 × 长输出 完整矩阵
concurrency ∈ {1,8,32} × input ∈ {32,256,2048} × output ∈ {256,1024},共 18 组,全部实测通过(C500 单卡 64GB,可跑完整矩阵):
图 3.2.3:沐曦 C500 TP=2
load_client.py完整矩阵(18 组)—— 输出吞吐随并发放大(in=256/out=256 段 bs 1→8→32 = 24.5 → 86.5 → 143.2 tok/s,峰值 270 tok/s);长输入 in=2048、bs=32 总吞吐达 1236.7 tok/s。输出吞吐随并发放大:同配置 bs 1→8→32 聚合 out_tok/s 逐级抬升(如 in=256/out=256 段
24.5 → 86.5 → 143.2,≈3.5×/5.8×),峰值 270 tok/s,调度器组批有效、grouped_gemm 批量 decode 持续获益。
总吞吐(含 prefill)在长输入下极高:in=2048、bs=32 达 1236.7 tok/s → prefill 大矩阵正是
grouped_gemm 批处理的强项。
3.2.4 端到端连贯输出
图 3.2.4:沐曦 C500 TP=2 —— "你好,请介绍一下自己" → 正常
<think>…</think>思考链 + 连贯中文自我介绍,total_time≈ 12.2 s。3.3 双平台一致性小结
→ 同一套 InfiniLM
T2-1-3+ InfiniCoregrouped_gemm代码在两平台正确性一致、性能趋势一致,grouped_gemm** 的 CPU/NVIDIA/MetaX 三后端均验证有效。**附录 A:关键代码位置
Honor Code.pdf