Skip to content

[PTO][IR] align textract tinsert and tquant advanced parameters#729

Open
FangRui0 wants to merge 1 commit into
hw-native-sys:mainfrom
FangRui0:align_args3
Open

[PTO][IR] align textract tinsert and tquant advanced parameters#729
FangRui0 wants to merge 1 commit into
hw-native-sys:mainfrom
FangRui0:align_args3

Conversation

@FangRui0
Copy link
Copy Markdown
Contributor

No description provided.

@reedhecre
Copy link
Copy Markdown

reedhecre commented May 30, 2026

Codex Review

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

Summary

发现 3 个问题:pto.textract 新增 acc->vec 形式的 pipeline 归类错误,pto.tquant 的 A5 扩展形式缺少架构限制和完整参数校验。

Findings

  1. P1 `pto.textract` 新增的 `acc->vec` 形式仍会被归到 `PIPE_V` include/PTO/IR/PTOOps.td:4136

本 PR 在 lib/PTO/IR/PTO.cpp:5842lib/PTO/IR/PTO.cpp:5915 新增了 loc=acc -> loc=vecpto.textract 合法路径,但 include/PTO/IR/PTOOps.td 里的 TExtractOp::getPipe() 只显式处理了 MAT->*VEC->MATACC->MAT,未覆盖 ACC->VEC,最终会落到默认的 PIPE_V。同步插入/降级逻辑会直接消费这个 OpPipeInterface 结果(例如 lib/PTO/Transforms/InsertSync/PTOIRTranslator.cpp:612),而 A5 上 PIPE_V barrier 还会被 lib/PTO/Transforms/LoweringSyncToPipe.cpp:123 删除。也就是说,新加的 acc->vec textract 会按错误 pipeline 参与同步推导,容易造成缺失或错误 barrier,属于真实运行时 correctness 回归。

  1. P2 A5 扩展版 `tquant` 在 A2/A3 上不会被拒绝 lib/PTO/IR/PTO.cpp:8801

PR 文档和新增测试都把 exp/max/scaling[/expZZ]quantScaleAlgvecStoreMode 当作 A5 扩展形式处理,但 A2/A3 verifier 这里只复用了通用检查并要求 src/dst row-major,没有禁止这些新 operand/attr。这样在 A2/A3 上也能接受扩展 tquant,随后还会继续走 lib/PTO/Transforms/PTOToEmitC.cpp:9436 的扩展 TQUANT<QuantType, QuantScaleAlg|VecStoreMode> lowering。这和本 PR 自己定义的目标范围不一致,实际效果很可能是编译阶段找不到对应接口,或者把本不支持的 IR 放到运行时。

  1. P2 `tquant` 没有强制 `exp/max/scaling` 成组出现 lib/PTO/IR/PTO.cpp:8715

这里把扩展 stats 家族是否存在定义成 hasExp || hasMax || hasScaling || hasExpZZ,但没有要求 exp/max/scaling 三个 helper tile 必须同时出现,也没有要求 expZZ 必须依赖这三者。结果像“只给 %exp”或“只给 %expZZ”这样的 IR 也会通过 verifier。后面的 EmitC lowering 一旦看到任意 stats operand,就会无条件为 exp/max/scaling 生成参数(见 lib/PTO/Transforms/PTOToEmitC.cpp:9436-9465),缺失项会变成空 Value/空指针,最终生成无效 EmitC,甚至在后续阶段直接崩溃。

@FangRui0
Copy link
Copy Markdown
Contributor Author

/run a3

@reedhecre
Copy link
Copy Markdown

已接收 /run a3,A3 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

Copy link
Copy Markdown

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

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 extends the PTO dialect's extraction, insertion, and quantization operations (textract, tinsert, and tquant) to support advanced parameters and attributes for the A5 architecture, updating their definitions, verifiers, and EmitC/Memref lowering passes. Feedback on the changes highlights a critical compilation error in lib/PTO/IR/PTO.cpp where mode->getValue() is incorrectly called on an optional enum, a parsing ambiguity in the declarative assembly format of TQuantOp due to sequential optional operands, and code duplication of vector pre-quant type pair helpers.

Comment thread lib/PTO/IR/PTO.cpp
Comment on lines +5551 to +5553
if (auto mode = getAccToVecMode();
hasMode && mode && mode->getValue() != pto::AccToVecMode::SingleModeVec0 &&
mode->getValue() != pto::AccToVecMode::SingleModeVec1)
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The getAccToVecMode() method returns std::optional<pto::AccToVecMode>. Since pto::AccToVecMode is an enum class, it does not have a getValue() member function. Calling mode->getValue() will result in a compilation error. Instead, you should dereference the optional using *mode or call mode.value().

Note that this same issue occurs at multiple other places in this file:

  • Line 5627-5629
  • Line 5760-5762
  • Line 5812-5815
  • Line 5989-5993
  • Line 6023-6027
  • Line 6098-6102
  • Line 6129-6133

Please fix all of them similarly.

Suggested change
if (auto mode = getAccToVecMode();
hasMode && mode && mode->getValue() != pto::AccToVecMode::SingleModeVec0 &&
mode->getValue() != pto::AccToVecMode::SingleModeVec1)
if (auto mode = getAccToVecMode();
hasMode && mode && *mode != pto::AccToVecMode::SingleModeVec0 &&
*mode != pto::AccToVecMode::SingleModeVec1)

