已合并
[AscendNPU-IR] Reapply "enable flatten by default" (!2326) & fix performance degradation caused by this patch #2583
[AscendNPU-IR] Reapply "enable flatten by default" (!2326) & fix performance degradation caused by this patch #2583
已合并
kiansitov创建于 5 天前
kiansitov
kiansitov
5 天前

title: "[AscendNPU IR]: Reapply "enable flatten by default" (!2326) and fix performance degradation caused this patch"

描述 Description

  1. Revert "Revert "!2326 merge migration into master"".
    Reapply previously reverted commit "[AscendNPU-IR] enable flatten by default" (af7a256c8d3e7e3cdbe4153b18a9fc747a07b2a8)

  2. Fix performance degradation.
    hfusion\FlattenOp pass is applied only to MIX kernels, while the pre-flattening PropagateReshape pass is also applied to AIV kernels.
    PropagateReshape and hfusion\FlattenOp should be applied consistently: PropagateReshape should be applied only when hfusion\FlattenOp is also applicable.
    Applying PropagateReshape without hfusion\FlattenOp leads to performance degradation because PropagateReshape pass can affect operation fusion.

    Skip PropagateReshape pass for AIV kernels (when flattening is not applicable)

类型 Category

  • Bug修复 Bugfix
  • 新功能 New feature
  • 代码风格更新(格式化,局部变量)Style changes
  • 重构(即不是新增功能,也不是修改bug的代码变动)Refactor
  • 构建过程或辅助工具的变动 Build-related
  • 文档内容更新 Docs

Checklist

  • 我的代码遵循这个项目的代码风格 My code follows the project's code style
  • 我已经自己测试过我的代码 I have self-tested my code
  • 我已经更新了相应的文档 I have updated the relevant documentation
  • 我已经根据需要更新了对应的变更日志 I have updated the changelog as needed
  • 我已经在标题中正确使用了类型标签(例如:feat:, fix:)I have correctly used type labels in the title (e.g., feat:, fix:)
likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 kiansitov 的贡献)
kiansitovkiansitov
5 天前 创建了 pull request,commit 9a46e449
atomgit-bot
atomgit-bot
5 天前 评论:

变更摘要

此 PR 主要包含两项核心修复:一是恢复了之前被回退的「enable flatten」功能,移除了 MixedCV 场景下自动禁用 flatten 的逻辑,并调整了 ProcessVsstbPass 在 pipeline 中的位置以确保 pass 顺序正确;二是修复了 TransferReadToGatheringLoadPattern 中处理三维循环置换排列映射(如 (d0, d1, d2) -> (d1, d2, d0))时的索引计算错误。此外还新增了一个 FoldWidenedTransferReadAfterWrite 优化模式,可将特定条件下被静态 mask 裁剪的宽 transfer_read 替换为 vector.broadcast,以及修复了 PropagateReshapePass 对 AIV 核心类型和空值检查的处理。

主要改动

  • 恢复 flatten 并调整 pass 顺序:在 PassPipeline.cpp 中移除了 MixedCV 下自动设置 enableFlatten=false 的约 12 行逻辑,恢复 flatten 的正常启用;同时在 HIVMAVEPipelines.cpp 中将 createProcessVsstbPass()OptimizeReductionLoop 之前移至 AveLoopOptimize 之后,避免 reduction 分裂干扰相邻 store 模式的识别。

  • 修复三维循环排列映射的索引逆推:在 NormalizeVector.cppTransferReadToGatheringLoadPattern 中,对排列映射 permMap 调用新增的 inversePermutation 得到 invPermMap,再用 invPermMap.compose(composeIndices) 替代原先错误的 permMap.compose,正确恢复 memref 索引空间中的 gather 偏移量。

  • 新增 FoldWidenedTransferReadAfterWrite 优化:在 RemoveRedundantWriteAndReadPair.cpp 中新增 getStaticMaskSizes 辅助函数(支持 vector.constant_maskvector.create_mask)及 FoldWidenedTransferReadAfterWrite 重写模式,当 transfer_read 的 mask 静态可推导且读向量的有效区域完全被写向量覆盖时,将 read 替换为对写向量的 vector.broadcast,消除冗余的 write-read 对。

  • PropagateReshapePass 扩展 AIV 跳过条件:在 PropagateReshape.cpp 中将 coreType 检查从直接解引用改为 has_value() 安全判断,并将跳过条件从仅 AIC 扩展为 AIC || AIV,使 AIV 核心类型的函数同样跳过 reshape 传播。

  • 新增测试覆盖:在 remove-redundant-write-and-read-pair.mlir 中新增 10 个测试用例(含 5 个正向折叠和 6 个负向不应折叠场景);新增 normalize_vector_gather_perm.mlir 测试文件,覆盖三维循环排列映射下 gather 生成的正确性。

likedislike
ascend-robot
ascend-robot成员
5 天前 评论:

libing-pipeline创建中,请等待……

likedislike
atomgit-bot
atomgit-bot
5 天前 评论:

代码审查

经过对所有 7 个变更文件的全面审查,我没有发现需要报告的真实问题。每个变更都是正确且有充分理由的:

  1. RemoveRedundantWriteAndReadPair.cpp:新增的 FoldWidenedTransferReadAfterWrite 模式逻辑正确,边界检查完备;getStaticMaskSizes 辅助函数正确处理了 ConstantMaskOpCreateMaskOp,并对非静态掩码保守地返回空。测试用例覆盖了正面和负面场景。

  2. HIVMAVEPipelines.cppProcessVsstbPass 的 pass 重排序不引入正确性问题。

  3. PropagateReshape.cpp:将 AIV core 类型加入跳过列表,是对 AIC 的合理扩展,同时将 coreType 使用方式从指针式改为显式 .has_value()/.value(),逻辑等价。

  4. NormalizeVector.cpp:使用 inversePermutation(permMap) 替代 permMap.compose() 是一个正确的 bug 修复——旧代码对 (d0,d1,d2)→(d1,d2,d0) 这样的循环置换计算了错误的 gather 索引。analyzeTransferRead 中的 isPermutation() 守卫保证了 inversePermutation 不会返回空 map。新增的测试文件 normalize_vector_gather_perm.mlir 正确验证了该修复。

  5. PassPipeline.cpp:移除了在 mixed CV 启用时禁用 flatten 的临时规避逻辑,直接传递原始 config。这是有意为之的变更,与其他修复一起使 flatten 能够正常运行。

  6. remove-redundant-write-and-read-pair.mlir:新增的 9 个测试用例(3 个正面 + 6 个负面)充分覆盖了新模式的各种场景。

  7. normalize_vector_gather_perm.mlir:新增的 2 个测试用例正确验证了 3D 循环置换的 gather 索引计算。

关闭总结

  • P0: 0
  • P1: 0
  • P2: 0
  • P3: 0

总体风险评估:该变更风险较低。所有改动逻辑正确,测试覆盖充分,不存在正确性、安全或可靠性回归。

类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

likedislike
ascend-robotascend-robot成员
5 天前 添加了label:stat/needs-squash
此处折叠了171条消息 查看更多
ascend-robot
ascend-robot成员
1 天前 评论:
流水线 PR-pipeline_npuir-smoke#2006 已完成
阶段 任务名 状态 详情
编译构建 Compile >>>
开发者测试 CVOps >>>
流水线 PR-pipeline_npuir-smoke >>>
likedislike
ascend-robot
ascend-robot成员
1 天前 评论:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
ascend-robot
ascend-robot成员
1 天前 评论:

The MR can not be merged, because of CodeReview discussion not resolved

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike
Roman RusyaevRoman Rusyaev成员
1 天前 解决了最后一个问题
ascend-robotascend-robot成员
1 天前 合入了pull request,合并节点 SHA:6c6c800c7df82ca4e96ba79cd2ab8dc6432f4182