[Cherry-Pick][Op][Optimization]Kernel fusion: cast+sigmoid+bias+noauxtc(#7777)#7832
[Cherry-Pick][Op][Optimization]Kernel fusion: cast+sigmoid+bias+noauxtc(#7777)#7832ShaneGZhu wants to merge 2 commits into
Conversation
|
Thanks for your contribution! |
PaddlePaddle-bot
left a comment
There was a problem hiding this comment.
🤖 Paddle-CI-Agent | pr_review |
2026-05-15 16:55:19
📋 Review 摘要
PR 概述:新增 grouped_topk 融合 CUDA kernel,将 cast+sigmoid+bias+noaux_tc 四步合并为单次 kernel launch,并通过 enable_moe_scores_elementwise_fuse 开关控制启用。
变更范围:custom_ops/gpu_ops/、fastdeploy/model_executor/layers/moe/、fastdeploy/scheduler/config.py、fastdeploy/engine/
影响面 Tag:[OP] [Optimization] [Engine] [Scheduler]
📝 PR 规范检查
存在两处规范问题:① 标题 Tag 大小写偏差;② PR 描述所有 section 均为空。
标题建议(可直接复制):
[Cherry-Pick][OP][Optimization] Kernel fusion: cast+sigmoid+bias+noaux_tc(#7777)
PR 描述建议(可直接复制):
## Motivation
通过将 MoE 路由中的 cast + sigmoid + bias + noaux_tc 四个操作融合到单一 CUDA kernel(`grouped_topk`),减少显存带宽占用和 kernel launch 开销,提升 MoE 路由性能(适用于 DeepSeek-V3/R1、GLM-4.5-Air、Qwen3-30B-A3B、Kimi-K2 等模型)。
## Modifications
- `custom_ops/gpu_ops/grouped_topk_kernels.cu`:新增融合 kernel,一次 pass 完成 cast+sigmoid+bias+grouped_topk 计算,支持 float32/float16/bfloat16 输入
- `custom_ops/gpu_ops/cpp_extensions.cc`:声明并通过 `PD_BUILD_STATIC_OP` 注册 `grouped_topk` custom op
- `custom_ops/setup_ops.py`:将 `grouped_topk_kernels.cu` 加入两处编译源文件列表
- `fastdeploy/model_executor/layers/moe/moe.py`:`get_moe_scores` 新增 `use_fused_cast` 参数,`use_fused_cast=True` 时走 `grouped_topk` 融合路径
- `fastdeploy/scheduler/config.py` + `engine/args_utils.py` + `worker/worker_process.py`:新增 `enable_moe_scores_elementwise_fuse` 开关(默认 False)
- `fused_moe_{blackwell,cutlass,deepgemm,triton}_backend.py`:同步新增 `use_fused_cast` 传参(仅 CUDA + flag 开启时生效)
- `fastdeploy/model_executor/layers/moe/ep.py`:EPLB 路径显式禁用 fusion(TODO),非 EPLB 路径启用
- `tests/operators/test_grouped_topk_op.py`:新增 grouped_topk 算子单测,覆盖多种模型配置和数据类型
## Usage or Command
```bash
# 启用 MoE 路由 elementwise 融合(默认关闭)
fastdeploy serve ... --enable-moe-scores-elementwise-fuse
```
## Accuracy Tests
N/A
## Checklist
- [x] Add at least a tag in the PR title.
- Tag list: [`[FDConfig]`,`[APIServer]`,`[Engine]`, `[Scheduler]`, `[PD Disaggregation]`, `[Executor]`, `[Graph Optimization]`, `[Speculative Decoding]`, `[RL]`, `[Models]`, `[Quantization]`, `[Loader]`, `[OP]`, `[KVCache]`, `[DataProcessor]`, `[BugFix]`, `[Docs]`, `[CI]`, `[Optimization]`, `[Feature]`, `[Benchmark]`, `[Others]`, `[XPU]`, `[HPU]`, `[GCU]`, `[DCU]`, `[Iluvatar]`, `[Metax]`]
- You can add new tags based on the PR content, but the semantics must be clear.
- [ ] Format your code, run `pre-commit` before commit.
- [x] Add unit tests. Please write the reason in this PR if no unit tests.
- [ ] Provide accuracy results.
- [x] If the current PR is submitting to the `release` branch, make sure the PR has been submitted to the `develop` branch, then cherry-pick it to the `release` branch with the `[Cherry-Pick]` PR tag.问题
| 级别 | 文件 | 概述 |
|---|---|---|
| 🟡 建议 | fastdeploy/model_executor/layers/moe/ep.py:541 |
use_fused_cast=True 硬编码,绕过 enable_moe_scores_elementwise_fuse 配置开关,与其他所有 backend 行为不一致 |
总体评价
新增融合 kernel 思路清晰,Custom Op 注册、编译集成、单测均已就绪。主要问题是 ep.py 非-EPLB 路径硬编码 use_fused_cast=True,绕过了统一的配置开关,建议与其他 backend 保持一致。
| layer.gate_correction_bias, | ||
| getattr(layer, "renormalize", True), | ||
| topk_reduce_func=getattr(layer, "topk_reduce_func", None), | ||
| use_fused_cast=True, # NOTE(zhushengguang): Kernel Fusion can be used in non-EPLB scenarios. |
There was a problem hiding this comment.
🟡 建议 use_fused_cast=True 被硬编码,未检查 enable_moe_scores_elementwise_fuse 配置开关。
其他所有 backend(blackwell/cutlass/deepgemm/triton)均已正确检查该 flag:
use_fused = (
layer.fd_config.scheduler_config.enable_moe_scores_elementwise_fuse
and current_platform.is_cuda()
)而此处直接写死 True,导致:当用户未开启 --enable-moe-scores-elementwise-fuse(默认 False)时,EP 非-EPLB 路径仍然走融合 kernel,与其他 backend 行为不一致,也违背了该 flag 的语义。
建议改为与其他 backend 保持一致:
use_fused = (
layer.fd_config.scheduler_config.enable_moe_scores_elementwise_fuse
and current_platform.is_cuda()
)
# ...
use_fused_cast=use_fused,
CI报告基于以下代码生成(30分钟更新一次): 1 任务总览存在 4 个 Required 任务失败,需优先处理后才可合并。
2 任务状态汇总2.1 Required任务 : 2/8 通过
2.2 可选任务 — 21/23 通过
3 失败详情(仅 required)Run Base Tests / base_tests — 测试失败(置信度: 高)Run Base Tests / base_tests
根因详情: 关键日志: 修复建议:
修复建议摘要: 为 mxfp4.py:38 的 paddle.compat 调用加 None 检查 关联变更: Run Four Cards Tests / run_4_cards_tests — 测试失败(置信度: 高)Run Four Cards Tests / run_4_cards_tests
失败用例:
根因详情: 关键日志: 修复建议:
修复建议摘要: batch_invariant_ops.py:809 添加 paddle.compat 调用 None 保护 关联变更: xpu_8cards_case_test / run_xpu_8cards_cases — 测试失败(置信度: 高)xpu_8cards_case_test / run_xpu_8cards_cases
失败用例:
根因详情: 关键日志: 修复建议:
修复建议摘要: moe.py noaux_tc 导入需加 CUDA 平台检测保护 关联变更: Extracted partial CE model tasks to run in CI. / run_ce_cases — 测试失败(置信度: 高)Extracted partial CE model tasks to run in CI. / run_ce_cases
根因详情: 关键日志: 修复建议:
修复建议摘要: 修复 mxfp4.py:38 paddle.compat 调用,添加 None 检查 关联变更: |
CI报告基于以下代码生成(30分钟更新一次): 1 任务总览
2 任务状态汇总2.1 Required任务 : 2/8 通过
2.2 可选任务 — 20/22 通过
3 失败详情(仅 required)xpu_8cards_case_test / run_xpu_8cards_cases — 环境问题(置信度: 高)
失败用例:
根因详情: 关键日志: 修复建议:
修复建议摘要: 关联变更: PR 在 Run Base Tests / base_tests — 环境问题(置信度: 高)
根因详情: 关键日志: 修复建议:
修复建议摘要: 关联变更: 本 PR 未改动 Run Four Cards Tests / run_4_cards_tests — 环境问题(置信度: 中)
失败用例:
根因详情: 关键日志: 修复建议:
修复建议摘要: 修复 mxfp4.py:38;可下载 Artifacts 查看详细日志 链接: 查看日志 Extracted partial CE model tasks to run in CI. / run_ce_cases — 环境问题(置信度: 高)
根因详情: 关键日志: 修复建议:
修复建议摘要: mxfp4.py:38 添加 callable 检查,修复环境兼容性 关联变更: 本 PR 未改动 |
Motivation
Modifications
Usage or Command
启动服务时加上参数
--enable-moe-scores-elementwise-fuseAccuracy Tests
Checklist
[FDConfig],[APIServer],[Engine],[Scheduler],[PD Disaggregation],[Executor],[Graph Optimization],[Speculative Decoding],[RL],[Models],[Quantization],[Loader],[OP],[KVCache],[DataProcessor],[BugFix],[Docs],[CI],[Optimization],[Feature],[Benchmark],[Others],[XPU],[HPU],[GCU],[DCU],[Iluvatar],[Metax]]pre-commitbefore commit.releasebranch, make sure the PR has been submitted to thedevelopbranch, then cherry-pick it to thereleasebranch with the[Cherry-Pick]PR tag.