[KDA] Add FlashKDA CUDA training backend for chunk_kda - #1112
Open
xy200303 wants to merge 4 commits into
Open
Conversation
@dispatch stacked over @torch.compiler.disable was silently discarded: functools.wraps copies _torchdynamo_orig_callable onto the dispatch wrapper, so dynamo's innermost_fn unwrapping bypasses it and backend selection never ran for the affected top-level entries: - chunk_kda (fla/ops/kda/chunk.py) — the flash_kda backend from fla-org#852 was never actually selected - fused_kda_gate (fla/ops/kda/gate.py) - chunk_gated_delta_rule (fla/ops/gated_delta_rule/chunk.py) Swap the decorator order so dispatch stays the outermost wrapper while the call remains excluded from compile graphs. Note this activates the flash_kda backend for inference-mode chunk_kda calls on systems with the flash_kda package installed, which is the intent of fla-org#852. Closes fla-org#1110
Dispatch chunk_kda fwd+bwd to the flash_kda CUDA training kernels (env FLA_FLASH_KDA_TRAIN, default off). Accepts grad-enabled bf16 calls with K=V=128, H=HV, chunk 64; everything else falls back to Triton. Adds dispatch tests (dense/varlen x gate/safe_gate) and the benchmark backend_env hook.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds a CUDA training backend (
flash_kda_train) forchunk_kda, dispatching the full fwd+bwd training step to the FlashKDA CUDA training kernels (WY representation, replicating the Triton pipeline stage by stage). The backend is opt-in (FLA_FLASH_KDA_TRAIN=1, default off) and conservatively gated: it only accepts grad-enabled bf16 calls withK=V=128,H=HV,chunk_size=64, no CP / no intermediate-states; everything else falls back to the Triton path.Depends on #1111 — without the dispatch fix, top-level
chunk_kdanever reaches backend selection, so this backend would be dead code. The branch currently contains that commit; only the second commit ([KDA] Add FlashKDA CUDA training backend for chunk_kda) is new, and I will rebase once #1111 merges.Kernel implementation: MoonshotAI/FlashKDA#28 (per-stage CUDA kernels, 105 stage/pipeline tests, fp64-gold precision sweep). Until that lands upstream, the package is installable from the fork branch:
The backend is discovered via
find_spec("flash_kda_train_C"), so any distribution of the package works; nothing in fla pins the source repo.Test plan
tests/ops/test_kda.py— 88/88, including 5 new dispatch tests (dense/varlen × gate/safe_gate, fwd + all gradients) that spy-verify calls genuinely route to the CUDA backend rather than silently falling backscripts/find_dependent_tests.pydependents offla/ops/kda/chunk.pywere run; the 4 D64 modeling failures are pre-existing on a clean base (unrelated)tests/train/105/105 (per-stage vs Triton reference + end-to-end pipeline)Benchmark
Hardware: RTX 5090 (sm_120a), CUDA 12.8, torch 2.8, bf16. End-to-end
chunk_kdaviabenchmarks/ops/run.py(dispatch-verified routing):fwd-only is slightly slower by design (the training path persists
Aqk/Akkfor backward); training steps are faster on every shape, up to 2.17× on small-grid shapes where the CUDA kernels' finer V-dim split wins occupancy. Per-stage breakdown and roofline analysis: MoonshotAI/FlashKDA#28.Breaking changes
None. The backend is registered with
default_enable=Falseand a strict verifier, so default behavior is bit-identical to today; systems without theflash_kdatraining package are unaffected.Checklist