Closed
[Requirement|需求建议]: 提供PyPTO Workspace 内存精细化调优旋钮 #3048
duanhaotianAdd on Aug 20Closed at Sep 14
Aug 20 Added label: requirement
Aug 20 Assign duanhaotian as responsible person
Aug 20 Associated pull request
Aug 20 Modify description of issue
Aug 20 Modify description of issue
Aug 21 Modify description of issue
duanhaotian
Aug 21 Comments:
Aug 21 Comments:
inner exclusive是否能合并
对外解释方案:
新PGO_json 替代jit形式呈现
核心:SKILL完善各种场景调优适配,目标性能;目标内存;配置和场景的适配
对外语义:内存,depth如何解释并行度
基于cache结果反馈调整内存


duanhaotian
Aug 27 Comments:
Aug 27 Comments:
模块一:Host 配置、精细预算和 memBudget 传递
模块目标
该模块把用户输入的三个“可容纳 RootFunction 深度”转换为三个运行时池的实际容量:
| 数组元素 | 对应池 | 单位需求 | 最终容量 |
|---|---|---|---|
d0 |
RootInner | rootInnerUnitBytes |
rootInnerUnitBytes × d0 |
d1 |
Inner-Temporal Outcast | assembleSlotsPerDepth |
assembleSlotsPerDepth × d1 个 Slot |
d2 |
Exclusive Outcast | exclusiveOutcastUnitBytes |
exclusiveOutcastUnitBytes × d2 |
Boundary Outcast 不属于精细配置,其 Slot 数仍由图拓扑固定计算。
修改后的模块流程图
flowchart TD
Json["tile_fwk_config.json / schema<br/>actual_stitch_function_num=[d0,d1,d2]"]:::added
Read["ConfigManager 读取 vector<int64_t>"]:::added
Shape{"长度是否为 3<br/>元素是否在 [0,1024]"}:::decision
RuntimeCfg["RuntimeWorkspaceConfig<br/>innerWorkspaceFunctionNum=d0<br/>outcastStitchFunctionNum=d1<br/>exclusiveOutcastStitchFunctionNum=d2"]:::added
Collect["CollectWorkspaceDesc<br/>收集每个 Root 原始需求和全局 Slot 类型"]:::legacy
Mode{"d0=d1=d2=0"}:::decision
Coarse["原有粗调路径<br/>由 max_workspace_kb 求统一 k_eff"]:::legacy
Required{"对实际需要的池<br/>对应 depth 是否大于 0"}:::decision
Units["计算单位需求<br/>max aligned RootInner<br/>max aligned Exclusive<br/>assemble slots per depth"]:::added
Layout["CalculatePreciseWorkspaceLayout<br/>三个池独立乘 depth"]:::added
DepthCfg["StitchDepthConfig<br/>形成统一决策结果"]:::added
Desc["回写 WorkspaceDesc 最终池容量<br/>及 config 诊断字段"]:::added
Apply["ApplyStitchDepthConfig"]:::added
TensorBudget["memBudget.tensor<br/>最终容量字段"]:::legacy
Control["memBudget.tensor.workspaceControl<br/>单位需求 + 配置深度 + 模式标记"]:::added
Metadata["runtimeOutcastPoolSize / cache depth 适配"]:::added
Serialize["DevAscendProgram 连续二进制"]:::legacy
Device["Device 根据 memBudget 注册 allocator"]:::legacy
Json -.-> Read --> Shape
Shape -->|通过| RuntimeCfg
Shape -->|失败| Error["配置错误"]:::decision
Collect --> Mode
RuntimeCfg -.-> Mode
Mode -->|是| Coarse --> DepthCfg
Mode -->|否| Required
Collect --> Units
Units -.-> Required
Required -->|通过| Layout --> DepthCfg
Required -->|失败| Error
DepthCfg -.-> Desc
DepthCfg -.-> Apply
Desc --> Apply
Apply --> TensorBudget
Apply -.-> Control
Apply -.-> Metadata
TensorBudget --> Serialize
Control -.嵌入 DevProg.-> Serialize
Metadata -.-> Serialize
Serialize --> Device
classDef legacy fill:#f5f5f5,stroke:#666,stroke-width:1px,color:#222
classDef added fill:#e6f4ff,stroke:#1677ff,stroke-width:3px,color:#102a43
classDef decision fill:#fff7e6,stroke:#d46b08,stroke-width:2px,color:#5c2b00


duanhaotian
Aug 27 Comments:
Aug 27 Comments:
模块二:Stitch Window 峰值采集和推荐计算
模块目标
精细配置将调优问题从“Workspace 总量应该设多少”转换为“三个 depth 各应该设多少”。因此需要运行时回答:
- 每个 Stitch Window 内三个池实际使用到多少;
- 整次 Launch 的最大使用量是多少;
- 峰值对应多少个 depth;
- 每增加一个 depth,会增加多少原始内存和最终对齐后总 Workspace。
6.2 修改后的模块流程图
flowchart TD
Macro{"ENABLE_PROFILE_GUIDED == 1"}:::decision
Init["DeviceWorkspaceAllocator::Init<br/>清零 WorkspaceTuningState"]:::added
Baseline["ResetTuningWindowPeaks<br/>记录新窗口基线"]:::added
Alloc["RootFunction 真实分配<br/>或 Slot 分配"]:::legacy
Sample["ObserveTensorPoolUsage(parallelId)"]:::added
Current["读取 current<br/>RootInner bytes<br/>Exclusive bytes<br/>InnerTemporal used slots"]:::added
Window["更新 windowUsagePeak"]:::added
Launch["更新 launchUsagePeak"]:::added
Submit["SubmitToAicoreAndRecycleMemory"]:::legacy
WindowLog["LogTuningUsage<br/>Pool + Tuning"]:::added
Depth["CeilDiv(peak, unit)<br/>跨 parallel 取最大"]:::added
Reset["窗口回收后<br/>ResetTuningWindowPeaks"]:::added
End["整次 Control Flow 结束"]:::legacy
Summary["LogTuningSummary<br/>Summary + Recommendation<br/>每 depth 内存影响"]:::added
Disabled["相关状态、调用和计算不参与编译"]:::added
Macro -->|否| Disabled
Macro -->|是| Init --> Baseline --> Alloc --> Sample
Sample -.-> Current
Current -.-> Window
Current -.-> Launch
Window -.-> WindowLog
Launch -.-> Summary
Sample --> Submit
Submit --> WindowLog --> Depth --> Reset --> Alloc
End --> Summary
classDef legacy fill:#f5f5f5,stroke:#666,stroke-width:1px,color:#222
classDef added fill:#e6f4ff,stroke:#1677ff,stroke-width:3px,color:#102a43
classDef decision fill:#fff7e6,stroke:#d46b08,stroke-width:2px,color:#5c2b00


duanhaotian
Aug 27 Comments:
Aug 27 Comments:
模块三:RootFunction 追加前预检与内存驱动 Submit
模块目标
精细模式下三个池深度彼此独立,不能再假设统一 Stitch 数达到阈值时三个池同时耗尽。运行时必须在追加 RootFunction 前根据当前真实余量判断:
- 当前 Root 的 Exclusive Outcast 连续空间是否足够;
- 当前 Root 立即需要的 Inner-Temporal Slot 是否足够;
- 创建当前 Root 的 Outcast 句柄所需 RuntimeOutcastTensor Item 是否足够。
如果不足,当前 Root 不能半分配、半失败,而应先 Submit 已完成的 Stitch Window,回收后从干净状态重试。
修改后的模块流程图
flowchart TD
Root["CallRootFunctionStitch<br/>动态表达式已求值"]:::legacy
Try["TryAllocateFunctionMemory"]:::added
Preflight["CanAllocateFunctionMemory"]:::added
Excl{"exclusiveOutcastWsMemoryRequirement<br/>能否由 Exclusive SeqPool 分配"}:::decision
Scan["扫描 slotList.isAssembleSlotNeedAlloc<br/>结合当前 Root outcast 映射"]:::added
Inner{"immediateInnerTemporalSlots<br/>是否小于等于 AvailableSlots"}:::decision
Items{"outcastSize 是否小于等于<br/>runtimeOutcastTensorPool.FreeItemNum"}:::decision
Real["真实分配 RootInner / Exclusive<br/>绑定 Incast / Outcast 地址"]:::legacy
Sample["可选峰值采样"]:::added
Stitch["Stitch 当前 Root 并 UpdateSlots"]:::legacy
Reason["记录 FunctionMemoryAllocFailure"]:::added
Empty{"当前 Stitch Window 是否为空"}:::decision
Error["报错:单个 Root 在空窗口仍无法容纳"]:::added
Submit["SubmitToAicoreAndRecycleMemory<br/>携带失败原因"]:::added
Freeze["冻结 Slot/Incast/Outcast 地址<br/>构造并下发 DeviceTask"]:::legacy
Recycle["RecycleTensorWorkspace<br/>Reset 顺序池并回收 Slot"]:::legacy
Retry["重新执行完整预检"]:::added
Root --> Try --> Preflight --> Excl
Excl -->|足够| Scan --> Inner
Excl -->|不足| Reason
Inner -->|足够| Items
Inner -->|不足| Reason
Items -->|足够| Real --> Sample --> Stitch
Items -->|不足| Reason
Reason --> Empty
Empty -->|是| Error
Empty -->|否| Submit --> Freeze --> Recycle --> Retry --> Try
classDef legacy fill:#f5f5f5,stroke:#666,stroke-width:1px,color:#222
classDef added fill:#e6f4ff,stroke:#1677ff,stroke-width:3px,color:#102a43
classDef decision fill:#fff7e6,stroke:#d46b08,stroke-width:2px,color:#5c2b00


