| Files | Last commit | Last update |
|---|---|---|
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
fix(dfx): Add memory events and change moduleid to PyPTO. Co-authored-by: meiqing2<meiqing2@hisilicon.com> # message auto-generated for no-merge-commit merge: !6215 merge mtr1 into master fix(dfx): Add memory events and change moduleid to PyPTO. Created-by: meiqing2 Commit-by: meiqing2 Merged-by: cann-robot Description: PR 应该避免没有描述信息,描述信息的最重要的作用是描述修改意图,应该清晰明了。例如:标题中包含了 fix 标签,如果没有描述信息,就需要Review人员猜测PR具体修复了哪些内容,或者怀疑修改的必要性。如果PR和特定的Issue相关,可以单独包含一行 Related Issue: 放到描述信息最后。 下面是一个典型的PR描述信息: The origin issue template brings too many noise, it's not suitable for git message. eg: comments are not removed, labels are redundant as the PR tile already contains the tag. The guide gives a clear suggestion on how to write the PR body. Related Issue: #1234,#5678 更多描述信息见:[Commit Message 规范](../docs/zh/contribute/pull-request.md#commit-message) See merge request: cann/pypto!6215 | 2 months ago | |
feat(machine): Migrate aicore print to host-side management for capture Co-authored-by: wan_jianfen<wanjianfen@hisilicon.com> # message auto-generated for no-merge-commit merge: !5216 merge feat/aicore-print-host-mgr into master feat(machine): Migrate aicore print to host-side management for capture Created-by: wan_jianfen Commit-by: wan_jianfen Merged-by: cann-robot Description: feat(machine): Migrate aicore print to host-side management for capture The aicore print log mechanism was managed on the device side (AICPU) via AicoreLogManager, which reads logs synchronously inside AiCoreManager::RunTask. This breaks Stream recording semantics in capture (aclgraph) mode and couples print buffer management with scheduling logic. This PR moves aicore print buffer management to the host side. Changes: - Add AicorePrintHostManager: host allocates per-core HBM ring buffers, injects addresses into shakeBuffer[5] via H2D, and D2H-copies + decodes TLV records to host log after task sync - Integrate AicorePrintHostManager into DeviceRunner; skip sync and dump in capture mode to preserve recording - Skip debug sync and data-dump sync in DeviceLauncher when in capture mode - Remove device-side AicoreLogManager, InitLogger/DumpAicoreLog/logger_ from AiCoreManager, and shakeBuffer write from AiCoreHal - Add Newline record type and EncodeNewline for per-log separation; support BindHostBuffer/GetConsumedTail for host-side decoding in AicoreLogger Related Issues: #2846 See merge request: cann/pypto!5216 | 3 months ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
feat(Machine): Add FedProf and AicpuProf for machine profiling Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !6980 merge pgo-framework into master feat(Machine): Add FedProf and AicpuProf for machine profiling Created-by: yinjie27_ Commit-by: Jie Yin Merged-by: cann-robot Description: feat(Machine): Add FedProf and AicpuProf for machine profiling Rework the machine-side profiling data path around a unified host profiler (FedProf). The old free-function dump machinery is replaced by a class-owned design; profiling buffers are allocated once at device init and addressed directly through DeviceArgs, so device cores no longer rely on the sharedBuffer slot to locate their per-core prof data. Changes: - Add the FedProf singleton: owns per-core prof buffers, the AICPU launch-round prof buffer, the dump thread and JSON dumping; split into fed_prof.{h,cpp}, fed_prof_aicpu_prof.cpp and fed_prof_io_json.{h,cpp}; remove dump_device_perf.* and runtime/runner/device_perf.* - Add DeviceArgs::perCoreProfAddr / aicpuProfAddr; per-core prof slots are addressed as perCoreProfAddr + idx * FED_PROF_PER_CORE_PROF_ALIGNED_SIZE, and aicore entry/dfx/drco, the AICPU task manager, AICoreHAL and the ESL model are updated accordingly; SHAK_BUF_PER_CORE_PROF_INDEX is removed - Allocate per-core prof data as a single contiguous 512B-aligned block at device init (DeviceDfx::Init -> FedProf::PerCoreProfAllocateData), gated by DUMP_DEVICE_PERF / HostProf / runtime debug mode, idempotent and released together with the AICPU prof buffer - Add AICPU launch-round profiling (FedProfAicpuProf): dump thread, swimlane JSON and machine_perf_trace.py integration, output via KEY_AICPU_PERF_GRAPH_PATH - Reorganize device headers: add aikernel_profile.h (prof structures and sizes) and aikernel_launch.h (KernelArgs/KernelSharedBuffer/TaskEntry/ParallelDevTask moved out of aicpu_common.h); rename aicpu_perf.h to aikernel_profile_aicpu.h and update the CMake install list - Update UT/ST cases, the ESL model hook and launcher call sites to the new interfaces Commits: - feat(Machine): Add FedProf as the profiler - feat(Machine): Add AicpuProf for profiling - feat(Machine): Normalize AicpuProf - feat(Machine): Normalize profiling allocation See merge request: cann/pypto!6980 | 1 month ago | |
fix(machine): report AICPU exceptions through the error queue Co-authored-by: W4termy<wangmingyang21@huawei.com> # message auto-generated for no-merge-commit merge: !6099 merge fix/aicpu-exception-report into master fix(machine): report AICPU exceptions through the error queue Created-by: W4termy Commit-by: W4termy Merged-by: cann-robot Description: 异常回调函数中异常信息入队后,直调outputerrormessage,将首条报错打屏 See merge request: cann/pypto!6099 | 2 months ago | |
fix(machine): report AICPU exceptions through the error queue Co-authored-by: W4termy<wangmingyang21@huawei.com> # message auto-generated for no-merge-commit merge: !6099 merge fix/aicpu-exception-report into master fix(machine): report AICPU exceptions through the error queue Created-by: W4termy Commit-by: W4termy Merged-by: cann-robot Description: 异常回调函数中异常信息入队后,直调outputerrormessage,将首条报错打屏 See merge request: cann/pypto!6099 | 2 months ago | |
fix(runtime): dump tensors for extended AICore launch arguments Co-authored-by: hubowen<hubowen17@huawei.com> # message auto-generated for no-merge-commit merge: !7311 merge fix/aicerr_noinput into master fix(runtime): dump tensors for extended AICore launch arguments Created-by: W4termy Commit-by: hubowen Merged-by: cann-robot Description: kernel启动参数扩展,argsize将会大于56,而异常dump严格校验argsize=56导致dump流程被跳过 See merge request: cann/pypto!7311 | 24 days ago | |
style(all): Apply additional clang-format corrections across codebase Co-authored-by: junjieHuang<huangjunjie38@h-partners.com> # message auto-generated for no-merge-commit merge: !4883 merge style-cpp into master style(all): Apply additional clang-format corrections across codebase Created-by: junjieHuang Commit-by: junjieHuang Merged-by: cann-robot Description: Apply additional clang-format corrections across codebase See merge request: cann/pypto!4883 | 4 months ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
feat(Machine): Add pgo frontend Co-authored-by: Snow__Land<songyangyang18@huawei.com> # message auto-generated for no-merge-commit merge: !7091 merge pgo-framework into master feat(Machine): Add pgo frontend Created-by: yinjie27_ Commit-by: Snow__Land Merged-by: cann-robot Description: # feat(Machine): Add pgo frontend ## 一、背景与目标 关联 Issue:[cann/pypto #3059](https://gitcode.com/cann/pypto/issues/3059) PyPTO 在 man-in-loop 调优时,可调旋钮粒度很细(具体到每个 leaf task 的依赖、每个 root 的内存占用等),人工调优成本高。PGO(Profiling Guided Optimization)的目标是:一次运行把运行期数据记录到本地,下一次编译 / launch 时读回本地数据作为优化输入。 本 PR 交付其中的**前半段**: 1. **前端接口**:算子级开关 experimental_pgo_path / experimental_pgo_update,通过 @pypto.frontend.jit(generic_options={...}) 配置; 2. **数据导出**:launch 同步完成后,把 FedProf 记录到的运行期数据按 prof 类型导出为 <pgo_path>/<类型>.json。 数据的**读取与消费**(pass / machine 侧按 PGO 数据调参)不在本 PR 内,由使用方按自己作用域里的选项处理。 ## 二、设计要点(关键决策) | 决策 | 原因 | |---|---| | 选项只走**带作用域**的 ConfigManagerNg(前缀 generic.),**不写**全局 config / run-data | PGO 选项是**算子级**的;放全局 config 会让不同算子的数据挤在同一个配置项里,互相覆盖、难以区分 | | experimental_pgo_path **没有默认值**,必须显式指定 | 与上一条一致:不应该存在"全局默认 PGO 文件"这种东西 | | 选项在**每次 launch** 重新读取(SetDevPerfAddr 内),而不是进程初始化时读一次 | 前端每个算子调用都有自己的作用域;只读一次会漏掉后续算子 | | 导出产物**按 prof 类型分文件**(per_core.json / aicpu.json) | 与 C++ 侧 fed_prof_name_list(按 FedProfKind)一一对应,使用方按类型取用 | | 导出时机 = **launch 同步之后**(DeviceLauncher::LaunchKernel 尾部) | prof 数据由 device 侧异步写入,必须同步后才能读到完整内容;LaunchKernel 是所有 launch 的唯一收口(前端 LaunchKernelTorch 与 DeviceRunOnce 都经过它) | | 记录侧**只记录**:不区分算子、不按算子切分数据 | 使用方决定怎么用;额外记账只会引入"多个算子路径互相覆盖"这类语义问题 | | 无 PGO 请求时**零开销** | IsPgoDumpRequested() 判断为空即跳过同步与导出 | ## 三、改动明细 | 文件 | 改动 | |---|---| | python/pypto/config.py | 新增 generic_options 选项组(_Options.INIT_FIELDS / PREFIX_MAP,前缀常量 _GENERIC_PREFIX)、set_generic_options(experimental_pgo_path, experimental_pgo_update)、get_generic_options()、ConfigScope.get_generic_options() | | python/pypto/frontend/parser/entry.py | jit(..., generic_options=...) 透传到 JitCallableWrapper 并在 kernel 调用前 pypto.set_generic_options(**self._generic_options) | | framework/src/interface/configs/config_manager_ng.h | 新增键常量 GENERIC_PREFIX / EXPERIMENTAL_PGO_PATH / EXPERIMENTAL_PGO_UPDATE,以及按前缀读取的 GetGenericConfig<T>() | | framework/src/interface/configs/config_manager_ng.cpp | SetScope / SetGlobalConfig 允许"仅在 schema 中声明、未写进 tile_fwk_config.json"的 key(PGO 这类**无全局默认值、只能作用域设置**的选项需要);未声明的 key 仍被拒绝 | | framework/src/interface/configs/tile_fwk_config_schema.json | 新增 generic 组声明 experimental_pgo_path(输出目录,无默认值)/ experimental_pgo_update(bool);schema 是这两个键的唯一声明处与类型校验来源 | | framework/src/interface/machine/device/tilefwk/aikernel_profile.h | FedProfKind 下新增 fed_prof_name_list[FED_PROF_KIND_MAX] = {"per_core", "aicpu"},作为导出文件名的唯一来源 | | framework/src/machine/runtime/runner/fed_prof_manager.h | 新增 PgoUpdatePath()(launch 期间同步选项、登记导出目录)、IsPgoDumpRequested()、PgoDump() 与成员 std::string pgoPath_ | | framework/src/machine/runtime/runner/fed_prof_manager_pgo.cpp(新增) | PGO 记录侧实现 | | framework/src/machine/runtime/launcher/device_launcher.cpp | SetDevPerfAddr() 内每次 launch 调 PgoUpdatePath();LaunchKernel() 尾部:非 capture 且 IsPgoDumpRequested() 时先 DynamicLaunchSynchronize() 再 PgoDump() | | python/tests/st/test_pgo.py(新增) | ST:用 1024x1024 fp32 / 32x32 tile 的 (A@A+A)@A+A 算子(与 test.py 一致)打开 PGO,校验 <tmp>/pgo 下产出 per_core.json、aicpu.json | | framework/src/utils/file_utils.cpp | 顺带修正 CreateDir 失败日志:errno → strerror(errno)(PGO 导出需要创建目录,失败信息可读性) | ### 记录/导出实现(fed_prof_manager_pgo.cpp) cpp void FedProfManager::PgoUpdatePath() { // 每次 launch(算子作用域生效期间)同步一次选项: // 仅在 experimental_pgo_update=true 时记下 experimental_pgo_path(导出目录) auto scope = ConfigManagerNg::CurrentScope(); if (scope == nullptr || !scope->HasConfig(std::string(GENERIC_PREFIX) + EXPERIMENTAL_PGO_PATH)) return; if (!scope->HasConfig(std::string(GENERIC_PREFIX) + EXPERIMENTAL_PGO_UPDATE) || !scope->GetGenericConfig<bool>(EXPERIMENTAL_PGO_UPDATE)) return; pgoPath_ = scope->GetGenericConfig<std::string>(EXPERIMENTAL_PGO_PATH); ... } void FedProfManager::PgoDump() { if (pgoPath_.empty()) return; (void)CreateDir(pgoPath_, true); // 枚举所有 prof 数据,逐类调用其 DumpJson 输出 FedProfLaunchArgs launchArgs{0, FED_PROF_DEV_TASK_MAX_COUNT}; for (uint32_t kind = FED_PROF_PER_CORE_PROF; kind < FED_PROF_KIND_MAX; ++kind) { auto& holder = profList_[kind]; if (holder == nullptr || holder->GetDevAddr() == 0) continue; // 该类数据未使能 std::vector<uint8_t> hostBuffer; std::vector<FedProfDataBase*> dataList; holder->DeviceDataGet(hostBuffer, dataList, args_, launchArgs); // D2H json pgoJson = json::array(); holder->DumpJson(pgoJson, hostBuffer, dataList, args_, launchArgs); // 复用 prof 的 DumpJson (void)SaveFile(pgoPath_ + "/" + fed_prof_name_list[kind] + ".json", pgoJson.dump(DUMP_JSON_INDENT)); } } ## 四、使用方式 python @pypto.frontend.jit( generic_options={"experimental_pgo_path": "/tmp/pgo", "experimental_pgo_update": True}, ) def kernel(...): ... # 等价写法 pypto.set_generic_options(experimental_pgo_path="/tmp/pgo", experimental_pgo_update=True) 产物: text /tmp/pgo/per_core.json # per-core prof 明细(leaf task 执行窗口等) /tmp/pgo/aicpu.json # aicpu prof 明细(CTRL/SCHED 阶段与 dev task 打点) 前提:PGO 数据来自 FedProf 记录,需要使能 prof 记录(DUMP_DEVICE_PERF=true,或 runtime debug),否则对应的 prof 数据未分配、不会产出对应文件。 ## 五、验证 **1) 编译**:cmake --build build -j48 全量通过(0 error)。 **2) 小算子 repro**(DUMP_DEVICE_PERF=true,128x128 加法 kernel)host 日志: text [PGO] PGO update requested to /tmp/repro_pgo_p0x1yebu/pgo [PGO] PGO data dumped to /tmp/repro_pgo_p0x1yebu/pgo/per_core.json [PGO] PGO data dumped to /tmp/repro_pgo_p0x1yebu/pgo/aicpu.json → dir listing: ['aicpu.json', 'per_core.json'] **3) ST 实跑**(gen / yj 环境,device 0): bash PATH=/root/miniconda3/envs/yj/bin:$PATH autodev pypto pyst -t python/tests/st/test_pgo.py # [Device[0]] PASSED python/tests/st/test_pgo.py::test_pgo_update_dump # 1 passed in 28.05s **4) 配置层**: - get_generic_options() 默认 {}(无全局默认值) - set_generic_options(experimental_pgo_path=..., experimental_pgo_update=True) 可正常回读 - 未在 schema 声明的 key 仍被拒绝:key: pass.not_a_declared_key does not exist. - 既有选项组(pass. / runtime. / debug. …)无回归 ## 六、影响面、开销与限制 - **选项无默认值**:不显式给 experimental_pgo_path 时不会有任何导出(记录侧直接返回)。 - **每次 launch 的额外开销(仅 PGO 开启时)**:PgoDump() 会做一次全量 D2H —— per-core 缓冲约 (nrAic + nrAiv + 1) × ~5.4 MiB(76 核算约 410 MB)+ aicpu 缓冲约 200 × 58 KB ≈ 11 MiB,并且导出前会显式同步一次。PGO 属于实验性调试能力,建议只在需要采集数据的那几次运行里打开。 - **数据粒度**:导出内容是该进程 prof 缓冲的聚合(轮次区间取 [0, FED_PROF_DEV_TASK_MAX_COUNT),空轮次由 DumpJson 跳过),同名文件每次导出覆盖;记录侧**不**按算子切分数据。 - **配置系统行为变化**:SetScope / SetGlobalConfig 现在接受"仅 schema 声明"的 key。这会放宽"必须先写进 tile_fwk_config.json"的限制,但仍然拒绝未声明的 key,配置校验能力不下降。 - 本 PR 未接入 PGO 数据的**消费侧**(pass / machine 读取并据此调参),后续按 #3059 继续。 See merge request: cann/pypto!7091 | 1 month ago | |
Normalized framework for pgo Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> Co-authored-by: Jie Yin<yinjie27@huawei.com> # message auto-generated for no-merge-commit merge: !7044 merge pgo-framework into master Normalized framework for pgo Created-by: yinjie27_ Commit-by: Jie Yin;LordOfMysteries Merged-by: cann-robot Description: # feat(machine): 重构 FedProf 为 Holder/Manager 实例化结构,并修复 CTRL 泳道只 dump 一轮的回归 ## 一、背景与问题 fed_prof(device 侧性能数据 dump)原实现是一个单例 FedProf:内部直接持有 perCoreProf_ / aicpuProf_ 两个裸 device 指针,dump 线程、轮次管理、JSON 序列化 全部堆在同一个类里;DeviceArgs 里也以 perCoreProfAddr / aicpuProfAddr 两个散落字段 传地址。新增一类 profile 需要同时改结构体、改分配逻辑、改 dump 分支,扩展成本高。 同时暴露出一个回归:DUMP_DEVICE_PERF=true 时 python/tests/st/test_ctrl_cpu_perf.py 断言 len(metrics) >= 30 失败,实际只解析出 **1 轮** CTRL 数据: AssertionError: expected >= 30 CTRL rounds, got 1 ... assert 1 >= 30 **根因链**: 1. DUMP_DEVICE_PERF=true ⇒ KernelBinary::GetEnableDumpDevPref() 恒为真, 于是**每次** LaunchKernel 都会走到 DeviceLauncher::SetDevPerfAddr()(device_launcher.cpp:658)→ FedProfManager::SetDebugEnable(); 2. SetDebugEnable() 每次都对**所有 core** 全量 memset per-core prof 元数据 (PerCoreProfResetMetadata()),把 device 侧 status.currLaunchRound 清零; 3. 而 currLaunchRound 只在 AICore 退出时 ++(aicore_entry_dfx.h:174), 因此连续 30 次 launch 后它仍然只有 **1**; 4. AicpuProfDumpSwimlaneCheck() 以 currLaunchRound 作为 dump 轮次上界 (fed_prof_manager_aicpu.cpp:101),FedProfLaunchAicpuDumpJson() 只遍历 [lastDumpRound, currLaunchRound)(fed_prof_io_json.cpp:181), 最终 dump 里只剩 round _0。 ## 二、改动总览 共 4 个提交,34 个文件。 ### 1. 数据结构:用 FedProfKind + 地址列表替代散落字段 framework/src/interface/machine/device/tilefwk/aikernel_profile.h - 新增 enum FedProfKind { FED_PROF_PER_CORE_PROF, FED_PROF_AICPU_PROF, FED_PROF_KIND_MAX } 与 struct FedProfDataBase {} 作为类型化基类; - FedProfPerCoreProf / FedProfAicpuProf 改为继承 FedProfDataBase。 framework/src/interface/machine/device/tilefwk/aicpu_common.h - DeviceArgs 中 perCoreProfAddr / aicpuProfAddr 两个字段合并为 uint64_t fedProfAddrList[FED_PROF_KIND_MAX]{},按 FedProfKind 索引。 ### 2. Host 侧:把单例拆成 Holder(数据面)+ Manager(控制面) - fed_prof.h / fed_prof.cpp → **删除**,拆为: - fed_prof_holder.h / fed_prof_holder.cpp:FedProfHolderBase 虚基类 + 模板 FedProfHolder<kind> + FedProfPerCoreProfHolder / FedProfAicpuProfHolder。 device buffer 的分配(DeviceDataAllocate)、清理(DeviceDataClear)、 D2H 搬运(DeviceDataGet<T>)、JSON 序列化(DumpJson<T> / DumpJsonAicpuProf) 全部收敛为按类型实现的虚接口;编排逻辑不再感知具体 profile 类型。 - fed_prof_manager.h / fed_prof_manager.cpp:FedProfManager 单例,负责 dump 线程生命周期、轮次判定(AicpuProfDumpSwimlaneCheck)、dump 编排与落盘。 - fed_prof_aicpu_prof.cpp → fed_prof_manager_aicpu.cpp。 - FedProfManager 内部用 std::unique_ptr<FedProfHolderBase> profList_[FED_PROF_KIND_MAX] 取代原来的两个具名成员,对外统一为 DeviceDataAllocate(FedProfKind), 新增 profile 类型只需在枚举和 profList_ 中登记一处。 - DeviceDfx::Init() 改为按 kind 遍历分配,并在拿到 devDfxArgAddr 后再次 InitDeviceArgs() 刷新 args_(device_dfx.cpp:57-70)。 - 顶层 CMakeLists.txt 的安装头文件清单同步更新(fed_prof.h → fed_prof_manager.h + fed_prof_holder.h)。 ### 3. Device 侧与调用方:地址来源统一切到 fedProfAddrList aicore_entry.h、aicore_entry_drco.h、device_sche.cpp、aicore_hal.h、 aicpu_task_manager.h、aicore_model_launcher.cpp、emulation_launcher.cpp、 kernel_binary.{h,cpp}、device_launcher.{h,cpp}、device_dfx.cpp、 framework/tests/st/utils/include/test_dev_func_runner.h - devArgs->perCoreProfAddr → devArgs->fedProfAddrList[FED_PROF_PER_CORE_PROF]; - devArgs->aicpuProfAddr → devArgs->fedProfAddrList[FED_PROF_AICPU_PROF]; - KernelBinary::InitMetaData() / GetEnableDumpDevPref() 改为按 kind 循环拷贝 / 判空。 ### 4. 关键缺陷修复:per-core 元数据不再被每轮清零 framework/src/machine/runtime/runner/fed_prof_manager.{h,cpp} - void PerCoreProfResetMetadata(const uint32_t coreId); + void PerCoreProfResetMetadata(bool multipleLaunch); - 外层 per-core 循环内移,函数自身负责 reset **全部** core; - 新增成员 bool perCoreProfDataInited_{false},在 multipleLaunch 下每个 core 只 reset 一次(恢复重构前的保护); - SetDebugEnable() 不再自己写循环,改为 PerCoreProfResetMetadata(AicpuProfDevAddr() != 0)——即 aicpu prof 使能 (DUMP_DEVICE_PERF=true)时传 true,否则传 false,保持「每次 launch 全量 reset」的原有语义。 修复后 status.currLaunchRound 随 launch 累加,dump 可覆盖全部 30 轮。 ### 5. 其它 - aikernel_define.h:UNUSED 由单参宏扩展为 1~16 变参宏 (UNUSED_SELECT 参数选择),满足 UNUSED(a, b, ...) 用法; 同时删除 framework/include/tilefwk/data_type.h 中重复的 UNUSED 定义。 - 626560468:析构时显式 AicpuProfReleaseData()(dump 线程 stop 之后), 修正 online 场景的资源释放顺序。 ### 6. UT 适配 test_device_perf.cpp(6 处调用点改 bool 实参,ResetMetadata_IndexOutOfRange 重命名为 ResetMetadata_MultipleLaunch)、test_dynamic_runner.cpp、 test_device_dfx.cpp、test_device_launcher_host/npu.cpp、 test_device_runner.cpp、test_dump_device_perf.cpp 等跟随接口调整。 ## 三、行为对比 | 场景 | 修复前 | 修复后 | |---|---|---| | DUMP_DEVICE_PERF=true(aicpu prof 使能)30 次 launch | 每次 launch 清零 currLaunchRound,dump 只含 1 轮 | 每 core 仅首次 reset,currLaunchRound 累加,dump 覆盖 30 轮 | | 仅 runtime debug / host prof(无 aicpu prof) | 每次 SetDebugEnable 全量 reset | 语义不变(multipleLaunch=false,仍每次全量 reset) | | 新增一类 profile | 需改结构体字段 + FedProf 成员 + dump 分支 | 加 FedProfKind 枚举项 + 一个 Holder 子类即可 | ## 四、验证 - **UT**(tile_fwk_utest,machine 模块,本机设备 0): 受影响的 14 个套件共 **43 例通过**,其中直接覆盖改动的 DevicePerfTest.*(含新增 ResetMetadata_MultipleLaunch)10/10 PASS, TestDeviceRunner.DevicePerf_SetDebugEnable、DeviceLauncherNpuTest.SetDevPerfAddr_*、 DumpDevicePerfTest.*、PerfEvtMgrTest.* / PerfettoMgrTest.* 等均 PASS。 另有 2 例(SetDevPerfAddr_CaptureMode、MiscNoOpFunctions_NoCrash)因本机 UT 运行环境未部署 libtilefwk_backend_server.so / pypto_op_info.json 而失败, 已确认抛错点在 SetDebugEnable 之前的条件求值阶段,与本次改动无关。 - **构建**:tile_fwk_runtime、tile_fwk_interface、tile_fwk_utest 编译通过。 - **待上板回归**:python/tests/st/test_ctrl_cpu_perf.py —— 期望 len(metrics) >= 30(本 PR 修复的目标用例);同时观察 mean/max EXEC_DYN、first DEV_TASK_BUILD 门限是否满足(该用例阈值按 ctrl cpu 效率标定,若仍有回退需单独定位)。 ## 五、影响面与风险 - **接口变更**:DeviceArgs.perCoreProfAddr / aicpuProfAddr → fedProfAddrList[FED_PROF_KIND_MAX],host/device 两侧同步修改, 属不兼容变更;生成侧、模拟器(aicore_model_kernel_meta_hook.h)与 ST 基建已同步。 - **文件结构变更**:fed_prof.h/.cpp 被删除并由 fed_prof_holder.* + fed_prof_manager.* 取代,需注意下游对旧头文件的直接 include(本仓已全部更新)。 - **风险点**:per-core 元数据 reset 时机的语义依赖 multipleLaunch 取值, 已在 UT 中分别覆盖 true / false 两条分支。 - **回滚**:本 PR 为独立重构 + 修复,回滚即回到 FedProf 单例实现。 See merge request: cann/pypto!7044 | 1 month ago | |
feat(Machine): Add pgo frontend Co-authored-by: Snow__Land<songyangyang18@huawei.com> # message auto-generated for no-merge-commit merge: !7091 merge pgo-framework into master feat(Machine): Add pgo frontend Created-by: yinjie27_ Commit-by: Snow__Land Merged-by: cann-robot Description: # feat(Machine): Add pgo frontend ## 一、背景与目标 关联 Issue:[cann/pypto #3059](https://gitcode.com/cann/pypto/issues/3059) PyPTO 在 man-in-loop 调优时,可调旋钮粒度很细(具体到每个 leaf task 的依赖、每个 root 的内存占用等),人工调优成本高。PGO(Profiling Guided Optimization)的目标是:一次运行把运行期数据记录到本地,下一次编译 / launch 时读回本地数据作为优化输入。 本 PR 交付其中的**前半段**: 1. **前端接口**:算子级开关 experimental_pgo_path / experimental_pgo_update,通过 @pypto.frontend.jit(generic_options={...}) 配置; 2. **数据导出**:launch 同步完成后,把 FedProf 记录到的运行期数据按 prof 类型导出为 <pgo_path>/<类型>.json。 数据的**读取与消费**(pass / machine 侧按 PGO 数据调参)不在本 PR 内,由使用方按自己作用域里的选项处理。 ## 二、设计要点(关键决策) | 决策 | 原因 | |---|---| | 选项只走**带作用域**的 ConfigManagerNg(前缀 generic.),**不写**全局 config / run-data | PGO 选项是**算子级**的;放全局 config 会让不同算子的数据挤在同一个配置项里,互相覆盖、难以区分 | | experimental_pgo_path **没有默认值**,必须显式指定 | 与上一条一致:不应该存在"全局默认 PGO 文件"这种东西 | | 选项在**每次 launch** 重新读取(SetDevPerfAddr 内),而不是进程初始化时读一次 | 前端每个算子调用都有自己的作用域;只读一次会漏掉后续算子 | | 导出产物**按 prof 类型分文件**(per_core.json / aicpu.json) | 与 C++ 侧 fed_prof_name_list(按 FedProfKind)一一对应,使用方按类型取用 | | 导出时机 = **launch 同步之后**(DeviceLauncher::LaunchKernel 尾部) | prof 数据由 device 侧异步写入,必须同步后才能读到完整内容;LaunchKernel 是所有 launch 的唯一收口(前端 LaunchKernelTorch 与 DeviceRunOnce 都经过它) | | 记录侧**只记录**:不区分算子、不按算子切分数据 | 使用方决定怎么用;额外记账只会引入"多个算子路径互相覆盖"这类语义问题 | | 无 PGO 请求时**零开销** | IsPgoDumpRequested() 判断为空即跳过同步与导出 | ## 三、改动明细 | 文件 | 改动 | |---|---| | python/pypto/config.py | 新增 generic_options 选项组(_Options.INIT_FIELDS / PREFIX_MAP,前缀常量 _GENERIC_PREFIX)、set_generic_options(experimental_pgo_path, experimental_pgo_update)、get_generic_options()、ConfigScope.get_generic_options() | | python/pypto/frontend/parser/entry.py | jit(..., generic_options=...) 透传到 JitCallableWrapper 并在 kernel 调用前 pypto.set_generic_options(**self._generic_options) | | framework/src/interface/configs/config_manager_ng.h | 新增键常量 GENERIC_PREFIX / EXPERIMENTAL_PGO_PATH / EXPERIMENTAL_PGO_UPDATE,以及按前缀读取的 GetGenericConfig<T>() | | framework/src/interface/configs/config_manager_ng.cpp | SetScope / SetGlobalConfig 允许"仅在 schema 中声明、未写进 tile_fwk_config.json"的 key(PGO 这类**无全局默认值、只能作用域设置**的选项需要);未声明的 key 仍被拒绝 | | framework/src/interface/configs/tile_fwk_config_schema.json | 新增 generic 组声明 experimental_pgo_path(输出目录,无默认值)/ experimental_pgo_update(bool);schema 是这两个键的唯一声明处与类型校验来源 | | framework/src/interface/machine/device/tilefwk/aikernel_profile.h | FedProfKind 下新增 fed_prof_name_list[FED_PROF_KIND_MAX] = {"per_core", "aicpu"},作为导出文件名的唯一来源 | | framework/src/machine/runtime/runner/fed_prof_manager.h | 新增 PgoUpdatePath()(launch 期间同步选项、登记导出目录)、IsPgoDumpRequested()、PgoDump() 与成员 std::string pgoPath_ | | framework/src/machine/runtime/runner/fed_prof_manager_pgo.cpp(新增) | PGO 记录侧实现 | | framework/src/machine/runtime/launcher/device_launcher.cpp | SetDevPerfAddr() 内每次 launch 调 PgoUpdatePath();LaunchKernel() 尾部:非 capture 且 IsPgoDumpRequested() 时先 DynamicLaunchSynchronize() 再 PgoDump() | | python/tests/st/test_pgo.py(新增) | ST:用 1024x1024 fp32 / 32x32 tile 的 (A@A+A)@A+A 算子(与 test.py 一致)打开 PGO,校验 <tmp>/pgo 下产出 per_core.json、aicpu.json | | framework/src/utils/file_utils.cpp | 顺带修正 CreateDir 失败日志:errno → strerror(errno)(PGO 导出需要创建目录,失败信息可读性) | ### 记录/导出实现(fed_prof_manager_pgo.cpp) cpp void FedProfManager::PgoUpdatePath() { // 每次 launch(算子作用域生效期间)同步一次选项: // 仅在 experimental_pgo_update=true 时记下 experimental_pgo_path(导出目录) auto scope = ConfigManagerNg::CurrentScope(); if (scope == nullptr || !scope->HasConfig(std::string(GENERIC_PREFIX) + EXPERIMENTAL_PGO_PATH)) return; if (!scope->HasConfig(std::string(GENERIC_PREFIX) + EXPERIMENTAL_PGO_UPDATE) || !scope->GetGenericConfig<bool>(EXPERIMENTAL_PGO_UPDATE)) return; pgoPath_ = scope->GetGenericConfig<std::string>(EXPERIMENTAL_PGO_PATH); ... } void FedProfManager::PgoDump() { if (pgoPath_.empty()) return; (void)CreateDir(pgoPath_, true); // 枚举所有 prof 数据,逐类调用其 DumpJson 输出 FedProfLaunchArgs launchArgs{0, FED_PROF_DEV_TASK_MAX_COUNT}; for (uint32_t kind = FED_PROF_PER_CORE_PROF; kind < FED_PROF_KIND_MAX; ++kind) { auto& holder = profList_[kind]; if (holder == nullptr || holder->GetDevAddr() == 0) continue; // 该类数据未使能 std::vector<uint8_t> hostBuffer; std::vector<FedProfDataBase*> dataList; holder->DeviceDataGet(hostBuffer, dataList, args_, launchArgs); // D2H json pgoJson = json::array(); holder->DumpJson(pgoJson, hostBuffer, dataList, args_, launchArgs); // 复用 prof 的 DumpJson (void)SaveFile(pgoPath_ + "/" + fed_prof_name_list[kind] + ".json", pgoJson.dump(DUMP_JSON_INDENT)); } } ## 四、使用方式 python @pypto.frontend.jit( generic_options={"experimental_pgo_path": "/tmp/pgo", "experimental_pgo_update": True}, ) def kernel(...): ... # 等价写法 pypto.set_generic_options(experimental_pgo_path="/tmp/pgo", experimental_pgo_update=True) 产物: text /tmp/pgo/per_core.json # per-core prof 明细(leaf task 执行窗口等) /tmp/pgo/aicpu.json # aicpu prof 明细(CTRL/SCHED 阶段与 dev task 打点) 前提:PGO 数据来自 FedProf 记录,需要使能 prof 记录(DUMP_DEVICE_PERF=true,或 runtime debug),否则对应的 prof 数据未分配、不会产出对应文件。 ## 五、验证 **1) 编译**:cmake --build build -j48 全量通过(0 error)。 **2) 小算子 repro**(DUMP_DEVICE_PERF=true,128x128 加法 kernel)host 日志: text [PGO] PGO update requested to /tmp/repro_pgo_p0x1yebu/pgo [PGO] PGO data dumped to /tmp/repro_pgo_p0x1yebu/pgo/per_core.json [PGO] PGO data dumped to /tmp/repro_pgo_p0x1yebu/pgo/aicpu.json → dir listing: ['aicpu.json', 'per_core.json'] **3) ST 实跑**(gen / yj 环境,device 0): bash PATH=/root/miniconda3/envs/yj/bin:$PATH autodev pypto pyst -t python/tests/st/test_pgo.py # [Device[0]] PASSED python/tests/st/test_pgo.py::test_pgo_update_dump # 1 passed in 28.05s **4) 配置层**: - get_generic_options() 默认 {}(无全局默认值) - set_generic_options(experimental_pgo_path=..., experimental_pgo_update=True) 可正常回读 - 未在 schema 声明的 key 仍被拒绝:key: pass.not_a_declared_key does not exist. - 既有选项组(pass. / runtime. / debug. …)无回归 ## 六、影响面、开销与限制 - **选项无默认值**:不显式给 experimental_pgo_path 时不会有任何导出(记录侧直接返回)。 - **每次 launch 的额外开销(仅 PGO 开启时)**:PgoDump() 会做一次全量 D2H —— per-core 缓冲约 (nrAic + nrAiv + 1) × ~5.4 MiB(76 核算约 410 MB)+ aicpu 缓冲约 200 × 58 KB ≈ 11 MiB,并且导出前会显式同步一次。PGO 属于实验性调试能力,建议只在需要采集数据的那几次运行里打开。 - **数据粒度**:导出内容是该进程 prof 缓冲的聚合(轮次区间取 [0, FED_PROF_DEV_TASK_MAX_COUNT),空轮次由 DumpJson 跳过),同名文件每次导出覆盖;记录侧**不**按算子切分数据。 - **配置系统行为变化**:SetScope / SetGlobalConfig 现在接受"仅 schema 声明"的 key。这会放宽"必须先写进 tile_fwk_config.json"的限制,但仍然拒绝未声明的 key,配置校验能力不下降。 - 本 PR 未接入 PGO 数据的**消费侧**(pass / machine 读取并据此调参),后续按 #3059 继续。 See merge request: cann/pypto!7091 | 1 month ago | |
fix(dfx): Add memory events and change moduleid to PyPTO. Co-authored-by: meiqing2<meiqing2@hisilicon.com> # message auto-generated for no-merge-commit merge: !6215 merge mtr1 into master fix(dfx): Add memory events and change moduleid to PyPTO. Created-by: meiqing2 Commit-by: meiqing2 Merged-by: cann-robot Description: PR 应该避免没有描述信息,描述信息的最重要的作用是描述修改意图,应该清晰明了。例如:标题中包含了 fix 标签,如果没有描述信息,就需要Review人员猜测PR具体修复了哪些内容,或者怀疑修改的必要性。如果PR和特定的Issue相关,可以单独包含一行 Related Issue: 放到描述信息最后。 下面是一个典型的PR描述信息: The origin issue template brings too many noise, it's not suitable for git message. eg: comments are not removed, labels are redundant as the PR tile already contains the tag. The guide gives a clear suggestion on how to write the PR body. Related Issue: #1234,#5678 更多描述信息见:[Commit Message 规范](../docs/zh/contribute/pull-request.md#commit-message) See merge request: cann/pypto!6215 | 2 months ago | |
refactor(machine): Remove DeviceRunner and redistribute init flow Co-authored-by: wan_jianfen<wanjianfen@hisilicon.com> # message auto-generated for no-merge-commit merge: !5426 merge refactor/remove-device-runner into master refactor(machine): Remove DeviceRunner and redistribute init flow Created-by: wan_jianfen Commit-by: wan_jianfen Merged-by: cann-robot Description: refactor(machine): Remove DeviceRunner and redistribute init flow DeviceRunner was a god-class singleton that mixed device init, HostProf management, DevicePerf management, stream synchronization, and AiCpu .so loading. This PR dissolves it entirely, redistributing each responsibility to its natural module. Changes: - Move init flow (InitDeviceArgs / InitDeviceArgsCore / GetAicoreRegs / InitAiCpuSoBin / SetAiCpuSoOverride) into KernelBinary with static g_deviceArgs + std::call_once guard (EnsureInitialized / InitMetaData / GetEnableDumpDevPref) - Move HostProf methods (SetHostProfFunction / GetHostProfType / ReportHostProfInfo) into HostProf::GetInstance() singleton - Move DevicePerf methods (ResetPerData / SyncProfData / SetDebugEnable) into DevicePerf::GetInstance() singleton - Move DynamicLaunchSynchronize into DeviceLauncher - Move KernelLaunchInfo struct into device_launcher_types.h - Delete device_runner.h and device_runner.cpp - Update all call sites: device_launcher.cpp, kernel_bundle_launch.cpp, pypto_bundle_api.cpp, test_dev_func_runner.h, UT files - Remove device_runner.h from CMakeLists.txt install list Related Issues: #2967 See merge request: cann/pypto!5426 | 3 months ago | |
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Co-authored-by: LordOfMysteries<duhongxiang@huawei.com> # message auto-generated for no-merge-commit merge: !7025 merge fix-ring-event-epoch into master feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena Created-by: LordOfMysteries Commit-by: LordOfMysteries Merged-by: cann-robot Description: # 需求设计:消减控制流 cache 路径上的 ctrl RelocIO 开销 > PR: https://gitcode.com/cann/pypto/pull/7025 > Commit: feat(machine): Cut ctrlflow cache IO Reloc overhead via host arena (1fd1e2ee2) ## 1. 背景与问题 控制流 cache 命中后,每次 launch 仍要把本轮 **incast/outcast 绝对地址**交给叶子任务。原先主要开销在 **AICPU ctrl 热路径**:GELaunchRunCached 对每个 cached DevTask 调用 RelocIncastOutcastTask,按 rawTensorAddrBackup 描述符解析并**写回共享内嵌 IO 表**。 问题: - RelocIO 落在 ctrl 热路径,与 stitch/restore 争用同一条 AICPU-CTRL。 - 写共享内嵌表时,若与 early-launch / 上一轮 AICore 读侧重叠,存在改表竞态风险。 - 每轮 O(全部 I/O 槽) 的解析无法被 Host 侧提前完成。 ## 2. 目标与约束 | 类型 | 内容 | |------|------| | **主目标** | cache replay 跳过 per-task RelocIncastOutcastTask;RelocIO 挪到 Host Materialize(随路 arena,与 AICore launch-args 同寿命) | | **隐含约束** | **不增加 AICore 热路径损失**:无多核 BindIo、无 /8;叶子入口解析一次表起点;GetTensorAddr 仍是 base[i] | | **正确性** | aclgraph 每次捕获各自随路表;partial-cache 后 stitch 的 func 不进 layout(kIoAddressArenaOffsetInvalid),继续内嵌表 | | **兼容** | emulation / aicore-model / dump / shmem 等仍可读内嵌表时,走原 Reloc 回退 | | **工程化** | KernelBinary 与 offline kernel-bundle 共用同一套 AICore launch/arena 帮助函数,避免双份布局逻辑漂移 | ## 3. 改造前:IO 地址刷新与使用 text 每次 Launch Host 下发 tensor / workspace │ ▼ AICPU ctrl: RelocIncastOutcastTask │ 写回共享 DynFuncData 内嵌 rawTensorAddr[] ▼ AICore Leaf GetTensorAddr └── 读 funcData->rawTensorAddr[i] (共享 CF 元数据) | 阶段 | 行为 | |------|------| | Ctrl RelocIO | 热路径写共享内嵌 rawTensorAddr[] | | Leaf | 读 funcData->rawTensorAddr[i] | ## 4. 改造后:流程与元数据路径 ### 4.1 总览 **可变绝对地址**只进本轮随路 arena;共享 CF 只留静态 ioAddressArenaOffset;ctrl 在纯 arena 场景不再 RelocIO;叶子把表起点放进 CoreFuncParam.rawTensorAddr。 text 建 Cache(一次性) AssignIoAddressArenaLayout │ ▼ 静态 ioAddressArenaOffset + ioAddressArenaBytes 每次 Launch Host 准备 AICore 7-slot args(可预留 arena 尾部容量) AICPU GELaunchRunCached └── 有 arena 且无需内嵌刷新时:仅挂 startArgs,跳过 RelocIncastOutcastTask Host MaterializeIncastOutcastAddrs ──► AICore host-args 尾部 arena(AICPU 之后) AICore launch(无 BindIo) KernelEntry: ctx.ioAddressArena = inputs Leaf 入口: param.rawTensorAddr = arena + offset(或内嵌表) GetTensorAddr → param.rawTensorAddr[i] > **时序约束**:Materialize 必须在 AICPU 之后、AICore 之前(与 DeviceLauncher::LaunchKernel / LaunchBundleKernelOnce 一致),避免把绝对 host 指针提前泄漏进仍需设备侧使用的缓冲语义;H2D CF 在 rebase 之前完成。 ### 4.2 元数据访问路径 text 共享 CF(静态) 本轮随路(可变) 叶子私有 ┌─────────────────────┐ ┌──────────────────┐ ┌─────────────────────────┐ │ ioAddressArenaOffset│──(+base)─►│ ioAddressArena[] │───────►│ CoreFuncParam.rawTensorAddr │ │ 内嵌 rawTensorAddr │ │ 绝对地址 │ └───────────┬─────────────┘ │ (stitch / fallback) │──Invalid─►│ │ │ └─────────────────────┘ └──────────────────┘ ▼ GetTensorAddr: base[i] | 阶段 | 写 / 读 | |------|---------| | Assign | 写静态 ioAddressArenaOffset(元素下标,**不是字节 /8**) | | Host Materialize | **写本轮 arena**(承接原 ctrl RelocIO) | | Ctrl replay | 条件满足时只挂 startArgs,**不 RelocIO** | | Leaf 入口 | offset != Invalid → param.rawTensorAddr = arena + offset;否则用内嵌表 | | GetTensorAddr | 读 param.rawTensorAddr[i] | ### 4.3 共享 Host 实现(本批补充) 抽出 framework/src/machine/runtime/runner/aicore_launch_args.{h,cpp},供 **在线 KernelBinary** 与 **离线 kernel_bundle_launch** 共用: | API | 作用 | |-----|------| | PrepareAicoreLaunchArgsBuffer | 按 7-slot + 可选 arena 容量扩缓冲,填 launch slots,先关掉 host-input | | EnsureHostCtrlFlowCacheRebased | 对本轮 host CF mirror 做一次 rebase,供 Materialize 解引用 | | FillAicoreIoAddressArenaBuffer | 调用 MaterializeIncastOutcastAddrs 写入 arena,再打开 RtHostInputInfo(inputs) | LaunchBundleKernelOnce 同步拆成:准备 AICPU / 准备 AICore slots → 推 AICPU → Materialize arena → 推 AICore,避免超长一次性函数、并与 DeviceLauncher 时序对齐。 **RingEvent / slot 复用**:arena 只解决「本轮 IO 绝对地址落点」;host CF cache 槽位复用与 epoch 同步仍依赖既有 RingEvent 机制,**不能**因引入 arena 而删掉。 ### 4.4 关键代码锚点 **Host 物化**(KernelBinary::MaterializeIoAddressArenaForAicore / BundleMaterializeAicoreIoArena):走共享 helper → rebase → Materialize → 挂到 AICore args 尾部。 **Ctrl 跳过条件**(GELaunchRunCached): cpp const bool arenaLive = (devProg->ctrlFlowCacheAnchor->ioAddressArenaBytes != 0); const bool needEmbeddedIo = (devProg->devArgs.hostPid != 0) || (startArgs->commGroupNum != 0); if (arenaLive && !needEmbeddedIo) { dynTask->dynFuncDataList->startArgs = startArgs; // 跳过 Reloc } else { RelocIncastOutcastTask(...); // dump/shmem/无 arena 回退 } **叶子解析**(aicore_entry.h): cpp __gm__ uint64_t* rawTensorAddr = funcData->rawTensorAddr; if (funcData->ioAddressArenaOffset != kIoAddressArenaOffsetInvalid) { rawTensorAddr = ctx->ioAddressArena + funcData->ioAddressArenaOffset; } CoreFuncParam param = { ..., rawTensorAddr }; **热路径**(aicore_runtime.h):GetTensorAddr 改为读 ctx->rawTensorAddr[i],指令形态仍是基址+下标。 ## 5. 与原 Reloc / Workspace 的关系 | 地址类型 | Arena 是否覆盖 | 说明 | |----------|----------------|------| | 用户 Input/Output 槽 | ✅ | Materialize 时按 index 取本轮绝对地址 | | Backup 中 Workspace kind 槽 | ✅ | Materialize 时 +wsShift 写成绝对地址进 arena | | DynFuncData.workspaceAddr / stack / runtime WS **基址** | ❌ | 仍走 TaskAddr*Workspace / TaskAddrRelocWorkspace | | LOCAL tensor | ❌ | AICore:workspaceAddr + offset,不经 rawTensorAddr | 原 RelocIncastOutcastTask:在 device ctrl **原地**把 backup 解到内嵌表;现与 Materialize 共用 ResolveLeafIncastOutcastAddrs,区别是目标缓冲(内嵌 vs arena)与执行位置(ctrl vs host)。 ## 6. 兼容矩阵 | 场景 | 行为 | |------|------| | Full / partial cache(已 Assign layout) | Host 物化;ctrl 可跳过 RelocIO;叶子用 arena;无 BindIo | | 同 launch 后续 device stitch | ioAddressArenaOffset = Invalid,走内嵌表 | | ioAddressArenaBytes == 0 / emulation / aicore-model | 清 arena 标记;ctrl Reloc 刷新内嵌表并置 Invalid | | dump(hostPid != 0)或通信(commGroupNum != 0) | 仍 Reloc 内嵌表(AICPU 侧仍读嵌入地址) | | aclgraph 多次捕获 | 各自随路 arena,互不影响 | ## 7. 主要改动清单 1. **Host arena**:AssignIoAddressArenaLayout / MaterializeIncastOutcastAddrs;launch 挂 AICore 随路 arena 2. **共享 helper**:新增 aicore_launch_args.{h,cpp};KernelBinary + kernel_bundle_launch 共用 Prepare / Rebase / Fill 3. **Bundle 拆分**:LaunchBundleKernelOnce 拆步骤,Materialize 固定在 AICPU 之后 4. **Ctrl**:arena 存活且无需内嵌刷新时跳过 RelocIncastOutcastTask 5. **Device AICore**:ExecuteContext.ioAddressArena;叶子解析 param.rawTensorAddr;stitch 置 kIoAddressArenaOffsetInvalid 6. **统一解析**:ResolveLeafIncastOutcastAddrs 供 Materialize 与 Reloc 共用 7. **测试**:test_aicore_launch_args.cpp(布局 / Prepare / null-safe Fill);既有 ctrlflow cache / kernel_binary UT 同步适配 ## 8. 验收要点 - [ ] cache replay 路径 ctrl 泳道上 RelocIO 段消失或显著缩短(纯计算算子) - [ ] GetTensorAddr 热路径无额外除法 / 多核写共享指针 - [ ] dump / shmem / 通信场景精度与功能不回退 - [ ] aclgraph capture/replay 与 eager 数值一致 - [ ] stitch / non-cache 仍走内嵌表且地址正确 - [ ] KernelBinary 与 bundle 路径 arena 布局一致;AicoreLaunchArgsTest.* 本地通过 See merge request: cann/pypto!7025 | 1 month ago | |
fix(machine): fix log quality issues in machine module. Co-authored-by: chai-hw<chaizhengtong@huawei.com> # message auto-generated for no-merge-commit merge: !5679 merge pyptolog_fix into master fix(machine): fix log quality issues in machine module. Created-by: chai-hw Commit-by: chai-hw Merged-by: cann-robot Description: PyPTO machine 模块日志整改 See merge request: cann/pypto!5679 | 3 months ago | |
refactor(machine): Reduce private variable of DeviceRunner and decouple runtime agent Co-authored-by: arthur_morgan_hw<lipeijie@huawei.com> # message auto-generated for no-merge-commit merge: !3342 merge br_machine into master refactor(machine): Reduce private variable of DeviceRunner and decouple runtime agent Created-by: arthur_morgan_hw Commit-by: arthur_morgan_hw Merged-by: cann-robot Description: 1、内存相关的操作从runtime agent中抽离出去,只保留对寄存器信息的获取 2、梳理device runner的代码,减少非必要的成员变量 See merge request: cann/pypto!3342 | 6 months ago | |
feat(machine): Change aicpu kernel launch method Co-authored-by: wan_jianfen<wanjianfen@hisilicon.com> # message auto-generated for no-merge-commit merge: !5045 merge br_aicpuKernelLaunch into master feat(machine): Change aicpu kernel launch method Created-by: wan_jianfen Commit-by: wan_jianfen Merged-by: cann-robot Description: 改变kernel_launch 接口,更换成acl 接口 See merge request: cann/pypto!5045 | 3 months ago | |
style(all): Apply additional clang-format corrections across codebase Co-authored-by: junjieHuang<huangjunjie38@h-partners.com> # message auto-generated for no-merge-commit merge: !4883 merge style-cpp into master style(all): Apply additional clang-format corrections across codebase Created-by: junjieHuang Commit-by: junjieHuang Merged-by: cann-robot Description: Apply additional clang-format corrections across codebase See merge request: cann/pypto!4883 | 4 months ago | |
fix(machine): Fix dump perf data under multi-round by another way Co-authored-by: arthur_morgan_hw<lipeijie@huawei.com> # message auto-generated for no-merge-commit merge: !3917 merge br_runtime into master fix(machine): Fix dump perf data under multi-round by another way Created-by: arthur_morgan_hw Commit-by: arthur_morgan_hw Merged-by: cann-robot Description: 1. 原本这个问题,在跑多轮情况下,只有第一轮dump的原因是只有第一轮从C侧获取的runtime_debug_mode是1,后续多轮获取的都是0,导致只有第一轮会同步数据,后续不会同步数据。 2. 之前修复方式是还原之前的代码逻辑,即从py侧触发的kernel launch每次都强制去同步数据。 3. 现在进一步定位发现,py侧的options同步给C侧只有在触发编译的时候才会做,多轮的情况下,只有第一轮才会触发编译,后续多轮复用第一次的编译结果。因此修复方案即为同步options给C侧的调用,每次都会调用,而不是只在编译的时候才调用。 See merge request: cann/pypto!3917 | 5 months ago | |
style(all): Apply additional clang-format corrections across codebase Co-authored-by: junjieHuang<huangjunjie38@h-partners.com> # message auto-generated for no-merge-commit merge: !4883 merge style-cpp into master style(all): Apply additional clang-format corrections across codebase Created-by: junjieHuang Commit-by: junjieHuang Merged-by: cann-robot Description: Apply additional clang-format corrections across codebase See merge request: cann/pypto!4883 | 4 months ago | |
fix(machine): Modify module id Co-authored-by: anyangle<anyangle@huawei.com> # message auto-generated for no-merge-commit merge: !6646 merge module_id into master fix(machine): Modify module id Created-by: quadrate Commit-by: anyangle Merged-by: cann-robot Description: 本次变更属于 machine 模块的修复,目的是在设备侧内存分配调用中传入正确的模块标识。原先 DevMemoryPool::CreateNewBlock 与 DevMallocWithAlignSize 在调用 RuntimeMalloc 时,将模块 id 参数硬编码为 0,现统一改为 PYPTO,使这两处设备内存分配路径归属到正确的模块。 See merge request: cann/pypto!6646 | 2 months ago |
| Files | Last commit | Last update |
|---|---|---|
| 1 month ago | ||
| 1 month ago | ||
| 2 months ago | ||
| 3 months ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 2 months ago | ||
| 2 months ago | ||
| 24 days ago | ||
| 4 months ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 2 months ago | ||
| 3 months ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 1 month ago | ||
| 3 months ago | ||
| 6 months ago | ||
| 3 months ago | ||
| 4 months ago | ||
| 5 months ago | ||
| 4 months ago | ||
| 2 months ago |