Closed
[Requirement|需求建议]: 提供PyPTO Workspace 内存精细化调优旋钮 #3048
duanhaotianAdd on  Aug 20Closed at  Sep 14
duanhaotian
duanhaotianCollaborators
Aug 20 Create

Thanks for sending an requirement! Please fill in the following template to help quickly solve your problem.

Background(背景信息)

当前问题

现有预算按最坏 root、最坏 outcast 深度统一放大,各内存池的真实并发需求不同;仅调 max_workspace_kb 会同时压缩内存和 stitch 并行度,难以在性能不退化的前提下消除结构性浪费。

穿刺结果给出两层证据:

  1. 粗调已经有效:44 个 eager case 中,24 个大内存 case 的 workspace 合计由约 170.7GB 降至 51.7GB,净省约 119GB(-70%),性能劣化上限 1.8%。
  2. 粗调仍未消除结构性浪费:按池精确控制原型在 GLM、incre MLA 等多类型 root/outcast 场景中达到 95%~99% 的 workspace 降幅,且性能基本持平。

因此最终方案只保留两种清晰模式:

  • 粗调模式:只配置 max_workspace_kb。
  • 精细模式:同时配置 rootInner、InnerExclusiveOutcasts、InnerTemporalOutcast 三个正交控制项;任意部分配置直接拒绝。

Origin(信息来源)

machine内部

Benefit / Necessity(价值/作用)

最终目标:

新手通过 max_workspace_kb 获得简单可用的粗调能力;
高级用户通过三池独立配置消除结构性浪费;
运行时通过 preflight 和 submit 机制保证收紧后的正确性。

Design(设计方案)

一、性能穿刺结果分析

1.1 统计口径

本次分析区分三类内存:

口径 内容 是否由本方案直接控制
Tensor Workspace rootInner、InnerExclusiveOutcasts、boundary、InnerTemporalOutcast、AICore spill/debug 是,本方案重点
Metadata / RTMalloc DevAscendProgram、ring buffer、stitch pool、ControlFlow cache 等 否,单独统计
IO 内存 输入输出 Tensor 否,不计入 Workspace

Tensor Workspace 近似组成:

Workspace
  = rootInner
  + InnerExclusiveOutcasts
  + boundaryOutcast
  + InnerTemporalOutcast
  + AICore spill
  + debug

1.2 全量 eager case 调优结果

调优准则:

性能劣化 ≤ 2% 前提下,选择最小 Workspace

总体结果:

指标 结果
全量 case 44
需要调优 24
小内存或无 Workspace 20
调优前 Workspace 合计 约 170.7GB
调优后 Workspace 合计 约 51.7GB
净节省 约 119GB(-70%)
最差性能变化 慢 1.8%

代表算子结果:

算子/场景 主要构成 基线 调优后 降幅 性能变化
flash_attention_mha 纯 rootInner 12,304MB 1,009MB -92% 快 1.6%
flash_attention_mha_grad 纯 rootInner 24,580MB 12,240MB -50% 慢 0.3%
incre_flash_attention_mla outcast主导 15,142MB 4,964MB -67% 慢 0.4%
lightning_indexer_quant outcast主导 6,323MB 803MB -87% 慢 1.8%
glm_attention low rootInner + outcast 1,948MB 126MB -94% 慢 1.1%
glm_attention high rootInner + outcast 1,948MB 508MB -74% 慢 0.3%
sparse_attention_antiquant 纯 rootInner 730MB 123MB -83% 快 3.5%
sparse_flash_attn_quant rootInner + 动态 outcast 1,110MB 394MB -65% 慢 0.5%

1.3 gym 仓补充结果

kda / gdr

算子 基线 调优后 降幅 性能变化
chunk_kda 1,179MB 254MB -78% 快 3.8%
gdr_fwd 1,390MB 510MB -63% 慢 0.9%
gdr_bwd 600MB 511MB -15% 慢 0.7%
合计 3,169MB 1,275MB -60% 劣化上限 0.9%

kimi / attenres

算子 方向 基线 调优后 降幅 性能变化
block_attn_res forward 285MB 189MB -34% 快 4.1%
block_attn_res backward 292MB 236MB -19% 快 8.1%

不同方向的性能拐点不同,说明配置必须由运行时真实需求和性能曲线共同决定,不能仅由 shape 或统一经验值推导。

1.4 穿刺结果分析

一:预算深度不等于实际并发需求

flash_attention_mha 的 rootInner:

预算:12,304MB
实际峰值:6,152MB
利用率:50%

预算按最坏单 root × stitch 深度放大,但实际同时存活的 root 只有约一半。

二:多类型 root 的 max 放大严重

GLM 包含约 28 类不同 root,单 root 需求从约 0.1MB 到 2.3MB。原预算把最重 root 乘到全部深度:

项 粗调后 按实际需求控制 变化
rootInner 池 100.38MB 9.12MB -91%
Tensor Workspace 125.86MB 34.6MB -72%
性能 99.2us 97.2us 快 2%

这类浪费不能靠继续降低总 Workspace 消除,否则会同步降低 kEff,先损失并行度。

三:Temporal outcast 预算与实际峰值差距极大

算子 Temporal 预算 slots 峰值 slots 超配
incre_flash_attention_mla 4,602 0 约 100%
lightning_indexer_quant 312 4 98.7%
glm_attention low 3,005 0 约 100%
glm_attention high 6,010 0 约 100%

Temporal 按最坏深度线性预留,但运行时真正同时存活的 slot 极少。

1.5 为什么必须做精细化控制

粗调模式的本质:

降低 max_workspace_kb
  → 反解更小 kEff
  → rootInner、Exclusive、Temporal 同时下降
  → 可能降低 stitch 并行度

穿刺中的性能断崖:

场景 安全点 继续压缩 性能结果
chunk_kda 256MB 128MB 慢 56%
gdr_fwd 512MB 256MB 慢 20.6%
GLM root ratio kEff=20 kEff=11 慢 39%

精细模式的目标是:

保持业务需要的 stitch 能力
  + 分别砍掉各池超配
  + 池不足时由运行时安全 submit

独立池控制原型的可达结果:

算子 基线 精确池控制后 降幅 性能
glm low 1,948MB 16.88MB -99.1% 快 1.6%
glm high 1,948MB 93.66MB -95.2% 慢 1.4%
incre MLA 15,142MB 284.66MB -98.1% 快 0.9%
lightning 6,323MB 194.81MB -96.9% 慢 3.9%,存在噪声/需复核

结论:精细控制的核心是基于实际执行时内存的实际使用情况来反馈控制我们输入端的预估内存


二、内存调优流程

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 示例日志的组成

示例:

/home/d00949735/PyPTO_Test/glm_new_a3/20260819143448/
glm_attention_l0_id0002_low_latency_uniform_capture/
workspace_tuning_extracted.txt

下图所示为执行glm_attention_l0_id0002_low_latency_uniform_capture算子时,调整不同内存池大小时,性能随时内存的变化。可以看出通过调整不同内存池大小能够获得较大的内存下降收益
image.png
提取文件分为五块:

区块 作用 关键问题
ENCODE_AND_LAYOUT 编码预算和最终申请布局 “申请了多少、怎么算出来”
RUNTIME_WINDOW 每个非空 stitch 窗口的 current/peak/capacity “窗口实际用了多少”
RUNTIME_SUMMARY 整个 launch 的最大峰值和推荐值 “三个精细项建议配置多少”
STITCH_AND_SUBMIT stitchCount、AIC/AIV、submit 原因 “是否被内存限制”
PERFORMANCE E2E/Kernel time “性能是否保持”

当前提取结果 PERFORMANCE count=0,性能需从 xrun 结果或性能 Excel 补充。

2.3 第一步:读编码预算

关键日志:

mode=default
max_workspace_kb=200000
k_eff=32
computedWorkspace=202835968

当前约 193.44MiB Context Workspace 的构成:

内存池 预算 计算
rootInner 76,546,048B 默认模式分配结果
InnerExclusiveOutcasts 229,376B 7,168 × 32
boundary 7,421,952B 6,144 × 1,208
InnerTemporalOutcast 118,161,408B 6,144 × 601 × 32
AICore spill 460,800B 固定项

结论:InnerTemporalOutcast 占比最大,rootInner 次之。

2.4 第二步:确认基线没有被内存限制

stitchedSize=23, maxC=20, maxV=40
[Stitch Finish] Finish Signal or Barrier

判断:

  • stitchedSize=23 < k_eff=32;
  • AIC/AIV 已达到 20/40;
  • 窗口由 Finish/Barrier 结束,而不是 workspace preflight 失败。

因此该运行可作为未受内存截断的峰值样本。

2.5 第三步:读运行时峰值

内存池 峰值 容量 使用率 结论
rootInner 12,707,840B 76,546,048B 16.60% 容量明显偏大
Exclusive 157,696B 229,376B 68.75% 实际深度 22
boundary 3 slots 1,208 slots 0.24% 固定预算超配,但本期不调
Temporal 0 slots 19,232 slots 0% 当前路径无需 Temporal
Runtime metadata 72 items 20,097 items 0.35% 非瓶颈

2.6 第四步:反推三个精细配置

rootInner

recommendedReuseWorkspaceKb
  = ceil(12,707,840 / 1024)
  = 12,410KB

InnerExclusiveOutcasts