Comment thread include/PTO/IR/PTOOps.td
Comment on lines 5060 to 5069
let assemblyFormat = [{
`ins` `(` $src `,` $fp (`,` $offset^)? `:` qualified(type($src)) `,` qualified(type($fp)) (`,` qualified(type($offset))^)? `)`
`ins` `(` $src
(`,` $fp^)? (`,` $offset^)? (`,` $exp^)? (`,` $max^)? (`,` $scaling^)? (`,` $expZZ^)?
`:` qualified(type($src))
(`,` qualified(type($fp))^)? (`,` qualified(type($offset))^)? (`,` qualified(type($exp))^)?
(`,` qualified(type($max))^)? (`,` qualified(type($scaling))^)? (`,` qualified(type($expZZ))^)?
`)`
`outs` `(` $dst `:` qualified(type($dst)) `)`
attr-dict
}];
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

critical

The declarative assembly format for TQuantOp has multiple optional operands ($fp, $offset, $exp, $max, $scaling, $expZZ) parsed sequentially without any distinguishing keywords or markers.

Because the parser cannot distinguish SSA values by name, if any of the leading optional operands are omitted (for example, in the A5 form where fp and offset are omitted but exp, max, and scaling are provided), the parser will sequentially assign the first parsed optional operand to $fp, the second to $offset, and so on. This results in completely incorrect operand mapping and silent correctness bugs.

To resolve this, you should either:

  1. Define a custom parser and printer (let hasCustomAssemblyFormat = 1;) to handle the two mutually exclusive forms unambiguously.
  2. Or introduce distinguishing keywords in the declarative assembly format, e.g., (, fp = $fp^)? (, offset = $offset^)? etc.

Comment thread lib/PTO/IR/PTO.cpp
Comment on lines +5453 to +5468
auto isA2A3VectorPreQuantTypePairLocal = [&](Type srcElem, Type dstElem) {
if (srcElem.isF32())
return dstElem.isInteger(8);
if (srcElem.isInteger(32))
return dstElem.isInteger(8) || dstElem.isF16() || dstElem.isInteger(16);
return false;
};
auto isA5VectorPreQuantTypePairLocal = [&](Type srcElem, Type dstElem) {
if (srcElem.isF32())
return dstElem.isInteger(8) ||
(isa<FloatType>(dstElem) && cast<FloatType>(dstElem).getWidth() == 8) ||
dstElem.isF16() || dstElem.isBF16() || dstElem.isF32();
if (srcElem.isInteger(32))
return dstElem.isInteger(8) || dstElem.isF16() || dstElem.isBF16();
return false;
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

These local lambdas (isA2A3VectorPreQuantTypePairLocal and isA5VectorPreQuantTypePairLocal) duplicate the logic of the global helper functions isA2A3VectorPreQuantTypePair and isA5VectorPreQuantTypePair declared at lines 2991-2992 and used elsewhere (e.g., in TInsertOp::verify()).

To improve maintainability and eliminate code duplication, please remove these local lambdas and use the global helper functions directly in TExtractOp::verify().

  // Using global helper functions instead of local lambdas

@reedhecre
Copy link
Copy Markdown

A3 板测失败

日志尾部

hongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp: In lambda function:
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘ScalarRelu’ not handled in switch [-Werror=switch]
 8660 |       switch (mode) {
      |              ^
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘VectorRelu’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘Pwl’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp: In lambda function:
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘ScalarRelu’ not handled in switch [-Werror=switch]
 8767 |       switch (mode) {
      |              ^
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘VectorRelu’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_114606_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘Pwl’ not handled in switch [-Werror=switch]
cc1plus: all warnings being treated as errors
[90/94] Building CXX object lib/PTO/IR/CMakeFiles/obj.PTOIR.dir/PTO.cpp.o
ninja: build stopped: subcommand failed.
===== END STAGE build-ptoas rc=1 @ 2026-05-30 11:49:08 =====

@FangRui0
Copy link
Copy Markdown
Contributor Author

/run a3

@reedhecre
Copy link
Copy Markdown

已接收 /run a3,A3 板测器会处理这条请求。

页面会自动刷新,可以直接看当前阶段、排队情况和最近结果。

@reedhecre
Copy link
Copy Markdown

A3 板测失败

日志尾部

hongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp: In lambda function:
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘ScalarRelu’ not handled in switch [-Werror=switch]
 8660 |       switch (mode) {
      |              ^
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘VectorRelu’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8660:14: error: enumeration value ‘Pwl’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp: In lambda function:
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘ScalarRelu’ not handled in switch [-Werror=switch]
 8767 |       switch (mode) {
      |              ^
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘VectorRelu’ not handled in switch [-Werror=switch]
/home/zhongxuan/ptoas-board-monitor/runtime/runs/20260530_115306_manual_pr729/repo/lib/PTO/Transforms/PTOToEmitC.cpp:8767:14: error: enumeration value ‘Pwl’ not handled in switch [-Werror=switch]
cc1plus: all warnings being treated as errors
[90/94] Building CXX object lib/PTO/IR/CMakeFiles/obj.PTOIR.dir/PTO.cpp.o
ninja: build stopped: subcommand failed.
===== END STAGE build-ptoas rc=1 @ 2026-05-30 11:56:09 =====

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.

2 participants