perf: optimize causal conv1d and mega chunk gdn for qwen3.5.#1999
Open
Sinle4Cat wants to merge 3 commits into
Open
perf: optimize causal conv1d and mega chunk gdn for qwen3.5.#1999Sinle4Cat wants to merge 3 commits into
Sinle4Cat wants to merge 3 commits into
Conversation
Sinle4Cat
requested review from
Clement-Wang26,
DongheJin,
DragonFive,
JimHsiung,
Kang-Meng,
RobbieLeung,
XuZhang99,
liujinguang0125,
liutongxuan,
walsonyang,
xiao-yu-chen,
yingxudeng,
yq33victor and
zhang-minchao
as code owners
July 21, 2026 14:13
yingxudeng
reviewed
Jul 22, 2026
| auto h = torch::zeros({num_matrices, K, V}, opts_fp16); | ||
| auto v_new = torch::empty({B, T, H, V}, opts_fp16); | ||
| auto beta_t = torch::empty({H, T}, opts_compute); | ||
| auto a = torch::empty({B, T, H, kMegaChunkSize}, opts_compute); |
Collaborator
There was a problem hiding this comment.
zero 和 empty 没区别是吗?都是安全的
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.
背景与目标
Qwen3.5 Gated DeltaNet 的 chunk prefill 路径原先按以下阶段执行:
该路径在 Conv1d 与 MegaChunkGdn 之间产生额外的张量变换、Q/K 归一化、dtype 转换和中间张量读写;MegaChunkGdn 内部部分完全覆写的输出也使用了
zeros初始化。本 PR 将 CausalConv1d 的 prefill 输出直接整理成 MegaChunkGdn 所需的 packed Q/K/V 布局,并为 MegaChunkGdn 增加 BF16 计算路径,减少中间算子和无效初始化。所有优化均带有运行时 contract 检查,不满足条件时继续走原始实现。
接入方案
1. CausalConv1dQkv direct-layout 输出
aclnnCausalConv1dQkv。narrow + view将 packed buffer 零拷贝包装为[1, T, H, 128],避免额外 split、concat 和 pack。2. 通用 prefill 路由
CausalConv1d + process_mixed_qkv路径。3. MegaChunkGdn BF16 specialization
minus_identitymask,保证输入 dtype 与 kernel contract 一致。4. 跳过无效输出初始化
a、a_inv、h、final_state等中间/输出张量由zeros改为empty。定位过程中遇到的问题与解决方法
代码改动规模与分布
当前 PR 相对目标分支共修改 5 个文件,新增 222 行、删除 53 行,净增 169 行。
npu_causal_conv1d.cpp、npu_ops_api.hqwen3_gated_delta_net_base.cppnpu_mega_chunk_gdn.cppthird_party/xllm_ops依赖关系
98a8b346a8b545ca1d21f50d465b5ff6c4a4c63a。验证环境
prefill_opt_conv1d_gdnb21f02f25ae95ea07dcae7fc5fef6155bd3b9dbc98a8b346a8b545ca1d21f50d465b5ff6c4a4c63a验证结果
当前 PR HEAD 构建与测试
python setup.py build:通过。All tests passed!。算子精度与 shape 覆盖
0.0;conv cache 最大绝对误差为0.0。性能
ATK 大 shape BF16 对比中,优化路径均快于原路径:
模型
模型 C-Eval
同一优化分支在 rebase 前的等价实现完成 Qwen3.5-27B TP2 MTP 全量 C-Eval:
0.9138,macro accuracy0.9163。PR 范围
本 PR 只包含 Qwen3.5 NPU prefill 的 CausalConv1dQkv direct-layout 接入、MegaChunkGdn BF16 specialization、无效初始化消除及对应 fallback。Decode 路径和不满足 contract 的输入仍使用原始实现。
Change Type