Closed
[Feature]: 支持 ascend 自定义图优化 pass 的动态 shape #3101
dezheng889Add on  Jul 22Closed at  Jul 23
dezheng889
dezheng889Collaborators
Jul 22 Create

提交提案之前,请先检索仓库内是否已有相同的提案,如已有请在同一提案中进行讨论。

💻 需求背景、当前现状、期望实现的功能内容、具体的设计方案、以及测试方案

背景 / 目的

当前 torch_npu/_inductor/fx_passes/ascend_custom_passes 下的图优化 pass 依赖
静态 shape(维度为具体整数),在动态 shape(维度为 SymInt)场景下会因
get_node_shape 返回 None、显式 SymInt bailout、以及基于具体整数的比较而失效。
本次改动构建符号 shape 基础能力,使这些优化在动态 shape 下达到与静态 shape 等价的
优化效果。

主要改动

  • 新增 utils/symbolic_shape_util.py:设备无关、免 guard 的符号推理工具层
    • 三值符号判定(恒真 / 恒假 / 不可判定),跨 torch 版本鲁棒,仅在可证明时才优化
    • 尺寸实参归一化(int / SymInt / sym_size 节点)与符号 shape 物化
      (从支配输入 anchor 构造 sym_size 引用,dominance 安全)
    • int32 值域三值判定、符号感知的 fake meta 传播、轻量命中/跳过统计
  • utils/get_binary_fold_result.py:get_node_shape 增加 allow_symbolic 双轨;
    binary fold、slice、slice_scatter、pad 维度提取全部支持符号
  • ascend_custom_passes/ascend_graph_pass.py:各 pass 符号化改造
    • fold_expand / view / reduce / sink_view / cat、cat_slice_cat、cat_to_view、
      pad_slice、repeat_to_expand、iota/arange dtype 降级、batch embedding fusion
    • cat_slice_cat 补充“末端完整覆盖”证明,修正原静态实现的潜在漏判
  • 全局开关 NPU_INDUCTOR_DYNAMIC_FX_PASS:关闭时所有符号处理退化为原静态行为
    (含 materialize 硬闸门),保证静态场景零回归

测试

  • 新增 test/_inductor/test_dynamic_shape_fx_passes.py:设备无关单测/集成测试,
    纯 CPU 可运行,覆盖工具层三值/归一/物化/值域、各 pass 动态 shape 行为、
    边界保护(不可判定、秩不一致、异号符号、开关关闭)与静态回归

兼容性 / 风险

  • 未改变重编译边界(不新增 guard);不可判定场景一律保留原节点
  • 关闭开关即可完全回退到旧的静态行为,风险可控

替代方案

补充说明

欢迎加入社区,感谢您对社区的贡献 🎉!

likedislike
dezheng889dezheng889Collaborators
Jul 22 Added label: feature
dezheng889dezheng889Collaborators
Jul 22 Associated pull request
ascend-robotascend-robotCollaborators
Jul 23 Close issue
ascend-robotascend-robotCollaborators
Jul 24 Added label: resolved
dezheng889dezheng889Collaborators
Aug 13 Associated pull request
TorchNPU-BotTorchNPU-BotCollaborators
Aug 13 Added label: bot-triaged
TorchNPU-Bot
TorchNPU-BotCollaborators
Aug 13 Comments:

检测到当前 issue 已关联 PR,自动添加标签:bot-triaged

likedislike
dezheng889dezheng889Collaborators
Aug 19 Associated pull request
dezheng889dezheng889Collaborators
Aug 20 Associated pull request
dezheng889dezheng889Collaborators
Sep 17 Associated pull request