已合并
fix: enforce block dim hardware core budget #1956
fix: enforce block dim hardware core budget #1956
已合并
zhang_shengjie创建于 9月3日
zhang_shengjie成员
9月3日

Pull Request

1. 主要解决的问题

PGO 多 Group/TopN 场景中,生成或恢复的 block_dim 可能超过硬件实际 AIV 数;同时存在以下关联失败链路:

  • 动态 CORENUM 表达式在 solver 前被提前求值,搜索变量尚未赋值时可能触发除 0(SIGFPE)。
  • tiling case、PGO 候选和静态 tiling 入口未完整传播失败,可能继续使用无效候选或未初始化输出。
  • PGO 配置文件可直接恢复超预算 block_dim,且失败回退前会污染实时 tiling 的初始状态。
  • 多 Group 场景未区分串行与并行 launch 语义,可能错误聚合 core budget。

2. 修改方案

  • 将 CORENUM 保留为显式 solver 约束;依赖搜索变量的 CORENUM 表达式不再在 DoTiling 前提前求值。
  • GeneralTiling 后校验每个 tiling case 的 block_dim <= corenum_,fused 顶层再次校验聚合结果;超预算返回失败,不做静默截断,避免 tile 漏算。
  • 串行 Group 的外层 block_dim 取各 Group 最大值;并行 Group 按求和语义安排 block offset,并限制在当前硬件 probe 范围内。
  • Inductor modeled/measured TopN 搜索统一使用 min(limit->aiv_num, g_no_limit_res.aiv_num) 作为可用 AIV 数。
  • PGO 配置增加 flag、数据长度、memcpy_s 和 block budget 校验;先读取到临时副本,仅校验成功后写回,失败时使用未污染的实时 tiling 状态重新搜索。
  • PGO 搜索只向 batch callback 传递有效候选,并在无新增候选时返回失败。
  • AutofuseTilingWithConfig 失败后立即向 C++ tiling/静态生成入口传播;静态生成返回空结果时,两个 Python 编译入口抛出明确异常。

3. 代码修改流程图

flowchart TD
    A[获取实际 AIV/UB 资源] --> B[初始化 tiling core budget]
    B --> C{PGO 配置有效且 block_dim 合法?}
    C -- 是 --> D[提交 PGO 临时副本]
    C -- 否 --> E[使用未污染状态实时搜索]
    E --> F[solver 执行 CORENUM 约束]
    F --> G[各 tiling case 计算并校验 block_dim]
    G --> H[串行 Group 取 max / 并行 Group 求和并安排 offset]
    H --> I{聚合 block_dim 不超过硬件预算?}
    D --> I
    I -- 是 --> J[TopN 仅保留有效候选并执行 profile]
    J --> K[输出选中 tiling 或静态 kernel]
    I -- 否 --> L[逐层返回失败]
    L --> M[实时搜索 fallback 或 Python 编译异常]

4. 测试用例说明

4.1 UT/静态检查

验证项 命令/范围 结果
ATT 编译 cmake --build build --target att_ut -j 8 通过
Codegen 编译 cmake --build build --target test_main -j 8 通过
Generator/solver UT GeneratorUT.* + CORENUM solver 用例 70 passed,1 disabled
Codegen 定向 UT PGO 配置隔离、失败输出保护、TopN 核预算、静态 tiling 6 passed
Python UT test_ascendc_compile.py + test_asc_codegen_compile_conv2d.py 96 passed
格式与提交检查 clang-format、ruff、pre-commit、OAT 通过

4.2 PGO 实机验证

验证环境:CANN 9.2.0,Ascend950PR_9579,硬件 AIV 数 56;用例位于 /workspace/CANN-DevTools/att_generalization。

选择 ATT-CG-01:6 输入连续 Add + Pow/Mean/Rsqrt RMSNorm,shape 为 (1, 2048, 1024),同时覆盖 Reduce、多个 schedule result/group 与 PGO TopN,是本问题最直接的回归图。

