Skip to content

Add TGATHERB#932

Open
frank-deng wants to merge 1 commit into
hw-native-sys:mainfrom
frank-deng:main
Open

Add TGATHERB#932
frank-deng wants to merge 1 commit into
hw-native-sys:mainfrom
frank-deng:main

Conversation

@frank-deng

Copy link
Copy Markdown

No description provided.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new TileLang DSL template for the pto.tgatherb operation along with corresponding test cases in test/tilelib-st/a5/tgatherb/case.py and minor .gitignore updates. Feedback on the changes includes correcting the dtypes specification in the @pto.vkernel decorator to prevent type-matching failures for non-i8 destinations, removing unused imports and variables in the template, and adding an assertion or dynamic masking in the test kernel to handle column sizes that are not multiples of 16 safely.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread lib/TileOps/tgatherb_template.py Outdated
Comment on lines +19 to +23
dtypes=[
(pto.i8, pto.f32, pto.f32, pto.f32, pto.f32),
pto.AnyInt,
(pto.i8, pto.i8, pto.i8, pto.i8, pto.i8),
],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The dtypes specification in the @pto.vkernel decorator contains redundant duplicate types and restricts the destination tile (dst) to only pto.i8 (repeated 5 times). Since the template is generic and designed to support other types like pto.f32 (as demonstrated in the test cases), this restriction will cause type-matching failures when using pto.tgatherb with non-i8 destinations. Please update the allowed types to correctly reflect the supported data types (e.g., pto.i8, pto.f16, pto.bf16, pto.f32) for both src and dst tiles.

Suggested change
dtypes=[
(pto.i8, pto.f32, pto.f32, pto.f32, pto.f32),
pto.AnyInt,
(pto.i8, pto.i8, pto.i8, pto.i8, pto.i8),
],
dtypes=[
(pto.i8, pto.f16, pto.bf16, pto.f32),
pto.AnyInt,
(pto.i8, pto.f16, pto.bf16, pto.f32),
],

Comment thread lib/TileOps/tgatherb_template.py Outdated
Comment on lines +11 to +12
import sys
from pathlib import Path

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The imports sys and Path are not used in this file and should be removed to keep the code clean.

Comment on lines +29 to +30
lanes = pto.get_lanes(dtype)
repeat_times = (valid_cols + lanes - 1) // lanes + 1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The variable repeat_times is calculated but never used in the function. It should be removed to keep the code clean and maintainable.

Suggested change
lanes = pto.get_lanes(dtype)
repeat_times = (valid_cols + lanes - 1) // lanes + 1
lanes = pto.get_lanes(dtype)

Comment thread test/tilelib-st/a5/tgatherb/case.py Outdated
Comment on lines +41 to +46
def _tgatherb_body(src_ptr, offset_ptr, dst_ptr, *, src_rows, src_cols, dst_rows, dst_cols):
"""Shared kernel body for the tgatherb cases.

Loads *src* and *offset* tiles from GM, performs block-gather using
``pto.vgatherb``, and stores *dst* back to GM.
"""

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The _tgatherb_body function uses a hardcoded loop step of 16 and an all-enabled mask (pto.MaskPattern.ALL). If a test case with a column size that is not a multiple of 16 is added in the future, this will lead to out-of-bounds memory accesses or writing garbage data. Please add an assertion to ensure dst_cols is a multiple of 16, or implement dynamic masking using pto.make_mask as done in the template.

Suggested change
def _tgatherb_body(src_ptr, offset_ptr, dst_ptr, *, src_rows, src_cols, dst_rows, dst_cols):
"""Shared kernel body for the tgatherb cases.
Loads *src* and *offset* tiles from GM, performs block-gather using
``pto.vgatherb``, and stores *dst* back to GM.
"""
def _tgatherb_body(src_ptr, offset_ptr, dst_ptr, *, src_rows, src_cols, dst_rows, dst_cols):
"""Shared kernel body for the tgatherb cases.
Loads *src* and *offset* tiles from GM, performs block-gather using
``pto.vgatherb``, and stores *dst* back to GM.
"""
assert dst_cols % 16 == 0, "dst_cols must be a multiple of 16 for the hardcoded mask"

