Merged
feat(Pass): Assume_divisible for dual_dst #6080
feat(Pass): Assume_divisible for dual_dst #6080
Merged
gggxinmengAdd on Sep 9
gggxinmeng
gggxinmengCollaborators
Sep 9

https://gitcode.com/cann/pypto/issues/2858

OoO阶段无法对动态validshape进行比较,导致dualdst使能失败

动态切分轴的逐 tile validShape 通常由编译器推导为:

min(max(rawShape - offset, 0), tileShape)

例如两个相邻半块可能分别得到:

min(max(vm, 0), TILE_M)
min(max(vm - TILE_M, 0), TILE_M)

InferDynShape::RecordStaticValidShapeOnL0CCopyUB 会在后续 InferShape 把 shape 归一化为动态表达式之前,将全 concrete 的 valid shape保存到 staticValidShape。但只要任意一维仍含动态成分,快照就整体跳过,DualDst 只能回退到动态表达式比较。

上述两个相邻半块的表达式 dump 不同,而且都没有 concrete 值,DynShapeEq 无法证明它们相等,因此 pair 识别失败。即使业务实际保证 vm 总是 TILE_M 的整数倍、两个半块运行时一定都是完整 tile,编译器仍然缺少这一事实。

根因不是 DualDst 搬运或 codegen 不支持,而是前端没有 Interface 表达“运行时标量可被 tile 整除”,编译期也就无法把逐 tile valid shape证明为静态满块。

方案目标

  1. 允许用户显式声明某个运行时表达式一定能被指定 tile 整除;
  2. 编译器依据该声明证明相关 validShape 恒等于完整 tile,并把它静态化;
  3. 在 InferShape 归一化之前让 staticValidShape 快照命中,使现有 DualDst 识别和融合逻辑直接受益;
  4. 融合后仅静态化切分轴,保留非切分轴原有 valid shape;
  5. 在调试模式下生成运行时取模断言,帮助发现错误的用户声明;
  6. 没有假设或证明条件不足时保留原动态表达式,不强行融合。

本方案解决的是“编译器缺少业务整除信息”的问题,而不是自动识别主块和尾块。它是一份由用户提供、编译器信任的语义契约。

总体设计

完整链路如下:

Python 用户代码
  │ assume_divisible(expr, 128)
  ▼
Function 保存整除假设
  │ expression -> {divisors}
  ▼
InferDynShape 证明 validShape 恒为 tile
  │ min/max/add/sub/mul + offset/cap 对齐证明
  ▼
动态 validShape -> 静态 tileShape
  │ 同步 Tensor 与需要保持一致的 Op attribute
  ▼
RecordStaticValidShapeOnL0CCopyUB
  │ 写入 staticValidShape
  ▼
现有 DualDst 匹配与融合
  │ 保留非切分轴的动态 validShape
  ▼
现有 leaf / codegen / dev_encode / runtime 流程

调试模式 runtime_debug_mode=4:
Function 中的显式假设 -> 生成 expr % divisor == 0 运行时断言
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for gggxinmeng's contribution.)
gggxinmenggggxinmengCollaborators
Sep 9 Created pull request, commit 1d91e6a7
gggxinmenggggxinmengCollaborators
Sep 9 Associated issue: [Task|任务跟踪]: DualDst 使能与 assume_divisible API
atomgit-bot
atomgit-bot
Sep 9 Comments:

变更摘要

本 PR 针对 DualDst 动态 valid shape 无法静态化导致 pair 识别失败的问题(issue #2858),引入用户可显式声明的整除语义契约 assume_divisible:用户通过 Python API 声明某个运行时标量可被指定 tile 整除,编译器在 InferDynShape 中依据该声明将逐 tile 的 validShape(形如 min(max(raw - offset, 0), tile))证明并化简为静态满块,使其在 InferShape 归一化之前命中 staticValidShape 快照,从而让现有 DualDst 识别与融合逻辑直接受益;同时新增 runtime_debug_mode=4 下的运行时取模断言,并在融合时仅静态化切分轴、保留非切分轴的动态 valid shape。

主要改动

  • 新增 assume_divisible 用户接口与假设登记机制: 在 python/pypto/experimental/operation.py 新增 assume_divisible(expr, divisor)(对编译期常量做整除校验、非法 divisor 抛 ValueError),并在 experimental/__init__.py 导出;底层由 Program::RegisterDivisibleAssumption / IsKnownDivisible 将归一化后的表达式与 divisors 集合存入 divisibleAssumptions_(新增 DivisibleAssumption 结构),并通过 function.cpp 绑定暴露给 Python。

  • InferDynShape 基于假设证明并静态化 valid shape: 新增 SimplifyAllValidShapes 与 SimplifyValidShapeWithAssumptions,通过 IsFullTileAfterOffset 递归下沉各层 View 的立即数 offset、IsExprDivisible 递归消费加/减/乘结构与整除假设,将可证明的 min/max valid shape 化简为常量 tile;SyncViewValidShapeAttribute / SyncCopyInValidShapeAttribute 将化简结果同步回 ViewOpAttribute / CopyOpAttribute,并置于 RecordStaticValidShapeOnL0CCopyUB 之前执行,使静态快照命中、DualDst 融合直接受益。

  • DualDst 融合仅静态化切分轴: dualdst_engine.cpp 的 SetDualDstCopyAttr 改为优先使用 attrE->GetToDynValidShape(),仅在维度数不符时回退为全立即数;融合后仅将切分轴(direction)的 valid shape 置为合并后的完整 tile,正交轴保留原有动态 valid shape。

  • runtime_debug_mode=4 生成运行时取模断言: 新增 EmitAssumeDivisibleChecks(control_flow_codegen.cpp/h),在 legacy 控制流(BuildControlFlowDynamic)与 IR 路径(ir_backend.cpp)的控制流入口为每个显式登记的假设表达式生成 if ((expr % divisor) != 0) __builtin_trap(); 断言;backend.cpp 将假设表达式加入 linker 符号表,tile_fwk_config_schema.json 与 config.py 同步更新了 mode 4 的语义说明。

  • 新增单元与场景测试: test_infer_shape.cpp 覆盖多级 View/FlashAttention 形态的化简、无假设时跳过、staticValidShape 快照命中及 attr 同步;test_schedule_ooo.cpp 验证融合后非切分轴动态 valid shape 保留;test_ir_backend.cpp 与新增的 test_assume_divisible.py、test_assume_divisible_runtime.py 分别验证 mode 4 断言生成及运行时对可整除/不可整除值的接受与 trap 行为。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 9 Comments:

🤖 AI 代码检视正在进行中,请稍候…

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Sep 9 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Sep 9 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Sep 9 Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.
For more, you also can visit HICANN.


PR Approval Progress

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
docs ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
framework/src/interface/configs ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
framework/src/interface/function ✅ congqiannan, 尹杰 (2/2) ✅ 尹杰 (1/1)
framework/src/interface/program ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
framework/src/machine ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
framework/src/passes ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
python/pypto/experimental/operation.py ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)
python/pypto/frontend ✅ congqiannan, 尹杰 (2/2) ✅ 尹杰 (1/1)
repo-cann/pypto ✅ 尹杰, congqiannan (2/2) ✅ 尹杰 (1/1)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

gggxinmeng, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
CANN-robotCANN-robotCollaborators
Sep 9 Set xinweiliu,lyz0328,cam_bxh,dai-zhentao,lululu40888,shi-dingkun,gguuaanngg,codehjp423,gaoxiang618,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,ym-HW,hw_zhangzhiqi,yinjie27_,congqiannan,zhangyuebing,tsungl4,gaoxingwang,zhiweiyuan,caodongdong,make_pto,zhoubotcam,linjiashu,wangbing110,WenboCodes as approver
CANN-robotCANN-robotCollaborators
Sep 9 Set xinweiliu,lyz0328,cam_bxh,dai-zhentao,lululu40888,shi-dingkun,gguuaanngg,codehjp423,gaoxiang618,wangzheng97,DCGDDD,syfeng,jason_yuan_ye,foundea,ym-HW,hw_zhangzhiqi,yinjie27_,congqiannan,zhangyuebing,tsungl4,gaoxingwang,zhiweiyuan,caodongdong,make_pto,zhoubotcam,linjiashu,wangbing110,WenboCodes as approver
gggxinmeng
gggxinmengCollaborators
Sep 9 Comments:

compile

likedislike
gggxinmenggggxinmengCollaborators
Sep 9 Pre merge succeeded (commit_id: 81558b9defebb5779248628cdeac244ace310d96)
CANN-robot
CANN-robotCollaborators
Sep 9 Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
Sep 9 Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 9 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 9 Added label: ci-pipeline-passed
Jie Yin
Jie YinCollaborators
Sep 9 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Sep 9 Added label: approved
congqiannan
congqiannanCollaborators
Sep 9 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Sep 9 Added label: lgtm
CANN-robotCANN-robotCollaborators
Sep 9 Merged pull request