Sep 1 Associated pull request
8 event messages are collapsed here。 See more
Sep 14 Added label: resolved
Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.
Background(背景信息)
当前问题
穿刺结果给出两层证据:
因此最终方案只保留两种清晰模式:
max_workspace_kb。Origin(信息来源)
machine内部
Benefit / Necessity(价值/作用)
最终目标:
Design(设计方案)
一、性能穿刺结果分析
1.1 统计口径
本次分析区分三类内存:
Tensor Workspace 近似组成:
1.2 全量 eager case 调优结果
调优准则:
总体结果:
代表算子结果:
1.3 gym 仓补充结果
kda / gdr
kimi / attenres
不同方向的性能拐点不同,说明配置必须由运行时真实需求和性能曲线共同决定,不能仅由 shape 或统一经验值推导。
1.4 穿刺结果分析
一:预算深度不等于实际并发需求
flash_attention_mha 的 rootInner:
预算按最坏单 root × stitch 深度放大,但实际同时存活的 root 只有约一半。
二:多类型 root 的 max 放大严重
GLM 包含约 28 类不同 root,单 root 需求从约 0.1MB 到 2.3MB。原预算把最重 root 乘到全部深度:
这类浪费不能靠继续降低总 Workspace 消除,否则会同步降低 kEff,先损失并行度。
三:Temporal outcast 预算与实际峰值差距极大
Temporal 按最坏深度线性预留,但运行时真正同时存活的 slot 极少。
1.5 为什么必须做精细化控制
粗调模式的本质:
穿刺中的性能断崖:
精细模式的目标是:
独立池控制原型的可达结果:
结论:精细控制的核心是基于实际执行时内存的实际使用情况来反馈控制我们输入端的预估内存
二、内存调优流程
2.1 总体调优流程
flowchart TD A[使用较大 max_workspace_kb] --> B[扫描 性能数据] B --> C{继续增大内存<br/>性能是否改善} C -->|是| A C -->|否| D[确定性能平台点] D --> E[开启 日志获取内存信息] E --> F[读取预算布局与运行时峰值] F --> G{submit 是否由内存触发} G -->|是| H[增大对应池或回到更大基线] G -->|否| I[反推 rootInner / Exclusive / Temporal] I --> J[加安全余量并一次性配置三个精细项] J --> K[复测内存、E2E、stitchCount、精度] K --> L{性能劣化是否在阈值内} L -->|否| M[按 submit 原因回调单个池] M --> K L -->|是| N[覆盖多 shape / 多路径] N --> O[跨样本取最大值并固化] O --> P[关闭 日志,正式验收]2.2 示例日志的组成
示例:
下图所示为执行glm_attention_l0_id0002_low_latency_uniform_capture算子时,调整不同内存池大小时,性能随时内存的变化。可以看出通过调整不同内存池大小能够获得较大的内存下降收益

提取文件分为五块:
ENCODE_AND_LAYOUTRUNTIME_WINDOWRUNTIME_SUMMARYSTITCH_AND_SUBMITPERFORMANCE当前提取结果
PERFORMANCE count=0,性能需从 xrun 结果或性能 Excel 补充。2.3 第一步:读编码预算
关键日志:
当前约 193.44MiB Context Workspace 的构成:
结论:InnerTemporalOutcast 占比最大,rootInner 次之。
2.4 第二步:确认基线没有被内存限制
判断:
stitchedSize=23 < k_eff=32;因此该运行可作为未受内存截断的峰值样本。
2.5 第三步:读运行时峰值
2.6 第四步:反推三个精细配置
rootInner
InnerExclusiveOutcasts
InnerTemporalOutcast
当前精细模式要求三个配置全部大于 0,因此本 case 的 Temporal 配置取最小值 1。
建议首轮保留余量:
runtime_options={ "max_workspace_kb": 200000, # 精细模式下仅作安全上限 "reuse_workspace_kb": 13664, "exclusive_outcast_stitch_function_num": 24, "outcast_stitch_function_num": 1, "workspace_tuning_log": 1, }预计:
接近观测下限的配置:
该配置缺少 shape/path 余量,不能直接作为生产值。
2.7 第五步:复测与回调
每轮只根据证据回调对应池:
reuse_workspace_kbexclusive_outcast_stitch_function_numoutcast_stitch_function_num验收条件:
三、配置项设计方案
3.1 仅保留两种模式
max_workspace_kb不存在 reuse-only、Exclusive-only、Temporal-only 或任意两项组合模式。
部分配置时直接报错:
3.2 配置项语义
max_workspace_kbreuse_workspace_kbexclusive_outcast_stitch_function_numexclusiveUnitBytes × depthoutcast_stitch_function_numassembleSlotsPerDepth × depth × slotBytes精细模式公式:
ControlFlow cache 和 runtime metadata 不属于两类 Tensor 池,为避免元数据不足,按:
保守配置,但不重新耦合两类 Tensor 申请量。
3.3 精细模式动态端到端流程
sequenceDiagram participant User as 用户配置 participant Encode as Encode预算 participant Program as DevAscendProgram participant Host as Host动态Shape participant Device as DeviceInitDyn participant Alloc as WorkspaceAllocator participant Stitch as StitchContext participant Submit as SubmitPipeline User->>Encode: rootInner KB + Exclusive depth + Temporal depth Encode->>Encode: 校验三个精细项必须全部开启 Encode->>Encode: 收集单root、exclusive unit、assemble slots、boundary Encode->>Encode: 分别计算三个池 + 固定boundary Encode->>Encode: 校验可选 max_workspace_kb 上限 Encode->>Program: 写入固定预算和两个独立depth Host->>Program: 求值动态 MaxOutcastMem Host->>Program: 按两个depth重新Rebalance并校验 Host->>Device: 下发最终预算 Device->>Program: 再次Rebalance,禁止静默降depth Device->>Alloc: 初始化rootInner/exclusive/boundary/temporal四个池 loop 每个root Alloc->>Alloc: preflight检查exclusive/temporal/boundary/metadata alt outcast与metadata足够 Alloc->>Alloc: 分配outcast和rootInner Alloc->>Stitch: rootInner地址重叠时添加复用依赖 else 当前窗口非空 Alloc->>Submit: 提交已有DeviceTask Submit->>Alloc: 回收task内资源 Alloc->>Alloc: 重试当前root else 当前窗口为空 Alloc-->>Device: 单root无法容纳,明确报错 end end Device->>Alloc: 输出launch峰值与三个独立推荐值3.4 已适配的运行时能力
rootInner 复用
outcast 不足触发 submit
动态 Shape 重平衡
MaxOutcastMem求值后重新计算物理字节数;max_workspace_kb安全上限时失败,不静默降低 depth。日志与推荐
boundary 安全策略
四、待评估问题
4.1 推荐值呈现:depth 还是直接内存大小
方案 A:全部呈现池大小
优点:
问题:
方案 B:三个内存池均按照 depth num呈现
优点:
问题:
建议
日志同时展示:
即“配置用 depth,汇报用 depth + bytes 双呈现”。
4.2 配置模式是否只保留两种
当前设计:
原因:
max_workspace_kb;4.3 内存池是否考虑合并
当前四类 Tensor Workspace 的职责
SeqWsAllocatorSeqWsAllocatorWsSlotAllocatorWsSlotAllocator两个顺序池虽然使用相同的 allocator 类型,但“allocator 类型相同”不等于“生命周期相同”。考虑到地址复用rootInner reset 后,新 root 可能获得与旧 root 相同的物理地址。
旧 root 对应的 AICore 操作此时未必已经执行,因此不能仅依赖 allocator reset。必须要增加依赖。
因此,因为生命周期的不同,当前是无法直接合并两个内存池的。
粗调模式下不使用rootinner复用,存在理论内存下降收益
当前 Host 预算模型
记第
i个 root:粗调模式
当前
BuildTensorWorkspaceFromDescriptor()分别取最大值:每个 parallel 副本的顺序池总预算为:
再加:
最后按 32KB 对齐并乘并行副本数。
当前模型的保守点
粗调模式使用:
这两个最大值可能来自不同 root。
例如:
当前每层预算:
但任意单 root 的组合最大值只有:
这说明存在利用 per-root 相关性降低预算的空间,但能否降低取决于运行时是否改变复用方式。