Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
147 commits
Select commit Hold shift + click to select a range
8078bdb
feat: first stage of vmi
mouliangyu Jun 17, 2026
943775f
feat: support num_groups layout
mouliangyu Jun 18, 2026
ce65db3
feat: new layout-lowering design
mouliangyu Jun 21, 2026
37ff218
Add VMI layout assignment lowering coverage
mouliangyu Jun 22, 2026
f038bc4
Support S32 partial grouped mask lowering
mouliangyu Jun 22, 2026
702ae07
Support dynamic S32 grouped mask lowering
mouliangyu Jun 22, 2026
849b6cb
Clarify VMI layout case coverage gaps
mouliangyu Jun 22, 2026
e24ab16
Record VMI layout coverage audit
mouliangyu Jun 22, 2026
a60463c
Add dynamic S32 group mask runtime coverage
mouliangyu Jun 22, 2026
93b5bbf
Detail VMI layout assignment request rules
mouliangyu Jun 22, 2026
cdf5006
Complete VMI layout request builder coverage
mouliangyu Jun 22, 2026
7381132
Inline private VMI physical helpers before VPTO emission
mouliangyu Jun 22, 2026
8115cb0
Validate required VMI selected plans
mouliangyu Jun 22, 2026
e6d95dc
Document VMI layout closure matrix
mouliangyu Jun 22, 2026
49d3a8d
Add VMI dense reduce multi-consumer case
mouliangyu Jun 22, 2026
621045b
Remove VMI selected plan attrs
mouliangyu Jun 22, 2026
fa28545
Implement VMI layout optimization pipeline
mouliangyu Jun 22, 2026
fcd2b22
Support multi-chunk VMI group reduce slots
mouliangyu Jun 22, 2026
6a09a25
Implement typed VMI group reduce lowering
mouliangyu Jun 22, 2026
9ab5773
Implement VMI layout support lowering
mouliangyu Jun 23, 2026
84c8f9b
Support partial packed VMI group slots
mouliangyu Jun 23, 2026
dbb635b
Add VMI introduction design doc
mouliangyu Jun 23, 2026
b809086
Fold deinterleaved VMI loads through vldsx2
mouliangyu Jun 23, 2026
7e4bf68
Document VMI layout assignment mechanism
mouliangyu Jun 23, 2026
c104345
Illustrate VMI layout equivalence classes
mouliangyu Jun 23, 2026
eff7414
Add VMI histogram lowering support
mouliangyu Jun 24, 2026
8d86019
Remove VMI load full read attribute
mouliangyu Jun 24, 2026
841c45b
Define VMI scatter as unique-index op
mouliangyu Jun 24, 2026
7337c05
Add VMI group max quant kernel case
mouliangyu Jun 24, 2026
16ebb24
feat: add cce-aligned vmi kernel cases
mouliangyu Jun 24, 2026
cb59b1b
Rename VMI sparse layout to lane stride
mouliangyu Jun 28, 2026
3c733e8
Adjust VMI reduction result shapes
mouliangyu Jun 28, 2026
93b8ac5
Add VMI simdvf per-block FP8 cast kernel case
mouliangyu Jun 28, 2026
2f8b806
Support two-way VMI interleaved memory ops
mouliangyu Jun 29, 2026
aaca882
Add VMI group broadcast E2B lowering
mouliangyu Jun 29, 2026
a368d82
Rename VMI layout fold pass
mouliangyu Jun 29, 2026
35f2289
Implement VMI relation-aware rematerialization
mouliangyu Jun 29, 2026
875135d
Optimize equivalent VPTO vcvt normalization
mouliangyu Jun 29, 2026
2aee652
Run public LICM in VMI pipeline
mouliangyu Jun 29, 2026
cf22015
Support VMI u8 to u16 integer extension
mouliangyu Jun 29, 2026
d6b8665
Optimize VMI trunci layout rematerialization
mouliangyu Jun 29, 2026
a81a1ff
Generalize VMI dense lane-stride layouts
mouliangyu Jun 30, 2026
7c3b8e9
Support arity-driven VMI cast layouts
mouliangyu Jun 30, 2026
c59193b
Clarify PTO-Gym validation skill scope
mouliangyu Jun 30, 2026
f6db48b
Fix arity-driven VMI cast layout selection
mouliangyu Jun 30, 2026
7873539
test(vmi): use group-slot result shapes in runtime cases
mouliangyu Jun 30, 2026
2a4b1f7
Optimize VMI group broadcast load layout
mouliangyu Jul 1, 2026
1535b02
Support compact VMI f32 to fp8 truncf layouts
mouliangyu Jul 2, 2026
08e1473
docs: describe vmi layout propagation model
mouliangyu Jul 1, 2026
8ea096f
fix: migrate VMI conversion to LLVM 21 APIs
mouliangyu Jul 3, 2026
2f01a56
Refine VMI layout propagation
mouliangyu Jul 5, 2026
c1e4141
Support BRC group broadcast loads
mouliangyu Jul 7, 2026
78527e7
Clean up VMI target capability checks
mouliangyu Jul 7, 2026
e07a14d
Avoid clearing inactive group-reduce lanes
mouliangyu Jul 7, 2026
37006ee
Prefer lane-stride layouts for narrow VMI stores
mouliangyu Jul 8, 2026
0eb0be6
feat(ptodsl): support vmi backend in ptodsl
Zhendong404 Jul 1, 2026
7591c9b
Add pto.vintlv/pto.vdintlv op in PTODSL
Zhendong404 Jul 9, 2026
bf64386
Add a kernel-test framework and rope kernels
Zhendong404 Jul 9, 2026
804f7e0
Add license headers to kernel test
Zhendong404 Jul 9, 2026
51703ec
Support manually specifying kernel directory
Zhendong404 Jul 9, 2026
b0b2b50
Fix deinterleaved VMI memory footprint
mouliangyu Jul 9, 2026
22ecba6
vmi new
Jul 8, 2026
742b6fd
Fix hardening build unused mask helper
mouliangyu Jul 10, 2026
6b1d3e9
Allow non-VMI unrealized casts after VMI lowering
mouliangyu Jul 10, 2026
0c5ddb3
feat(ptodsl): Support vmi in ptodsl
Zhendong404 Jul 9, 2026
e4e9bf7
fix(vmi): fix missing attributes after print
Zhendong404 Jul 10, 2026
7a7420a
fix(vmi): bypass mask operand to the legacy vmi ops
Zhendong404 Jul 10, 2026
4cb7a4b
test: implement rope vmi version with ptodsl
Zhendong404 Jul 10, 2026
790cc1c
Add a rewrite-kernel-with-vmi skill
Zhendong404 Jul 10, 2026
0e22a44
docs(ptodsl): update vmi user guide of ptodsl
Zhendong404 Jul 10, 2026
c1306af
test(kernel-test): add anti_mx_quant ptodsl kernel
Zhendong404 Jul 10, 2026
23fd769
test(kernel-test): fix the precision of the dequant op
Zhendong404 Jul 11, 2026
76e15f7
vcvt sign/unsign in type
Jul 12, 2026
f028b13
vmula support mask but drop it while lowering
Jul 12, 2026
7a6a486
handle passthru in vgather and vscatter
Jul 12, 2026
0fa0e75
add and fix test in vmi_new
Jul 10, 2026
70377d2
move todo lit to vmi_new/TODO/
Jul 12, 2026
db12255
update create_mask in cases
Jul 12, 2026
d506f51
vload add lowering to group_broadcast_load
Jul 12, 2026
add0a09
vand、vor、vxor、vnot support mask
Jul 12, 2026
c663b99
delete fail test and some tmp file
Jul 12, 2026
954567e
dsl delete sign for vcvt
Jul 13, 2026
93eed11
test(kernel-test): add ComputeScale part for the dequant op
Zhendong404 Jul 13, 2026
43154e9
Add table-driven VMI interleave layouts
mouliangyu Jul 13, 2026
804425a
Extend VMI reduction type support
mouliangyu Jul 13, 2026
fe283d8
[VMI] Run lowering after VPTO tile pipeline
mouliangyu Jul 13, 2026
c3c2b03
[VMI] Enable VPTO pipeline by default
mouliangyu Jul 13, 2026
85dc2bc
Fix VMI integer compare signedness lowering
mouliangyu Jul 13, 2026
44dd617
[VMI] Lower dequant scale broadcast with E2B
mouliangyu Jul 14, 2026
b4ae735
fix(ptodsl): change ast rewrite order to fix liveness pollution issue
Zhendong404 Jul 13, 2026
130fabc
fix(ptodsl): support comprehension-aware region during ast rewrite
Zhendong404 Jul 13, 2026
454ff79
fix(ptodsl): normalize conditional expression before ast rewrite
Zhendong404 Jul 13, 2026
a54edd4
fix(vmi): constraint vmi.vcvt rounding mode to A/H/Z
Zhendong404 Jul 13, 2026
5bb9e9e
fix(ptodsl): explicitly request reassoc argument for float vmi.vcadd
Zhendong404 Jul 13, 2026
164f85b
test(kernel-test): align dequant implementation with cce
Zhendong404 Jul 13, 2026
5dcf60b
fix(emission): enable --cce-long-scbz=true by default
Zhendong404 Jul 14, 2026
b928d66
test(kernel-test): add --emit-mlir option to generate vmi/mi IR
Zhendong404 Jul 14, 2026
d117716
docs: update license header
Zhendong404 Jul 14, 2026
ae18a4f
fix(vmi): fix mask granularity for vintlv/vdintlv
Zhendong404 Jul 14, 2026
f2d75d6
feat(ptodsl): support ast rewrite for static list slot
Zhendong404 Jul 14, 2026
cd5e235
test(kernel-test): fix rope op implementation
Zhendong404 Jul 14, 2026
be4f172
add vmi docs
Jul 13, 2026
4a0c76e
add chist
Jul 14, 2026
99e9be3
rename chist/dhist to vchist/vdhist
Jul 14, 2026
770ad74
Support mask carrier granularity layout lowering
mouliangyu Jul 14, 2026
36868e4
test: update intlv mask remat expectations
mouliangyu Jul 14, 2026
cc17114
fix sim cases
mouliangyu Jul 14, 2026
57a17a7
Fix VMI fp8 narrow and host stub build on znver4.
learning-chip Jul 14, 2026
8e27843
Revert GCC and FP8 fixes; keep znver4/5 host CPU clamp only.
Jul 15, 2026
df438d0
Narrow VPTO loop counters to i16
mouliangyu Jul 15, 2026
71c1802
feat(vpto): add Bisheng vector scheduler switch
mouliangyu Jul 15, 2026
97f25b8
feat(vmi): add canonical TileOp provider
Jul 15, 2026
a19f769
fix(vmi): stop after replacing mask ensure
Jul 15, 2026
a56dcd9
docs(vmi): define RFC-minimal VF fusion pipeline
Jul 15, 2026
1c17ded
fix(vmi): register canonical templates explicitly
Jul 15, 2026
d79558a
test(vmi): clarify static softmax coverage
Jul 15, 2026
738bcc2
feat(vmi): lower static flash attention softmax
Jul 15, 2026
9dd7b03
fix(vmi): defer vecscope inference to emission
Jul 15, 2026
204129e
refactor(vmi): declare candidate context constraints
Jul 16, 2026
ee7ab3f
feat(vmi): inline private helpers in VPTO pipeline
mouliangyu Jul 16, 2026
75d182a
Revert "refactor(vmi): declare candidate context constraints"
mouliangyu Jul 16, 2026
3e65c01
Revert "fix(vmi): defer vecscope inference to emission"
mouliangyu Jul 16, 2026
4beae56
Revert "feat(vmi): lower static flash attention softmax"
mouliangyu Jul 16, 2026
9f32f50
Revert "test(vmi): clarify static softmax coverage"
mouliangyu Jul 16, 2026
ca9feff
Revert "fix(vmi): register canonical templates explicitly"
mouliangyu Jul 16, 2026
223692f
Revert "docs(vmi): define RFC-minimal VF fusion pipeline"
mouliangyu Jul 16, 2026
76d661f
Revert "fix(vmi): stop after replacing mask ensure"
mouliangyu Jul 16, 2026
c143280
Revert "feat(vmi): add canonical TileOp provider"
mouliangyu Jul 16, 2026
238d71f
fix(ptodsl): add missing pto.vmula interface in ptodsl
Zhendong404 Jul 14, 2026
fa96f18
fix(ptodsl): fix mi/vmi mask type tracing
Zhendong404 Jul 14, 2026
f936f35
feat(ptodsl): add missing mi/vmi ops and missing vcvt rounding modes
Zhendong404 Jul 14, 2026
80447d0
fix(ptodsl): replace pto.copy_ubuf_to_ubuf with pto.mte_ub_ub
Zhendong404 Jul 15, 2026
f6b6ab4
fix(ptodsl): remove result_type argument in some vmi ops
Zhendong404 Jul 15, 2026
70c2c37
fix(vmi): support ui16 -> ui8 group vcvt
Zhendong404 Jul 15, 2026
cc7234d
fix(vmi): propagate deinterleave load layouts (#518)
mouliangyu Jul 16, 2026
25488ba
fix(ptoas): map cortex-x925 host cpu for Bisheng
peanutchan Jul 16, 2026
24c360f
feat(vmi): implement vmull pair-result lowering (#516)
HecreReed Jul 16, 2026
2838947
fix(vmi): lower group-slot lane-stride stores
mouliangyu Jul 16, 2026
1b53e26
feat(ptodsl): update ptodsl vmi interfaces
Zhendong404 Jul 16, 2026
453875d
feat(ptodsl): align vmi.vmull/vmi.vmula with vmi spec v0.1
Zhendong404 Jul 16, 2026
ea7c97a
skill: update rewrite-kernel-with-skill to align with vmi-v0.1
Zhendong404 Jul 16, 2026
24ce247
fix(ptodsl): adapt emission for vmi op with variadic mask
Zhendong404 Jul 17, 2026
8405832
chore: split ptoas and vmi release versions
Zhendong404 Jul 16, 2026
8b20a7f
chore(release): Bump VMI version to v0.1.1
Zhendong404 Jul 17, 2026
2d7b2b2
feat(vmi): support signed right shift lowering
mouliangyu Jul 17, 2026
13d507e
chore(ptoas): replace ptoas bin with python wrapper
Zhendong404 Jul 17, 2026
c792c90
ptoas: fix bisheng .ll input and Release quick_install
learning-chip Jul 20, 2026
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
12 changes: 6 additions & 6 deletions .codex/skills/pto-gym-vpto-validation/SKILL.md
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
---
name: pto-gym-vpto-validation
description: Run PTO-Gym validation from this PTOAS repo. Use when the user asks to run PTO-Gym SIM or board validation from the current source tree. Always force PTOAS onto the VPTO LLVM path instead of relying on the repo default backend.
description: Run bundled PTO-Gym exercise/validation cases. Use when the user explicitly asks for PTO-Gym, 3rdparty/PTO-Gym, or the PTO-Gym validation scripts. Always force PTOAS onto the VPTO path instead of relying on the repo default backend.
---

# PTO-Gym VPTO Validation

Use this skill when the task is specifically about:
- running `3rdparty/PTO-Gym/examples/pto/scripts/run_host_vpto_validation.sh`
- running `3rdparty/PTO-Gym/examples/pto/scripts/run_host_vpto_validation_parallel.sh`
- validating PTO-Gym cases from this PTOAS source tree
- validating bundled PTO-Gym exercise cases

## Required Rule

When PTO-Gym is run from this repo, do not rely on the default PTOAS backend.

Always pass PTOAS flags that force the VPTO LLVM path.
The current `ptoas` CLI spellings in this repo are `--pto-backend=vpto` and
`--vpto-emit-hivm-llvm`; do not shorten `--pto-backend` to `--backend`.
The current `ptoas` CLI spelling in this repo is `--pto-backend=vpto`; do not
shorten `--pto-backend` to `--backend`.

Use:

```bash
PTOAS_FLAGS='--pto-backend=vpto --vpto-emit-hivm-llvm --pto-arch a5'
PTOAS_FLAGS='--pto-backend=vpto --pto-arch a5'
```

If the caller already provides `PTOAS_FLAGS`, make sure these options are still
Expand All @@ -44,7 +44,7 @@ Typical simulator environment:
source /home/mouliangyu/.local/ascend/beta.2/cann-9.0.0-beta.2/set_env.sh
export ASCEND_HOME_PATH=/home/mouliangyu/.local/ascend/beta.2/cann-9.0.0-beta.2
export PTOAS_BIN=$PWD/build/tools/ptoas/ptoas
export PTOAS_FLAGS='--pto-backend=vpto --vpto-emit-hivm-llvm --pto-arch a5'
export PTOAS_FLAGS='--pto-backend=vpto --pto-arch a5'
```

## Canonical Commands
Expand Down
6 changes: 6 additions & 0 deletions .codex/skills/ptoas-project-development/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ description: Project development guidance for PTOAS. Use when Codex modifies PTO

When changing any user-visible behavior, update every relevant layer in the same change. Treat ODS, verifiers, lowering, command-line behavior, bindings, docs, examples, and tests as one public contract.

## File Orientation Before Editing

Before editing any existing file, read the file's header or top-level comments first. Check whether they describe the file's purpose, structure, section boundaries, or editing constraints, and follow those constraints when making changes.

When a change creates or depends on a file-level structure rule, ownership boundary, or important functional description, add or update that guidance in the file's top-level comments so future edits see it before jumping into local code.

## Layers To Keep In Sync

1. ODS and dialect definitions: `include/PTO/IR/*.td`
Expand Down
229 changes: 229 additions & 0 deletions .codex/skills/rewrite-kernel-with-vmi/SKILL.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
---
name: rewrite-kernel-with-vmi
description: Rewrite a complete AscendC/CANN kernel into an equivalent PTODSL Python implementation that mixes VMI compute with MI/MTE/sync orchestration. Use when Codex is given AscendC kernel code or CCE-style device code and must preserve the kernel ABI, split compute from movement/synchronization, translate pure vector/SIMT compute regions into pto.vmi semantics, translate non-compute regions into PTODSL micro-instructions, and validate the result with kernel.compile().mlir_text() plus the PTOAS VMI path.
---

# Rewrite Kernel With VMI

Rewrite the source kernel by preserving observable semantics first, then choosing
the clean PTODSL spelling. Do not mechanically replay physical register
choreography when a logical VMI expression captures the same algorithm.

## Workflow

1. Read the complete source kernel and all helpers/macros needed to understand
its ABI, loop bounds, offsets, memory movement, synchronization, and compute.
2. Read references only as needed:
- [vmi-dsl-spec.md](references/vmi-dsl-spec.md) for current PTODSL `pto.vmi`
API spelling, including `vload` / `vstore`, `create_mask`, `vci`, `vbrc`,
and common AscendC SIMD to VMI patterns.
- [mi-dsl-spec.md](references/mi-dsl-spec.md) for PTODSL non-VMI user-guide
navigation: kernel entry, buffers, control flow, MTE, sync, masks, SIMT.
- [vmi-mlir-spec.md](references/vmi-mlir-spec.md) when VMI IR semantics,
layout, mask, or PTOAS validation details matter.
3. Extract the PTODSL function interface:
- Convert each host-visible kernel argument one-for-one.
- Convert C++ template parameters to keyword-only `pto.const_expr` arguments.
- For template/generic variants, prefer one PTODSL function with
`pto.const_expr` parameters that select dtype, constants, and specialized
branches at compile time. Avoid generating many outer wrapper/probe
functions that only differ by dtype or template value.
- Use this default entry unless the source requires otherwise:

```python
@pto.jit(
name="<kernel_name>",
target="a5",
backend="vpto",
mode="explicit",
kernel_kind="vector",
insert_sync=False,
)
def <kernel_name>(..., *, CONST: pto.const_expr = ...):
...
```

Compile-time selection example:

```python
@pto.jit(...)
def kernel(x: pto.ptr(pto.f8e5m2, "ub"), y_addr: pto.i64, *, OUT_DTYPE: pto.const_expr = pto.f32):
y = pto.castptr(y_addr, pto.ptr(OUT_DTYPE, "ub"))
if OUT_DTYPE is pto.f32:
...
elif OUT_DTYPE is pto.bf16:
...
```

4. Split the source body into regions:
- Compute regions: pure vector/SIMT arithmetic, compare/select, conversion,
reduction, rearrange, or math. `membar` may stay in or near a compute
region when it only orders vector-visible UB effects.
- Non-compute regions: GM/UB/L1 movement, tile allocation, pointer/view
setup, `set_flag`/`wait_flag`, cross/intra flag sync, buffer handoff,
pipeline barriers, and host/core indexing.
5. Before writing PTODSL for each nontrivial compute region, show the user a
VMI rewrite design and wait for confirmation when the interaction allows it.
If the user is unavailable or explicitly asked for an end-to-end conversion,
continue with clearly stated assumptions.
6. Implement the PTODSL Python file:
- Use `from ptodsl import pto` and add `scalar` only when needed.
- Keep imports environment-agnostic. Do not add `Path(__file__)` parent
walks, local repo discovery, or `sys.path.insert(...)` blocks to find the
`ptodsl` package; configure installation, runner paths, or `PYTHONPATH`
outside the generated DSL file instead.
- Keep the source ABI recognizable.
- Write VMI/MI operations inline by default. Create small helper functions
only when a nontrivial instruction sequence is reused many times or when a
named helper preserves an important source-level abstraction.
- Use native Python `for` / `if` control flow. Use `pto.const_expr` and
`pto.static_range` for intentional compile-time specialization or
unrolling. Do not use `pto.for_` / `pto.if_` in new rewrites.
- Translate non-compute regions with PTODSL MI/MTE/sync operations.
- Translate compute regions with `pto.vmi` logical vectors.
7. Validate and iterate:
- First run or provide a compile helper that calls
`kernel.compile(...).mlir_text()`.
- Then pass the emitted MLIR to PTOAS with the VMI path:
`ptoas --pto-arch=a5 --pto-backend=vpto --enable-vmi <mlir_or_pto> -o /dev/null`.
- When the goal is to inspect lowered VMI output locally and no CANN/toolchain
environment is needed, prefer the bundled helper script:
`scripts/compile_vmi_to_vpto.sh <input.vmi.pto> [output.mi.pto]`.
It wraps `ptoas --pto-backend=vpto --enable-vmi --emit-vpto` and is the
default path for generating reviewable lowered artifacts from emitted
`vmi.pto` files.
- Fix PTODSL trace errors before PTOAS errors. Fix semantic mismatches before
layout/lowering workarounds.

## VMI Rewrite Design Gate

For every nontrivial compute region, present this compact review artifact before
coding the region:

```text
Compute region: <source lines / logical name>
Inputs:
- <UB pointer or scalar>, offset/stride formula, dtype, shape
Outputs:
- <UB pointer>, offset/stride formula, dtype, shape
Semantic algorithm:
- <elementwise/reduction/rearrange description independent of hardware parts>
Physical-only source details to collapse:
- <PART_*, EVEN/ODD, PK*, vintlv/vdintlv trees, temporary register halves>
VMI plan:
for off in ...:
mask = pto.vmi.create_mask(active_lanes, size=lanes) # or size=lanes, group=groups
x = pto.vmi.vload(..., size=lanes)
y = <logical compute>
pto.vmi.vstore(y, ..., mask)
Lane choice:
- lanes=<N>, dtype=<T>, reason=<full vreg/tail/per-row/grouping>
Assumptions:
- <tail, alignment, contiguous access, dtype reinterpretation, unsupported op>
```

Ask the user to confirm or correct this design when the algorithm is ambiguous,
when the source uses heavy physical packing/interleave, or when multiple VMI
forms are plausible. If there is no reply and progress is requested, implement
the stated assumptions and call them out in the result.

## Translation Rules

- Preserve the boundary contract. Do not drop source arguments merely because a
first rewrite does not use them yet.
- Do not embed local import bootstrapping in generated DSL code. The file should
import PTODSL normally, e.g. `from ptodsl import pto`, without walking parent
directories or mutating `sys.path` to locate a workspace checkout.
- Convert common source types as follows:
- `__gm__ half*` / semantic f16 storage -> `pto.ptr(pto.f16, "gm")`
- `__gm__ float*` -> `pto.ptr(pto.f32, "gm")`
- `int32_t` -> `pto.i32`
- `uint32_t` -> `pto.ui32`
- `int64_t` / addresses and byte counters -> `pto.i64` unless source intent is
clearly index-like.
- If a C++ boundary uses raw storage (`uint16_t*`, `uint8_t*`) but the semantic
element type is `f16`, `bf16`, fp8, or packed f4, keep the safest ABI spelling
and use `pto.castptr` internally when needed.
- Preserve templates and generic dtype choices with `pto.const_expr` whenever
possible. Use Python compile-time `if` branches and dtype variables to choose
pointer casts, VMI lane/dtype choices, conversion targets, and store paths. Only add
separate wrapper/probe functions when the ABI truly differs or the test
harness explicitly requires separate entry symbols.
- Use the current VMI surface shape: `vload(..., size=...)`, `vstore(..., mask=...)`,
`vci(..., size=...)`, `vbrc(..., size=...)`, and `create_mask(..., size=..., group=...)`.
Do not use the retired `create_group_mask` helper or legacy `result_type`
spellings in new rewrites.
- Keep source offset and stride formulas symbolic. Do not replace expressions
such as `vlForHalfNumber * 2` with a constant unless the source is already
specialized and the user asked for specialization.
- Avoid small one-off helpers around VMI/MI instruction sequences. Inline direct
PTODSL operations unless the code is long, reused repeatedly, or the source
abstraction is important enough to preserve by name.
- Use Python-native control flow for both dynamic device-side branches/loops and
ordinary structured code. Use `pto.static_range(...)` only for trace-time
static loops driven by Python values or `pto.const_expr` parameters. Do not
write `with pto.for_(...)` or `with pto.if_(...)` in new kernel rewrites.
- Use explicit `mode="explicit"` orchestration for full kernel rewrites. Do not
rely on auto-inserted sync unless the user requests an auto-mode rewrite.
- Prefer `pto.vmi.vload` from UB, logical compute, then `pto.vmi.vstore` to UB.
GM movement belongs to MTE/tile movement outside the VMI compute region.
- Use `pto.vmi.create_mask(active, size=lanes)` for dynamic tails. Put masks on
compute and store; do not assume a masked load is legal on every backend.
For grouped tails, use `create_mask(..., size=lanes, group=...)`.
- Collapse physical-only details such as `PART_P0`, `PART_EVEN`, `PART_ODD`,
packed store modes, and interleave trees when they only describe hardware
lowering of one logical vector.
- Do not introduce UB store+reload round trips just to satisfy a lowering issue
unless the user accepts the performance tradeoff.
- For TileLang input, treat it as future scope unless the user explicitly asks.
Preserve the same VMI design-gate workflow and translate the logical parallel
loop body rather than physicalizing it early.

## Optimization Tips

- When the logical algorithm is "an `N x VL` vector where each `VL` chunk is
multiplied by the same `VL`-lane scale vector", prefer one widened/grouped
VMI expression over a scalarized inner chunk loop.
- Good PTODSL spelling when legal on the current backend:

```python
wide_x = pto.vmi.vcvt(x, pto.f32) # e.g. N*VL lanes
scale_wide = pto.vmi.vload(scale_ptr, scale_off, size=N * VL, stride=0, group=N)
wide_y = pto.vmi.vmul(wide_x, scale_wide, full_mask)
```

- This pattern is often better than:
1. spilling the widened `N x VL` value to UB,
2. reloading `VL` chunks in a Python loop,
3. multiplying each chunk by the same `VL` scale vector,
4. storing chunk-by-chunk.
- In practice, this zero-stride grouped `vload` is a useful way to express
"repeat the same `VL` vector across `N` groups" directly in VMI, keeping
the computation at full-vector width.
- Still validate with both `kernel.compile(...).mlir_text()` and PTOAS VMI
lowering, because legality depends on the source dtype, lane count, and
backend support for the grouped load shape.

## Validation Checklist

Before finishing a rewrite, verify:

- Function parameters and template/constexpr parameters match the source ABI.
- Template/generic variants are represented by `pto.const_expr` compile-time
selection unless separate entry symbols are explicitly needed.
- Pointer memory spaces and semantic dtypes are justified.
- GM/UB/L1 movement sizes, offsets, strides, and padding match the source.
- Sync ordering between MTE, Vector, Cube/SIMT, and stores is preserved.
- New VMI/MI code is not hidden behind one-off helper functions.
- Generated DSL code has no manual `ptodsl` path lookup, `Path(__file__)`
parent search, or `sys.path` mutation for local package discovery.
- Control flow uses native Python `for` / `if`, with `pto.static_range` only for
intentional compile-time loops.
- Every VMI compute region has a user-visible design note or stated assumptions.
- Tail masks and lane counts match the processed element count.
- `kernel.compile(...).mlir_text()` succeeds or the remaining trace error is
reported with the exact failing construct.
- PTOAS validation uses `--pto-backend=vpto --enable-vmi`; do not diagnose VMI
legality from a plain non-VMI invocation.

Numeric NPU or simulator validation is optional unless the user requests it.
4 changes: 4 additions & 0 deletions .codex/skills/rewrite-kernel-with-vmi/agents/openai.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface:
display_name: "Rewrite Kernel With VMI"
short_description: "AscendC kernel to PTODSL VMI/MI rewrite"
default_prompt: "Use $rewrite-kernel-with-vmi to rewrite this AscendC kernel into PTODSL VMI/MI and validate the generated MLIR."
Loading
Loading