Skip to content

[FlyDSL] Preserve FP32 precision in GDN prepare inverse block merges - #6911

Open
apinge wants to merge 13 commits into
ROCm:mainfrom
apinge:gdn-prepare-merge-precision
Open

apinge wants to merge 13 commits into
ROCm:mainfrom
apinge:gdn-prepare-merge-precision

Conversation

@apinge

@apinge apinge commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

#4952 has changed the inversion of the 16×16 diagonal blocks to fp32 in gdn_prepare_kernel, but the off-diagonal block merge still converts the inverse blocks and the intermediate results across GEMMs to BF16. When keys are highly correlated, beta is close to 1, and gate decay is weak, these conversions can increase the final error of gdn_prepare_kernel.

This PR preserves fp32 inputs to the multiplications and fp32 intermediate results during the off-diagonal block merge. The diagonal block inversion, merge formula, and execution order remain unchanged.

In practice, the changes in this PR improve numerical accuracy, and no clear performance regression was observed on the tested MI308X using the prepare benchmark in op_test.

Technical Details

Test Plan

acc test

Verified with op_tests/test_gdn_prepare.py::test_gdn_prepare_merge_precision using a fixed random seed (2026) and B=1, T=64, K=V=128. The keys were made strongly correlated by adding 0.01 × noise to the same base, then normalizing and converting to BF16. Parameters: beta=0.999, g=-1e-4.

python -m pytest -q -s \
  op_tests/test_gdn_prepare.py::test_gdn_prepare_merge_precision

The reference implementation reuses the existing run_torch_gdn_prepare(). The error is measured as ||actual-reference||₂ / ||reference||₂, equivalent to a normalized RMS error, and is reported as a percentage, the lower the better (gfx942 flydsl0.4.0)

Hg / H use_exp2 Base Line w PR w Base Line u PR u
8 / 24 false 2.3163% 0.0963% 0.5033% 0.1721%
8 / 24 true 2.3163% 0.0963% 0.5033% 0.1721%
4 / 12 false 2.3327% 0.1532% 0.5234% 0.1734%
4 / 12 true 2.3327% 0.1532% 0.5234% 0.1734%

Baseline represents the version without this pr's change.
Across all 4 cases, the baseline w/u exceeded the 0.3% regression threshold, while the patched version stayed below the same threshold in every case.

perf regression tests

python3 op_tests/test_gdn_prepare.py \
  --batch 1 \
  --shapes qwen35_35b_tp2 qwen35_397b_tp8 qwen38_flash_next_tp2 dense_tail varlen_ragged decode_prefix \
  --use-exp2 true false \
  --pipeline-shapes

Measured on a MI308X with FlyDSL 0.4.0, using the existing test_gdn_prepare(). This PR adds a new case qwen38_flash_next_tp2.

Each measurement uses 3 warmup iterations and 21 profiler iterations. Reported latency is the median of three measurements with alternating baseline/patch order, after populating compilation caches.

Both kernels use identical shape definitions and inputs. All prepare correctness checks reported err=0. Here Change = (patched / baseline − 1) × 100%; negative values indicate lower latency.

Shape T Hg / H use_exp2 Baseline (μs) This PR (μs) Change
qwen35_35b_tp2 2500 8 / 16 true 47.047 45.833 −2.58%
qwen35_35b_tp2 2500 8 / 16 false 46.951 45.815 −2.42%
qwen35_397b_tp8 1024 2 / 8 true 16.163 16.209 +0.28%
qwen35_397b_tp8 1024 2 / 8 false 16.613 16.283 −1.99%
qwen38_flash_next_tp2 12000 8 / 24 true 292.555 289.881 −0.91%
qwen38_flash_next_tp2 12000 8 / 24 false 292.697 289.801 −0.99%
dense_tail 300 4 / 8 true 13.167 12.657 −3.87%
dense_tail 300 4 / 8 false 12.473 12.773 +2.41%
varlen_ragged 600 4 / 16 true 24.099 23.637 −1.92%
varlen_ragged 600 4 / 16 false 22.915 22.625 −1.27%
decode_prefix 428 4 / 8 true 13.747 13.251 −3.61%
decode_prefix 428 4 / 8 false 13.773 13.255 −3.76%

Test Result

PR Self-Review

Reviewed revision

36222698d127c616fb853e6ade12bbf7f289918b

Behavior and intent

Preserve FP32 operands and intermediate results in the off-diagonal inverse
block merges of FlyDSL GDN prepare. The existing BF16 conversions between
GEMMs cause avoidable w/u error for strongly correlated keys, beta near 1,
and weak gate decay. This extends the FP32 diagonal-block inversion from
#4952 to the subsequent merge products. The final inverse-to-BF16 conversion
for WY and the public output contract remain unchanged.

Validation and unverified scope

Recorded validation used one AMD Instinct MI308X (gfx942, 80 CUs), FlyDSL
0.4.0, PyTorch 2.12.0+rocm7.14.0.git6bbd260, and Triton
3.8.0+rocm7.14.0.git4cff872.

From the repository root, in that AITER/FlyDSL environment:

python -m pytest -q -s \
  op_tests/test_gdn_prepare.py::test_gdn_prepare_merge_precision

python op_tests/test_gdn_prepare.py --batch 1 \
  --shapes dense_tail --pipeline-shapes dense_tail \
  --hidden-backends flydsl triton --use-exp2 false true

The timed regression reported 1 passed, covering all four head-ratio/exp2
configurations, and the CLI emitted precision, prepare, and pipeline summaries
with err=0. Replacing only the prepare implementation with the baseline
kernel makes the unchanged regression fail: the first w relative RMS is
0.02316339, above 0.003. The accuracy table above records all four
baseline/patch comparisons. The performance command, measurement procedure,
and 12-case results are in the Test Plan above.

These are recorded measurements from October 25-28, not new GPU runs at this
head. The kernel and test files at the reviewed revision were compared with
the locally validated versions and are byte-for-byte identical. The complete
dependency/integration state of this revision has not been rerun. gfx950,
other devices, and end-to-end model quality/serving performance remain
unverified by this PR's evidence. The reference is the FP32/BF16 specification
described above, not a fully FP64 reference.

PR-specific question

test_gdn_prepare_merge_precision() fails on the pre-patch implementation
because the w/u relative RMS exceeds 0.3% for the fixed correlated-key inputs.
The existing canonical test_gdn_prepare() uses independent random keys,
stronger decay, and an absolute w/u tolerance of 0.05; it does not exercise
this input condition or enforce this relative-error bound. The new regression
reuses its reference and output checks in the same file, with FP32 final
reference outputs for the RMS assertion.

The added qwen38_flash_next_tp2 shape in the existing prepare sweep is
performance and correctness coverage for T=12000, Hg=8, H=24. It is not
claimed to fail numerically before the patch; the correlated-key regression
provides the demonstrated pre-patch failure.

Changed op tests

  • op_tests/test_gdn_prepare.py: adds the correlated-key merge regression for
    (Hg,H)=(8,24)/(4,12) and both exp2 modes. This covers a numerical failure
    absent from the original random-input sweep. It uses the shared reference,
    @benchmark(), run_perftest, and a summary table while retaining the tight
    RMS assertion. It also adds the 12K Qwen3.8 TP2 shape to the existing prepare
    benchmark; no previous catalog entry covers that token count and head ratio.
    Existing callers retain the reference's default BF16 output behavior.

Submission Checklist

Preserve the upstream FP32 diagonal inverse and add standalone correlated-key regression coverage for both gate domains.
Replace the BF16 two-component compensation with the existing FP32 MFMA helper. Keep merge operands and intermediate products in FP32 while preserving the upstream diagonal solve and regression tolerances.
Reuse the prepare reference and FlyDSL wrapper, retaining FP32 reference outputs for the correlated-key relative RMS check. Run the regression from both the existing CLI and targeted pytest, and keep the 0.003 tolerance.
@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 6911 --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.