验证前执行:

cmake --build build --target aihac_codegen -j 8
cmake --build build --target pyautofuse -j 8

运行时通过本地 package path/LD_LIBRARY_PATH 成套加载本分支 pyautofuse.so、libaihac_codegen.so 及 graph/expression 依赖,避免新旧 C++ ABI 混用;实测加载路径为:

/workspace/graph-autofusion/build/autofuse/compiler/py_module/pyautofuse.so
/workspace/graph-autofusion/build/autofuse/libaihac_codegen.so

分别执行:

python -m att_generalization.run_att_cache_guard --topn 1  --warmup 2 --repeat 5 --device npu:0 --att-profiling
python -m att_generalization.run_att_cache_guard --topn 10 --warmup 2 --repeat 5 --device npu:0 --att-profiling

结论:Top1/Top10 均完成 PGO host/device 编译、候选 profiling、NPU 执行和精度比对,未出现 SIGFPE、core dump 或超核数 block_dim。

配置 PGO 搜索结果 实际编译候选 最终 block_dim 硬件上限 最大绝对误差 结果
Top1 56 个可行 tiling,block_dim 27–56 1 53 56 4.76837158203125e-07 PASS
Top10 58 个可行 tiling,block_dim 27–56 10 53 56 4.76837158203125e-07 PASS

Top10 实际编译的 10 个候选外层 block_dim 为 53, 52, 53, 54, 54, 52, 49, 52, 49, 46;PGO 搜索结果中 block_dim > 56 的数量为 0。

5. 核对清单

6. 其他信息

  • 原问题环境为 Ascend950PR_9589(AIV=64);本次补充验证环境为 Ascend950PR_9579(AIV=56),覆盖了更小 core budget 下的 TopN 行为。
  • 首次尝试仅覆盖本地 libaihac_codegen.so、仍使用安装包旧 pyautofuse.so 时,在 graph/expression 对象析构处出现 ABI 混用崩溃;改为成套加载本分支 binding 和依赖后 Top1/Top10 均通过。该现象属于验证部署组合问题,不是 PGO 流程回归。
likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 zhang_shengjie 的贡献)
Zzhang_shengjie成员
9月3日 创建了 pull request,commit 9dff0c88
此处折叠了11条事件消息 查看更多
CANN-robotCANN-robot成员
9月3日 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
9月3日 评论:

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
repo-cann/graph-autofusion ✅ yangyongqiang0606, liyuewei, xchu42 (3/2) ✅ yangyongqiang0606 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robotCANN-robot成员
9月3日 将niuyuhu,yangyongqiang0606,liyuewei,xchu42,depeng1994,wqtshg_wt,peiyang,shengnan666,XuebinYang,sjtulxh,zhanj,zhujingjing,xuyafei,wangxiaotian995设为评审人
此处折叠了12条事件消息 查看更多
Zzhang_shengjie成员
29 天前 修改了pull request 的描述
zhang_shengjie成员
29 天前 评论:

compile

likedislike
Zzhang_shengjie成员
29 天前 预合并成功(commit_id: 14662b7af56493237a241857aea9963310d3ab4b)
CANN-robot
CANN-robot成员
29 天前 评论:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robot成员
29 天前 添加了label:ci-pipeline-running
CANN-robotCANN-robot成员
29 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
29 天前 添加了label:ci-pipeline-passed
xchu42
xchu42成员
29 天前 评论:

/lgtm

likedislike
liyuewei
liyuewei成员
29 天前 评论:

/lgtm

likedislike
CANN-robotCANN-robot成员
29 天前 添加了label:lgtm
yangyongqiang
yangyongqiang成员
28 天前 评论:

/approve

likedislike
CANN-robotCANN-robot成员
28 天前 添加了label:approved
CANN-robotCANN-robot成员
28 天前 合入了pull request