Skip to content

[GDN] Key the fused GDN-2 WY/dqkg backward autotuner on K and V - #1120

Merged
zhiyuan1i merged 1 commit into
fla-org:mainfrom
tianxiao-rgb:gdn2-autotune-key-kv
Sep 3, 2026
Merged

[GDN] Key the fused GDN-2 WY/dqkg backward autotuner on K and V#1120
zhiyuan1i merged 1 commit into
fla-org:mainfrom
tianxiao-rgb:gdn2-autotune-key-kv

Conversation

@tianxiao-rgb

Copy link
Copy Markdown
Contributor

Summary

chunk_gdn2_bwd_kernel_wy_dqkg_fused autotunes over BK/BV tile configs
(plus num_warps/num_stages), but its autotune key was
['BT', 'STATE_V_FIRST']K and V, the head dimensions that bound the
tiles and determine each config's occupancy, were not part of the key.

Consequently the first (K, V) geometry that reaches the kernel in a process
tunes it for every other geometry sharing BT: a model with K=64, V=128
heads running after a K=128, V=256 warmup silently inherits the larger
geometry's winning config instead of tuning for its own. We hit this while
benchmarking GDN-2 at multiple head geometries — back-to-back shapes in one
process reproduced with the first shape's config, skewing every subsequent
measurement until each geometry was quarantined into its own process with a
fresh Triton cache.

This PR adds K and V to the key, matching the sibling kernels in
fla/ops/gdn2/wy_fast.py (which key on
['H', 'K', 'V', 'BT', 'BK', 'BV', 'IS_VARLEN']), and adds a regression
test. Only this kernel in fla/ops/gdn2/ sweeps BK/BV while omitting
K/V from its key.

Cost: one extra autotune sweep per distinct (K, V) geometry, on first
encounter only; results are cached as before. Single-geometry runs (the
common case) see no change.

Test plan

New in tests/ops/test_gdn2.py:

  • test_chunk_bwd_autotune_key_covers_head_dims — backwards through two
    (K, V) geometries at the same chunk size must leave two distinct
    autotuner cache entries. Fails on main (the second geometry cache-hits on
    the first's winner), passes on this branch.
    Verified on H200: 1 failed on the base commit, 1 passed on this branch, same environment, with the key line as the only tree difference.

Dependent tests (python scripts/find_dependent_tests.py fla/ops/gdn2/chunk_bwd.py):

  • tests/ops/test_gdn2.py — 29 passed (full suite, incl. the new test)
  • tests/layers/test_attn_varlen_pack_layout.py — 13 passed, 2 skipped

Local test environment:

  • GPU: NVIDIA H200, driver 570.195.03
  • Python: 3.12.3
  • torch: 2.14.0.dev20260707+cu126
  • triton: 3.8.0

Benchmark / NCU

Neutral for single-geometry processes: the key change does not alter which
config wins for a geometry tuned on its own — it only ensures each geometry
is tuned at all.

For multi-geometry processes it removes a measurable penalty. On H200
(bf16, T=32768 packed varlen in 32 documents, chunk 64, medians of 30 iters),
the per-geometry winners genuinely differ (BK64/BV32/warps4/stages4 at
K=64/V=128 vs BK64/BV64/warps4/stages4 at K=128/V=256), and running
K=64/V=128/H=32 on the config leaked from a K=128/V=256 warmup (what main
does today) costs (the leaked run's tuner cache confirms it selected K128/V256's winner, BK64/BV64/warps4/stages4, instead of the geometry's own BK64/BV32):

K64/V128/H32, fused kernel tuned for itself on leaked K128/V256 winner
wy_dqkg median 2.774 ms 3.111 ms (+12.2%)
full bwd median 8.689 ms 9.030 ms (+3.9%)

Breaking changes

None.

Checklist

  • I have read CONTRIBUTING.md and follow its conventions (code style, docstrings, commit prefixes).
  • I have read AGENTS.md and, where my change matches its scope, the relevant skill under .agents/skills.
  • This is not a minor/cosmetic-only PR (typo, formatting, style-only tweaks).
  • Dependent tests pass locally or in CI; new behavior is covered by tests where applicable.
  • Kernel changes include same-hardware before/after benchmark numbers (dense + varlen where applicable).

chunk_gdn2_bwd_kernel_wy_dqkg_fused sweeps BK/BV tile configs, whose
relative performance depends on the head dimensions K and V, but its
autotune key was ['BT', 'STATE_V_FIRST']. The first (K, V) geometry
tuned in a process therefore pinned its winning config for every other
geometry sharing BT, silently running later shapes on a config tuned
for a different problem size.

Key on K and V as well, matching the wy_fast.py kernels in this op, and
add a regression test asserting that two geometries produce two
autotuner cache entries.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@tianxiao-rgb

Copy link
Copy Markdown
Contributor Author

The same pattern — the config space sweeps tile sizes whose best choice depends on the head dimensions, while the autotune key omits them — appears elsewhere (found by scanning @*autotune blocks whose triton.Config dicts set BK/BV against their key= lists):

fla/ops/kda/chunk_bwd.py is the direct sibling (the KDA ancestor of this PR's kernel, same sweep and same key shape). Some entries may be benign (e.g. a fused_recurrent keying on BK partially covers K); I have only verified and fixed the GDN-2 site here. Lmk if this PR should propagate the fix to any/all of these.

@zhiyuan1i zhiyuan1i left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Confirmed this is the one remaining autotune-key gap left by #1171 — thanks for catching it. Two things before merge:

  1. Rebase needed: your new test calls _rand_inputs with 6 args in tests/ops/test_gdn2.py, but after #1165/#1166 the signature takes 7 (HV was added), so it will TypeError on current main.
  2. Test cost: the new test pays a real cold-autotune in CI (~4m measured). Please follow the _unwrap_autotuner precedent in test_gdn.py and assert the full key directly — that also pins K and V in the key explicitly.

@zhiyuan1i zhiyuan1i added the waiting-author Reviewer acted; ball is in the author's court label Aug 29, 2026
@zhiyuan1i
zhiyuan1i merged commit a37d13a into fla-org:main Sep 3, 2026
23 of 24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working performance waiting-author Reviewer acted; ball is in the author's court

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants