Skip to content

feat(codegen): support single-row GEMV on A2/A3 - #2185

Open
hashiqiqixian wants to merge 3 commits into
hw-native-sys:mainfrom
hashiqiqixian:feat/ptoas-b04-gemv
Open

feat(codegen): support single-row GEMV on A2/A3#2185
hashiqiqixian wants to merge 3 commits into
hw-native-sys:mainfrom
hashiqiqixian:feat/ptoas-b04-gemv

Conversation

@hashiqiqixian

Copy link
Copy Markdown
Contributor

Summary

  • select PTO-ISA's single-row Mat layout for GEMV lhs and bias loads
  • model GEMV results as a 16-row physical Acc tile while preserving logical [1, N] validity
  • add A2/A3 same-name ST coverage for pto.tgemv, pto.tgemv.acc, and pto.tgemv.bias, including FP16/BF16, narrow-K, bias, and accumulation cases
  • document the physical/logical GEMV contract and update the verified operator-status rows

Validation

  • ruff check and ruff format --check on touched Python tests
  • clang-format --dry-run --Werror on touched C++ files
  • diff-scoped clang-tidy --jobs=1 passed (remote host LLVM 18.1.8; project pin warning expects 21.1.0)
  • focused UT/codegen: 11 passed, 604 deselected
  • A2/A3 codegen-only ST: 13 passed; emitted 13 .pto files with 16-row Acc tiles and exact base/acc/bias ops
  • A2/A3 hardware ST through one task-submit batch: 13/13 passed on device 1

@coderabbitai

coderabbitai Bot commented Jul 28, 2026

Copy link
Copy Markdown

Review Change Stack

Important

Review skipped

Auto incremental reviews are disabled on this repository.

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: a1e1cde4-ff64-4975-979c-5947cfbb67e7

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
📝 Walkthrough

Walkthrough

GEMV tile inference now models padded physical shapes separately from logical valid regions. Mat loads select the required single-row layout, runtime and codegen tests cover the GEMV family, and English/Chinese documentation records the updated shape and validation contracts.

Changes

GEMV shape and execution flow

Layer / File(s) Summary
GEMV type inference
src/ir/op/tile_ops/matmul.cpp
Dedicated GEMV builders and registrations produce padded physical result types, preserve valid shapes, and validate accumulator shape and dtype.
Mat loading and tile constraints
src/ir/op/tile_ops/memory.cpp
Single-row Mat loads use row_major and none_box; Mat and Acc tile creation constraints compare target memory directly.
Runtime, IR, and codegen validation
tests/st/runtime/ops/test_gemv.py, tests/ut/codegen/test_pto_codegen.py, tests/ut/ir/operators/test_tile_ops.py
Tests cover GEMV, bias, accumulation, narrowed K dimensions, FP16/BF16 inputs, padded shapes, valid regions, exact PTO ops, and layouts.
GEMV contract and status documentation
docs/en/dev/ir/05-operators.md, docs/zh-cn/dev/ir/05-operators.md, docs/en/dev/ptoas-op-status.md, docs/zh-cn/dev/ptoas-op-status.md
Documentation describes logical versus physical GEMV shapes, and status tables mark distributed ST as passing for the GEMV operations.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant TestGemv
  participant tile.gemv
  participant PTOAS
  participant Output
  TestGemv->>tile.gemv: construct GEMV tiles
  tile.gemv->>PTOAS: lower to pto.tgemv
  PTOAS->>Output: write padded physical result
  Output-->>TestGemv: validate logical valid region
Loading

Poem

A rabbit packs rows in a padded array,
With GEMV hopping neatly its way.
Bias joins the dance,
Accumulators advance,
While valid shapes keep logic at bay.

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main change: single-row GEMV codegen support on A2/A3.
Description check ✅ Passed The description is directly related to the changeset and covers the codegen, tests, and docs updates.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 37b5704323

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread src/ir/op/tile_ops/matmul.cpp

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
tests/st/runtime/ops/test_gemv.py (1)

457-468: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Nit: hoist the loop-invariant valid_k.

valid_k does not depend on chunk; compute it once above the loop (as get_program already does).

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/st/runtime/ops/test_gemv.py` around lines 457 - 468, In
compute_expected, move the loop-invariant valid_k assignment before the for
chunk loop, matching the existing calculation in get_program, and keep the
loop’s matrix multiplication behavior unchanged.
src/ir/op/tile_ops/matmul.cpp (1)

299-316: 🗄️ Data Integrity & Integration | 🔵 Trivial | 💤 Low value

gemv_acc result drops the accumulator's own valid extent.

The returned type takes its valid_shape purely from lhs/rhs. If a later chunk narrows the rhs valid N, the result claims a smaller valid region than the accumulator actually holds from earlier chunks. Consider taking the element-wise max of acc's valid shape and the product's, or explicitly checking they agree.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/ir/op/tile_ops/matmul.cpp` around lines 299 - 316, Update the gemv_acc
result-type logic around BuildGemvResultType so the returned valid_shape
preserves the accumulator’s existing valid extent instead of relying solely on
the lhs/rhs product. Combine acc_type’s valid shape with the product’s using the
intended element-wise maximum, or reject mismatches explicitly if equal extents
are required, while preserving the existing physical-shape and dtype validation.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/ir/op/tile_ops/matmul.cpp`:
- Around line 263-279: Validate in BuildGemvResultType that lhs_valid[0]
represents exactly one logical row before constructing the result, rejecting
non-single-row lhs inputs. Preserve the physical output shape [16, N] and
existing rhs validity handling, and do not propagate a larger lhs row extent
into tile_view.valid_shape.

---

Nitpick comments:
In `@src/ir/op/tile_ops/matmul.cpp`:
- Around line 299-316: Update the gemv_acc result-type logic around
BuildGemvResultType so the returned valid_shape preserves the accumulator’s
existing valid extent instead of relying solely on the lhs/rhs product. Combine
acc_type’s valid shape with the product’s using the intended element-wise
maximum, or reject mismatches explicitly if equal extents are required, while
preserving the existing physical-shape and dtype validation.

In `@tests/st/runtime/ops/test_gemv.py`:
- Around line 457-468: In compute_expected, move the loop-invariant valid_k
assignment before the for chunk loop, matching the existing calculation in
get_program, and keep the loop’s matrix multiplication behavior unchanged.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: f660f89e-3544-4493-8a05-a8a3b1d8486c

📥 Commits

Reviewing files that changed from the base of the PR and between 478ddad and 37b5704.

📒 Files selected for processing (9)
  • docs/en/dev/ir/05-operators.md
  • docs/en/dev/ptoas-op-status.md
  • docs/zh-cn/dev/ir/05-operators.md
  • docs/zh-cn/dev/ptoas-op-status.md
  • src/ir/op/tile_ops/matmul.cpp
  • src/ir/op/tile_ops/memory.cpp
  • tests/st/runtime/ops/test_gemv.py
  • tests/ut/codegen/test_pto_codegen.py
  • tests/ut/ir/operators/test_tile_ops.py

Comment thread src/ir/op/tile_ops/matmul.cpp
@hashiqiqixian

Copy link
Copy Markdown
Contributor Author

Review follow-up: 97230be additionally requires gemv_acc accumulator valid_shape to match the computed GEMV result, preventing accumulation into a physically compatible but logically narrower tile. The runtime loop-invariant suggestion is test-only cleanup and does not affect the operator contract, so it was left unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

1 participant