Skip to content

feat: deprecate pl.Tensor[..., pl.DN] + strict TensorViewCanonical (RFC #1300 §2.4 + supplementary 1)#1347

Merged
Hzfengsy merged 4 commits into
hw-native-sys:mainfrom
lyfne123:rfc1300/dsl-cleanup
May 12, 2026
Merged

feat: deprecate pl.Tensor[..., pl.DN] + strict TensorViewCanonical (RFC #1300 §2.4 + supplementary 1)#1347
Hzfengsy merged 4 commits into
hw-native-sys:mainfrom
lyfne123:rfc1300/dsl-cleanup

Conversation

@lyfne123
Copy link
Copy Markdown
Collaborator

@lyfne123 lyfne123 commented May 12, 2026

Summary

Final DSL + verifier cleanup for RFC #1300. Two changes:

  1. Q4 (RFC supplementary 1) — deprecate the user-facing pl.Tensor[..., pl.DN] layout-only shorthand.
  2. §6 roadmap item — switch the TensorViewCanonical verifier from weak-mode (default) to strict-mode, making the "codegen entry has explicit stride" contract a hard invariant.

Q5 (pl.load(transpose=True) deprecation) is not included — the proposed migration path (pl.transpose + pl.load) doesn't compose under the current pipeline (ConvertTensorToTileOps lowers tensor.transpose → tile.transpose, which is the intentional and correct behaviour), so deprecating without a working migration would be misleading. Reverted from an earlier iteration of this PR.


Q4 — pl.Tensor[..., pl.DN] deprecation

What: TypeResolver emits a DeprecationWarning when the layout-only DN shorthand is parsed in a tensor type annotation (both 3-arg and 4-arg forms). pl.TensorView(stride=[...], layout=pl.TensorLayout.DN) (explicit stride form) is still accepted for canonical pretty-print round-trip.

Migration:

# ❌ deprecated:
b: pl.Tensor[[K, N], pl.FP32, pl.DN]

# ✅ replacements:
b: pl.Tensor[[N, K], pl.FP32]                # source tensor shape, no layout marker
b: pl.Tensor[[K, N], pl.FP32, pl.TensorView(stride=[1, K], layout=pl.TensorLayout.DN)]  # explicit stride
# For matmul B^T:
tile_b = pl.load(b, [0, 0], [N, K], target_memory=pl.Mem.Mat, transpose=True)  # unchanged

Why: the layout-only shorthand forces users to mentally hold two coordinate systems at once (the IR-logical post-view shape and the runtime row-major shape) — exactly the ambiguity RFC #1300 aims to eliminate.


Verifier — strict mode default

What: The registry-default verifier for IRProperty::TensorViewCanonical now requires explicit stride (require_materialized=true). Since this verifier auto-fires after MaterializeTensorStrides (which declares it as a produced property), the "codegen entry has explicit stride" contract becomes a hard invariant.

Catches:

  • view.has_value() && stride.empty() reaching the post-MaterializeTensorStrides boundary. Previously tolerated; callers that constructed empty-stride views and bypassed the materialization pass would silently slip through.

Stays accepted:

  • Bare TensorType (!view.has_value()) — implicit ND-packed is canonical by construction.
  • Canonical strided views (stride[-1]==1 for ND, stride[-2]==1 for DN, outer-dim stride[k] >= stride[k+1]*shape[k+1]).

Escape hatch: passes.verify_tensor_view_canonical(program, require_materialized=False) still callable for the parse-time / early-pass window before materialization runs.


Files changed

File Change
python/pypto/language/parser/type_resolver.py Q4: DeprecationWarning at 3-arg / 4-arg tensor type annotations
src/ir/verifier/property_verifier_registry.cpp Strict-mode default for IRProperty::TensorViewCanonical
tests/ut/ir/transforms/test_verify_tensor_view_canonical.py test_registry_returns_weak_verifiertest_registry_returns_strict_verifier; new test_registry_accepts_bare_tensor_type
docs/{en,zh-cn}/dev/passes/26-materialize_tensor_strides.md Update Produces / verifier-interaction notes to reflect the new strict default
docs/{en,zh-cn}/user/01-language_guide.md Rewrite Tensor Layouts section; document Q4 migration

Validation

  • cmake --build build --parallel — clean.
  • pytest tests/ut/ -n auto --maxprocesses 84632 passed / 41 skipped / 0 failed.
  • CI (previous commit, before strict-mode commit was added) passed all checks including a2a3 system tests.

Test plan

  • Q4: DeprecationWarning fires when pl.Tensor[..., pl.DN] is parsed.
  • Strict-mode verifier: view.has_value() && stride.empty() is now rejected by the registry default.
  • Bare TensorType still accepted by registry default.
  • All existing unit tests pass.
  • CI: clang-tidy, pre-commit, unit-tests (macos + ubuntu), fuzz-tests-sim, system-tests, system-tests-a5sim, pypto-lib-model.

Related

…pl.DN] (RFC hw-native-sys#1300 supplementary 1 + 2)

Implements both RFC hw-native-sys#1300 supplementary proposals (Q4 + Q5) as
``DeprecationWarning``s at the user-facing surface. End-to-end behaviour
is unchanged — existing code continues to compile, but each deprecated
form now emits a warning suggesting the canonical replacement.

**Q5 — ``pl.load(..., transpose=True)`` (DSL deprecation):**

- ``pl.load`` in ``python/pypto/language/op/tile_ops.py`` emits a
  ``DeprecationWarning`` when ``transpose=True`` is passed. The migration
  hint points to ``pl.transpose(tensor, -2, -1) + pl.load(...)`` — see
  RFC §4.2 canonical pair: ``pl.transpose`` is a pure metadata
  reinterpret, and a DN-tagged ``tile.load`` produces the same TileType
  the old ``transpose=True`` swap produced.
- Rationale (RFC supplementary 2): mixing a view-reinterpret into a
  memory-copy op breaks the orthogonality of ``pl.slice`` / ``pl.reshape``
  / ``pl.transpose``. The new pattern is also more reusable — one
  ``b_t = pl.transpose(b, -2, -1)`` can feed multiple K-tile loads.

**Q4 — ``pl.Tensor[..., pl.DN]`` (DSL deprecation):**

- ``TypeResolver`` in ``python/pypto/language/parser/type_resolver.py``
  emits a ``DeprecationWarning`` when the layout-only DN shorthand is
  parsed in a tensor type annotation (both 3-arg and 4-arg forms). The
  migration hint lists the three canonical replacement patterns:
  - source tensor shape, no layout marker;
  - derive DN at use site via ``pl.transpose(x, -2, -1)``;
  - inherit DN through slice/reshape from a DN-producing op.
- Rationale (RFC supplementary 1): the layout-only shorthand forces
  users to mentally hold two coordinate systems at once (the IR-logical
  post-view shape and the runtime row-major shape) — exactly the
  ambiguity RFC hw-native-sys#1300 aims to eliminate. ``pl.TensorView(stride=[...],
  layout=pl.TensorLayout.DN)`` is still accepted (explicit stride form
  for canonical pretty-print round-trip).

**Migration:**

- ``examples/models/04_paged_attention.py`` and ``06_paged_attention_dynamic.py``
  are migrated to demonstrate the new pattern. Other examples and tests
  that still use the deprecated forms will surface ``DeprecationWarning``s
  during execution; they can be migrated incrementally before the eventual
  hard removal.
- ``docs/en/user/01-language_guide.md`` and ``docs/zh-cn/user/01-language_guide.md``
  rewrite the Tensor Layouts section and add a new "Matmul B^T via
  Transposed View" pattern.

Unit tests: cmake build clean, ``pytest tests/ut/ -n auto`` →
**4631 passed / 41 skipped / 0 failed**. (Existing tests continue to
exercise the deprecated forms — pytest doesn't fail on
``DeprecationWarning`` by default; we observe ~140 new warnings from
internal test fixtures that intentionally use the legacy patterns.)

Related: closes Q4 + Q5 from the RFC hw-native-sys#1300 supplementary proposal.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 12, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

This PR deprecates two transpose-related shortcuts in the pypto language: the pl.load(..., transpose=True) parameter and the pl.DN layout shorthand in type annotations. It adds DeprecationWarning infrastructure at type resolution and load time, updates documentation with migration guidance across English and Chinese, and migrates example kernels to use explicit pl.transpose views.

Changes

Transpose and DN layout deprecation

Layer / File(s) Summary
Type annotation deprecation for pl.DN
python/pypto/language/parser/type_resolver.py
Added warnings import and _warn_on_user_facing_dn_layout helper to emit DeprecationWarning when pl.DN is used in pl.Tensor[..., layout] subscript forms (both 3-arg and 4-arg), before tensor type resolution completes.
Load operation transpose parameter deprecation
python/pypto/language/op/tile_ops.py
Added DeprecationWarning when transpose=True is passed to pl.tile.load, with updated docstring documenting the migration to explicit pl.transpose before load.
Migration documentation in English and Chinese
docs/en/user/01-language_guide.md, docs/zh-cn/user/01-language_guide.md
Rewrote Tensor Layouts sections to explain layouts are derived from producing ops, documented pl.DN deprecation with migration guidance, added Matmul B^T pattern showing explicit pl.transpose before pl.load, and included deprecated back-compat form with deprecation rationale.
Example migrations to explicit transpose pattern
examples/models/04_paged_attention.py, examples/models/06_paged_attention_dynamic.py
Updated QK matmul kernels to explicitly transpose tensors via pl.transpose(..., -2, -1) before pl.load, replacing pl.load(..., transpose=True) shorthand.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • hw-native-sys/pypto#1339: Rewrites and lowers tile.load(..., transpose=True) to use DN as_layout views inside InCore bodies while this PR deprecates the transpose kwarg in favor of explicit pl.transpose composition.
  • hw-native-sys/pypto#1241: Records explicit strides on tensor.transpose and adjusts codegen for tile.load(transpose=True) handling, complementing this PR's deprecation of transpose shortcuts.
  • hw-native-sys/pypto#753: Clarifies load/transpose semantics and offset/shape conventions in the same tile_ops.py and example code areas affected by this transpose deprecation.

Suggested reviewers

  • Hzfengsy

Poem

🐰 Transpose views now stand alone,
No shortcuts hidden, clearly shown!
The warnings guide the way forward clear—
From deprecated shortcuts we steer,
To composition, structured and bright! 🎉

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
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.
Description check ✅ Passed The description comprehensively explains the PR's purpose, changes, and rationale related to RFC #1300 supplementary proposals Q4 and Q5.
Title check ✅ Passed The title accurately reflects the main change: deprecating pl.Tensor[..., pl.DN] and introducing stricter TensorViewCanonical handling, which aligns with the core modifications across the codebase and documentation.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


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 and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 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 `@docs/en/user/01-language_guide.md`:
- Around line 78-80: The doc text now states pl.NZ is tile-only but the earlier
example still uses pl.NZ as a TensorType annotation (pl.Tensor[[64, 128],
pl.FP16, pl.NZ]); update that example to match the rule by removing pl.NZ from
the TensorType and instead showing NZ used in a tile context (e.g., use
pl.Tensor[[64,128], pl.FP16] and show pl.Tile[..., pl.NZ] for the NZ usage) so
examples and explanation (pl.NZ, pl.Tile, pl.Tensor) are consistent.

In `@docs/zh-cn/user/01-language_guide.md`:
- Around line 73-74: The NZ usage in earlier example is inconsistent: NZ is
tile-only and should not appear as a TensorType annotation; update the earlier
example that uses pl.Tensor[..., pl.NZ] (the one showing pl.Tensor[[64, 128],
pl.FP16, pl.NZ]) to remove pl.NZ from the TensorType and instead show NZ in a
pl.Tile expression (e.g., demonstrate pl.Tile[..., pl.NZ] or use pl.Tensor[[64,
128], pl.FP16] with a separate pl.Tile[..., pl.NZ] note), ensuring all examples
consistently treat pl.NZ as tile-only and keep references to pl.NZ, pl.Tile, and
pl.Tensor accurate.
🪄 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: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: aaf859bf-e9ac-4c96-8ce0-4fc386f32b6a

📥 Commits

Reviewing files that changed from the base of the PR and between daa6f9f and 83ae26b.

📒 Files selected for processing (6)
  • docs/en/user/01-language_guide.md
  • docs/zh-cn/user/01-language_guide.md
  • examples/models/04_paged_attention.py
  • examples/models/06_paged_attention_dynamic.py
  • python/pypto/language/op/tile_ops.py
  • python/pypto/language/parser/type_resolver.py

Comment thread docs/en/user/01-language_guide.md
Comment thread docs/zh-cn/user/01-language_guide.md
Copy link
Copy Markdown
Contributor

@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 deprecates the explicit pl.DN layout shorthand in tensor type annotations and the transpose=True parameter in pl.load, favoring the use of pl.transpose to derive transposed views. These changes align with RFC #1300 to ensure orthogonality and reduce coordinate system ambiguity. Documentation and examples have been updated accordingly, and deprecation warnings are now emitted during parsing and at runtime. Feedback suggests refining the deprecation warning message for distributed tensors to avoid potential namespace confusion.

if layout != ir.TensorLayout.DN:
return
warnings.warn(
f"pl.{type_name}[..., pl.DN] is deprecated (RFC #1300 supplementary 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 warning message uses pl.{type_name} as a prefix. While this is correct for Tensor, DistributedTensor is typically imported from the pypto.language.distributed namespace (often aliased as pld). If a user uses pld.DistributedTensor[..., pl.DN], the warning pl.DistributedTensor[...] might be slightly confusing. Consider making the prefix more generic or detecting the actual namespace if possible, though pl. is a reasonable default for the project.

lyfne123 added 3 commits May 12, 2026 18:52
…recation guidance

System tests (a2a3) on PR hw-native-sys#1347 failed because the migrated examples
(``04_paged_attention.py`` / ``06_paged_attention_dynamic.py``) used
``pl.transpose(tensor) + pl.load(transposed)``, but this composition is
not currently supported end-to-end: ``ConvertTensorToTileOps`` registers
``tensor.transpose → tile.transpose`` (via ``RegisterSimple``), so the
``pl.transpose`` result becomes a TileType, and the downstream
``pl.load`` type check rejects it ("requires first argument to be a
TensorType, but got TileType" at memory.cpp:98).

This commit:

- Reverts the two example migrations back to the deprecated
  ``pl.load(..., transpose=True)`` form so the system tests pass again.
- Softens the ``DeprecationWarning`` text in ``tile_ops.py:load`` to be
  honest about the migration status — it explains that the intended
  end state (tensor-level view + plain ``pl.load``) is not yet
  composable in the pipeline, asks callers to avoid introducing new
  uses, and clarifies that no rewrite is required yet.
- Rewrites the "Matmul B^T via Transposed View" section in
  ``docs/{en,zh-cn}/user/01-language_guide.md`` to surface the same
  migration-status caveat, with the deprecated form labeled as
  "keep using" until the lowering fix lands.

Q4 deprecation (``pl.Tensor[..., pl.DN]``) is unaffected — that one has
a working migration today via the three patterns listed in the warning
(source-shape, ``pl.transpose`` at use site for non-load consumers,
``pl.slice`` from a DN-producing op).

Follow-up: a separate PR will update ``ConvertTensorToTileOps`` so
``tensor.transpose`` whose consumer is ``tile.load`` stays as a
tensor-level view (and the existing ``LowerTransposeLoadParamLayout``
pass picks up the slack). After that, the ``pl.transpose + pl.load``
migration becomes usable and we can re-migrate examples + tighten the
deprecation message.

Unit tests: cmake build clean, ``pytest tests/ut/ -n auto`` → 4631
passed / 41 skipped / 0 failed.
…ToTileOps lowering is intentional

Per maintainer feedback: ``ConvertTensorToTileOps`` registering
``tensor.transpose → tile.transpose`` (via ``RegisterSimple``) is the
correct lowering for the existing pipeline; the RFC hw-native-sys#1300
supplementary 2 ``pl.transpose + pl.load`` migration path was the
wrong shape for it (and broke system tests on PR hw-native-sys#1347's first
attempt). Deprecating ``pl.load(..., transpose=True)`` without a
working migration target is misleading, so this commit drops Q5
entirely from this PR.

Changes:

- ``python/pypto/language/op/tile_ops.py``: remove the
  ``DeprecationWarning`` in ``load(...)`` and revert the docstring
  back to its original form (no migration note for ``transpose=True``).
- ``docs/{en,zh-cn}/user/01-language_guide.md``: delete the "Pattern:
  Matmul B^T via Transposed View" section that was promoting the
  unsupported migration; also tone down the Tensor Layouts section so
  it no longer recommends ``pl.transpose(t, -2, -1)`` as a migration
  target for the deprecated ``pl.Tensor[..., pl.DN]`` shorthand. The
  recommended replacement now is simply: drop the layout marker, write
  the runtime shape, and continue using ``pl.load(transpose=True)`` for
  matmul B^T.

Q4 (``pl.Tensor[..., pl.DN]`` deprecation) is unchanged — that path
has a working migration today via "drop the layout marker, use runtime
shape" plus the existing ``transpose=True`` mechanism.

Validation: ``cmake --build`` clean; ``pytest tests/ut/ -n auto`` →
4631 passed / 41 skipped / 0 failed. Test-suite DeprecationWarning
count dropped from 650 → 532, matching the removal of the per-call
Q5 warning.
…ative-sys#1300 §2.4 codegen-entry contract)

Closes RFC hw-native-sys#1300 §6 roadmap item: the registry-default verifier for
``IRProperty::TensorViewCanonical`` now enforces explicit stride
(``require_materialized=true``). Because the verifier auto-fires after
``MaterializeTensorStrides`` (which declares it as a produced property),
the "codegen entry has explicit stride" contract becomes a hard invariant
instead of a convention.

What this catches:

- ``view.has_value() && stride.empty()`` reaching the post-MaterializeTensorStrides
  boundary. That state was previously tolerated by the weak-mode default;
  callers that constructed empty-stride views and bypassed the
  materialization pass would silently slip through.

What stays accepted:

- Bare ``TensorType`` (``!view.has_value()``) — implicit ND-packed is canonical
  by construction.
- Explicit canonical strided views (``stride[-1]==1`` for ND, ``stride[-2]==1``
  for DN, outer-dim ``stride[k] >= stride[k+1]*shape[k+1]``) — the RFC §2.2
  strided-family forms.

Public API: ``passes.verify_tensor_view_canonical(program, require_materialized=)``
still accepts both modes; pass ``False`` for the parse-time / early-pass
window before materialization runs.

Files changed:

- ``src/ir/verifier/property_verifier_registry.cpp``: flip
  ``require_materialized`` from ``false`` to ``true`` at the registry
  registration; rewrite the explanatory comment.
- ``tests/ut/ir/transforms/test_verify_tensor_view_canonical.py``: the
  ``test_registry_returns_weak_verifier`` test asserted the old default
  (empty stride accepted). Replaced with
  ``test_registry_returns_strict_verifier`` (expects the rejection +
  matches the diagnostic message) and added
  ``test_registry_accepts_bare_tensor_type`` to lock in the bare-tensor
  carve-out.
- ``docs/{en,zh-cn}/dev/passes/26-materialize_tensor_strides.md``: update
  the Produces / verifier-interaction notes to reflect the new default.

Validation: cmake build clean; ``pytest tests/ut/ -n auto`` →
4632 passed / 41 skipped / 0 failed.
@lyfne123 lyfne123 changed the title feat(language): deprecate pl.load(transpose=True) and pl.Tensor[..., pl.DN] (RFC #1300 supplementary 1+2) feat: deprecate pl.Tensor[..., pl.DN] + strict TensorViewCanonical (RFC #1300 §2.4 + supplementary 1) May 12, 2026
@Hzfengsy Hzfengsy merged commit 2b6d2a1 into hw-native-sys:main May 12, 2026
9 checks passed
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.

2 participants