@apinge
apinge marked this pull request as ready for review October 28, 2026 13:00
@apinge
apinge requested review from a team and a balanced review from Copilot October 28, 2026 13:00

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.

🟡 Changes recommended

The documented pytest invocation bypasses the architecture gate, causing gfx1250 to raise a ValueError instead of skipping the unsupported test.

1 open issue
What changed in this PR

This PR extends #4952’s numerical-stability work by preserving FP32 precision during off-diagonal inverse block merges in the FlyDSL GDN prepare kernel.

Changes:

  • Replace BF16 merge operands and intermediate conversions with FP32 operations while retaining the merge formula and execution order.
  • Add a correlated-key precision regression test with an FP32 reference.
  • Add a production benchmark shape and support skipping empty benchmark sweeps.
File Description
op_tests/​test_gdn_prepare.py Adds precision regression coverage, optional FP32 reference outputs, and benchmark sweep updates.
aiter/​ops/​flydsl/​kernels/​gdr_prefill/​gdn_prepare.py Keeps off-diagonal inverse merge operands and intermediates in FP32.

Review effort: Balanced

Comment thread op_tests/test_gdn_prepare.py Resolved
Skip test if graphics hardware is not supported.

Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Copilot AI balanced review requested due to automatic review settings October 28, 2026 13:07

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.

🟡 Changes recommended

The new correlated-key tests omit the required benchmarking and summary-reporting flow, leaving their performance absent from the standard test report.

1 open issue
1 resolved since last review

Review effort: Balanced

Comment thread op_tests/test_gdn_prepare.py Resolved
Copilot AI balanced review requested due to automatic review settings October 28, 2026 13:32

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.

🔵 Needs a closer look

The FP32 MFMA changes require head-matched GPU accuracy and base/head latency validation on gfx942 and gfx950, which this static review cannot perform and the supplied gfx942-only measurements do not establish.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@zufayu
zufayu requested review from coderfeli and yadaish October 29, 2026 01:05
@zufayu
zufayu requested review from xytpai and removed request for yadaish October 29, 2026 09:32
Copilot AI balanced review requested due to automatic review settings November 2, 2026 08:28
@aiter-bot

aiter-bot commented Nov 2, 2026 •

Copy link
Copy Markdown

WAITING ON AUTHOR

PR Self-Review

Revision b408fefe8001 · mode shadow

Waiting for the PR author. The readiness report will appear after the self-review answers are complete for this revision.

Questions to answer in the PR description

  1. What behavior does this PR change, and why is the existing behavior insufficient?
  2. What did you validate, and what remains unverified? Include commands, hardware/configuration and results.
  3. What behavior would each changed op test fail on before this PR, and why is that coverage not already provided by an existing canonical test?
  4. For every changed op_tests/ file, what behavior does it cover and why is it not redundant? Write None if no op test changed.

Edit the PR Self-Review section in the PR description. Include the current revision SHA; this comment refreshes automatically.

Missing or invalid answers

  • FAIL — Reviewed revision must contain at least the first 7 characters of the current head SHA

Shadow mode reports what would block but keeps the status successful. Set AITER_PR_SELF_REVIEW_MODE=enforce only after reviewing real false positives.

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.

🔵 Needs a closer look

The FP32 MFMA merge requires correctness and base-versus-head latency validation on gfx942/gfx950 with pinned FlyDSL 0.4.1, but GPU execution and required dependencies are unavailable in this review environment.

No open issues 🎉

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings November 5, 2026 01:30

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.

🔵 Needs a closer look

The FP32 MFMA merge needs accuracy and latency validation at this head on gfx942/gfx950 with pinned FlyDSL 0.4.2, but supplied results used 0.4.0 on gfx942 and GPU execution is unavailable in this review.

No open issues 🎉

Review effort: Balanced

@zufayu
zufayu removed the request for review from coderfeli November 11, 2026 06:51
@zufayu zufayu assigned xytpai and unassigned coderfeli Nov 11, 2026
@zufayu
zufayu requested a review from coderfeli November 11, 2026 06:51
@github-actions

Copy link
Copy Markdown
Contributor

No activity for 15 days, so this is now labelled stale. A push or a comment clears it; keep-open exempts it. Nothing is closed automatically.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Nov 27, 2026
Copilot AI balanced review requested due to automatic review settings November 27, 2026 01:55

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.

🟡 Changes recommended

The regression gate and summary use build-time GPU architecture rather than the runtime device, causing incorrect skips on gfx942 and allowing unsupported devices through.

1 open issue

Review effort: Balanced

Comment thread op_tests/test_gdn_prepare.py Outdated

def test_gdn_prepare_merge_precision():
"""Pytest entry point for the same timed regression sweep used by the CLI."""
if get_gfx() not in SUPPORTED_GFX:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in b408fef. Replaced get_gfx() with get_gfx_runtime() for the pytest and CLI architecture gates, the skip log, and all three benchmark summaries in test_gdn_prepare.py.

Validated six cases with mocked hardware detection, including gfx942 with GPU_ARCHS="gfx942;gfx1250" and gfx1250 with GPU_ARCHS="gfx942". Supported devices pass the gates, unsupported devices are skipped, and the summaries report the runtime architecture. Black and Ruff checks also pass.

@github-actions github-actions Bot removed the stale The PR hasn't been updated in 2 weeks label Nov 28, 2026
Copilot AI balanced review requested due to automatic review settings December 1, 2026 06:14

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.

🔵 Needs a closer look

The new test gate and benchmark metadata use the build-time GPU target rather than the live GPU, causing incorrect test gating and inaccurate hardware reporting.

1 open issue

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 2, 2026 03:54

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.

🔵 Needs a closer look

The FP32 MFMA changes lack current-head GPU validation with pinned FlyDSL 0.5.3; supplied results use 0.4.0 on gfx942 and leave supported gfx950 behavior unverified.

1 open issue

Review effort: Balanced

@xytpai xytpai 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.

Read at b408fef (2 files, +144 -59). I did not run anything on a GPU. The numbers below are from this head's CI logs.

Checked, no finding

  • Kernel, by reading. All six off-diagonal blocks (c21, c32, c43, c31, c42, c41) now load their fp32 tiles raw, multiply through _mfma16_f32 (the helper the diagonal inversion already uses) and keep the intermediate as fp32. The formulas and their order are unchanged, and so is the final conversion of C to bf16 for WY. Operand and accumulator layouts are consistent between the loads, the MFMA chain and the stores.
  • Accuracy on gfx950 (CI, MI35X shard 0). The new merge-precision sweep reports a relative RMS of w / u of 0.096% / 0.172% for (hg, h) = (8, 24) and 0.153% / 0.173% for (4, 12), in both exp2 modes. These are the values the description reports for gfx942. Every row of the gdn_prepare table has err 0.
  • Performance on gfx950 (same log). prepare_flydsl us of each row lies inside the spread of five recent CI runs of the base, for example qwen35_35b_tp2 at batch 1: 17.9 us against 15.7 to 18.0 us. So there is no visible regression there. This covers the "gfx950 unverified" note in the last Copilot review.
  • The new test can fail. MERGE_REL_RMS_TOL = 3e-3 is about 1.7x the measured value and below the 0.50% (u) and 2.3% (w) the description reports without the change, and _check_prepare_outputs raises on any element beyond the absolute bound.

Two notes, not blocking

  1. gfx942 at this head. The PR has no ci:mi300x label, so CI ran the MI35X shards only, and the gfx942 numbers in the description are from October on FlyDSL 0.4.0. The change is arch-generic and the gfx950 numbers above match the October ones, so I do not expect a difference. Adding ci:mi300x would confirm it on the pinned FlyDSL.
  2. Description. "Reviewed revision" still names 3622269, and the head is b408fef.

This branch has not been deployed

No deployments
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.

5 participants