@reedhecre

reedhecre commented Jul 13, 2026

Copy link
Copy Markdown

Codex Review

该评论由 review 机器人自动更新。

  • PR: Add TGATHERB #932 Add TGATHERB
  • Author: frank-deng
  • Base/Head: main / main
  • Head SHA: 23f8ba97a81b
  • Trigger: PR 有新提交
  • Generated At: 2026-07-15T02:32:13Z
  • Previous Head SHA: 21634d46589f
  • Status: failed at codex-review (exit=1)

Summary

Review failed at stage codex-review: exit=1

Findings

未生成结构化 findings,因为 review 过程提前失败。

Log Tail

Updating files:  95% (4193/4413)
Updating files:  96% (4237/4413)
Updating files:  97% (4281/4413)
Updating files:  98% (4325/4413)
Updating files:  99% (4369/4413)
Updating files: 100% (4413/4413)
Updating files: 100% (4413/4413), done.
From https://github.com/hw-native-sys/PTOAS
 * [new ref]           refs/pull/932/head -> pr-932
From https://github.com/hw-native-sys/PTOAS
 * branch              main       -> FETCH_HEAD
Switched to branch 'pr-932'
23f8ba97a81b8194b662621e036d54b80397d5d8
 .gitignore                                     |   7 ++
 lib/PTO/Transforms/PTOViewToMemref.cpp         |  12 ++-
 ptodsl/ptodsl/_ops.py                          |   9 ++
 ptodsl/ptodsl/_tile_namespace.py               |   1 +
 ptodsl/ptodsl/tilelib/templates/__init__.py    |   1 +
 ptodsl/ptodsl/tilelib/templates/a5/tgatherb.py |  40 ++++++++
 test/tilelib-st/a5/tgatherb/case.py            | 128 +++++++++++++++++++++++++
 7 files changed, 194 insertions(+), 4 deletions(-)
===== END STAGE clone rc=0 @ 2026-07-15 10:31:54 =====

===== STAGE codex-review @ 2026-07-15 10:31:55 =====
set -euo pipefail
cd '/tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/repo'
'codex' exec -C '/tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/repo' -s read-only -c 'model_provider="codereview"' -c 'model="gpt-5.4"' -c 'model_reasoning_effort="xhigh"' --output-schema '/tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/review_schema.json' -o '/tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/codex_last_message.json' --color never - < '/tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/review_prompt.txt'
[monitor] stage timeout: 1800s
OpenAI Codex v0.115.0 (research preview)
--------
workdir: /tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/repo
model: gpt-5.4
provider: codereview
approval: never
sandbox: read-only
reasoning effort: xhigh
reasoning summaries: none
session id: 019f639d-e5bb-7802-9977-0fbaf27b9fae
--------
user
你现在在审查 GitHub PR。

仓库:hw-native-sys/PTOAS
PR:#932 Add TGATHERB
作者:frank-deng
base branch:origin/main
head branch:HEAD(当前已 checkout 到 PR head)

要求:
1. 只审查这个 PR 相对 origin/main 的改动,必要时可以看上下文文件。
2. 重点找真实的 correctness / regression / contract mismatch / CI / runtime / compatibility 问题。
3. 不要提纯风格建议,不要提低价值猜测。
4. 严格按优先级输出:
   - P1:高概率会导致错误结果、编译/运行失败、严重回归、发布阻断
   - P2:重要缺陷、行为回归、遗漏校验/测试、较大兼容性问题
   - P3:次要但明确可改的问题
5. 如果没有问题,summary 直接写:未检查到 PR #932 存在问题,并返回 findings=[]。
6. 如果有问题,summary 简洁概括,findings 里每条都要给出:
   - severity
   - title
   - body(说明为什么是问题,尽量具体)
   - file(尽量给相对路径)
   - line(能确定就填整数,否则 null)

建议先查看:
- git status --short
- git diff --stat origin/main...HEAD
- git diff --unified=80 origin/main...HEAD

最终输出必须严格匹配 JSON schema。

mcp startup: no servers
Reconnecting... 1/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55ac36ba36e2c-LAX, request id: 55c0d965-5337-4d0d-a21f-6acd2a7a6e2d)
Reconnecting... 2/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55ac918872f3e-LAX, request id: 78190556-1730-4087-9323-fbec5a7ec619)
Reconnecting... 3/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55acebbe71d3e-LAX, request id: 01902fdd-d100-4e33-aa40-1eba5cea78ca)
Reconnecting... 4/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55ad6dfc19898-LAX, request id: 66f4a796-ba05-42a1-a2e3-d3f656a9a2e7)
Reconnecting... 5/5 (unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55ae33f2108c8-LAX, request id: 4b0062e9-b472-452e-af52-d30caa530cd2)
ERROR: unexpected status 403 Forbidden: {"code":"INSUFFICIENT_BALANCE","message":"Insufficient account balance"}, url: https://codex.0u0o.com/responses, cf-ray: a1b55afaef21b668-LAX, request id: 41d51fe0-2397-4087-9073-99ca0b286f25
Warning: no last agent message; wrote empty content to /tmp/ptoas-pr-review-monitor/runs/20260715_103119_pr932/codex_last_message.json
===== END STAGE codex-review rc=1 @ 2026-07-15 10:32:13 =====

@frank-deng
frank-deng force-pushed the main branch 8 times, most recently from f58bde3 to 29233e4 Compare July 17, 2026 02:28
valid_rows, valid_cols = dst.valid_shape
lanes = pto.elements_per_vreg(dtype)
repeat_times = (valid_cols + lanes - 1) // lanes + 1
with pto.for_(0, valid_rows, step=1) as row:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

请直接使用python 原生for语法

Comment thread ptodsl/ptodsl/_ops.py
Comment on lines +3545 to +3551
def tgatherb(src, offsets, dst):
"""``pto.tgatherb`` – tile gather using byte offsets (DPS)."""
_pto.tgatherb(
unwrap_surface_value(src),
unwrap_surface_value(offsets),
unwrap_surface_value(dst),
)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

新增OP需要补充PTODSL文档

Comment thread .gitignore Outdated
build_plain/
build_plan/
install/
/build-sim

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

非普适性的本地的ignore目录不要提上来

@frank-deng frank-deng Jul 17, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已删除

src_ptr = src.as_ptr()
valid_rows, valid_cols = dst.valid_shape
lanes = pto.elements_per_vreg(dtype)
repeat_times = (valid_cols + lanes - 1) // lanes + 1

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

冗余变量?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已去除

mask, remained = pto.make_mask(dtype, col_loop.remained)
dst_reg = pto.vgatherb(src_ptr, offset_reg, mask)
pto.vsts(dst_reg, dst[row, col:], mask)
col_loop.update(remained=remained)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

删掉这一行

@Zhendong404 Zhendong404 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

总体方向没问题:为 pto.tgatherb 补上 ptodsl 高层封装(_ops.py / tile namespace)、a5 TileLib 模板和 ST 测试,另带一个 PTOViewToMemref.cpp 的属性保留修复。但新增的 TileLib 模板目前跑不起来,且测试完全没有覆盖到它,建议修好后再合入。

🔴 Blocker 1:模板中 col_loop 未定义,渲染时必然 NameError

ptodsl/ptodsl/tilelib/templates/a5/tgatherb.py:39-43

for row in range(0, valid_rows, step=1):
    for col in range(0, valid_cols, step=lanes):
        ...
        mask, remained = pto.make_mask(dtype, col_loop.remained)   # col_loop 不存在
        ...
        col_loop.update(remained=remained)

证据:

  • 对该函数做 symtable 分析,col_loop 是自由变量(global ref),函数内从未赋值,模块中也没有这个全局量;
  • 模板追踪机制(ptodsl/ptodsl/_ast_rewrite.py:1129)重写 for x in range(...) 时使用内部新鲜名字(self._fresh("loop")),不会注入 col_loop;全仓库 col_loop 只出现在模板文件里;
  • 其他用到 col_loop 的模板都是显式定义的,如 templates/a5/tcvt.py:96col_loop = pto.for_(0, valid_cols, step=lanes).carry(...);而使用裸 range 循环的模板(templates/a5/_elementwise.py:47-60)用的是普通局部变量 remained

按项目惯例建议改成:

for row in range(0, valid_rows, step=1):
    remained = valid_cols
    for col in range(0, valid_cols, step=lanes):
        offset_reg = pto.vlds(offset[row, col:])
        mask, remained = pto.make_mask(dtype, remained)
        dst_reg = pto.vgatherb(src_ptr, offset_reg, mask)
        pto.vsts(dst_reg, dst[row, col:], mask)

🔴 Blocker 2:新模板没有任何测试覆盖

新增的 ST 测试走 explicit 模式 pto.tile.gatherb(直接 lowering 成 op),完全不经过 templates/a5/tgatherb.pytemplates/__init__.py 里的注册项——这正是上面的 NameError 能混进来的原因。建议至少补一个类似 ptodsl/tests/test_tilelib_render.py(tadd 的那个)的渲染测试,specialize(src, offset, dst).mlir_text() 一次即可挡住这类问题。

🟡 应修:测试文件头注释与事实不符

test/tilelib-st/a5/tgatherb/case.py:13-21

  • 声称 "there is no high-level pto.tile.gatherb wrapper" —— 本 PR 恰恰新增了这个 wrapper,且测试正文就在调用它;
  • 声称内核 "call pto.vgatherb directly" —— 实际调的是 pto.tile.gatherb
  • 引用 lib/TileOps/tgatherb_template.py —— 该文件不存在。

🟡 应修:测试覆盖偏弱

  • 9 个 case 全部 src_shape == dst_shape,且 offset 生成的是恒等映射(offset[i] = i * 32,期望输出恒等于 src)——索引/步长搞反了也测不出来。建议至少加一个乱序 offset 的 case,以及一个 dst 小于 src 的 case;
  • _make_expected 最后 dst.reshape(src.shape) 用的是 src 的 shape 而非 dst_shape,目前只因为两者总相等才没出错;
  • _make_inputsrng_seed/rng 是死代码(rng 创建后从未使用),且 hash(name) 跨进程不确定(PYTHONHASHSEED 随机化)——直接删掉即可;
  • offset 用 np.int32 生成而内核声明 pto.ui32,正数下字节相同无碍,建议统一为 np.uint32

🟢 小问题

  • ptodsl/ptodsl/_ops.pytgatherb 未加入 __all__(同类 tgather 在 5922 行的列表里);
  • .gitignore:混入与本 PR 无关的个人工具目录(.opencode/.arts/),且 # OpenCode 配置 中文注释与该文件全英文惯例不符,/build-sim 写法也与其他目录条目(xxx/)不一致——建议拆出本 PR 或统一风格;
  • lib/PTO/Transforms/PTOViewToMemref.cpp:3717setAttrs(op->getAttrs()):与紧挨着的 TLogOp 处理模式一致,本身没问题;但 TGatherBOpPTOOps.td:4980 并未声明任何 attribute,这行保留的只是上游 pass 挂上的元数据属性——加一句注释说明要保留什么属性会更易读。

做得好的地方

  • C++ 修复与文件内既有模式(TLogOp)一致;
  • _ops.py / _tile_namespace.py 的封装严格跟随 tgather 的现有写法;
  • ST 测试结构符合 tilelib-st 的 golden_output_case / auto_main 约定,dtype 覆盖较全(含 ui16_257x128 这种非 2 的幂行数)。

结论:请先解决模板 NameError 和模板零覆盖这两个问题,其余为打磨项。修好后这个 PR 就可以了。

@frank-deng
frank-deng force-pushed the main branch 3 times, most recently from 100ef65 to 5815798 Compare July 17, 2026 10:42
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.

3 participants