Skip to content

feat(ir): add MX LeftScale/RightScale memspaces and scale load/move - #2195

Open
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:feat/mx-scale-memspace
Open

feat(ir): add MX LeftScale/RightScale memspaces and scale load/move#2195
yanghaoran29 wants to merge 1 commit into
hw-native-sys:mainfrom
yanghaoran29:feat/mx-scale-memspace

Conversation

@yanghaoran29

@yanghaoran29 yanghaoran29 commented Jul 29, 2026

Copy link
Copy Markdown
Contributor

Summary

  • Add Ascend950 MX scale memory spaces (LeftScale / RightScale) with matching SoC, reuse, bindings, and memory-map support.
  • Enable tile.load(..., mx_layout=mx_a_zz|mx_b_nn) (Mat-only, stamp target_memory) and Mat→scale tile.move with UINT8/FP8E8M0 checks and UINT8→FP8E8M0 promotion; preserve fractal=32 through Infer.
  • Align Python parser implicit TileView defaults with C++; ship en/zh docs and UTs for the scale load/move path only (no matmul yet).

Test plan

  • SKIP=pyright pre-commit run --from-ref upstream/main --to-ref HEAD (pyright skipped locally due to env install/network)
  • UT: tests/ut/ir/operators/test_mx_scale_memspace.py, tests/ut/ir/operators/test_mx_ops.py (mx load / dtype-space)
  • UT: tests/ut/backend/test_backend_950.py, tests/ut/tools/test_memory_map.py
  • Confirm branch has a single commit, no Co-authored-by, and docs do not claim matmul/tget landing

@coderabbitai

coderabbitai Bot commented Jul 29, 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: 2cd874a5-7301-4441-86d5-4e4865d62943

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

Adds Ascend950 MX scale support through new LeftScale and RightScale memory spaces, MX-aware tile.load and tile.move handling, preserved fractal-32 tile views, PTO code generation, memory paths, inference rules, tests, and documentation.

Changes

MX scale memory support

Layer / File(s) Summary
Memory contracts and backend topology
include/pypto/ir/*, src/ir/memref.cpp, src/backend/common/soc.cpp, python/pypto/tools/*
Defines LeftScale and RightScale across IR, Python bindings, memory conversions, implicit tile views, Ascend950 topology, memory display ordering, and pipeline classification.
MX tile API and type inference
python/pypto/{ir,language}/op/tile_ops.py, src/ir/op/tile_ops/memory.cpp, src/ir/op_registry.cpp, src/ir/transforms/infer_tile_memory_space_pass.cpp
Adds mx_layout handling, MX validation, Mat target stamping, scale destination types, dtype promotion, and preservation of fractal-32 tile views during inference.
PTO load and move code generation
src/backend/common/pto_ops_memory.cpp, src/backend/common/pto_ops_elementwise.cpp
Emits MX-specific tensor views and layout attributes, and makes tile.move emission and elision shape-aware.
Coverage and documentation
tests/ut/*, docs/en/*, docs/zh-cn/*
Covers MX loads, scale moves, backend paths, memory maps, and inference behavior, and documents the new Ascend950 MX rules.

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

Sequence Diagram(s)

sequenceDiagram
  participant PythonTileOps
  participant OpRegistry
  participant TileTypeDeduction
  participant InferTileMemorySpace
  participant PTOCodegen
  PythonTileOps->>OpRegistry: create tile.load with mx_layout
  OpRegistry->>OpRegistry: default target_memory to Mat
  OpRegistry->>TileTypeDeduction: validate layout, dtype, rank, and derive TileView
  TileTypeDeduction-->>OpRegistry: return Mat tile with fractal=32
  OpRegistry->>InferTileMemorySpace: infer memory space
  InferTileMemorySpace-->>OpRegistry: preserve MX TileView
  OpRegistry->>PTOCodegen: lower MX tile.load
  PTOCodegen->>PTOCodegen: emit MX tensor view and pto.tload
Loading

Possibly related PRs

Suggested labels: enhancement

Poem

I’m a bunny with scales in two halls,
LeftScale and RightScale answer calls.
MX tiles hop, fractal thirty-two,
Mat keeps their views crisp and true.
PTO moves with a cheerful glow—
Hop, hop, and let the scale paths flow!

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 38.18% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
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.
Title check ✅ Passed The title clearly summarizes the main change: new MX LeftScale/RightScale memspaces plus scale load/move support.
Description check ✅ Passed The description is directly related and accurately summarizes the memspaces, load/move behavior, docs, and tests in the PR.

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: 99aa17eaf9

ℹ️ 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/memory.cpp
Comment thread src/ir/op/tile_ops/memory.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 (1)
src/ir/op/tile_ops/memory.cpp (1)

395-465: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Missing 2D-shape validation for tile.move into LeftScale/RightScale.

DeduceTileLoadType's MX path enforces tensor_type->shape_.size() == 2 (line 162-164), but DeduceTileMoveType's LeftScale/RightScale path only validates dtype, not shape/rank — even though fractal=32 is only meaningful for a 2D scale tile, and the project's own docs (docs/zh-cn/dev/ir/05-operators.md, PTOAS constraints table) state a shape-matching precondition for Mat→Scale tmov. A malformed-rank tile moved into LeftScale/RightScale would pass this type deduction and only fail deep in codegen or on hardware.

♻️ Proposed check
   DataType out_dtype = tile_type->dtype_;
   if (space == MemorySpace::LeftScale || space == MemorySpace::RightScale) {
+    CHECK(input_shape.size() == 2)
+        << "The operator " << op_name << " into LeftScale/RightScale requires a 2D tile, got rank "
+        << input_shape.size();
     CHECK(out_dtype == DataType::UINT8 || out_dtype == DataType::FP8E8M0)
         << "The operator " << op_name
         << " into LeftScale/RightScale requires UINT8 or FP8E8M0 dtype, but got " << out_dtype.ToString();
🤖 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/memory.cpp` around lines 395 - 465, Update
DeduceTileMoveType so the LeftScale and RightScale branches validate that the
input tile shape is 2D before constructing the destination TileType. Preserve
the existing UINT8/FP8E8M0 dtype validation and reject invalid ranks with an
operator-context error consistent with DeduceTileLoadType.
🤖 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 `@tests/ut/ir/operators/test_mx_scale_memspace.py`:
- Around line 83-96: Update test_mx_layout_load_defaults_to_mat so tile.load is
called with mx_layout="mx_a_zz" but without the explicit target_memory argument.
Keep the existing assertions, ensuring the test validates that the default
memory space is ir.MemorySpace.Mat.

---

Nitpick comments:
In `@src/ir/op/tile_ops/memory.cpp`:
- Around line 395-465: Update DeduceTileMoveType so the LeftScale and RightScale
branches validate that the input tile shape is 2D before constructing the
destination TileType. Preserve the existing UINT8/FP8E8M0 dtype validation and
reject invalid ranks with an operator-context error consistent with
DeduceTileLoadType.
🪄 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: 9bd5fe4e-15d1-4b56-96d1-4dcb8898817d

📥 Commits

Reviewing files that changed from the base of the PR and between 1784c63 and 99aa17e.

📒 Files selected for processing (31)
  • docs/en/dev/07-memory-map.md
  • docs/en/dev/ir/05-operators.md
  • docs/en/dev/passes/17-infer_tile_memory_space.md
  • docs/zh-cn/dev/07-memory-map.md
  • docs/zh-cn/dev/ir/05-operators.md
  • docs/zh-cn/dev/passes/17-infer_tile_memory_space.md
  • include/pypto/ir/memory_space.h
  • include/pypto/ir/pipe.h
  • include/pypto/ir/tile_view_semantics.h
  • include/pypto/ir/transforms/utils/attrs.h
  • python/bindings/modules/ir.cpp
  • python/pypto/ir/op/tile_ops.py
  • python/pypto/ir/type.py
  • python/pypto/language/op/tile_ops.py
  • python/pypto/language/parser/type_resolver.py
  • python/pypto/pypto_core/ir.pyi
  • python/pypto/tools/memory_map.py
  • python/pypto/tools/templates/memory_map.html
  • src/backend/common/pto_ops_elementwise.cpp
  • src/backend/common/pto_ops_memory.cpp
  • src/backend/common/soc.cpp
  • src/codegen/pto/pto_type_utils.cpp
  • src/ir/memref.cpp
  • src/ir/op/tile_ops/memory.cpp
  • src/ir/op_registry.cpp
  • src/ir/transforms/infer_tile_memory_space_pass.cpp
  • src/ir/transforms/memory_reuse_pass.cpp
  • tests/ut/backend/test_backend_950.py
  • tests/ut/ir/operators/test_mx_ops.py
  • tests/ut/ir/operators/test_mx_scale_memspace.py
  • tests/ut/tools/test_memory_map.py

Comment thread tests/ut/ir/operators/test_mx_scale_memspace.py
yanghaoran29 added a commit to yanghaoran29/pypto that referenced this pull request Jul 29, 2026
- Validate 2D MX load windows and scale tiles
- Require Mat inputs for LeftScale and RightScale moves
- Cover default Mat inference and invalid scale paths
@yanghaoran29
yanghaoran29 force-pushed the feat/mx-scale-memspace branch 3 times, most recently from 5668158 to b1fb419 Compare July 29, 2026 04:39
yanghaoran29 added a commit to yanghaoran29/pypto that referenced this pull request Jul 29, 2026
Reject unsafe MX loads from strided or slice-derived tensors.

Enforce fixed scale move layouts and reject scale tile.create targets.

Add PTO codegen and orchestration provenance regressions for PR hw-native-sys#2195.
Add LeftScale and RightScale memory spaces with MX load and move lowering.

Preserve hardware-fixed scale layouts, reject unsupported create and unsafe tensor views, and cover PTO codegen and orchestration slice provenance.
@yanghaoran29
yanghaoran29 force-pushed the feat/mx-scale-memspace branch from 4232652 to 72caa6f Compare July 29, 2026 07:44
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