Pull Request已成功合入, 合并人@CANN-robot
(感谢 zhang_shengjie 的贡献)变更摘要
此 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/.h中TilingLib新增GenInductorPgoProxyEntry、GenInductorPgoProxySpawn、GenInductorPgoProxyManifest、GenInductorPgoProxySha256、GenInductorPgoProxyResultParser等函数,在 PGO true 时生成posix_spawn+waitpid+ 超时kill+ParseInductorPgoResult完整代理逻辑,公共GenerateTopnSolutions变为代理调用方。 -
私有 measured TopN 入口:
GenGenerateTopnSolutionsEntry新增entry_name参数,PGO true 时生成extern "C" int64_t GenerateMeasuredTopnSolutions(...)私有入口;GenGetTopnSolutionsFuncForInductor、GenTopnGetTilingFunc、GenTopnSearchTilingSetup、GenTopnCollectCandidates等函数新增use_measured_perf参数,实测路径使用best_perf排序和MeasuredTopnSelectorHelpers去重。 -
编译侧 PGO sidecar 构建链路:
ascendc_compile.py新增link_pgo_executable、extract_aicore_binary、build_pgo_sidecars、file_sha256、build_pgo_manifest、publish_pgo_bundle等函数;main在 PGO 编译时先链接 tiling.so,再编译 runner + 提取 AICore 二进制,最后原子发布 bundle;link_host_target在 PGO 场景下额外链接ascendcl和runtime。 -
编译适配器的 PGO 源码分离与 MSPTI 配置:
compile_adapter.py新增write_inductor_pgo_sources,将PgoRunner和PgoDeviceSource分割内容分别输出到 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 false;test_e2e_load_abs_store.cpp中WrapperOnlyLaunch的 tiling key 校验从tiling_key == -1改为tiling_key < 0 || static_cast<uint64_t>(tiling_key) >= tiling_key_count。 -
PGO runner 独立进程生成:
GenInductorPgoRunner生成完整的 MSPTI runnermain函数,包含dlopen加载 tiling.so、SetTopnPgoContext/ClearTopnPgoContext上下文管理、aclrtBinaryLoadFromFile加载 AICore 二进制、MSPTI profiling 设置/回收、AUTOFUSE_PGO_TOPN_V1结果写入协议,以及FindBestTilingKey、WrapperOnlyLaunch、PGOGetProfilingBatch/PGOGetProfiling等完整实测调用链。


代码审查
审查总结
各文件审查结果
| 文件 | 审查结论 |
|---|---|
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 可在后续迭代中优化。
⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。


描述
本 PR 使 Inductor 静态 shape 场景的
GenerateTopnSolutions可在开启 Autofuse PGO 时,复用 TF PGO 的候选生成、全核遍历、solver、完整 tiling data 归一和 MSPTI 实测统计能力,返回实测 TopN。不修改 TorchAir/TorcHair 调用流程和GenerateTopnSolutions接口。PGO 关闭时,Inductor 仍使用原有性能公式 TopN,不启动子进程,不执行全核 PGO 遍历。
一、主要解决的问题
GenerateTopnSolutions,但原实现只按性能公式排序,--autofuse_enable_pgo=true不会触发实际采样。二、修改内容
2.1 候选生成与选解
2.2 独立 PGO runner
tiling.so、PGO runner 可执行文件、device kernel binary 和 manifest。GenerateTopnSolutions的 proxy 校验 sidecar 协议、ABI、generation 和 SHA256,然后使用posix_spawn启动子进程。GenerateMeasuredTopnSolutions,通过有界二进制文件返回 TopN。2.3 编译产物与回退
tools/mspti发现;不需要AUTOFUSE_MSPTI_PATH,不需要用户手工设置LD_PRELOAD。proxy 创建 runner 时自动将libmspti.so及可用的libprof_common.so合入子进程LD_PRELOAD。2.4 采集语义对齐
MSPTI_ACTIVITY_KIND_KERNEL记录。三、调用流程
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 包内需包含:
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 不新增前后端接口。不需要配置:
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 当前支持边界
五、维测信息
5.1 关键日志
GenerateTopnSolutions enter: topn=...[PGO] MSPTI is unavailable, skip Inductor PGO sidecarsGenerateMeasuredTopnSolutions failedInductor PGO failed, fallback to modeled TopNInductor PGO runner or result parsing failed5.2 sidecar 产物
tiling.so同级目录下生成:manifest 可用于检查:
protocol/version/generationrunner_abi/proxy_abi/device_source_abitiling.so的 SHA2565.3 建议排查顺序
AUTOFUSE_FLAGS和前端传入 TopN。tools/mspti内容,避免混用多套 CANN lib。GenerateMeasuredTopnSolutions和 MSPTI record 数。六、文件结构与职责
autofuse/codegen/codegen_tiling_inductor_topn.cppcodegen_tiling_pgo_common.cppcodegen_tiling_pgo_memory.cppcodegen_tiling_pgo_search.cppcodegen_tiling_pgo_runtime.cppcodegen_tiling_inductor_pgo_proxy.cppcodegen_tiling_inductor_pgo_runner.cppautofuse/compiler/python/compile_adapter.pyautofuse/compiler/python/ascendc_compile.py七、测试与实测结果
7.1 回归
TestCodegenTiling定向回归7.2 A5收益实测
环境:Python 3.12.9、torch 2.12.0+cu130、torch_npu 2.12.0。

变更类型
变更统计
develop:39 个文件,+5268/-517。docs/目录修改。提交记录
994c971c4e698e4ae84c8f6c6ec8156fd0af8bdce723220a核对清单