| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
feat: 【编译性能优化】TilingFunc支持按需引用头文件 Co-authored-by: zhang_shengjie<804425610@qq.com> # message auto-generated for no-merge-commit merge: !1530 merge fix/tiling-func-split-header-resubmit into develop feat: 【编译性能优化】TilingFunc支持按需引用头文件 Created-by: zhang_shengjie Commit-by: zhang_shengjie Merged-by: cann-robot Description: # Pull Request ## 描述 TilingFunc 拆分编译场景中,每个翻译单元原先都会展开完整的 autofuse_tiling_func_common.h,多 group 并发编译时会重复解析与当前 .cpp 无关的标准库、运行时声明和结构体定义。本 PR 引入显式源码依赖模型,将公共内容拆为自包含原子头,并按生成代码的实际依赖渲染每个翻译单元,降低 host 编译的预处理、解析时间和峰值内存。 ## 修改方案 ### 显式依赖模型 新增 autofuse/common/tiling_source_dependencies.h,统一描述和渲染生成源码: | 接口 | 作用 | | --- | --- | | GeneratedCode | 保存代码正文及直接依赖 | | SourceDependencies | 分别登记标准头、外部头和生成头 | | AppendGeneratedCode | 合并正文及依赖并集 | | RenderTranslationUnit | 输出仅包含直接依赖的 .cpp | | RenderGeneratedHeader | 输出自包含原子头 | ### 自包含原子头与按需 include - 生成 State、Log、PGO、Solver、API 五类原子头,autofuse_tiling_data.h 保持独立。 - 原子头之间不互相 include,仅包含定义自身所必需的标准头和外部头。 - group、tail、solver、entry 根据实际生成的代码片段登记依赖;可选的变量关系、PGO runtime、CV fusion、workspace 表达式仅在使用对应能力时引入相关头文件。 - Operator Cache 的 HIT/MISS/SAVE 日志、命中计数和缓存老化行为保持不变。 ### TF、Inductor 与兼容路径 - TF 路径由 asc_codegen_compile.py 按固定 key 将原子头和 .cpp 直接落盘;原子格式不再落盘 autofuse_tiling_func_common.h。 - Inductor 路径通过 marker stream 传输固定 key,由 compile_adapter.py 解析并落盘;marker 不进入实际编译文件。 - 未携带 State key 的既有输入继续使用 common 格式;Base、Entry、Tail key 保留为历史输入兼容项。 mermaid flowchart LR A[ATT 与 Codegen 生成代码片段] --> B[登记正文与直接依赖] B --> C[统一合并去重与渲染] C --> D[State Log PGO Solver API 原子头] C --> E[按需 include 的翻译单元] D --> F{消费路径} E --> F F -->|TF| G[按固定 key 直接落盘] F -->|Inductor| H[marker 解析后落盘] F -->|Legacy| I[common 格式兼容] G --> J[host compile] H --> J I --> J ## 变更类型 - [ ] Bug 修复 - [x] 新功能 - [ ] 代码风格更新 - [x] 重构 - [x] 构建过程或辅助工具的变动 - [ ] 文档内容更新 ## 关联的 Issue 无。 ## 测试验证 | 范围 | 结果 | 覆盖点 | | --- | --- | --- | | ATT UT | 997/997 通过 | 依赖登记、原子头生成、Operator Cache 行为 | | Codegen UT | 699 通过,6 跳过 | renderer、TF/Inductor entry、marker、兼容路径和 CV fusion | | Optimize UT | 484 通过,8 跳过 | 既有功能回归 | | Common UT | 78/78 通过 | 既有功能回归 | | Python UT | 43/43 通过 | 固定 key 落盘、marker 解析和兼容路径 | | Inductor split compile E2E | 1/1 通过 | 多翻译单元独立编译、链接和运行 | | 静态检查 | 通过 | clang-format、git diff check、pre-commit、OAT | ## 编译性能收益 使用相同生成正文和相同翻译单元数量,以“每个翻译单元包含完整 common 头”为基线。测试环境为 CANN 9.1、GCC 9;串行编译 5 轮、8 并发编译 7 轮,取中位数;RSS 为单个编译进程峰值。 | 场景 | TU | 预处理行数 | 串行编译 | 8 并发编译 | max RSS | | --- | ---: | ---: | ---: | ---: | ---: | | TF 多 group | 8 | 741912 → 634617,下降 14.5% | 7.435 s → 5.548 s,提升 25.4% | 1.783 s → 1.279 s,提升 28.3% | 192172 KB → 154488 KB,下降 19.6% | | Inductor 多 group | 6 | 574836 → 452723,下降 21.2% | 6.792 s → 4.840 s,提升 28.7% | 2.014 s → 1.635 s,提升 18.8% | 225052 KB → 179872 KB,下降 20.1% | TF 和 Inductor 场景的预处理展开代码分别减少约 10.7 万行和 12.2 万行,include tree 节点分别下降 15.4% 和 21.4%。 五组原子头 marker 的协议开销为 460 bytes,相对单组 marker 净增约 368 bytes;在 unique topn 和 multi concat 的 host_impl 中分别约占 0.22% 和 0.15%。marker 解析后不进入 .h/.cpp,不影响 host 编译。 ## 核对清单 - [x] 代码遵循项目代码风格 - [x] 已完成相关自测和回归测试 - [x] 已更新相关测试 - [x] 标题使用合适的类型标签 - [x] 已阅读并遵守贡献指南 ## 变更范围 | 范围 | 说明 | | --- | --- | | autofuse/common/ | 显式依赖模型和统一 renderer | | autofuse/att/、autofuse/codegen/ | 登记直接依赖并生成自包含原子头和按需翻译单元 | | autofuse/compiler/python/ | TF/Inductor 原子文件落盘和既有格式兼容 | | autofuse/tests/ | 覆盖原子头、按需依赖、缓存行为、marker、兼容路径和多 group 编译 | See merge request: cann/graph-autofusion!1530 | 1 个月前 | |
refactor: 面向VV融合的求解器,删除L0/L2求解器 Co-authored-by: gcw_NLOuEjCz<1309002649@qq.com> # message auto-generated for no-merge-commit merge: !1684 merge dev_test into develop refactor: 面向VV融合的求解器,删除L0/L2求解器 Created-by: gcw_NLOuEjCz Commit-by: gcw_NLOuEjCz Merged-by: cann-robot Description: # Pull Request ## 描述 【重构】删除 att HighPerf Tiling 三段式架构( L0 → L2 → General)中的 L0/L2 求解器,收敛为面向 **VV(纯 Vector)融合**的单一 General(SEARCH_TILE)求解路径。 - **删除** L0/L2 求解器本体(l0/l2_solver_code.h)、代码生成器(l0/l2_solver_gen)、SolverType 枚举(L0_TILE/L2_TILE)与专属测试(12 文件 + 死副本) - **剥离** 3 个 live 模型(matmul/ffn/solver)中的 L0/L2 变量与 Cube 容量约束;4 个 tiling_func_*_main 同批联动 - **保留** SEARCH_TILE/General 求解、HardwareDef、axes_reorder、UB Tiling 机制;IsNeedSolver 简化为"可搜索变量 > 0" - 遵循"直接删除、不补偿"原则(无 SetSolvedVars 掩盖逻辑) ### 验收指标核对(6 项逐条) | # | 验收指标 | 结果 | |:--:|:--|:--:| | 1 | 编译通过,无 L0/L2 符号残留(L0TileSolver/L2TileSolver/L0TileSolverGen/L2TileSolverGen/ExecuteL0Solver/ExecuteL2Solver) | ✅ 通过 | | 2 | General 路径只生成 General 代码(solver_pass_gen/manager UT) | ✅ 通过 | | 3 | HighPerf ST 全绿(concat/matmul/ffn 单段 General 可执行、结果正确) | ✅ 通过 | | 4 | AxesReorder 默认路径 ST 全绿 | ✅ 通过 | | 5 | 求解质量改前/改后对比无显著退化 | ⚠️ 随方向变更失效 | | 6 | L0/L2 测试清理,CMake 无残留 glob | ✅ 通过 | > **综合结果**:UT **1024/1024** + ST **109/109** 全绿;6 项中 **5 项实证通过、1 项随方向变更失效**。 > **⚠️ #5 随方向变更失效(非"未执行")**:该验收点定义在 **CV 融合**场景——改前 L0/L2 求解器求解 L0/L2 块变量(tilem/tilen(L2)、basem/basen(L0A/L0C/L1)、basek(L0A/L0B));方向已改为 **VV 融合**,L0/L2 搜索变量已从 General 求解空间**移除、不再支持求解**,General 仅搜 stepka/stepkb(L1) → **改后无对比对象,对比前提不成立**。单段 General 正确性由 UT 1024/1024 + ST 109/109 覆盖。 ## 变更类型 <!-- [x] 表示选中 --> - [ ] 🐛 Bug 修复 - [ ] ✨ 新功能 - [ ] 💄 代码风格更新(格式化,局部变量) - [x] ♻️ 重构(既不修复错误也不增加功能的代码变动) - [ ] 📦 构建过程或辅助工具的变动 - [ ] 📝 文档内容更新 ## 关联的Issue https://gitcode.com/cann/graph-autofusion/issues/207 ## 如何测试 描述测试此变更的步骤和前提条件: 1. **编译**:cmake --build build/autofuse/tests/ut/att --target att_ut -j 8 + cmake --build build/autofuse/tests/st/att --target att_st -j 8;grep -rI 'L0TileSolver\|L2TileSolver\|ExecuteL0Solver\|ExecuteL2Solver' autofuse/ 应无残留 2. **UT**:att_ut --gtest_filter='-*Matmul*:*TilingFuncMain*'(1018/1018)+ att_ut --gtest_filter='*Matmul*'(6/6)→ **1024/1024** 3. **ST**:LD_PRELOAD=/usr/lib/aarch64-linux-gnu/libasan.so.5 ASAN_OPTIONS=detect_leaks=0 att_st → **109/109**(含 HighPerf concat/matmul/ffn + AxesReorder) ## 核对清单 <!-- [x] 表示选中 --> - [x] 我的代码遵循了项目的代码风格 - [x] 我已对代码进行了自测 - [x] 我已更新了相关的文档 - [x] 我在标题中使用了合适的类型标签(如:feat:, fix:) - [x] 我已经详细阅读了贡献指南(CONTRIBUTING.md),并遵守了其中的所有规定,包括但不限于commit message的格式、无效commit的合并等 ## 其他信息 在此添加任何其他关于本次 PR 的说明。 - **验证结果(如实)**:6 项验收指标 5 项实证通过、1 项**随方向变更失效**(#5 求解质量对比定义于 CV 融合场景;方向改 VV 后 L0/L2 搜索变量已删除、General 不再求解,无对比对象);单段 General 正确性由 UT 1024/1024 + ST 109/109 覆盖 - **风险点**:stepm/stepn(L1 作用域,related_scope={L1,CORENUM})作为 **General 搜索变量保留**,属 L0/L2 之外,未删除;无"L1 求解器",由 SEARCH_TILE 搜索 - 详细改动对照见 复现清单与逐文件对照.md See merge request: cann/graph-autofusion!1684 | 27 天前 | |
feat: Inductor支持PGO(复用TF流程) Co-authored-by: zhang_shengjie<804425610@qq.com> # message auto-generated for no-merge-commit merge: !1541 merge feature/inductor-mspti-pgo-a into develop feat: Inductor支持PGO(复用TF流程) Created-by: zhang_shengjie Commit-by: zhang_shengjie Merged-by: cann-robot Description: ## 描述 本 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 不修改。 ## 三、调用流程 mermaid 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 包,例如: bash source "${CANN_ROOT}/bin/setenv.bash" CANN 包内需包含: text tools/mspti/include/mspti.h tools/mspti/lib64/libmspti.so ### 4.2 开启 Inductor PGO Top3 bash 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 不新增前后端接口。 不需要配置: text AUTOFUSE_MSPTI_PATH LD_PRELOAD ### 4.3 关闭 PGO bash 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 同级目录下生成: text 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。  ## 变更类型 - [ ] Bug 修复 - [x] 新功能 - [x] 重构 - [x] 测试相关 - [x] 构建过程或辅助工具变动 - [ ] 文档内容更新 ## 变更统计 - 相对 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 采集语义 | ## 核对清单 - [x] 未修改 TorchAir/TorcHair 前后端接口 - [x] PGO 关闭路径保持原性能公式 TopN - [x] TF legacy PGO callback 未修改 - [x] PGO 失败按请求 TopN 回退 - [x] 默认解参与实测并受保护 - [x] 不需要额外 MSPTI 路径或手工 LD_PRELOAD 配置 See merge request: cann/graph-autofusion!1541 | 1 个月前 |
| 文件 | 最后提交记录 | 最后更新时间 |
|---|---|---|
| 1 个月前 | ||
| 27 天前 | ||
| 1 个月前 |