Skip to content

[FlyDSL] [Misc] Simplify PA decode reducer bound and reuse shared helpers - #7077

Merged
coderfeli merged 1 commit into
mainfrom
pa-decode-cleanup
Oct 28, 2026
Merged

coderfeli merged 1 commit into
mainfrom
pa-decode-cleanup

Conversation

@fsx950223

Copy link
Copy Markdown
Contributor

Motivation

Cleanups in the FlyDSL PA decode wrapper and kernels. The reducer launcher took two partition limits where one is enough, and several spots duplicated helpers that aiter or FlyDSL already provide.

Changes

  • One reducer partition bound: launch_pa_decode_ps_reduce takes a single max_partitions, which is the tight per-call bound. pa_decode computes it from the plan cap, plan capacity, context tiles and the sliding-window cap, and the separate partition_bound argument is removed.
    • Small bounds still compile exactly, so short contexts keep the register-only reducer.
    • Above a wave, the power-of-two round-up is now capped by the device CU count instead of the plan's max_partitions. With a non-default plan cap that isn't a power of two, the compiled bound can round past it (for example 100 → 128). This is safe because the reducer's per-sequence buffer bound zero-fills partitions beyond the live count.
  • Shared helpers instead of local copies:
    • pa_decode uses dtypes.fp8 instead of its own arch → FP8 dtype table. The supported-arch check stays.
    • The reducer uses tensor_shim._FX_ELEM instead of a local _DTYPE_MAP, and still accepts only f32, f16 and bf16.
    • The gfx1250 WMMA and FP8-conversion calls use fx.as_ir_value instead of tensor_shim._to_raw.
  • gfx1250 thread count: Gfx1250Traits.BLOCK_THREADS (NWARP * WAVE = 128) replaces the hardcoded 128 in known_block_size, the launch block, the direct-output copy loop and stage_vector_dma. It's a class constant, not a dataclass field, so traits cache keys don't change.
  • sliding_window: pa_decode and plan_pa_decode treat any nonpositive value as disabled instead of raising for values below -1. The docstrings say so.

Test

  • op_tests/test_flydsl_pa_decode.py on gfx1250 with FlyDSL 0.4.0: 119 passed, 5 skipped.
  • plan_pa_decode(..., sliding_window=-5) builds the same plan as -1.
  • gfx942 and gfx950 were not run on hardware. The reducer-bound, dtypes.fp8 and _FX_ELEM changes also apply there.
  • black and ruff pass.

🤖 Generated with Claude Code

- Fold the reducer's partition_bound into max_partitions: pa_decode
  passes one tight per-call bound; above a wave the power-of-two
  round-up is capped by the CU count.
- Use dtypes.fp8 for the KV FP8 dtype, tensor_shim._FX_ELEM for the
  reducer's dtype lookup, and fx.as_ir_value instead of _to_raw.
- Name the gfx1250 thread count Gfx1250Traits.BLOCK_THREADS instead of
  hardcoding 128.
- Treat any nonpositive sliding_window as disabled in pa_decode and
  plan_pa_decode instead of rejecting values below -1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@fsx950223
fsx950223 requested review from a team and a balanced review from Copilot October 28, 2026 07:08

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every PR:

  • ✅ Pre-checks (submodule verification, code formatting)
  • ✅ Aiter op tests (gfx942 + gfx950)
  • ✅ Triton tests on MI35X (only when aiter/ops/triton/** or related paths are changed)

Extended tests (opt-in via labels):

Label Tests
ci:gfx1250-ffm-triton Run the five-shard gfx1250 FFM Triton test suite
ci:triton-300x Run an additional Triton test job on MI300X in PRs (added automatically when gfx942 configs change); main branch always runs both MI35X and MI300X
ci:triton-355 Run the full Triton test suite on MI35X, not only the tests the change affects
ci:windows Windows JIT op smoke test on the gfx1201 ROCm runner
ci:multi-gpu Aiter multi-GPU tests on the 8-GPU runner
ci:sglang SGLang integration tests: DeepSeek-R1-MXFP4 accuracy, Qwen 3.5 accuracy
ci:atom ATOM benchmark: DeepSeek-R1-0528, GPT-OSS-120B
ci:atom_full ATOM accuracy suite for PR and main models from ATOM models_accuracy.json
ci:vllm vLLM benchmark: DeepSeek-V4-Pro, MiniMax-M3-MXFP4
ci:all All standard extended tests (excludes ci:atom_full)

Only add ci:atom_full for FlyDSL or Triton upgrades.
Add labels via the sidebar or gh pr edit 7077 --add-label <label>

One backend per PR:
A PR changes one kernel backend: [Triton/Gluon] (Triton and Gluon count as one), [HIP], [ASM], [CK], [OPUS] or [FlyDSL]. If the title ends up with two backend tags, split the PR -- as stacked pull requests when one part cannot merge without the other.

PR title tags & labels:
Component tags ([Triton/Gluon], [HIP], [CK], [ASM], ...) are added to the PR title and as PR labels automatically from the changed files and re-synced on every push — change-type tags like [fix]/[Perf], op tags like [MLA], and human labels (ci:*) are left untouched. Add the no-auto-title label to stop the title rewrites; labels stay in sync either way.

@coderfeli
coderfeli merged commit b0043b6 into main Oct 28, 2026
47 checks passed
@coderfeli
coderfeli deleted the pa-decode-cleanup branch October 28, 2026 11:54
samremes added a commit that referenced this pull request Oct 29, 2026
#7077 made pa_decode require dtypes.fp8 for FP8 KV instead of a per-arch
dtype table. The capability and default-budget helpers still used that
table, so they could accept a dtype pa_decode rejects. Compare against
dtypes.fp8 there too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
solinzby1 pushed a commit that referenced this pull request Nov 2, 2026
…7077)

- Fold the reducer's partition_bound into max_partitions: pa_decode
  passes one tight per-call bound; above a wave the power-of-two
  round-up is capped by the CU count.
- Use dtypes.fp8 for the KV FP8 dtype, tensor_shim._FX_ELEM for the
  reducer's dtype lookup, and fx.as_ir_value instead of _to_raw.
- Name the gfx1250 thread count Gfx1250Traits.BLOCK_THREADS instead of
  hardcoding 128.
- Treat any nonpositive sliding_window as disabled in pa_decode and
  plan_pa_decode instead of rejecting values below -1.

Co-authored-by: who who who <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
JohnNikolay84 pushed a commit that referenced this pull request Nov 18, 2026
… the gfx950 head-256 PA decode pipe (#5949)

* [FlyDSL] Add the NHD page-64 layout to the gfx950 head-256 PA decode pipe

A 4D key_cache selects NHD: K and V are [num_blocks, 64, num_kv_heads, 256]
views with a contiguous head axis and arbitrary other strides, such as both
halves of one token row. The pipe reads K with NHD addressing and copies V
into LDS with nontemporal buffer-to-LDS DMA, read back with transposed LDS
reads. Two V buffers overlap the next tile's copy with PV; a grid wider than
the CU count uses one when two single-buffered CTAs fit in LDS. Hardware
out-of-bounds zero-fill covers partial-page tails.

NHD supports per-tensor and per-token scales and positive sliding windows.
It requires gfx950, FP8 KV, head 256, page 64, and at most 32 query rows;
other shapes, including page 16, raise NotImplementedError. Strides must be
16-byte multiples and fit int32, and wide addressing is chosen from the
highest byte either view reaches. Tuning reads KV heads and page size from
NHD caches and keys results by layout.

NHD folds the softmax scale into exp2 rather than scaling scores before
masking, which is up to 8% faster on its attention kernel at long context.

Packed caches keep main's code paths and timings.

Co-authored-by: fsx950223 <[email protected]>
Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Default dense page-64 head-256 PA decode plans to one CTA per CU

A prepare_pa_decode_plan cache miss used a 2*CU workgroup budget for every
shape. On the gfx950 FP8 head-256 pipe with page 64 and no sliding window, a
CU-count budget is as fast or faster at every measured batch and context:
up to 12% faster for packed per-token caches at query length 4 and up to 16%
for NHD. Batch 1 plans are identical. Page 16 and windowed shapes are as fast
or slightly faster at 2*CU, so they and every other path keep it.

The tuning search adds the CU count to its candidates, and the pa_decode
docstring and tuning README describe the per-shape default.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Simplify NHD validation and the PA decode default budget

Check NHD support once, early in pa_decode, with a shared page-64 pipe
predicate that the default budget also uses. Validate NHD strides in one
block and pass the validated strides to the kernel, instead of computing them
again after the plan checks. Skip the NHD LDS footprint for packed caches.

The default budget takes the autotune key directly, maps the KV dtype the
same way pa_decode does, and rejects a non-integer sliding window.

The pre-PV V-DMA wait is constant for single-tile plans, and its comment
notes that later loads only make the wait stricter. Tests read named trait
fields instead of cache-key positions, poison K as well as V tails, add a
two-KV-head case with every head in one token row, and use aiter.dtypes.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Add a public NHD-support capability API for downstream callers

Downstream integrations had to probe private symbols and inspect
pa_decode's signature to learn whether this build serves the NHD layout
(4D K/V caches, selected by key_cache.dim() == 4 / trans_k=False).

Export a small documented surface from aiter.ops.flydsl.pa_decode and
aiter.ops.flydsl:

- pa_decode_nhd_supported(): whether pa_decode can serve an NHD call for
  an architecture, KV dtype, head dim, page size, query length, GQA group
  and sliding window.
- PA_DECODE_NHD_MAX_QUERY_ROWS: the query_length * query_group_size limit.
- PA_DECODE_NHD_MIN_FLYDSL_VERSION: the FlyDSL release NHD requires.
- pa_decode_default_workgroup_budget(): the budget prepare_pa_decode_plan
  uses on a tuning-cache miss (one workgroup per CU for dense page-64
  head-256 FP8 pipe shapes, 2*CU otherwise).

The predicate, pa_decode and PaDecodeSchedule share one validation
(require_nhd_pipe plus the FlyDSL version gate), and the tuning module
reuses the public budget function, so none of them can drift from the
kernel's behavior.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Narrow the PA decode NHD capability API

Callers need the longest NHD query length and the untuned work budget, not a per-query boolean plus version and row-limit constants. Both functions take the device, and plan_pa_decode and PADecodePlan are exported from aiter.ops.flydsl.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Keep the gfx1250 PA decode launch unchanged by the NHD strides

The NHD strides were inserted positionally in PaDecodeContext and always
appended to the launch arguments, so the gfx1250 kernel's 21-value context
call and its 19-parameter launcher no longer bound. Make the six strides
keyword-only context arguments that default to None, pass them by keyword
from the gfx942/gfx950 tile task, and leave them out of the gfx1250 launch.
NHD stays gfx950-only and the gfx1250 kernel is untouched.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Reject a non-positive or non-finite PA decode softmax_scale

Paths that apply the scale after masking, including NHD and the generic
per-tensor path, give masked tokens weight for a negative or zero scale
and return wrong values or NaN. pa_decode now raises ValueError unless
softmax_scale is finite and positive, before any kernel is compiled.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Use get_gfx_runtime in the PA decode capability API

pa_decode_nhd_max_query_length and pa_decode_default_workgroup_budget read
the architecture from gcnArchName, while pa_decode and plan preparation use
get_gfx_runtime. Use the shared helper so the API cannot disagree with
dispatch; the device still supplies the CU count.

Co-authored-by: Cursor <[email protected]>

* [FlyDSL] Match the PA decode capability API to pa_decode's FP8 dtype

#7077 made pa_decode require dtypes.fp8 for FP8 KV instead of a per-arch
dtype table. The capability and default-budget helpers still used that
table, so they could accept a dtype pa_decode rejects. Compare against
dtypes.fp8 there too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] Keep pa_decode exported as the function after sibling imports

Loading a lazy export from aiter.ops.flydsl.pa_decode or pa_decode_tuning
imports the pa_decode submodule, and Python binds it on the package under
the same name as the pa_decode export. `from aiter.ops.flydsl import
plan_pa_decode, pa_decode` then returned the module. Drop such bindings
after each lazy load so the export resolves instead.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* [FlyDSL] Skip the PA decode import-order test when FlyDSL is missing

The subprocess imports aiter.ops.flydsl, which imports flydsl, so without
FlyDSL it failed instead of skipping like the rest of the module.

Co-Authored-By: Claude Code <[email protected]>

* [FlyDSL] Skip the V tail masking for NHD layouts

NHD V is loaded by an LDS DMA that already zero-fills past the context, so
the mask_unowned_kv V byte masks are redundant there and cost time at the
2xCU budget. K/V scale masking is unchanged.

Co-Authored-By: Claude Code <[email protected]>

* [FlyDSL] Tolerate concurrent lazy imports when dropping shadow bindings

Two threads resolving exports at once could both see the submodule binding,
and the second delete raised KeyError. Use get and pop so the cleanup is
idempotent.

Co-Authored-By: Claude Code <[email protected]>

* [FlyDSL] Reject zero KV heads before computing the PA decode group size

prepare_pa_decode_plan divided the query heads by the cache's KV head
count before the planner validated it, so a zero-head cache raised
ZeroDivisionError instead of ValueError.

Co-authored-by: Copilot <[email protected]>

* Fix FlyDSL formatting checks

Co-authored-by: samremes <[email protected]>

* [FlyDSL] Reject non-positive host K/V scales in pa_decode

Per-tensor FP8 paths, including NHD, mask scores before applying the scale,
so a negative or zero scale corrupts the row max. Reject non-finite or
non-positive host-scalar key_scale/value_scale; device tensors are not read
back, to avoid a sync, and are documented as requiring positive values.

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Check the K/V scale rejection in the softmax_scale test

Pass each rejected value as a host-scalar key_scale in the existing
softmax_scale rejection test instead of a separate test.

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Use packed caches in the scale rejection test

NHD caches raise NotImplementedError on gfx942 and gfx1250 before the
K/V scale check runs. Packed caches reach scale validation on every arch.

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Mask scores on multi-tile NHD q=1 partial tails

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Use plan_pa_decode in NHD partial-tile zero-logit test

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Reject negative stepped NHD strides in pa_decode

Co-authored-by: Copilot <[email protected]>

* [FlyDSL] Remove unconstructible negative-stride as_strided test case

Co-authored-by: Copilot <[email protected]>

---------

Co-authored-by: fsx950223 <[email protected]>
Co-authored-by: Cursor <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-authored-by: copilot-swe-agent[bot] <[email protected]>
Co-authored-by: samremes <[email protected]>
Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants