[Feat] Support explicit workspace arenas for Ascend tile APIs 🤖 - #1496
Open
platelett wants to merge 2 commits into
Open
[Feat] Support explicit workspace arenas for Ascend tile APIs 🤖#1496platelett wants to merge 2 commits into
platelett wants to merge 2 commits into
Conversation
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
👋 Hi! Thank you for contributing to the TileLang project. Please remember to run We appreciate you taking this step! Our team will review your contribution, and we look forward to your awesome work! 🚀 |
- Expose keyword-only tmp arenas across public workspace APIs - Centralize target-specific sizing, typed views, and zero-workspace policies - Preserve explicit BufferRegion byte geometry in AscendC and PTO lowering
- Keep focused frontend, IR, codegen, and dav-2201 runtime regressions - Document explicit arena geometry and target-specific workspace policy - Record the current AscendC LocalTensor region-boundary limitation
platelett
force-pushed
the
feat/restore-explicit-tmp
branch
from
July 29, 2026 19:43
fb8a3be to
ff165f7
Compare
Contributor
Author
|
/re-test |
|
🔄 Re-running failed jobs Original workflow run: View details Only the failed jobs will be re-executed. |
platelett
marked this pull request as ready for review
July 31, 2026 06:33
Contributor
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
一些 Tile API(例如 Reduce、Sort)执行时需要额外的 UB 临时空间。默认情况下,这些临时 buffer 由编译器自动插入。
这对普通 kernel 很方便,但会影响手动内存规划。用户使用
T.annotate_address安排 UB 地址时,编译器后来插入的 buffer 不在原有规划中,可能与用户指定的区域冲突。这正是 #1164 希望解决的问题。此前,如果用户想手动控制这些临时空间,只能对整个 kernel 关闭自动注入,并依赖
tmp_ub、tmp_ub_reduce_out等编译器内部名称。这样无法表达“这个调用自动分配,另一个调用使用我提供的 arena”。本 PR 改为逐调用控制:
上例中的显式 arena 是
float32,region 从第 8 个元素开始,也就是 byte 32。Reduce 后端实际需要的是uint8workspace,因此 lowering 会在同一个 byte 32 地址上建立uint8view。这里不会进行数据转换或额外拷贝,只是改变后端解释这段存储的 dtype。
是否使用显式临时空间,只取决于当前调用有没有传入
tmp=;不需要新的 kernel 级开关,也不需要用户了解任何tmp_ub*内部名称。User-facing contract
tmp=是 keyword-only 参数,接受:shared.ubBuffer;Buffer 和 BufferRegion 可以使用任意定宽标量 dtype,起始 byte address 必须 32B 对齐。
用户始终只提供一个 arena。AscendC 和 PTO 需要的 workspace 数量、dtype 和对齐方式可能不同,但这些后端差异由 lowering 处理,不暴露到前端。
tmptmptmpoperand,不生成零尺寸 allocation同一 kernel 可以混用自动和显式调用。
显式非空 arena 的容量由调用者负责。本 PR 会检查 rank、scope、静态连续性、region 边界和起始地址对齐,但不会把内部启发式容量作为公开的最小值检查。
Supported public APIs
reduce_sum/max/minbroadcast/sort/merge_sort/topkgather_mask/select/gathersigmoid/sin/cos/pow/bitwise_xorclamp/clamp_max/clamp_min/roundbilinear_interpolationreduce_sum_experiment/reduce_sum_mask_experimentHow one arena can provide multiple internal workspaces
前端只接收一个
tmp=arena。如果后端内部需要多段空间,compiler pass 会从这个 arena 中自动建立互不重叠的 view。以 PTO
clear=FalseReduce 为例:clear=False表示需要保留dst中的已有值。PTO 会先把本次 Reduce 的结果写入临时输出,再与dst合并:因此,一个 frontend arena 在 PTO 内部会被解释为:
用户仍然只传一个
tmp=arena,不需要分别声明两个 buffer。对于 first-axis Reduce,PTO 不需要主 workspace,因此只建立临时输出 view。
Compiler changes
本 PR 继续使用现有的
InjectTmpBuffer。它现在统一决定:tmp时需要分配多少字节;同一 kernel 中所有隐式调用仍共享一个最大尺寸的
uint8allocation,每个调用在这段存储上建立自己需要的 typed view。显式调用不参与这个最大值计算,因此不会放大隐藏 allocation。显式 workspace 也必须参与流水线依赖分析。例如,在一次调用完成前,后续 DMA 不能覆盖同一段 arena。
以前,相关分析会根据 API 名称和固定参数下标判断“哪个参数是 workspace”。
tmp=变为可选参数后,不同调用可能没有 workspace、只有一个 workspace,或者在 lowering 后产生多个 view,固定下标不再可靠。现在
InjectTmpBuffer会直接标记 lowering 后实际存在的 workspace view;后续的流水线规划和同步插入读取这些标记,而不再重新猜测参数位置。Backend-specific behavior
PTO
此前 PTO 的 workspace 都由编译器创建,是从 offset 0 开始的完整 Buffer,因此部分 codegen 路径只使用 backing buffer 的起始地址,没有完整处理 BufferRegion。
加入
tmp=arena[offset:end]后,继续使用旧路径会丢失 slice offset。例如,用户传入:这里 workspace 应从 byte 32 开始。如果 codegen 退回
arena的起点,Select 会从 byte 0 写入,可能覆盖 arena 中属于其他调用的数据。此外,不同 PTO API 对 workspace dtype 的要求不同。假如某个 view 从 float32 元素 8 开始,而后端需要
uint8workspace,不能把元素 offset 8 原样套到uint8上,否则地址会从 byte 32 错误地变成 byte 8。本 PR 因此统一按 byte address 处理中间几何:
当前公开 PTO workspace 路径都使用这一逻辑:
uint8PTO
clear=FalseReduce 还会在保留 arena 起始 offset 的基础上,将一个 frontend arena 切分为互不重叠的主 workspace 和临时输出。AscendC(dav-2201)
将
tmp=变成公开接口后,compiler policy 必须与 CANN 函数实际是否接收 workspace 保持一致。逐项核对 dav-2201 实现时发现了三类旧的不一致:Mins/Maxs,float32 Round 直接生成CAST_RINT。uint8workspace view。uint8view;其接口实际要求与输入相同的 dtype,本 PR 已按源 dtype 建立 view。这些规则同时用于显式 arena 和隐式 allocation,避免两条路径再次产生不同的函数签名或 workspace 类型。
Scope
tmp=get_tmp_buffer_size()其他边界:
LowerAndLegalize的 pass 顺序。--npu-arch=dav-2201实现。LocalTensor::SetSize将 region extent 变成严格访问上界;这部分留给后续 PR。Validation
仓库中只保留覆盖公开契约和主要回归风险的最小测试;更大的 API、dtype、workspace 组合矩阵仅在本地运行,避免增加后续 CI 的长期负担。
tmp=;Buffer/BufferRegion 检查;旧 Reduce positional 调用兼容68 passed,兼容测试另有7 passedLocalTensor和 PTO tile 的实际地址、dtype、extent,以及显式 arena 的流水线读写依赖3 passed154/154 passed138 passed, 2 xfailedCommands
检查工具:
git diff --checkReview order
4d852b36:公开tmp=接口、workspace lowering、PTO/AscendC codegen 和依赖分析。ff165f7c:最小永久测试集和文档。