已关闭
[RFC]: 自动融合: 扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活 #298
blian创建于 8月9日关闭于 8月20日
8月9日 添加了label:rfc
8月9日 修改了issue 的描述
8月9日 修改标题为 “[RFC]: 自动融合: 扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活”,原标题为“[RFC]: 自动融合——扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活”
8月9日 修改标题为 “[RFC]: 自动融合: 扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活”,原标题为“[RFC]: 自动融合——扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活”
8月9日 修改了issue 的描述
8月11日 修改了issue 的描述
8月20日 关闭了 issue
8月20日 添加了label:resolved
8月22日 关联了pull request:[Feature][compilation] Wan2.2 编译融合增强:Pattern 覆盖 5 类算子 + 双精度支持 + 模型级 bf16
8月22日 关联了pull request:[Feature][compilation] MiniMax-H3 编译融合:bf16 推理 + RMSNorm/RoPE pattern + dummy run 验证
8月22日 关联了pull request:[Feature][compilation] MiniMax-H3 编译融合:bf16 推理 + RMSNorm/RoPE pattern + dummy run 验证
8月23日 删除了label:resolved
8月23日 关联了pull request:[Feature][Compile] MiniMax-H3: bf16 dummy run with compile-time RMSNorm/RoPE fusion
8月23日 关联了pull request:[Feature][Compile] MiniMax-H3: bf16 dummy run with compile-time RMSNorm/RoPE fusion
8月23日 关联了pull request:[Feature][Fusion] Add MiniMax-H3 AdaLN/SwiGLU/gate triton fusion patterns
8月23日 关联了pull request:[Feature][Fusion] Add MiniMax-H3 AdaLN/SwiGLU/gate triton fusion patterns
8月23日 关联了pull request:[Feature][quant] Unify --quant {fp32,bf16,w8a8} with device-aware w8a8 and fix quant-layer guard stability
8月23日 关联了pull request:[Feature][quant] Unify --quant {fp32,bf16,w8a8} with device-aware w8a8 and fix quant-layer guard stability
8月23日 关联了pull request:[Feature][compilation] Add Qwen-Image RoPE and norm_out AdaLN fusion patterns (register + BF16/FP32 tests)
8月23日 关联了pull request:[Feature][compilation] Add Qwen-Image RoPE and norm_out AdaLN fusion patterns (register + BF16/FP32 tests)
8月31日 关联了看板:MindIE-SD
9月7日 关联了pull request:[Feature][ops] Add catlass MXFP8 FFN fusion: mm_swiglu_mxquant / mm_gelu_mxquant with real-graph compile enablement
9月7日 关联了pull request:[Feature][ops] Add catlass MXFP8 FFN fusion: mm_swiglu_mxquant / mm_gelu_mxquant with real-graph compile enablement
9月21日 关联了pull request:[Feature][ops] Add the MXFP8 FFN fusion kernels and the fused attention prologue kernels
9月21日 关联了pull request:[Feature][ops] Add the MXFP8 FFN fusion kernels and the fused attention prologue kernels
RFC:自动融合: 扩展统一 Pattern Matcher 覆盖量化融合与 GMM/MM+激活
1. 概述
1.1 简介
"自动融合"的目标是扩展 MindIE-SD 统一 Pattern Matcher,支持常见融合算子的自动替换,提升开箱即用的加速覆盖度。重点覆盖三类目标:
npu_quant_matmul_gelu)。npu_grouped_matmul_swiglu_quant(_v2))、Linear 与激活的组合融合。本 RFC 的融合算子候选来源已明确纳入以下三个昇腾算子仓并完成算子盘点与筛选:
cann/ops-nn(https://gitcode.com/cann/ops-nn)torch.ops.cann_ops_nn.*单算子接入cann/ops-transformer(https://gitcode.com/cann/ops-transformer)Ascend/op-plugin(https://github.com/Ascend/op-plugin,即 torch_npu 自定义 API 插件)torch_npu自定义 API(npu_swiglu/npu_quant_matmul_gelu/npu_linear等)三者与
torch_npu原生算子共同构成全部融合算子候选来源,见 §3.2 完整清单。所有候选融合算子必须能在 DiT 系扩散模型(文本到图像 / 文本到视频的 Transformer 主干)的计算图中被实际使用。模型侧使用情况以上游框架 vLLM Omni 的扩散模型注册表(vllm_omni/diffusion/registry.py)与各模型实现为准,本文 3.3 与第 8 节的匹配清单均来自对E:\tmp\vllm-omni\vllm_omni\diffusion\models\的逐模型结构扫描(标注为结构分析 + 源码定位,非实机验证)。1.2 动机
当前 MindIE-SD 的 Pattern Matcher 仅覆盖 elementwise / norm 类融合算子:
npu_fast_gelu)、MulAdd(→muls_add),注册于mindiesd/compilation/patterns/,三段注册(patterns/__init__.py+passes/__init__.py+compiliation_config.py)。nn.Module层由quantize()改图完成(mindiesd/quantization/),层实现直接调用npu_quant_matmul/npu_dynamic_quant等算子,不经 Pattern Matcher;量化后的层图内npu_quant_matmul后接激活的组合仍可被进一步融合。DiT 系扩散模型(FLUX / Wan / Qwen-Image / HunyuanImage-3.0 / HunyuanVideo / SD3 / Cosmos3 / MiniMax H3 等)的 Transformer 主干中,FFN(Linear → GELU/SiLU → Linear)、MoE(GroupedMatMul → SwiGLU)、Q/K 投影后(RMSNorm → RoPE)都是高频出现的公共子结构,具备显著的融合收益。
1.3 目标
register_replacement路径,新增覆盖量化融合与 GMM/MM+激活的 pattern,保持三段注册机制不变。npu_quant_matmul_gelu(A8W8/A4W4)。npu_grouped_matmul_swiglu_quant(_v2)、npu_linear等融合算子。非目标(本 RFC 不包含):
torch_npu/ op-plugin / ops-transformer / ops-nn,本仓只做 pattern 替换)。2. 用例分析
2.1 用例一:量化 MatMul + GELU 自动融合
Linear → GELU(approx=tanh) → Linear。经quantize()改图后,图中出现npu_quant_matmul(或npu_dynamic_quant+npu_quant_matmul)后接npu_fast_gelu的组合;未量化图中为matmul + add + gelu组合。npu_quant_matmul_gelu(gelu_type支持 tanh/erf),减少 kernel 启动与中间张量读写。npu_quant_matmul+npu_fast_gelu分开执行相比耗时下降。enable_quant_mm_gelu默认开启;不匹配的 GELU(非 tanh/erf)自动跳过。2.2 用例二:MoE GMM + SwiGLU 自动融合
npu_grouped_matmul(W13 融合)→npu_swiglu→npu_grouped_matmul(W2)。mindiesd/layers/moe/moe_mlp.py:48-157已手工编排该组合。npu_grouped_matmul+npu_swiglu组合替换为npu_grouped_matmul_swiglu_quant(未量化)或_v2(量化),将同一组合在编译层自动收敛。enable_gmm_swiglu默认开启;group_list / 分组语义不匹配时自动跳过。2.3 用例三:通用 Linear + 激活自动融合
Linear → SiLU/GELU或 gated 形式(silu(x)*x)。npu_linear(带激活)或npu_swiglu(gated 形式)。2.4 用例四:非 MM 融合
RMSNorm → RoPE连续子结构;部分 FFN 出现gelu(x) * gate(npu_gelu_mul);非 tanh 近似的F.gelu(erf 精确版)。RMSNorm+RoPE替换为kv_rms_norm_rope_cache(若语义匹配)、gelu(x)*y替换为npu_gelu_mul、silu(x)*x替换为npu_swiglu、erf GELU 替换为npu_gelu。kv_rms_norm_rope_cache的输入/输出语义需 NPU 实机确认,未确认前标注实验性并默认关闭。3. 方案设计
3.1 总体方案
设计思路:沿现有
register_replacement路径扩展 Pattern Matcher。所有新增 pattern 遵守PatternBase接口约定(name()/inputs()/pattern()/replacement()),通过三段注册接入(patterns/__init__.py+passes/__init__.py的pattern_registry+compiliation_config.py的FusionPatterns)。双路径覆盖:
matmul + add + gelu等 torch 原生算子组合,pattern 以 placeholder 输入表达,replacement 调用torch_npu融合算子。quantize()改图后的图中为npu_quant_matmul/npu_dynamic_quant+ 激活的组合,pattern 匹配torch_npu算子序列,replacement 调用更高阶融合算子。优先级划分:
npu_quant_matmul_gelunpu_grouped_matmul_swiglu_quant(_v2)、npu_linearkv_rms_norm_rope_cache、npu_gelu_mul、npu_swiglu、npu_gelu约束与前置条件:
npu_available判断)。torch_npu/ op-plugin / ops-transformer / ops-nn 提供的算子,版本兼容性在实现阶段核实。3.2 融合算子来源与筛选
候选来源:
cann/ops-transformergmm/grouped_matmul_swiglu_quant_v2、moe/moe_init_routing_quant_v2、posembedding/kv_rms_norm_rope_cache、attention/fused_infer_attention_score、attention/quant_flash_attnAscend/op-plugintorch_npu自定义 APInpu_swiglu、npu_quant_matmul_gelu、npu_linear、npu_gelu_mul、npu_gelutorch_npunpu_fast_gelu(已有)、npu_quant_matmul、npu_dynamic_quant、npu_grouped_matmulcann/ops-nntorch_extension(torch.ops.cann_ops_nn.*)单算子接入matmul/quant_matmul_activation_quant(量化 MM+激活+量化,匹配npu_quant_matmul_gelu目标)、matmul/quant_matmul_dequant、quant/dequant_swiglu_quant、activation/gelu_quant;P1 GMM/MM+激活:matmul/fused_mat_mul、activation/swi_glu、activation/swiglu_group、activation/swiglu_group_quant;P2 非 MM:norm/group_norm_silu(_quant)、norm/ada_layer_norm_v2、activation/gelu_mul、vfusion/modulate、vfusion/scaled_masked_softmax_v2cann/ops-nn(experimental)experimental/目录,实验性算子matmul/fused_patch_mlp(Linear+GELU×N 融合 MLP)、quant/turbo_quant_compress_latent(MLA KV latent 4bit 压缩,vllm-omni 现役 DiT 无 MLA,不命中)筛选准则:
cann/ops-nn(quant/、matmul/、norm/、vfusion/、activation/、experimental/等目录)、cann/ops-transformer(gmm/、moe/、posembedding/、attention/等目录)、Ascend/op-plugin(torch_npu自定义 API)三个仓库的算子逐目录、逐算子盘点,列出全部候选融合算子(见上表;ops-nn 的子目录算子清单在实现阶段可对照仓内op_api/README 复核)。_DIFFUSION_MODELS,共 60+ pipeline 入口)的 Transformer 主干公共子结构逐一匹配。匹配结论已完整呈现于本文:file:line),见 §3.3 覆盖矩阵与 §8 任务清单。来源缺口:
cann/ops-nn仓内无grouped_matmul/GMM 专用算子(MoE 专家侧 GMM 依赖 ops-transformer),也无kv_rms_norm_rope_cache类算子(Norm+RoPE 依赖 ops-transformer)。两类目标不受 ops-nn 补充影响。3.3 DiT 模型覆盖矩阵
以下矩阵以 vLLM Omni 注册表(
vllm_omni/diffusion/registry.py的_DIFFUSION_MODELS)为参考,覆盖 DiT 系扩散模型及其公共子结构。模型侧事实来自对E:\tmp\vllm-omni\vllm_omni\diffusion\models\的全量逐模型源码扫描(覆盖全部 60+ pipeline 入口对应的 Transformer/去噪器实现),并将 §3.2 盘点出的全部候选融合算子逐一与之匹配;命中与未命中结论均完整呈现,具体命中位置见第 8 节。标注为结构分析推断,非实机验证。遍历与匹配范围声明:
cann/ops-nn(quant/matmul/norm/vfusion/activation/experimental 全目录)、cann/ops-transformer(gmm/moe/posembedding/attention 等)、Ascend/op-plugin(torch_npu自定义 API)全部候选融合算子已盘点(§3.2)。file:line);未命中项(如 AudioX Conv1d FFN、Cosmos3-Edge ReLU²、Magi-Human SWIGLU7、SD3 无 RoPE、GR00T 无 RoPE、StableAudio/CosyVoice3 无 QK norm、ops-nn 无 GMM/kv_rms_norm_rope_cache、turbo_quant_compress_latent 不命中现役 DiT)均已显式注明。修正说明(相对初稿):
transformer/transformer_2双模型切换(pipeline_wan2_2.py:317-344),transformer 内无专家路由,不命中 GMM 融合。minimax_h3_transformer.py:517-542),无 MoE。torch._grouped_mm)与 HunyuanImage-3.0(FusedMoE)。npu_grouped_matmul_swiglu_quant_v2、npu_swiglu、npu_gelu_mul(无)npu_quant_matmul_gelu、npu_linearnpu_quant_matmul_gelu、npu_linearnpu_swiglu(merged fc1)npu_quant_matmul_gelu、npu_linearnpu_quant_matmul_gelu、npu_linearnpu_swiglunpu_swiglu(主模型);Edge 为 ReLU² 不适用npu_quant_matmul_gelu、npu_linear(无 RoPE)SiluAndMul)npu_swiglu(已 fused)npu_swiglu(gate/up 分离,需合并)npu_quant_matmul_gelu、npu_linearnpu_grouped_matmul_swiglu_quant(未量化)、npu_swiglunpu_quant_matmul_gelu、npu_linearnpu_gelu_mul(唯一命中)、npu_quant_matmul_gelu(erf)npu_quant_matmul_gelu、npu_linearnpu_swiglunpu_quant_matmul_gelu、npu_linearnpu_swiglu(分离)npu_swiglu;改造后npu_grouped_matmul_swiglu_quantSiluAndMul)npu_swiglu(已 fused)SiluAndMul)npu_swiglunpu_swiglu(merged)npu_swiglu(merged);已自带 Triton QK-Norm+RoPE 融合npu_quant_matmul_gelu、npu_linearnpu_swiglu/npu_gelu不适用npu_swiglu不适用npu_swiglunpu_quant_matmul_gelu、npu_linearnpu_quant_matmul_gelu、npu_linear(无 RoPE)npu_swiglu(分离)npu_swiglu(需改写 HF MLP);条件 MLP 为npu_linearnpu_swiglu3.4 P0:量化融合 pattern
目标:
npu_quant_matmul_gelu(A8W8/A4W4,gelu_type支持 tanh/erf),覆盖量化 MatMul + GELU 组合。匹配形态(两条来源路径):
matmul + add(bias) + gelu组合,经量化描述符语境替换为npu_quant_matmul_gelu。npu_quant_matmul(...)(或npu_dynamic_quant+npu_quant_matmul)+npu_fast_gelu→npu_quant_matmul_gelu。关键约束:
register_replacement的 pattern 用 torch 原生算子表达,replacement 用torch_npu调用。npu_quant_matmul_gelu的 scale 参数(x1Scale / x2Scale)需从图中提取,输出 dtype 规则按算子文档处理。gelu_type由原 GELU 的approximate参数决定:tanh→ gelu_tanh,否则 → gelu_erf。npu_dynamic_quant + npu_quant_matmul形态(quantization/int8_config.py:442-451、mxfp8_config.py:400-403、mxfp4_config.py:284),路径 B 的匹配形态成立。三段注册:
patterns/quant_mm_gelu_pattern.py:QuantMMGeluPatternGrouppasses/__init__.py:pattern_registry["enable_quant_mm_gelu"] = ("QuantMMGeluPatternGroup", "..patterns")compiliation_config.py:FusionPatterns.enable_quant_mm_gelu: bool = True3.5 P1:GMM / MM+激活 pattern
目标 1:
npu_grouped_matmul_swiglu_quant(未量化)/_v2(量化),覆盖npu_grouped_matmul+npu_swiglu组合(MoE 专家侧 W13)。npu_grouped_matmul(x, w13, split_item=2, ...)+npu_swiglu(...)。mindiesd/layers/moe/moe_mlp.py:48-157已手工编排该组合。lingbot_video_transformer.py:368-380/:502-526,未量化)、HunyuanImage-3.0(hunyuan_image3_transformer.py:1624,量化穿透,走_v2)。HiDream-Image(hidream_image_transformer.py:415-480)为逐专家循环,需先改 grouped GEMM 才可命中。patterns/gmm_swiglu_pattern.py→enable_gmm_swiglu。目标 2:
npu_linear(带激活),覆盖 Linear + SiLU/GELU 组合(非 gated 形式)。matmul + add(bias) + activation。npu_quant_matmul_gelu,否则走npu_linear。patterns/linear_bias_gelu_pattern.py→enable_linear_activation。3.6 P2:非 MM 融合 pattern
目标 1:
kv_rms_norm_rope_cache(RMSNorm + RoPE,可选 KV cache)。RMSNorm+apply_rotary_emb连续子结构(Q/K 投影后)。rms_norm_pattern+rope_pattern两个独立 pattern 冲突(重复匹配风险确认存在:vllm-omni 通过apply_rope_to_qk对 Q/K 连续调用 RoPE,Norm 独立于:436-437等位置调用)。未确认前标注实验性并默认关闭(enable_norm_rope_cache: bool = False)。patterns/norm_rope_cache_pattern.py→enable_norm_rope_cache。目标 2:
npu_gelu_mul(GELU × Mul),覆盖gelu(x) * gate组合。ernie_image_transformer.py:396(up_out * F.gelu(gate_out))。其余 GELU 模型均为非 gated 独立 GELU。patterns/gelu_mul_pattern.py→enable_gelu_mul。目标 3:
npu_swiglu(独立 SiLU 门控),覆盖silu(x) * xgated FFN 组合。SiluAndMul(可直接映射:Z-Image、OmniGen2、Bagel/Lance、HunyuanImage-3.0)与显式silu(x)*x(需合并 gate:Krea2、LingBot、MiniMax H3、Cosmos3、StableAudio、MammothModa2、OVIS、Boogu、HiDream、FLUX2/klein、NextStep、SenseNova-U1)。patterns/swiglu_pattern.py→enable_swiglu。目标 4:
npu_gelu(erf 精确版),覆盖非 tanh 近似的F.gelu。GELUPattern(tanh →npu_fast_gelu)按approximate参数分流。ernie_image_transformer.py:396)、GLM-Image glyph_projector("gelu"erf,glm_image_transformer.py:1032)。patterns/gelu_erf_pattern.py→enable_gelu_erf。3.7 与 quantize() 路径的关系、配置项与 DFX
与 quantize() 的关系:
mindiesd/quantization/quantize.py)在nn.Module层完成,先于 torch.compile;Pattern Matcher 在编译期作用于量化后的层图(路径 B)。新增配置项:
enable_quant_mm_geluTruenpu_quant_matmul_gelu(P0)enable_gmm_swigluTruenpu_grouped_matmul_swiglu_quant(_v2)(P1)enable_linear_activationTruenpu_linear(P1)enable_norm_rope_cacheFalsekv_rms_norm_rope_cache(P2,实验性,需 NPU 确认)enable_gelu_mulTruenpu_gelu_mul(P2)enable_swigluTruesilu(x)*x→npu_swiglu(P2)enable_gelu_erfTruenpu_gelu(P2)安全隐私与 DFX:
torch_npu/ op-plugin / ops-transformer / ops-nn 提供的算子,不引入外部依赖;沿用仓库对算子调用与文件的规范。enable_norm_rope_cache);pattern 不匹配时自动跳过,不改变既有图行为。enable_<op>规范;三段注册不变。torch_npu);NPU 实机按各用例进行 kernel diff 验证。gelu_type/ scale 参数不匹配时跳过并给出日志。4. 测试设计
npu_quant_matmul_gelupattern 命中与输出一致性torch_npu):cosine_similarity > 2^-7;NPU 实机:kernel diff 确认npu_quant_matmul_gelu出现、原始 kernel 消失npu_grouped_matmul_swiglu_quant(_v2)patternnpu_linearpatternkv_rms_norm_rope_cache(实验性)npu_gelu_mul/npu_swiglu/npu_gelu(erf)tests/compilation/test_pattern_registration.py扩展tests/compilation/patterns/*)回归约束:新增测试不得破坏既有
tests/compilation/、tests/quantization/用例;沿用仓库既有测试框架与 CI 门禁。5. 缺点和风险
npu_quant_matmul/npu_dynamic_quant的 meta/fake 支持需核实;若不支持register_replacement,回退到自定义 Graph Pass。kv_rms_norm_rope_cache语义不确定:输入/输出语义、与现有两 pattern 的重复匹配风险需 NPU 实机确认,默认关闭。npu_quant_matmul_gelu与npu_linear、npu_swiglu与npu_gelu_mul在部分图中可能同时可匹配,需明确的优先级裁定。npu_swiglu命中面宽但形态不一:部分模型已用SiluAndMul(已 fused,无需 pattern),部分为 gate/up 分离nn.Linear(需先改写为 merged column 布局),pattern 需分别处理。6. 现有技术
npu_quant_matmul_gelunpu_grouped_matmul_swiglu_quant(_v2)gmm/npu_linear/npu_swiglu/npu_gelu_mul/npu_gelukv_rms_norm_rope_cacheposembedding/quant_matmul_activation_quant/dequant_swiglu_quant/group_norm_silu/swi_glu等matmul/、quant/、norm/、activation/)vllm_omni/diffusion/registry.pyvllm_omni/diffusion/models/mindiesd/compilation/patterns/、passes/mindiesd/quantization/quantize.py、layer.py7. 未解决问题
npu_quant_matmul/npu_dynamic_quant等量化算子的 meta/fake 支持与 trace 能力。kv_rms_norm_rope_cache的输入/输出语义及与现有rms_norm_pattern+rope_pattern的重复匹配风险。npu_swiglu对 gate/up 分离布局模型的改写成本与命中率(LingBot / Cosmos3 / MammothModa2 / OVIS / Boogu / HiDream / FLUX2 / StableAudio 等)。npu_grouped_matmul_swiglu_quant的可行性。8. 模型×融合算子匹配任务清单
本节为明确任务内容:将 §3.2 全量盘点出的候选融合算子与 vLLM Omni 全量扩散模型逐模型扫描匹配的结果固化为各融合算子的命中模型与具体使用位置。位置引用格式为
文件路径:行号(文件路径相对E:\tmp\vllm-omni\vllm_omni\diffusion\)。每个融合算子均给出命中清单与显式"不适用/未命中"说明;实现阶段以对应模型源码为最终事实源。8.1 P0:
npu_quant_matmul_gelu(量化 MM + GELU)匹配形态:
npu_dynamic_quant + npu_quant_matmul + F.gelu(tanh/erf)。vllm-omni NPU 量化路径已产出该形态:quantization/int8_config.py:442-451(NPUInt8LinearMethod.apply)、mxfp8_config.py:400-403、mxfp4_config.py:284。命中模型(GELU-tanh FFN,
quant_config已接线):models/qwen_image/qwen_image_transformer.py:473 FeedForward:445 ColumnParallelApproxGELU→F.gelu:468-470:759-787models/glm_image/glm_image_transformer.py:700 ColumnParallelGELUF.gelu:725quant_configmodels/flux/flux_transformer.py:76 FeedForward:48 ColumnParallelApproxGELU→:71-73:60-73models/sd3/sd3_transformer.py:41 FeedForward:29 GELU→F.gelu:35-38models/helios/helios_transformer.py:138 HeliosFeedForward:110 ColumnParallelGELU→:133-135quant_configmodels/wan2_2/wan2_2_transformer.py:114 WanFeedForward:84 ColumnParallelGELU→:109-111:94(cross-attn KV 量化关闭:587)WanFeedForward:246:104FeedForward:535-546quant_config(FP8 注释:419)models/ltx2/ltx2_transformer.py:236 LTX2FeedForward:208 ColumnParallelApproxGELU→:231-233:611-694models/dreamzero/causal_wan_model.py:667-671nn.GELU(tanh):668-669models/cosyvoice3_audio/cosyvoice3_dit.py:55-68 FeedForwardnn.GELU(tanh):63-64FeedForward(models/gr00t/modeling/modules/dit.py:91-98,activation_fn="gelu-approximate")erf 分支命中:Ernie-Image
models/ernie_image/ernie_image_transformer.py:396(gated GELU,gelu_type=erf)。任务:实现
QuantMMGeluPatternGroup;为上述模型构造 FFN 片段单测;NPU 实机 kernel diff 验证。8.2 P1:
npu_grouped_matmul_swiglu_quant(_v2)(GMM + SwiGLU)匹配形态:
grouped_matmul(W13) → silu(x)*x → grouped_matmul(W2)。仅 2 个真实命中:torch._grouped_mm显式 W13→silu→W2,bf16 未量化models/lingbot_video/lingbot_video_transformer.py:368-380(GroupedExperts w1/w2/w3)、:502-504(for-loop 路径)、:510-532(_run_grouped_expertsgrouped_mm 路径)npu_grouped_matmul_swiglu_quant(未量化)FusedMoE+SiluAndMul,量化穿透models/hunyuan_image3/hunyuan_image3_transformer.py:1556-1648 HunYuanSparseMoeBlock、:1624 FusedMoE、:1547 SiluAndMul、get_expert_mapping:2100-2116npu_grouped_matmul_swiglu_quant_v2(量化)次生候选(非稀疏专家,需另行评估):
models/hidream_image/hidream_image_transformer.py:415-480逐专家循环,moe_infer:457-480,已留 TODO 待改 grouped GEMM(:464-466)→ 改造后为目标。models/bagel/bagel_transformer.py:458-477)与 SenseNova-U1(MoT,models/sensenova_u1/sensenova_u1_transformer.py:251-270)为 token 路由双权重集,非 top-k 专家,GMM 融合不适用。任务:实现
GMMSwigluPatternGroup;以 LingBot 的_grouped_mm图与 HunyuanImage-3.0 的FusedMoE图分别构造单测;group_list / 分组语义在 NPU 实机验证。8.3 P1:
npu_linear(Linear + 激活)匹配形态:非 gated
matmul + bias + activation。命中模型与位置同 8.1 表(未量化时走npu_linear)。优先级裁定:
quant_config存在 →npu_quant_matmul_gelu;否则 →npu_linear。此裁定需在LinearBiasActivationPatternGroup与QuantMMGeluPatternGroup之间建立显式优先级(P0 优先)。任务:实现
LinearBiasActivationPatternGroup;与 P0 构建裁定单测。8.4 P2:
kv_rms_norm_rope_cache(RMSNorm + RoPE)匹配形态:
RMSNorm → apply_rotary_emb(Q/K 投影后)。RMSNorm→RoPE 是 vllm-omni 普适结构,几乎所有带 RoPE 的模型命中;带 KV cache 路径(cache 版前提)仅以下模型。全量命中(RMSNorm→RoPE 于 Q 和 K):
models/qwen_image/qwen_image_transformer.py:557-558:650-653models/glm_image/glm_image_transformer.py:526-527:621-622/:660-661GlmImageLayerKVCache:348/:404,666-673)models/z_image/z_image_transformer.py:291-292:340models/ovis_image/ovis_image_transformer.py:70-71:145-146models/ernie_image/ernie_image_transformer.py:294-295:343-344models/longcat_image/longcat_image_transformer.py:81-82:146-150models/boogu_image/boogu_image_transformer.py:397-398:432-433models/hidream_image/hidream_image_transformer.py:233-237/:256-261:319-327models/omnigen2/omnigen2_transformer.py:161-162:228-229MoTRMSNormmodels/bagel/bagel_transformer.py:476-477:550-551NaiveCache:326,649-653)models/flux/flux_transformer.py:150-151:251models/flux2/flux2_transformer.py:138-139:267/models/flux2_klein/flux2_klein_transformer.py:283-284DistributedRMSNormmodels/helios/helios_transformer.py:306-307:360-361:757)Krea2RMSNormmodels/krea2/krea2_transformer.py:117-118:147-148models/nextstep_1_1/modeling_nextstep_llama.py:191DynamicCache:193-199)models/sensenova_u1/sensenova_u1_transformer.py:295-304(4 norm):369-429:444-499)DistributedRMSNormmodels/wan2_2/wan2_2_transformer.py:391-396:447-448models/wan2_2/wan2_2_s2v_transformer.py:112-113:154-155models/hunyuan_video/hunyuan_video_15_transformer.py:352-353:437-438models/ltx2/ltx2_transformer.py:661-683:515-526LingBotVideoRMSNormmodels/lingbot_video/lingbot_video_transformer.py:188-189:211-212models/minimax_h3/minimax_h3_transformer.py:346-347:485-486MultiModalityRMSNormmodels/magi_human/magi_human_dit.py:769-770:827-828models/dreamzero/causal_wan_model.py:510-511+fused_qk_rms_norm:227-267:551-570:584-602、:604-609)models/cosmos3/transformer_cosmos3.py:533-534:565AudioXRMSNormmodels/audiox/audiox_transformer.py:175-176:211-212Qwen2RMSNormmodels/mammoth_moda2/mammothmoda2_dit_model.py:442-443:322-323models/hunyuan_image3/hunyuan_image3_transformer.py:1757-1758:1785-1790:1742-1749)LlamaAdaptiveRMSNormmodels/soulx_singer/modules/llama.py:30-48llama.py:355不命中(无 RoPE / 无 QK norm):SD3(PatchEmbed)、StableAudio(仅 RoPE 无 QK norm)、CosyVoice3(仅 RoPE)、GR00T(无 RoPE)。
特例:SenseNova-U1 已自带 Triton 融合版 QK-RMSNorm+RoPE(
models/sensenova_u1/fused_rmsnorm_rope.py:9-198,qk_norm_rope_kernel),是kv_rms_norm_rope_cache的模型侧同构参考;但其头维分半(t/hw)的 3D 结构不匹配标准 1D 版。重复匹配风险确认:vllm-omni 通过
layers/rope.py:306-329 apply_rope_to_qk对 Q/K 连续调用 RoPE,且 Norm 独立于注意层前调用——与现有rms_norm_pattern+rope_pattern存在重复匹配,需冲突裁定。任务:实现
NormRopeCachePatternGroup(默认关闭);先以 GLM-Image / Bagel / DreamZero(带 KV cache)为语义验证目标,NPU 实机确认输入/输出语义后开启。8.5 P2:
npu_gelu_mul(GELU × Mul)全仓唯一命中:Ernie-Image
models/ernie_image/ernie_image_transformer.py:396——up_out * F.gelu(gate_out)(gated GELU FFN,erf 版)。无其他模型存在gelu(x)*y组合。任务:实现
GeluMulPatternGroup;以 Ernie-Image 片段构造单测。8.6 P2:
npu_swiglu(silu(x)*x)命中面最广。两类形态:
已用 vLLM
SiluAndMul(已 fused,pattern 可直接映射):models/z_image/z_image_transformer.py:385/:399models/omnigen2/omnigen2_transformer.py:472/:485models/bagel/bagel_transformer.py:251/:258models/hunyuan_image3/hunyuan_image3_transformer.py:1547显式
silu(x)*x(gate/up 分离或已 merged,需按形态处理):models/flux2/flux2_transformer.py:53-55/models/flux2_klein/flux2_klein_transformer.py:69-71Flux2SwiGLU(silu(x1)*x2)models/krea2/krea2_transformer.py:171F.silu(gate)*up(gate/up 分离,需合并)models/nextstep_1_1/modeling_nextstep_llama.py:260act(gate)*up(merged,可直接映射)models/sensenova_u1/sensenova_u1_transformer.py:217act(gate)*up(merged)models/minimax_h3/minimax_h3_transformer.py:541silu(gate)*up(merged fc1)models/lingbot_video/lingbot_video_transformer.py:295-296silu(gate)*up(分离,需合并)models/cosmos3/transformer_cosmos3.py:462F.silu(gate)*up(分离)models/stable_audio/stable_audio_transformer.py:352x*silu(gate)(nn.Linear分离)models/mammoth_moda2/mammothmoda2_dit_model.py:99F.silu(h1)*h2(分离)models/ovis_image/ovis_image_transformer.py:205silu(gate)*x(单流);双流用 diffusersSwiGLUmodels/boogu_image/boogu_image_transformer.py:54-55/:135silu(x)*y(分离)models/hidream_image/hidream_image_transformer.py:57silu(w1)*w3(分离)不适用:AudioX(Conv1d FFN,
models/audiox/audiox_transformer.py:144)、Cosmos3-Edge(ReLU²,models/cosmos3/transformer_cosmos3_edge.py:75-76)、Magi-Human(SWIGLU7 自定义 clamp/+1,models/magi_human/magi_human_dit.py:97-112)、SoulX-Singer(HF LlamaMLP 在 lib 内)。任务:实现
SwiGLUPatternGroup;merged 布局(NextStep / SenseNova-U1 / MiniMax-H3)可直接映射,分离布局(Krea2 / LingBot / Cosmos3 / MammothModa2 / OVIS / Boogu / HiDream / FLUX2)需先合并 gate/up 列,评估改写成本后分批接入。8.7 P2:
npu_gelu(erf 精确版)命中:Ernie-Image gated GELU(erf,
models/ernie_image/ernie_image_transformer.py:396)、GLM-Image glyph_projector("gelu"erf,models/glm_image/glm_image_transformer.py:1032)。任务:实现
GeluErfPatternGroup;与现有GELUPattern(tanh →npu_fast_gelu)按approximate分流。8.8 模型侧已接入的 NPU 算子(pattern 上游现状)
torch_npu原生算子已被 vllm-omni 直接使用,是 pattern 的天然上游:torch_npu.npu_rms_norm:layers/norm.py:130/:198-200(RMSNorm/RMSNormVAE的 NPU forward)。torch_npu.npu_dynamic_quant+torch_npu.npu_quant_matmul:quantization/int8_config.py:442-451、mxfp8_config.py:400-403、mxfp4_config.py:284。mindiesd.fast_layernorm(layers/norm.py:45)、mindiesd.layernorm_scale_shift(layers/adalayernorm.py:57)、mindiesd.rotary_position_embedding(layers/rope.py:62-68)。附录
参考资料
D:\research\vllm-omni\vllm_omni\diffusion\registry.pyE:\tmp\vllm-omni\vllm_omni\diffusion\models\mindiesd/compilation/patterns/、mindiesd/compilation/passes/术语表
torch._inductor.pattern_matcher.register_replacement的自动融合机制文档更新计划
docs/docs/zh/features/compilation.md(新增融合清单章节,含第 8 节模型命中表)及英文版;更新docs/zh/release_note.md文档站点验证
本 RFC 不涉及
docs/内容与 Sphinx 构建;实现阶段若更新文档,需执行以下最小验证路径(本次未执行,仅给出建议命令):