Skip to content

[GDN] Add gdn_chunk_prepare: fused intra-chunk GDN prefill prep kernel (opus, arch-specialized) - #4016

Closed
jayzlee147 wants to merge 3 commits into
ROCm:mainfrom
jayzlee147:fgdn_k1_prefill
Closed

jayzlee147 wants to merge 3 commits into
ROCm:mainfrom
jayzlee147:fgdn_k1_prefill

Conversation

@jayzlee147

@jayzlee147 jayzlee147 commented Jun 30, 2026 •

Copy link
Copy Markdown
Member

Motivation

The Gated-DeltaNet (GDN) chunkwise prefill runs four separate FLA kernels before
the h-state recurrence:

chunk_local_cumsum → chunk_scaled_dot_kkt_fwd → solve_tril → recompute_w_u_fwd

This costs 4 kernel launches, an A_raw[B,T,H,64] HBM round-trip, and repeated
reloads of g/beta/k. This PR fuses all four into a single token-parallel HIP
kernel, gdn_chunk_prepare, built on the opus.hpp template library, with
per-arch specialization that ships only the best-performing path for each target.

Technical Details

  • New op gdn_chunk_prepare (module_gdn_chunk_prepare): one HIP kernel that
    fuses cumsum + gated KKT Gram + triangular inverse + WY-factor recompute.
    Inputs k,v (bf16), g,beta (fp32); outputs g_cumsum (fp32), w_bar,u_bar (bf16).
    BT=64, K=V=128.
  • opus.hpp usage: all global↔LDS traffic via make_gmem/make_smem/make_layout
    (vectorized b128 dispatch); MFMA via opus::mfma<bf16,bf16,fp32,16,16,16>;
    LDS MFMA-tile access via opus::make_smem.
  • Numerics: triangular inverse via Neumann squaring (I+B)(I+B²)(I+B⁴)(I+B⁸)
    (6 MFMAs vs 15 Horner), 3-level warp-parallel Schur-complement merge, and a
    single-warp __shfl_up cumsum (drops ~6 block barriers).
  • Arch specialized (best-only) via #if defined(__gfx950__):
    • gfx950 (MI350): OCC=2, C⁻¹ staged in LDS as bf16 (160KB LDS is not the limiter).
    • gfx942 (MI300): OCC=3, C⁻¹ register-cached → smaller LDS lifts occupancy 2→3.
  • Files: kernel template + defs (csrc/include/opus_gdn/gdn_chunk_prepare_*),
    TU + host launcher (csrc/py_itfs_cu/opus_gdn_chunk_prepare*.cu), pybind/header,
    Python op (aiter/ops/gdn_chunk_prepare.py), JIT config entry, and tests.

Test Plan

  • Correctness (op_tests/triton_tests/test_gdn_chunk_prepare.py): compares the
    fused output against an fp32 ground-truth reference and the Triton non-fused
    k14 across multiple shapes (incl. a non-BT-multiple T).
  • Performance (op_tests/triton_tests/bench_gdn_chunk_prepare.py): CUDA-graph
    capture/replay, fused 1-kernel vs Triton 4-kernel k14, median µs, across the
    35B/FULL model configs (K=V=128).

Test Result

Precision (vs fp32 ground truth and Triton non-fused, both arches): g_cumsum
exact (~1e-7); w_bar rel-L2 ~3e-3, u_bar rel-L2 ~4.3e-3 — bf16-level, aligned
with Triton.

Performance — fused gdn_chunk_prepare (1 kernel) vs Triton non-fused k14
(4 kernels), under CUDA graph (launch overhead removed). A structural win from
single-kernel fusion + LDS staging (no A_raw HBM round-trip): MI300X is flat at
~2.4–2.5×; MI350 is ~1.9–2.3× (gfx950's 32×32×16 MFMA makes Triton's k14
GEMMs relatively faster, narrowing the margin slightly). Both arch-specialized
paths are runtime-validated.

MI300X (gfx942)

model T Opus K1 (µs) Triton k14 (µs) speedup w relL2 u relL2
35B-tp1 (H=32) 1024 77.6 196.5 2.53× 3.7e-3 4.3e-3
2048 130.0 324.0 2.49× 3.0e-3 4.3e-3
4096 240.3 574.1 2.39× 2.9e-3 4.3e-3
8192 470.0 1123.5 2.39× 3.0e-3 4.3e-3
16384 996.7 2415.2 2.42× 3.2e-3 4.3e-3
32768 1968.5 4854.7 2.47× 3.1e-3 4.3e-3
65536 3963.2 9913.2 2.50× 3.0e-3 4.3e-3
35B-tp2 (H=16) 1024 51.0 114.1 2.24× 3.3e-3 4.6e-3
8192 242.8 595.2 2.45× 3.2e-3 4.4e-3
65536 1995.9 4896.7 2.45× 3.1e-3 4.3e-3
FULL-tp1 (H=64) 1024 134.6 326.1 2.42× 2.9e-3 4.3e-3
8192 1028.8 2491.2 2.42× 2.9e-3 4.3e-3
65536 8469.6 20353.7 2.40× 3.1e-3 4.3e-3

Anchor (35B-tp1 T=8192): Opus K1 470 µs vs Triton k14 1124 µs (2.39×).

MI350 (gfx950)

model T Opus K1 (µs) Triton k14 (µs) speedup w relL2 u relL2
35B-tp1 (H=32) 1024 23.8 53.6 2.26× 3.1e-3 4.1e-3
2048 30.9 70.1 2.27× 2.9e-3 4.3e-3
4096 53.0 121.7 2.30× 3.2e-3 4.3e-3
8192 106.1 209.2 1.97× 3.1e-3 4.3e-3
16384 211.9 398.4 1.88× 3.3e-3 4.3e-3
32768 399.1 800.8 2.01× 3.0e-3 4.3e-3
65536 780.3 1607.6 2.06× 3.2e-3 4.3e-3
35B-tp2 (H=16) 1024 20.0 45.1 2.26× 2.9e-3 4.4e-3
8192 52.8 124.7 2.36× 3.1e-3 4.3e-3
65536 391.4 796.3 2.03× 2.9e-3 4.3e-3
FULL-tp1 (H=64) 1024 30.2 69.8 2.31× 3.9e-3 4.3e-3
8192 210.2 402.8 1.92× 3.3e-3 4.3e-3
65536 1565.0 3328.6 2.13× 3.1e-3 4.3e-3

Anchor (35B-tp1 T=8192): Opus K1 106.1 µs vs Triton k14 209.2 µs (1.97×).

Single-kernel profile (gfx950, B1T8192H32): 98.8 µs, VGPR=32, LDS=25.3 KB.

Submission Checklist

jayzlee147 and others added 3 commits June 30, 2026 07:58
…rch-specialized)

Unified single-template K1 op built on opus.hpp, branched fresh from main.
Fuses the four FLA prefill kernels that run before the h-recurrence:
  chunk_local_cumsum + chunk_scaled_dot_kkt_fwd + solve_tril + recompute_w_u_fwd
into one token-parallel HIP kernel (BT=64, K=V=128).

Numerics = the proven neumann path (Neumann-squaring triangular inverse: 6 MFMAs;
Schur-complement merge; single-warp __shfl_up cumsum). Arch偏特化, best-only:
  gfx950 (MI350): OCC=2, C^-1 staged in LDS bf16
  gfx942 (MI300): OCC=3, C^-1 register-cached (smaller LDS -> OCC 2->3)
Underperforming variants (bt16/bt32/bt128/basic) dropped.

opus.hpp: all global<->LDS traffic via make_gmem/make_smem/make_layout (vectorized
b128 dispatch); MFMA via opus::mfma; LDS tile access via opus::make_smem.

Validated on gfx950 vs fp32 ground truth (7 shapes incl. non-BT-multiple):
g_cumsum exact (~1e-7); w_bar/u_bar rel-L2 ~3-5e-3 (bf16-level, on par with Triton).
Perf: 98.8us @ B1T8192H32, VGPR=32, LDS=25.3KB — matches the neumann baseline.
gfx942 path is arch-gated (compiles here; needs MI300 to runtime-validate).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
Fused 1-kernel gdn_chunk_prepare vs Triton 4-kernel k14 (cumsum + kkt + solve_tril
+ recompute_w_u) under CUDA graph capture/replay. gfx950 measured:
  1.86-2.32x end-to-end speedup; precision aligned vs Triton (w/u rel-L2 ~3-4e-3, bf16-level).

Co-Authored-By: Claude Opus 4.8 <[email protected]>
@jayzlee147
jayzlee147 requested a review from a team June 30, 2026 08:19
@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:triton-300x Run an additional Triton test job on MI300X in PRs; main branch always runs both MI35X and MI300X
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: GPT-OSS-120B, DeepSeek-R1-0528, Kimi-K2.5
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 4016 --add-label <label>

@yiijin

yiijin commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Nice fusion work. One suggestion: add varlen and GQA support, otherwise gdn_chunk_prepare can't be used on packed/GQA prefill paths.

  • varlen: handle ragged batches packed via cu_seqlens, resolving each chunk's own [bos, eos) instead of a uniform dense T.
  • GQA: let H query heads share Hg key heads (H % Hg == 0), indexing k by i_hg = i_h / (H/Hg).

@github-actions

github-actions Bot commented Oct 7, 2026

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.

@github-actions github-actions Bot added the stale The PR hasn't been updated in 2 weeks label Oct 7, 2026
@zufayu

zufayu commented Oct 9, 2026

Copy link
Copy Markdown
Contributor

Closing due to inactivity: no pushes, reviews, or comments for 100 days. This is housekeeping, not a judgement on the change — if you want to continue this work, comment here and we'll reopen, or open a fresh PR rebased on the latest main.

@zufayu zufayu closed this Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stale The PR hasn't been updated in 2 weeks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants