Skip to content

Fix: track reader anti-dependencies in TensorMap - #1810

Draft
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:fix/track-reader-anti-dependencies
Draft

Fix: track reader anti-dependencies in TensorMap#1810
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:fix/track-reader-anti-dependencies

Conversation

@yanghaoran29

@yanghaoran29 yanghaoran29 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

变更摘要

  • 新增 TRACKED_INPUT / add_tracked_input(),让需要约束后续重叠写的 reader 显式发布到 TensorMap。
  • A2/A3、A5 的 HBG 与 TMR 统一采用 opt-in 语义:普通 INPUT 只查询 writer,TRACKED_INPUT 额外注册 reader。
  • INOUTOUTPUT_EXISTING 和 host write 查询 tracked reader 并生成 WAR;多个只读 reader 保持并行。
  • NO_DEP 保留 creator 依赖以维持 tensor 生命周期,但不查询或发布 TensorMap access。
  • HBG host write 改为 graph node,只等待重叠 writer、其直接 consumer 和 tracked reader;Graph 内部 TRACKED_INPUT 自动回退到普通执行路径。
  • HBG fanin 保留 128 个 inline 项,超出部分进入 spill pool;补齐 A2/A3、A5 对称的 WAR、fanout、host-write、Graph fallback 和 dep-gen 回归测试。

证据索引

验证口径

架构 read 全进 TensorMap 当前 TMR opt-in 方案 性能基线
A2/A3 15d168f1 9ad5edfb main
A5 9ad5edfb cc121441 main

最终 head 99a933fe 在 A2/A3、A5 的 HBG 与 TMR 上统一采用显式 reader 发布。性能数据分别比较“read 全进 TensorMap”和“当前 TMR opt-in 方案”与 main;A5 当前方案相对 main 的结果由已有两阶段配对数据复合计算。

所有硬件命令均通过 task-submit 执行。

验证结论

维度 A2/A3 A5
当前 head 本地回归 C++ 无硬件测试 98/98;完整 pre-commit 通过 C++ 无硬件测试 98/98;完整 pre-commit 通过
正确性证据 历史全量 reader 版本的定向硬件 WAR、issue #1306 fanout 和对应仿真通过 当前 TMR opt-in 方案的 sim、dep-gen、硬件定向用例通过
容量证据 历史全量 reader fanout-256:reader 256/65536、TMR spill 192/16384、HBG spill 128/262144 已采集的全量 reader 容量数据均有超过 2 倍余量
Onboard 回归 代表性 HBG/TMR 和双卡 SDMA 通过 完整 CI 形态 sweep 通过
性能 7 个 workload:read 全进 TensorMap 相对 main 的 H/D/O 中位数为 +0.79% / +0.34% / +5.87%;当前方案为 +0.83% / +0.05% / +0.90% 4 个具备完整 main 对比数据的 workload:read 全进 TensorMap 相对 main 的 H/D/O 中位数为 -0.10% / +0.12% / +8.54%;当前方案为 -3.00% / -1.81% / -1.05%

正数表示变慢,H/D/O 表示 Host / Device / Orch。

A2/A3 性能

负载 read 全进 TensorMap 相对 main H/D/O 当前方案相对 main H/D/O
alternating_matmul_add Case1 +0.79% / +4.72% / +5.00% +0.49% / +5.56% / +5.68%
benchmark_bgemm Case0 -0.63% / +4.14% / +5.87% +1.42% / +1.41% / +0.91%
paged_attention_unroll Case1 +2.77% / +0.17% / +6.56% +1.44% / -0.26% / -0.02%
paged_attention_unroll Case2 +4.73% / +0.34% / +7.42% +3.40% / +0.67% / +4.34%
paged_attention_unroll_manual_scope Case1 +0.76% / -1.63% / -2.54% +0.83% / +0.05% / -2.00%
paged_attention_unroll_manual_scope Case2 +1.74% / -2.14% / +1.46% -0.34% / -0.49% / +0.90%
batch_paged_attention Case1 -3.33% / +13.98% / +21.41% -0.30% / -2.39% / -3.31%
方案 Host 中位数 Device 中位数 Orch 中位数
read 全进 TensorMap 相对 main +0.79% +0.34% +5.87%
当前方案相对 main +0.83% +0.05% +0.90%