recommendedExclusiveDepth
  = ceil(157,696 / 7,168)
  = 22

InnerTemporalOutcast

recommendedInnerTemporalDepth
  = ceil(0 / 601)
  = 0

当前精细模式要求三个配置全部大于 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,
}

预计:

computedWorkspace ≈ 25,757,696B
相对 202,835,968B 降低约 87.30%

接近观测下限的配置:

reuse_workspace_kb=12416
exclusive_outcast_stitch_function_num=22
outcast_stitch_function_num=1

该配置缺少 shape/path 余量,不能直接作为生产值。

2.7 第五步:复测与回调

每轮只根据证据回调对应池:

现象 判断 调整
rootInner reset/复用依赖明显增多且性能下降 rootInner 偏小 增大 reuse_workspace_kb
exclusive preflight 不足触发 submit Exclusive 偏小 增大 exclusive_outcast_stitch_function_num
inner-temporal slots 不足触发 submit Temporal 偏小 增大 outcast_stitch_function_num
boundary 不足 当前固定预算或实现问题 不通过精细项掩盖
metadata item pool 不足 metadata 预算问题 单独分析
stitchCount 下降但无内存 submit 控制流/Barrier/业务路径变化 不应盲目增内存

验收条件:

内存下降
+ E2E 劣化在阈值内
+ stitchCount 不异常下降
+ submit 原因符合预期
+ 精度通过

三、配置项设计方案

3.1 仅保留两种模式

模式 配置规则 预算行为 面向用户
粗调模式 三个精细项全部为 0;可配置 max_workspace_kb 根据总量反解统一 kEff 默认、新手
精细模式 三个精细项全部 >0 各池按配置精确求和 高级调优

不存在 reuse-only、Exclusive-only、Temporal-only 或任意两项组合模式。

部分配置时直接报错:

reuse_workspace_kb,
outcast_stitch_function_num,
exclusive_outcast_stitch_function_num
must be configured together

3.2 配置项语义

配置项 控制对象 单位 精细模式语义
max_workspace_kb Context Workspace 总量 KB 可选安全上限,不主动填满
reuse_workspace_kb rootInner 可复用池总容量 KB 所有 parallel 副本合计
exclusive_outcast_stitch_function_num InnerExclusiveOutcasts depth exclusiveUnitBytes × depth
outcast_stitch_function_num InnerTemporalOutcast depth assembleSlotsPerDepth × depth × slotBytes

精细模式公式:

R  = rootInnerPerParallel
Ne = exclusive_outcast_stitch_function_num
Nt = outcast_stitch_function_num
E  = exclusiveUnitBytes
A  = assembleSlotsPerDepth
S  = slotBytes
B  = boundarySlots

tensorRawPerParallel
  = R
  + E × Ne
  + B × S
  + A × Nt × S

ControlFlow cache 和 runtime metadata 不属于两类 Tensor 池,为避免元数据不足,按:

kEffMetadata = max(Ne, Nt)

保守配置,但不重新耦合两类 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 复用

  • rootInner 使用固定池循环复用;
  • 地址重叠时向 StitchContext 添加依赖,保证前一使用者完成后再覆盖;
  • rootInner 紧张不直接按“总量不足”失败,而是通过依赖维持正确生命周期;
  • 推荐值来自 launch 级 rootInner peak。

outcast 不足触发 submit

  • stitch 当前 root 前执行 preflight;
  • 独立检查 Exclusive bytes、Temporal slots、boundary slots 和 metadata items;
  • 当前窗口非空时先提交已有窗口、回收 task 内资源,再重试当前 root;
  • 空窗口仍无法容纳时明确报错,避免死循环。

动态 Shape 重平衡

  • 动态 MaxOutcastMem 求值后重新计算物理字节数;
  • 保持用户配置的两个 depth 不变;
  • 超过 max_workspace_kb 安全上限时失败,不静默降低 depth。

日志与推荐

  • 编码期打印完整预算公式;
  • 窗口级记录 current/peak/capacity/ratio;
  • launch 级分别推荐 rootInner、Exclusive depth、Temporal depth;
  • 记录 stitchCount、submit reason 和最近一次 preflight。

boundary 安全策略

  • boundary 暂不暴露精细配置;
  • 保留编译期固定预算;
  • 后续从 assemble need-alloc 精确统计和生命周期复用方向继续优化。

四、待评估问题

4.1 推荐值呈现:depth 还是直接内存大小

方案 A:全部呈现池大小

优点:

  • 用户看到的都是 KB/MB,直观;
  • 易于与总 Workspace 对账。

问题:

  • 当前控制流都按root粒度管理,要考虑对不满足单root分配的配置项做拦截;
  • 动态 Shape 下相同 bytes 不一定对应相同可支持深度;
  • 运行时 submit 的直接约束是 depth/slot,而不是单纯 bytes。

方案 B:三个内存池均按照 depth num呈现

优点:

  • 与当前预算和运行时分配单位一致;
  • 动态 Shape 下语义更稳定;
  • 能直接解释“支持多少个最坏深度单位”。

问题:

  • 用户需要结合预算公式理解最终字节数。

建议

日志同时展示:

推荐配置值 + 对应物理容量 bytes/slots + 预计总 Workspace

即“配置用 depth,汇报用 depth + bytes 双呈现”。

4.2 配置模式是否只保留两种

当前设计:

  • 粗调:三个精细项全 0;
  • 精细:三个精细项全开;
  • 中间组合:直接拒绝。

原因:

  • 避免组合语义和优先级爆炸;
  • 避免某个池精确、其他池继续填满 max_workspace_kb;
  • 配置结果可复现、可对账;
  • 错误配置尽早暴露。

4.3 内存池是否考虑合并

当前四类 Tensor Workspace 的职责

内存池 分配方式 主要生命周期 回收方式
rootInner SeqWsAllocator root 内部临时 Tensor;允许跨 root 地址复用 stitch window 内可 reset;submit 后 reset
exclusive outcast SeqWsAllocator DeviceTask 内 root 输出;可能被后续 root 消费 仅 submit 后 reset
inner-temporal outcast WsSlotAllocator assemble 临时 slot 引用释放/延迟回收
boundary outcast WsSlotAllocator 跨 DeviceTask 保留的 outcast 延迟回收

两个顺序池虽然使用相同的 allocator 类型,但“allocator 类型相同”不等于“生命周期相同”。考虑到地址复用rootInner reset 后,新 root 可能获得与旧 root 相同的物理地址。
旧 root 对应的 AICore 操作此时未必已经执行,因此不能仅依赖 allocator reset。必须要增加依赖。

因此,因为生命周期的不同,当前是无法直接合并两个内存池的。

粗调模式下不使用rootinner复用,存在理论内存下降收益

当前 Host 预算模型

记第 i 个 root:

I_i = AlignUp(rootInner_i, 512)
E_i = AlignUp(exclusive_i, 512)
K   = effective stitch depth
P   = device_sched_parallelism

粗调模式

当前 BuildTensorWorkspaceFromDescriptor() 分别取最大值:

RootInnerBudget = K × max_i(I_i)
ExclusiveBudget = K × max_i(E_i)

每个 parallel 副本的顺序池总预算为:

SequentialBudgetCurrent
    = K × max_i(I_i) + K × max_i(E_i)

再加:

BoundaryBudget
InnerTemporalBudget

最后按 32KB 对齐并乘并行副本数。

当前模型的保守点

粗调模式使用:

max(I) + max(E)

这两个最大值可能来自不同 root。

例如:

Root I E
A 900 100
B 100 900

当前每层预算:

max(I) + max(E) = 900 + 900 = 1800

但任意单 root 的组合最大值只有:

max(I_i + E_i) = 1000

这说明存在利用 per-root 相关性降低预算的空间,但能否降低取决于运行时是否改变复用方式。

likedislike
duanhaotianduanhaotianCollaborators
Aug 20 Added label: requirement
duanhaotianduanhaotianCollaborators
Aug 20 Assign duanhaotian as responsible person
duanhaotianduanhaotianCollaborators
Aug 20 Associated pull request
duanhaotianduanhaotianCollaborators
Aug 20 Modify description of issue
duanhaotianduanhaotianCollaborators
Aug 20 Modify description of issue
duanhaotianduanhaotianCollaborators
Aug 21 Modify description of issue
duanhaotian
duanhaotianCollaborators
Aug 21 Comments:

inner exclusive是否能合并
对外解释方案:
新PGO_json 替代jit形式呈现
核心:SKILL完善各种场景调优适配,目标性能;目标内存;配置和场景的适配
对外语义:内存,depth如何解释并行度
基于cache结果反馈调整内存

likedislike
duanhaotian
duanhaotianCollaborators
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&lt;int64_t&gt;"]:::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
likedislike
duanhaotian
duanhaotianCollaborators
Aug 27 Comments:

模块二:Stitch Window 峰值采集和推荐计算

模块目标

精细配置将调优问题从“Workspace 总量应该设多少”转换为“三个 depth 各应该设多少”。因此需要运行时回答:

  1. 每个 Stitch Window 内三个池实际使用到多少;
  2. 整次 Launch 的最大使用量是多少;
  3. 峰值对应多少个 depth;
  4. 每增加一个 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
likedislike
duanhaotian
duanhaotianCollaborators
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
likedislike
duanhaotianduanhaotianCollaborators
Sep 1 Associated pull request
8 event messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 14 Added label: resolved