已合并
feat: Inductor支持PGO(复用TF流程) #1541
feat: Inductor支持PGO(复用TF流程) #1541
已合并
zhang_shengjie创建于 29 天前
zhang_shengjie成员
29 天前

描述

本 PR 使 Inductor 静态 shape 场景的 GenerateTopnSolutions 可在开启 Autofuse PGO 时,复用 TF PGO 的候选生成、全核遍历、solver、完整 tiling data 归一和 MSPTI 实测统计能力,返回实测 TopN。不修改 TorchAir/TorcHair 调用流程和 GenerateTopnSolutions 接口。

PGO 关闭时,Inductor 仍使用原有性能公式 TopN,不启动子进程,不执行全核 PGO 遍历。

一、主要解决的问题

  1. Inductor 已默认使用 GenerateTopnSolutions,但原实现只按性能公式排序,--autofuse_enable_pgo=true 不会触发实际采样。
  2. TF PGO 已具备 tiling key、核数、多 Group 和 Reduce RCore 多阶段候选实测能力,Inductor 需要复用该能力,不另起一套候选算法。
  3. MSPTI Activity 与前端 profiling 可能存在同进程资源冲突,需在独立 runner 子进程内执行采样。
  4. PGO sidecar 缺失、损坏、runner 失败或候选无效时,不应导致 Inductor 编译中断,需要保持原 TopN 能力作为回退。

二、修改内容

2.1 候选生成与选解

  • PGO 开启时,Inductor 使用 TF PGO 同源的 tiling key/核数遍历、阈值、solver 和 MSPTI 采样逻辑。
  • 支持多 Group、Reduce RCore phase1/phase2 多阶段和完整 tiling data 返回。
  • 候选归一化、去重和排序时强制保留默认解;实测候选均不优于默认解时,最终保留默认解。
  • PGO 关闭时保持原性能公式 TopN 行为。

2.2 独立 PGO runner

  • host compile 产生 tiling.so、PGO runner 可执行文件、device kernel binary 和 manifest。
  • GenerateTopnSolutions 的 proxy 校验 sidecar 协议、ABI、generation 和 SHA256,然后使用 posix_spawn 启动子进程。
  • runner 独立初始化 ACL/MSPTI,加载 host tiling 与 device binary,执行 GenerateMeasuredTopnSolutions,通过有界二进制文件返回 TopN。
  • 父进程校验返回数量、字段长度和结果完整性,临时结果文件在解析后删除。

2.3 编译产物与回退

  • PGO sidecar 按 generation 原子发布,保留当前和上一代。
  • manifest 记录 protocol/version/ABI、产物文件名、SHA256 和 MSPTI preload 路径。
  • MSPTI 自动从当前 CANN 根目录的 tools/mspti 发现;不需要 AUTOFUSE_MSPTI_PATH,不需要用户手工设置 LD_PRELOAD。proxy 创建 runner 时自动将 libmspti.so 及可用的 libprof_common.so 合入子进程 LD_PRELOAD
  • MSPTI 不可用时跳过 sidecar 生成;sidecar 缺失/损坏、runner 异常或结果解析失败时,按请求数量回退到原性能公式 TopN。

2.4 采集语义对齐

  • Inductor 与 TF 一致接收所有 MSPTI_ACTIVITY_KIND_KERNEL 记录。
  • 删除 Inductor 专属的 kernel name/type/correlationId/timestamp 过滤,避免合法 Reduce 候选被误判为无采样数据。
  • 保留空指针、内存分配、MSPTI 状态、重复时间 key 和预期 record 数的完整性校验。TF legacy callback 不修改。

三、调用流程

sequenceDiagram
    participant Frontend as TorchAir/Inductor
    participant Proxy as tiling.so proxy
    participant Runner as PGO runner process
    participant Host as measured tiling host
    participant Device as PGO device kernel
    participant MSPTI as MSPTI Activity

    Frontend->>Proxy: GenerateTopnSolutions(input_configs, topn, res_limit)
    Proxy->>Proxy: validate manifest, ABI and SHA256
    Proxy->>Runner: posix_spawn with automatic LD_PRELOAD
    Runner->>Runner: aclInit, set device, create stream
    Runner->>Host: GenerateMeasuredTopnSolutions
    Host->>Device: launch every measured candidate
    MSPTI-->>Host: kernel activity duration
    Host-->>Runner: measured and protected TopN
    Runner-->>Proxy: bounded binary result
    Proxy-->>Frontend: tiling data, workspace and block dimensions

任意 sidecar/runner/MSPTI/IPC 基础设施失败,proxy 都转入原性能公式 TopN,不要求前端增加 PGO 分支。

四、使能方式

4.1 环境准备

仅加载当前要验证的 CANN 包,例如:

source "${CANN_ROOT}/bin/setenv.bash"

CANN 包内需包含:

tools/mspti/include/mspti.h
tools/mspti/lib64/libmspti.so

4.2 开启 Inductor PGO Top3

export AUTOFUSE_FLAGS="--autofuse_enable_pgo=true"
export TORCHINDUCTOR_NPU_EXT_AUTOTUNE_TOPN=3
python <inductor_case.py>

TORCHINDUCTOR_NPU_EXT_AUTOTUNE_TOPN 由现有前端流程决定传入 GenerateTopnSolutions 的 TopN,本 PR 不新增前后端接口。

不需要配置:

AUTOFUSE_MSPTI_PATH
LD_PRELOAD

4.3 关闭 PGO

export AUTOFUSE_FLAGS="--autofuse_enable_pgo=false"
export TORCHINDUCTOR_NPU_EXT_AUTOTUNE_TOPN=1
python <inductor_case.py>

关闭后 GenerateTopnSolutions 按原性能公式返回 TopN,不遍历 PGO 全核候选。

4.4 当前支持边界

  • 支持 Inductor 静态 shape、非 Cube 融合 kernel。
  • 支持多 Group 和 Reduce RCore phase1/phase2 多阶段候选。
  • 动态 shape 或 Cube 融合不进入当前 Inductor PGO 路径。

五、维测信息

5.1 关键日志

日志 含义
GenerateTopnSolutions enter: topn=... Inductor TopN 入口及请求数量
[PGO] MSPTI is unavailable, skip Inductor PGO sidecars 当前 CANN 根目录下未找到完整 MSPTI 头文件/动态库
GenerateMeasuredTopnSolutions failed runner 内实测选解失败
Inductor PGO failed, fallback to modeled TopN PGO 基础设施失败,已转入原性能公式 TopN
Inductor PGO runner or result parsing failed 子进程退出或 IPC 结果校验失败

5.2 sidecar 产物

tiling.so 同级目录下生成:

tiling.so
tiling.so.pgo.<generation>/
├── manifest.json
├── tiling.so.pgo_runner
└── tiling.so.pgo_kernel.aicore_binary_elf_v1

manifest 可用于检查:

  • protocol/version/generation
  • runner_abi/proxy_abi/device_source_abi
  • runner、kernel 和 tiling.so 的 SHA256
  • 子进程自动使用的 MSPTI preload 路径

5.3 建议排查顺序

  1. 确认 AUTOFUSE_FLAGS 和前端传入 TopN。
  2. 确认当前 CANN 根目录及 tools/mspti 内容,避免混用多套 CANN lib。
  3. 检查 sidecar generation 与 manifest SHA256/ABI。
  4. 检查 runner 退出码、GenerateMeasuredTopnSolutions 和 MSPTI record 数。
  5. 如已回退,确认返回的候选数是否与请求 TopN 一致。

六、文件结构与职责

模块 文件 职责
Inductor TopN autofuse/codegen/codegen_tiling_inductor_topn.cpp 生成模型 TopN/实测 TopN、默认解保护及回退入口
PGO 共用层 codegen_tiling_pgo_common.cpp TF/Inductor 共用 wrapper、MSPTI Activity 采集与重复测量
PGO 内存 codegen_tiling_pgo_memory.cpp tensor/workspace/launch params 设备内存准备与回收
PGO 搜索 codegen_tiling_pgo_search.cpp 候选归一、实测结果聚合、核数搜索和排序
PGO runtime codegen_tiling_pgo_runtime.cpp runner 入口翻译单元组装
父进程 proxy codegen_tiling_inductor_pgo_proxy.cpp sidecar/manifest 校验、环境组装、子进程创建、IPC 解析和回退
子进程 runner codegen_tiling_inductor_pgo_runner.cpp 参数校验、ACL 初始化、加载 host/device 产物、调用实测入口和结果写回
Python compile autofuse/compiler/python/compile_adapter.py 拆分 PGO host/runner/device 源码,从当前 CANN 根自动发现 MSPTI
Python publish autofuse/compiler/python/ascendc_compile.py 编译 runner/device binary,生成 manifest,原子发布并清理历史 generation

七、测试与实测结果

7.1 回归

范围 结果
PGO 定向 C++ UT 35/35 PASS
TestCodegenTiling 定向回归 113/113 PASS
Python compile flow 106/106 PASS
ARM PGO UT 31/31 PASS
历史 13 个编译失败用例 baseline 13/13 PASS,PGO Top3 13/13 PASS

7.2 A5收益实测

环境:Python 3.12.9、torch 2.12.0+cu130、torch_npu 2.12.0。
image.png

变更类型

变更统计

  • 相对 develop:39 个文件,+5268/-517
  • PR 不包含 docs/ 目录修改。

提交记录

Commit 描述
994c971c 支持 Inductor MSPTI PGO TopN
4e698e4a 修复 Reduce PGO 候选内存大小解析
e84c8f6c 修复 Inductor PGO ST 预期
6ec8156f 拆分模块并解决代码检查告警
d0af8bdc 修复 compile adapter 导入顺序告警
e723220a 对齐 Inductor 与 TF PGO Activity 采集语义

核对清单

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 zhang_shengjie 的贡献)
Zzhang_shengjie成员
29 天前 创建了 pull request,commit 9fd3bb79
atomgit-bot
atomgit-bot
29 天前 评论:

变更摘要

此 PR 实现了 Inductor 静态 shape 路径复用 MSPTI PGO 实测 TopN 的方案 A:当 enable_autofuse_pgo=true 时,不修改 host_compile -> get_topn -> static kernel -> autotune 既有流程,而是在 Graph-Autofusion 内部将 GenerateTopnSolutions 变为一个独立子进程 measured TopN 代理。代理通过 posix_spawn 创建独立 runner 进程,动态加载当前 tiling.so,调用私有 GenerateMeasuredTopnSolutions 入口,借助 MSPTI 实测、去重、排序并返回 TopN。编译侧新增 PGO sidecar 构建链路,生成 runner 可执行文件、AICore 二进制及 manifest,并在 tiling.so 原子替换的最后一步发布 bundle。所有失败场景(sidecar 缺失、hash/ABI/协议错误、runner 超时、MSPTI 失败)均直接返回失败,不回退性能公式,避免静默改变 PGO 语义。

主要改动

  • PGO 代理入口与 spawn 机制: codegen_tiling.cpp/.hTilingLib 新增 GenInductorPgoProxyEntryGenInductorPgoProxySpawnGenInductorPgoProxyManifestGenInductorPgoProxySha256GenInductorPgoProxyResultParser 等函数,在 PGO true 时生成 posix_spawn + waitpid + 超时 kill + ParseInductorPgoResult 完整代理逻辑,公共 GenerateTopnSolutions 变为代理调用方。

  • 私有 measured TopN 入口: GenGenerateTopnSolutionsEntry 新增 entry_name 参数,PGO true 时生成 extern "C" int64_t GenerateMeasuredTopnSolutions(...) 私有入口;GenGetTopnSolutionsFuncForInductorGenTopnGetTilingFuncGenTopnSearchTilingSetupGenTopnCollectCandidates 等函数新增 use_measured_perf 参数,实测路径使用 best_perf 排序和 MeasuredTopnSelectorHelpers 去重。

  • 编译侧 PGO sidecar 构建链路: ascendc_compile.py 新增 link_pgo_executableextract_aicore_binarybuild_pgo_sidecarsfile_sha256build_pgo_manifestpublish_pgo_bundle 等函数;main 在 PGO 编译时先链接 tiling.so,再编译 runner + 提取 AICore 二进制,最后原子发布 bundle;link_host_target 在 PGO 场景下额外链接 ascendclruntime

  • 编译适配器的 PGO 源码分离与 MSPTI 配置: compile_adapter.py 新增 write_inductor_pgo_sources,将 PgoRunnerPgoDeviceSource 分割内容分别输出到 host 和 device 目录,并进行 ABI 验证;execute_compile 检测到 PGO 分割时自动配置 pgo_mspti_config;新增 get_inductor_pgo_mspti_config 支持 AUTOFUSE_MSPTI_PATH 环境变量或 asc_codegen_compile 委托。

  • callback 返回值检查与 tiling key 校验加固: tiling_code_gen_impl.cpp 中对 batch_callback 调用新增 != 0 返回值检查,失败时 return falsetest_e2e_load_abs_store.cppWrapperOnlyLaunch 的 tiling key 校验从 tiling_key == -1 改为 tiling_key < 0 || static_cast<uint64_t>(tiling_key) >= tiling_key_count

  • PGO runner 独立进程生成: GenInductorPgoRunner 生成完整的 MSPTI runner main 函数,包含 dlopen 加载 tiling.so、SetTopnPgoContext/ClearTopnPgoContext 上下文管理、aclrtBinaryLoadFromFile 加载 AICore 二进制、MSPTI profiling 设置/回收、AUTOFUSE_PGO_TOPN_V1 结果写入协议,以及 FindBestTilingKeyWrapperOnlyLaunchPGOGetProfilingBatch/PGOGetProfiling 等完整实测调用链。

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

代码审查

审查总结

各文件审查结果

文件 审查结论
autofuse/att/generator/solver_pass_gen/axes_reorder_solver/axes_reorder_solver_gen.cpp 无问题 — 仅移除未使用参数名的编译警告修复
autofuse/att/generator/tiling_code_gen_impl.cpp 无问题 — batch_callback 返回值检查改进,从忽略返回值变为检查非零
autofuse/codegen/codegen.cpp 无问题 — GE_CHK_BOOL_RET_STATUS 条件逻辑正确
autofuse/codegen/codegen_tiling.cpp P3: runner 中包含未调用的 dead code(WrapperOnlyLaunch 等)
autofuse/codegen/codegen_tiling.h 无问题 — 新增方法和常量声明,签名一致
autofuse/compiler/python/ascendc_compile.py 无问题 — PGO sidecar 构建、manifest、原子发布逻辑正确
autofuse/compiler/python/compile_adapter.py P2: stage="all" 时 PGO sidecar 不构建;P3: preload_files 未被消费
autofuse/tests/st/backend_e2e/inductor_tail_brc_tail_reduce_test/CMakeLists.txt 无问题
autofuse/tests/st/backend_e2e/inductor_tail_brc_tail_reduce_test/inductor_tail_brc_tail_reduce_backend_generate.cpp 无问题
autofuse/tests/st/backend_e2e/inductor_tail_brc_tail_reduce_test/test_e2e_inductor_tail_brc_tail_reduce_split_compile.cpp 无问题
autofuse/tests/st/backend_e2e/pgo_add_abs_inductor_test/CMakeLists.txt 无问题
autofuse/tests/st/backend_e2e/pgo_add_abs_inductor_test/pgo_add_abs_inductor_backend_generate.cpp 无问题
autofuse/tests/st/backend_e2e/pgo_add_abs_inductor_test/pgo_measured_topn_fake_callback_main.cpp 无问题 — 测试辅助代码
autofuse/tests/st/backend_e2e/pgo_add_abs_inductor_test/test_e2e_pgo_add_abs_inductor_split_compile.cpp 无问题
autofuse/tests/st/python/test_inductor_pgo_compile_flow.py 无问题
autofuse/tests/ut/att/testcase/generator/core/att_generator_unittest.cpp 无问题 — 期望字符串同步更新
autofuse/tests/ut/att/testcase/solver_pass_gen/axes_reorder_gen/test_axes_reorder_gen.cpp 无问题 — 断言同步更新
autofuse/tests/ut/codegen/test_codegen_tiling.cpp 无问题
autofuse/tests/ut/e2e/test_e2e_load_abs_store.cpp 无问题 — tiling_key 边界检查改进
autofuse/tests/ut/python/test_ascendc_compile.py 无问题
autofuse/tests/ut/python/test_compile_adapter.py 无问题

问题统计

  • P0: 0
  • P1: 0
  • P2: 1 个(stage="all" 不构建 PGO sidecar)
  • P3: 2 个(runner dead code、preload_files 未使用)

整体风险评估

此 PR 整体质量较高,代码生成链路设计清晰(proxy → posix_spawn → runner → dlopen tiling.so → MSPTI 实测)。三个问题均不涉及生产运行时正确性:P2 仅在未预期的 stage="all" 调用路径下触发,P3 两个为代码质量改进建议。核心逻辑(PGO sidecar 构建、manifest 校验、proxy 超时/kill、结果解析边界检查、batch_callback 返回值检查)均无缺陷。建议在合入前处理 P2(stage="all" 时报错或实现完整构建),两个 P3 可在后续迭代中优化。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
CANN-robotCANN-robot成员
29 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
29 天前 评论:

CLA Signature Pass

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

likedislike
此处折叠了284条消息 查看更多
CANN-robotCANN-robot成员
21 天前 删除了label:ci-pipeline-running
CANN-robotCANN-robot成员
21 天前 添加了label:ci-pipeline-passed
张德鹏成员
21 天前 评论:

/approve

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