A5 性能

A5 当前方案相对 main 的结果由已有两阶段数据复合得到:(1 + read 全进 TensorMap 相对 main) × (1 + 当前方案相对 read 全进 TensorMap) - 1

负载 read 全进 TensorMap 相对 main H/D/O 当前方案相对 main H/D/O
alternating_matmul_add Case1 +0.77% / -0.63% / -0.60% +0.52% / -6.16% / -6.18%
paged_attention_unroll Case1 -0.23% / +0.05% / +4.93% -6.53% / -2.12% / -5.08%
paged_attention_unroll Case2 +0.04% / +0.18% / +12.14% +9.82% / -1.50% / +2.99%
batch_paged_attention Case1 -2.97% / +18.99% / +26.82% -7.38% / +11.91% / +16.95%
方案 Host 中位数 Device 中位数 Orch 中位数
read 全进 TensorMap 相对 main -0.10% +0.12% +8.54%
当前方案相对 main -3.00% -1.81% -1.05%

最终 head 已通过 git diff --check、完整 pre-commit 和公共无硬件 C++ 测试 98/98。

Refs #1388
Refs #1306

@coderabbitai

coderabbitai Bot commented Aug 12, 2026

Copy link
Copy Markdown

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7ccac9f1-3cea-4dd6-a41c-567d75f9d57f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@yanghaoran29
yanghaoran29 force-pushed the fix/track-reader-anti-dependencies branch 4 times, most recently from 46c518b to 1cf1b8c Compare August 13, 2026 04:19
@yanghaoran29

yanghaoran29 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

A5 验证原始证据(全量 reader 语义)

这些数据采集自 A5 的历史全量 reader 实现。最终 head 99a933fe 已将 A2/A3、A5 的 HBG 与 TMR 统一为通过 add_tracked_input() 显式发布 reader,因此下面的数据只作为历史正确性与容量证据,不代表最终 head 的性能。当前 A5 TMR opt-in 方案相对 main 的结果见性能评论

所有硬件命令均通过 task-submit 执行,并清除了代理环境变量。

任务编号

  • 完整 CI 形态的 A5 onboard sweep:task_20260813_121231_64772827360
  • WAR 与 reader fanout 正确性:task_20260813_121128_64402517330
  • 三组配对的 100 轮性能测试:task_20260813_115521_5525363866
  • HBG orchestration 配对 profiling:task_20260813_121000_63670614184
  • candidate 容量 profiling:task_20260813_114241_4509309423
  • merge-base 容量 profiling:task_20260813_114556_47550414927
  • batch PA dep-gen 审计:task_20260813_115305_5343498461
  • 修复后的独立 batch PA 100 轮探测:task_20260813_115134_51834613837

各组平均值(base -> candidate,单位:微秒)

TMR Device 与 Orch 序列
  • alternating Case1 Device:[1507.1, 1375.0, 1512.8] -> [1500.2, 1470.2, 1497.6];Orch:[1472.6, 1338.4, 1476.4] -> [1466.4, 1432.3, 1463.7]
  • batch PA Case1 Device:[6946.4, 6929.6, 6954.4] -> [8265.3, 8387.2, 8244.8];Orch:[5802.9, 5784.7, 5822.2] -> [7359.0, 7495.6, 7316.6]
  • PA unroll Case1 Device:[1949.6, 1932.7, 1945.8] -> [1946.8, 1919.1, 1949.6];Orch:[1597.0, 1598.8, 1542.0] -> [1684.2, 1633.1, 1675.8]
  • PA unroll Case2 Device:[1074.4, 1082.4, 1084.2] -> [1082.4, 1084.3, 1092.8];Orch:[686.2, 700.4, 700.9] -> [775.3, 785.4, 786.0]
HBG 插桩后的 Orch 序列
  • vector default:[6.899, 4.051, 4.408] -> [4.435, 6.465, 4.253];中位数 4.408 -> 4.435(+0.61%)
  • paged attention SmallCase2:[25.433, 25.153, 25.718] -> [27.345, 26.031, 29.873];中位数 25.433 -> 27.345(+7.52%)

中位数汇总

负载 Base Host Candidate Host Host 变化 Base Device Candidate Device Device 变化 Base Orch Candidate Orch Orch 变化
HBG PA SmallCase2 6815.1 7100.8 +4.19% 154.7 154.4 -0.19% 25.433 27.345 +7.52%
HBG vector default 6889.9 6914.9 +0.36% 89.5 88.7 -0.89% 4.408 4.435 +0.61%
TMR alternating Case1 123490.1 124436.4 +0.77% 1507.1 1497.6 -0.63% 1472.6 1463.7 -0.60%
TMR batch PA Case1 188121.5 182538.0 -2.97% 6946.4 8265.3 +18.99% 5802.9 7359.0 +26.82%
TMR PA unroll Case1 170868.5 170471.0 -0.23% 1945.8 1946.8 +0.05% 1597.0 1675.8 +4.93%
TMR PA unroll Case2 41272.8 41288.7 +0.04% 1082.4 1084.3 +0.18% 700.4 785.4 +12.14%

@yanghaoran29
yanghaoran29 force-pushed the fix/track-reader-anti-dependencies branch from 1cf1b8c to 15d168f Compare August 13, 2026 08:34
@yanghaoran29

yanghaoran29 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

A2/A3 验证证据(历史全量 reader 版本 15d168f1

本评论只记录 15d168f1 的硬件验证数据;该版本会为每个 INPUT 发布 reader。最终 head 99a933fe 已将 A2/A3、A5 的 HBG 与 TMR 统一为通过 add_tracked_input() 显式发布 reader,因此下面的数据只作为历史正确性和容量证据,不作为最终 head 的性能结果。

无卡预检任务 task_20260812_230824_163633426889 确认测试环境为 A2/A3(NPU Name 9392Chip Ascend910Short_SoC_version Ascend910_93)。以下硬件任务均通过 task-submit 执行。

正确性

  • task_20260812_231525_21188921706:HBG loop-carried WAR,以及 TMR loop-carried/cross-ring WAR 回归均在 A2/A3 硬件上通过。生成的依赖图包含精确的 reader anti-dependency 边 (1, 2)(1<<32, 1),属性为 source=tensormaphazard=WARaccess_kind=READER
  • 相同的三个用例在 a2a3sim 上通过,并校验了精确依赖边。
  • task_20260812_232749_288973430456:issue [Bug] Task dep-gen misses WAR edge: reader task not ordered before a later aliasing inout writer #1306 的原始拓扑(24 个 SPMD reader、34 个 chunk、两个 band)在 A2/A3 硬件上通过,输出形状精确为 (256, 8704) 且数值正确;对应仿真复现也通过。

Profiling 与容量

  • task_20260812_233443_34065644135(TMR,fanout 256):reader high-water 256/65536;writer high-water 259;最大 writer chain 257;ring-0 fanin spill high-water 192/16384,其他 ring 为零。
  • task_20260812_233550_351854921960(HBG,fanout 256):reader high-water 256/65536;writer high-water 259;最大 writer chain 257;host fanin spill 128/262144;共完成 258 个 orchestration task。
  • 这些任务同时确认 A2/A3 能在既有 profiling 开关下输出 TensorMap 与 spill 统计。

回归覆盖

  • 最终 head:C++ 非硬件测试 98/98,完整 pre-commit 和 git diff --check 通过。
  • 代表性 onboard smoke:HBG predicated dispatch、vector 和 dep-gen 通过;TMR mixed dispatch、SPMD 隔离重试通过。
  • 双卡 SDMA 示例:task_20260813_013233_210752413701 passed

当前 TMR opt-in 方案相对 main 的性能数据见配对性能评论。最终验收状态以 PR 描述为准。

@yanghaoran29

yanghaoran29 commented Aug 13, 2026

Copy link
Copy Markdown
Contributor Author

设计说明:TensorMap WAR 跟踪与统一 opt-in 语义

原有问题

修改前,TensorMap 只索引 writer:

INPUT:            查询 writer -> RAW
INOUT:            查询 writer -> RAW/WAW;注册 writer
OUTPUT_EXISTING:                           注册 writer

因此在下面的访问序列中只能发现 W0 -> R0,无法发现仍在执行的 reader,也就缺少 R0 -> W1

W0: write X  ->  R0: read X  ->  W1: overwrite X

这正是 issue #1306 暴露的 WAR 正确性缺口。把 reader 改成 INOUT 会借 WAW 强制排序,但也会错误地串行化彼此独立的 reader;手工 task dependency 又不能让后续基于 buffer 的 host write 发现该 reader。

当前访问语义

最终 head 99a933fe 在 A2/A3、A5 的 HBG 与 TMR 上统一启用显式 TRACKED_INPUT / add_tracked_input()。它仍是只读参数,区别只在于是否把 reader 发布到 TensorMap:

参数 查询对象 注册内容 含义
INPUT 重叠 writer(RAW) 普通只读访问
TRACKED_INPUT 重叠 writer(RAW) reader 必须约束后续重叠写的只读访问
INOUT writer(RAW/WAW)和 reader(WAR) writer 读、改、写
已存在 tensor 的 OUTPUT reader(WAR) writer 不读取旧值的覆盖写;保留 unordered-writer 契约
新分配 tensor 的 OUTPUT 新分配输出
NO_DEP creator 只保留 tensor 生命周期,不查询或发布 TensorMap access

标记必须放在 reader 上。后续 writer 无法追溯发现一个没有留下 reader entry 的普通 INPUTmanual_dep 保留 creator、跳过 TensorMap 查询与发布;manual scope 跳过自动依赖。

依赖与生命周期

新的依赖链为:

W0: INOUT X --RAW--> R0: TRACKED_INPUT X --WAR--> W1: INOUT X

多个 reader 仍可并行,因为 reader 不查询其他 reader:

              +--> R0 --+
W0: write X --+--> R1 --+--> W1: overwrite X
              +--> R2 --+

实现上的关键约束:

  • 四套 runtime 使用相同的 reader/writer 访问规则;reader 和 writer 使用独立 bucket index,但共享既有 entry pool 和按 task 回收机制。
  • 先计算当前任务的完整 fanin,再注册 access,避免同一任务内 alias 参数形成 self-dependency。
  • writer 枚举所有重叠且仍存活的 tracked reader 并生成 WAR;fully covered entry 可在建立 edge 后移除,其余按各 runtime 的 task watermark 回收。
  • HBG 保留 128 个 inline fanin,超出部分进入 scheduler spill pool并继续去重,依赖不会被静默截断。
  • get_tensor_data() 只等待重叠 writer;set_tensor_data() 还等待重叠 writer 的直接 consumer 和可发现的 tracked reader。HBG 用 scheduler-local host-write graph node 表达这些依赖,不再串行化整个 task 前缀。
  • HBG Graph boundary 接受 TRACKED_INPUT;Graph 内部出现 TRACKED_INPUT 时不缓存 Definition,自动回退到普通执行路径,以免遗漏后续 WAR 发布。
  • NO_DEP 不再丢失 creator;它只跳过 TensorMap 查询与注册。

性能含义

旧的全量 reader 版本 15d168f1 让每个普通 INPUT 都承担 entry 分配、bucket/per-task chain 插入和退休回收成本,即使最终没有生成 WAR edge。真正的 writer 还会按存活且重叠的 reader fanout 扫描、去重并存储 fanin。

最终实现把固定成本限制在使用 add_tracked_input() 的访问上;没有使用该接口的 workload 不承担 reader entry 的发布与回收成本。A2/A3 与 A5 的具体性能结论见配对性能评论,两种方案均统一换算为相对 main 的结果。

完整用户语义见 WAR anti-dependency 文档

@yanghaoran29

yanghaoran29 commented Aug 14, 2026

Copy link
Copy Markdown
Contributor Author

A2/A3 与 A5 性能变化:read 全进 TensorMap、当前 TMR opt-in 方案与 main

两个架构均使用相同口径:分别给出“read 全进 TensorMap 相对 main”和“当前 TMR opt-in 方案相对 main”的性能变化。正数表示变慢,H/D/O 分别表示 Host / Device / Orch;每列取三组组内配对百分比的中位数,每个 case 每组运行 100 轮。最终 head 99a933fe 延续该 TMR opt-in 语义;性能数据对应下列已实测提交。

A2/A3

数据来源

  • read 全进 TensorMap 版本:15d168f1
  • 当前 TMR opt-in 方案:9ad5edfb
  • read 全进 TensorMap 与 main 的配对测试:task_20260813_022906_794286809
  • 当前 TMR opt-in 方案与 main 的同卡配对测试:task_20260813_184243_166516431087
  • 当前 TMR opt-in 方案测试采用 AB/BA/AB 顺序,42/42 次 measured case 执行通过
  • PTOAS 0.58 SHA256:1c2ea6ff42f289ab78936a92ecc0f4cb309b4cd0c5f9ec7b2394f624cd39caf5

性能结果

负载 read 全进 TensorMap 相对 main H/D/O 当前 TMR opt-in 方案相对 main H/D/O
alternating_matmul_add Case1 +0.79% / +4.72% / +5.00% +0.49% / +5.56% / +5.68%
benchmark_bgemm Case0 -0.63% / +4.14% / +5.87% +1.42% / +1.41% / +0.91%
paged_attention_unroll Case1 +2.77% / +0.17% / +6.56% +1.44% / -0.26% / -0.02%
paged_attention_unroll Case2 +4.73% / +0.34% / +7.42% +3.40% / +0.67% / +4.34%
paged_attention_unroll_manual_scope Case1 +0.76% / -1.63% / -2.54% +0.83% / +0.05% / -2.00%
paged_attention_unroll_manual_scope Case2 +1.74% / -2.14% / +1.46% -0.34% / -0.49% / +0.90%
batch_paged_attention Case1 -3.33% / +13.98% / +21.41% -0.30% / -2.39% / -3.31%

中位数汇总

方案 Host Device Orch
read 全进 TensorMap 相对 main +0.79% +0.34% +5.87%
当前 TMR opt-in 方案相对 main +0.83% +0.05% +0.90%

A5

数据来源

  • read 全进 TensorMap 版本:9ad5edfb
  • 当前 TMR opt-in 方案:cc121441
  • read 全进 TensorMap 与 main 的配对测试:task_20260813_115521_5525363866
  • 当前 TMR opt-in 方案与 read 全进 TensorMap 的同卡配对测试:task_20260814_115012_340747014227
  • baseline 和 candidate 使用独立 worktree-local .venv,两边 PTO-ISA 均固定为 0cefc9a5a1c24c62655cc345d408559595a8af32
  • 当前 TMR opt-in 方案测试采用 AB/BA/AB 顺序,先各预热 3 轮,42/42 次 measured case 均首次执行通过
  • 当前 A5 硬件定向正确性用例在 task_20260814_114723_33881903430 中 3/3 通过

A5 当前 TMR opt-in 方案相对 main 的结果由已有两阶段数据复合得到:(1 + read 全进 TensorMap 相对 main) × (1 + 当前 TMR opt-in 方案相对 read 全进 TensorMap) - 1

性能结果

负载 read 全进 TensorMap 相对 main H/D/O 当前 TMR opt-in 方案相对 main H/D/O
alternating_matmul_add Case1 +0.77% / -0.63% / -0.60% +0.52% / -6.16% / -6.18%
paged_attention_unroll Case1 -0.23% / +0.05% / +4.93% -6.53% / -2.12% / -5.08%
paged_attention_unroll Case2 +0.04% / +0.18% / +12.14% +9.82% / -1.50% / +2.99%
batch_paged_attention Case1 -2.97% / +18.99% / +26.82% -7.38% / +11.91% / +16.95%

中位数汇总

方案 Host Device Orch
read 全进 TensorMap 相对 main -0.10% +0.12% +8.54%
当前 TMR opt-in 方案相对 main -3.00% -1.81% -1.05%

@yanghaoran29
yanghaoran29 force-pushed the fix/track-reader-anti-dependencies branch 2 times, most recently from a0c03fe to 99a933f Compare August 14, 2026 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant