Skip to content

vgather support i8/ui8 -> i16/ui16 - #1315

Merged
mouliangyu merged 1 commit into
hw-native-sys:mainfrom
Likai-19:feature_vmi_vgather
Aug 31, 2026
Merged

vgather support i8/ui8 -> i16/ui16#1315
mouliangyu merged 1 commit into
hw-native-sys:mainfrom
Likai-19:feature_vmi_vgather

Conversation

@Likai-19

@Likai-19 Likai-19 commented Aug 21, 2026

Copy link
Copy Markdown

解决的问题

pto.vmi.vgather 原先只支持 B32 路径(32-bit 结果 + i32 offsets + b32 mask),无法处理量化场景中常见的 byte/halfword gather。本 PR 新增 B16 路径,使 vgather 能直接服务 8-bit/16-bit 数据类型。

主要改动

  • 新增 B16 gather 路径
    16-bit 结果 + ui16 offsets + b16 mask
    支持同宽度 gather:i16/ui16/f16/bf16 source → matching result
    支持 8→16 零扩展提升:i8/ui8 source → i16/ui16 result(按符号标签匹配,零扩展,不支持符号扩展)

  • 多 chunk gather 支持
    移除原先 16-bit gather 只能单物理寄存器的限制
    支持最多 4 个物理寄存器(ISA 上限):16-bit → 最多 512 lanes,32-bit → 最多 256 lanes

  • 静态全活跃 mask 下消除冗余 vsel
    Lowering 层:当 mask 静态全活跃时(create_mask(active_lanes >= L) 或全 true constant_mask),跳过 trailing vsel,直接返回 gather 结果
    VPTO 层:新增 SimplifyVselAllTrueMask pattern,在 VPTOMaskSimplify pass 中消除全 true mask 的 vsel,使该优化对非 gather 路径也生效

  • PTODSL 自动推导
    pto.vmi.vgather 的 8-bit 整数 source 自动推导为 16-bit 结果类型(i8→i16、ui8→ui16、si8→si16),无需用户显式指定

  • 测试
    14 个 lit 用例:覆盖 B16/B32 路径、同宽度/提升、multi-chunk、granularity pred/explicit/conflict、all-active mask 优化、invalid 场景
    7 个 NPU 板端 runtime case:i8→i16、u8→u16、ui16→ui16、f16→f16、bf16→bf16(256/512 lanes),全部 PASS

@Likai-19
Likai-19 force-pushed the feature_vmi_vgather branch from 3476eb4 to 712e8c7 Compare August 21, 2026 07:12
Comment thread lib/PTO/IR/VMI.cpp Outdated
static bool isSupported16BitGatherResult(Type sourceElemType,
Type resultElemType) {
auto resInt = dyn_cast<IntegerType>(resultElemType);
if (!resInt || resInt.getWidth() != mlir::pto::kValue16) {

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.

[P1] 文档声明与 verifier 不一致:这里的 isSupported16BitGatherResult 只接受 IntegerType 且宽度为 16,因此 f16/bf16 的 B16 gather 会在 VMIGatherOp::verify() 阶段被拒绝,无法到达 lowering。PR 文档却把 f16/bf16 -> matching result 列为已支持类型。请在本 PR 中实现并补 runtime 覆盖,或删除/修正文档中的 float 支持声明。

@mouliangyu mouliangyu 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.

检视结论:需要修正文档与 verifier 的支持范围不一致。当前实现并不支持文档声明的 f16/bf16 B16 gather;请实现并补 runtime 覆盖,或撤回该声明。

| `offsets` | `VRegType` | Per-lane element offsets (integer VMI vector) |
| `mask` | VMI mask | **Required.** Predicate mask gating lane participation |
| `pmode` | `str` or `None` | Optional predicate mode: `"merge"` keeps predicate-inactive lanes at their prior value; `"zero"` writes 0 |
| `result_type` | `VRegType` or scalar dtype | Optional result VMI vector type; defaults to the source element type. Required for i8/ui8 -> i16/ui16 promotion. |

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.

result_type能否自动推导

| `mask` | VMI mask | **Required.** Predicate mask gating lane participation |
| `pmode` | `str` or `None` | Optional predicate mode: `"merge"` keeps predicate-inactive lanes at their prior value; `"zero"` writes 0 |
| `result_type` | `VRegType` or scalar dtype | Optional result VMI vector type; defaults to the source element type. Required for i8/ui8 -> i16/ui16 promotion. |
| `result_type` | `VRegType` or scalar dtype | Optional result VMI vector type; defaults to the source element type. For i8/ui8/si8 sources it is automatically widened to the matching i16/ui16/si16 type when omitted. |

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.

如果没有推导不了的场景,应该直接删掉result_type参数

@mouliangyu mouliangyu 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.

重新检视最新 head be5adaf:\n\n前一轮指出的功能问题已修复:f16/bf16 支持已补齐,公开 vgather API 中冗余的 result_type 也已删除;本地在当前 main 合并树上完成构建、推导测试、lit 和 f16/bf16 simulator 验证,功能结果通过。\n\n仍需修改以下合规阻塞项:\n\n1. lib/PTO/IR/VMI.cpp:571、610 以及 lib/PTO/Transforms/VMIToVPTO.cpp:1970 的 if/loop 控制体未使用花括号,违反必选规则 G.FMT.11-CPP。\n2. 新增的 7 个 runtime case 的 main.cpp 存在同类无花括号控制体;请统一修复,而不是只改某一个 case。\n3. 这些 main.cpp 使用 atoi 解析外部输入,无法报告非法输入和越界(G.FUU.22);请改用可检查转换结果及范围的接口。\n\n复跑 changed-code checker 结果为 31 errors / 14 warnings。功能 blocker 已解除,但上述 required-rule errors 清零前仍需保持 Changes Requested。

| `active_lanes` | `IndexLike` | Number of active lanes in the prefix |
| `size` | `int` | Total logical lane count |
| `group` | `int` or `None` | When provided, creates a grouped prefix mask instead of a whole-vector prefix mask. The group size is inferred as `size / group` |
| `granularity` | `str` | Optional mask granularity: `"pred"`, `"b8"`, `"b16"`, or `"b32"`. Defaults to `"pred"` |

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.

vgather 的结果类型是输入类型的纯函数,mask 粒度被 ISA 绑死在结果位宽上,这个granularity参数还是没有必要

@Likai-19
Likai-19 force-pushed the feature_vmi_vgather branch 5 times, most recently from f78a72a to 76c065d Compare August 31, 2026 02:40

@mouliangyu mouliangyu 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.

重新检视最新 head 76c065d:此前阻塞项已修复,PTODSL 不再暴露 mask granularity,并补充了直接调用 pto.vmi.vgather 的 i8/ui8 -> i16/ui16 runtime case;CI 与 simulator 均通过。

仍需整理新增的 test/vpto/cases/vmi_new/vgather-vmi.py。第 406-478 行包含多组编译期恒真/恒假的字符串比较,例如固定 case 名与固定字符串比较后选择输入和 golden。这是生成脚本残留,降低测试可读性,也容易掩盖各 case 实际使用的数据类型;同时其中 15 行超过项目 120 列限制(G.FMT.02)。请将五个 case 的 inputs/expected 改为直接、明确的实现并修复超长行。

功能验证已通过,当前 Changes Requested 仅针对上述新增测试代码质量问题。

@Likai-19
Likai-19 force-pushed the feature_vmi_vgather branch from 76c065d to 04daa2d Compare August 31, 2026 06:59

@mouliangyu mouliangyu 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.

重新检视最新 head 04daa2d:上一轮指出的新增测试代码问题已全部修复。五个 vgather case 的 inputs/expected 已改为直接、明确的实现,恒真/恒假字符串分支已删除,超过 120 列的问题已清零。

PTODSL mask granularity 不再对用户暴露;i8/ui8 -> i16/ui16 的公共 DSL 推导和 runtime 覆盖完整。最新 build-and-test、vpto-sim-validation、wheel 和合规检查均通过,与当前 main 合并无冲突。Approve。

@mouliangyu
mouliangyu merged commit a08cfdf into hw-native-sys:main Aug 31, 2026
12 checks passed
@reedhecre

Copy link
Copy Markdown

A3 板测完成(有跳过)

  • 触发方式:merged
  • 源码提交:a08cfdfec95c
  • 结果汇总:OK 323 / FAIL 0 / SKIP 29
  • 日志:/home/zhongxuan/ptoas-board-monitor/runtime/logs/20260831_020908_merged_pr1315.log
  • 结果 TSV:/home/zhongxuan/ptoas-board-monitor/runtime/logs/20260831_020908_merged_pr1315.tsv

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.

4 participants