Repository navigation
[models] GDN/KDA chunk kernels: always fused - #6294
felipemello1 wants to merge 6 commits into
Conversation
chunk_kernel_options picked Attention Gym's cuDNN chunk kernels (cudnn-frontend's CuTeDSL GDN kernels, vendored at v1.30.0) for packed fp16/bf16 inputs on SM100/SM103. On one 78k-token Qwen3.8-27B sequence on GB300, the model's logprob for one token then differed between the full sequence and the token's own prefix (-1.91 vs -10.35 with Flash attention). With the fused kernels both read -0.0146, next to an fp32 reference of -0.016. Kernel-only checks, on random inputs and on that sequence's real per-layer inputs, don't reproduce it. The trainer and the generator's prefill call this function, so both now use the fused kernels. GDN fwd+bwd is 1.9-2.1x slower on GB300 (attn_gym 0.0.20). KDA's cuDNN kernel shares the vendored code, so it stays fused too. The RL README no longer says GDN runs cuDNN on Blackwell.
Main renamed Attention Gym's chunk backend "cudnn" to "warpdrive" and made it the default on SM100/SM103. Conflicts keep this PR's choice: chunk_kernel_options returns "fused" for every input, and its TODO names "warpdrive" with the old "cudnn" kernel's numbers at 78k tokens. The RL README says GDN runs the fused chunk kernels.
…ention Gym issue yet The TODO gave warpdrive's speed-up as 2.3-3.9x, an older docstring's number. The PR's own measurement, GDN fwd+bwd per layer on GB300 under the backend's old name "cudnn", is 1.9-2.1x vs fused. The TODO also says no Attention Gym issue is filed yet. Comment only.
"No Attention Gym issue is filed yet" goes stale once someone files one. The TODO now says what to do: file one with this repro and link it here. Comment only.
|
Reply from claude: @claude please review |
|
Claude finished @felipemello1's task in 2m 30s —— View job Needs discussion -- review posted. |
There was a problem hiding this comment.
Verdict: Needs discussion -- maintainers have to decide whether a ~2x slower GDN fwd+bwd on SM100/SM103 (end-to-end cost not measured) is worth it. The evidence is one flipped token, measured on an older version of the kernel main runs, and the PR itself says it isn't ready yet.
What this PR does. chunk_kernel_options used to choose Attention Gym's warpdrive GDN/KDA chunk kernels for packed fp16/bf16 inputs on SM100/SM103, outside deterministic and batch-invariant modes. It now always returns KernelOptions(backend="fused"). All 4 callers change with it: trainer GDN/KDA and generator GDN/KDA prefill. Hopper, other dtypes, and the deterministic and batch-invariant modes keep the same kernel and stay bitwise unchanged.
Example.
Qwen3.5 training step on GB300, bf16, packed batch 1, default (non-deterministic) mode
main: chunk_gdn(..., kernel_options=KernelOptions(backend="warpdrive")) ~11.8 ms/layer fwd+bwd @77k
PR: chunk_gdn(..., kernel_options=KernelOptions(backend="fused")) ~22.4 ms/layer fwd+bwd @77k
Same on H100, or with debug.deterministic=True, or in batch-invariant mode: fused before and after.
Must fix
- PR description. "Summary (human-written)" is empty. The description also says the evidence came from attn_gym 0.0.20 ("cudnn") and that the full-vs-prefix check has to be re-run at main's pin 755691f ("warpdrive") before the PR is ready. Until that re-run, it is not shown that the problem exists in the kernel main runs today.
linear_attention.py:63-- TODO with no upstream issue link (inline).
Should consider
linear_attention.py:67--del qleaves the warpdrive plumbing half-alive (generator conditionals, spmd registrations). Either keep the switch on purpose or delete all of it (inline).
Questions
- Root cause. The PR's own test says outputs must agree "up to float noise". The cuDNN kernel's prefix dependence is ~1 bf16 ulp per layer, which is float noise. The 8.4-nat flip needs layers 0-23 to compound at one fragile position. With main's cuDNN attention, the fused GDN kernel also scores that token -6.87 instead of -0.016. On the 32,768 sampled tokens, the bad-ratio rate, mean gap and KL are the same with either kernel set. So is this a warpdrive bug, or a bf16-fragile position that any 1-ulp change would flip? A metric over many positions and sequences (e.g. the full-vs-prefix logprob gap across all completion tokens, warpdrive vs fused) would settle whether fused helps in aggregate.
- Cost. What is the end-to-end tokens/s change for the Qwen3.5/3.8 GB300 trainer and the generator prefill? The rules say main-path performance must not change silently, and a 2x GDN kernel slowdown is not small for a hybrid model.
- Compile failure on main. With nvidia-cutlass-dsl 4.7.1, main's warpdrive kernels fail to compile, and torchtitan does not pin cutlass-dsl. That failure is concrete and on main today. Is the fix a cutlass-dsl pin or an Attention Gym issue, separate from this PR?
Details
- Gate: the problem is a numerics claim about an upstream (meta-pytorch) kernel. Disabling it in torchtitan with a linked TODO is an acceptable workaround in form. The open question is whether the evidence justifies the cost.
- Callers checked:
models/common/attention/gdn.py:351,kda.py:106,rl/generator/attention/gdn.py:134,kda.py:141. CP paths don't callchunk_kernel_options. Batch-invariant RL parity (test_bitwise_parity.py) already used fused, so it is unaffected. - Removing the
is_in_batch_invariant_modeimport doesn't break any test monkeypatch.test_gdn_graph_metadata.py:98patches the vLLMlinear_attentionmodule, not this one. - The README edit matches the new behavior.
Not verified: all GPU numerics and timing claims (no GPU here). A maintainer still needs to review, since this touches models/common/.
| # TODO: bring back backend="warpdrive" once it is causally consistent at long packed lengths. Under its old name | ||
| # "cudnn" it was not (Qwen3.8-27B, 78k tokens, pos 562: full -1.91, prefix -10.35; fused -0.0146), though its GDN | ||
| # fwd+bwd was 1.9-2.1x faster than fused on GB300. KDA's is unchecked. No Attention Gym issue yet: file one with | ||
| # this repro and link it here. |
There was a problem hiding this comment.
This TODO works around an upstream kernel (meta-pytorch/attention-gym) but has no issue link. Its own text says to file one ("No Attention Gym issue yet: file one with this repro and link it here"). Without a link, nobody can tell when the switch back to warpdrive is safe, and the 2x GDN cost stays in place with no owner.
Fix: file the Attention Gym issue with the full-vs-prefix repro before merging. Then cut this down to a 2-line TODO with the link, and put the per-token numbers in the issue rather than in the code, e.g.
# TODO(<issue link>): bring back backend="warpdrive" (~2x faster GDN fwd+bwd on GB300) once its
# output at a position no longer depends on the packed length after it. KDA's is unchecked.| # "cudnn" it was not (Qwen3.8-27B, 78k tokens, pos 562: full -1.91, prefix -10.35; fused -0.0146), though its GDN | ||
| # fwd+bwd was 1.9-2.1x faster than fused on GB300. KDA's is unchecked. No Attention Gym issue yet: file one with | ||
| # this repro and link it here. | ||
| del q |
There was a problem hiding this comment.
q is now unused, and so is the warpdrive plumbing that only this switch could reach:
torchtitan/rl/generator/attention/gdn.py:130-137andkda.py:135-144:chunk_kernel_options(...) if checkpoints is None else KernelOptions(backend="fused"). Both branches now return the same thing, and the attention-gym#980 TODO above them describes a path that can no longer run.models/common/attention/gdn.py:50andkda.py:54still registerChunkGdnWarpdrivePacked/ChunkKdaWarpdrive, so the warpdrive modules are still imported.
That is fine if warpdrive is coming back soon, since keeping the plumbing is what makes this a one-function switch. If the upstream fix has no timeline, the repo's rule is to delete it: drop q (or the whole function, in favor of KernelOptions(backend="fused") at the 4 call sites), collapse the two generator conditionals, and remove the warpdrive registrations and exports. Please choose one of these on purpose, not the half-way del q.
Summary (human-written)
Agent details
TLDR:
chunk_kernel_optionsnow always picks Attention Gym's fused GDN/KDA chunk kernels. On SM100/SM103, main picks Attention Gym's warpdrive kernels, the backend it called "cudnn" until the version main pins. With that cuDNN GDN chunk kernel and the attention main trains with at head dim 256 (FA4), one 78k-token Qwen3.8-27B sequence on GB300 scores one token -1.91 in the full sequence and -10.35 in the token's own prefix, against an fp32 reference of -0.016. Fused GDN gives -0.0146 for both. On that sequence's real inputs, the cuDNN kernel's output at a position changes with the number of tokens after it, by about 1 bf16 ulp per layer. Cost: GDN fwd+bwd is 1.9-2.1x slower on GB300; end to end is unmeasured.The image predates main's rename of the backend "cudnn" to "warpdrive".
Problem
chunk_kernel_optionspicks the warpdrive GDN chunk kernel for packed (batch 1) fp16/bf16 CUDA inputs on SM100/SM103, outside deterministic and batch-invariant modes. Before the Attention Gym commit main pins, this backend was named "cudnn" (cudnn-frontend's CuTeDSL kernels); its registered ops keep thecudnnnames. The trainer packs documents into one sequence, so its bf16 steps there run warpdrive. The RL generator's GDN prefill calls the same function.Solution
chunk_kernel_optionsreturnsbackend="fused"for every input. Its 4 callers (trainer GDN and KDA, generator GDN and KDA prefill) keep calling it, so bringing warpdrive back is a one-function change.Results
You may ask:
chunk_kernel_optionshas no config today, and no known input marks the cuDNN kernel safe. The TODO keeps the function as the single switch.NOTE: All the evidence here used the backend under its old name "cudnn" (attn_gym 0.0.20). Before this PR is ready, re-run the full-vs-prefix check at main's pin 755691f, where it is "warpdrive".
NOTE: The evidence is one position of one sequence on one GB300. The trigger is unknown. Shorter sequences and B200 were not measured.
NOTE: In a no-grad forward with main's cuDNN attention, fused GDN scores that token -6.87 in both the full sequence and the prefix: consistent, but wrong. #6295 (varlen attention: FlashAttention only) covers that case.
NOTE: The generator's GDN prefill also moves to fused. End-to-end cost, in the trainer or the generator, is not measured.
NOTE: Not run: the 2-GPU
test_bitwise_parity.py(this box has 1 GPU).Test plan (this head and main c561820, same venv: torch 2.16.0.dev20261107, attn-gym 755691f, nvidia-cutlass-dsl 4.7.1, torch_checkpointing at main's pin, 1 GB300)
pytest tests/rl/unit_tests/cpu: 824 passed, 6 skipped; main the same.pytest tests/unit_tests/cpu/models/common: 365 passed, 1 failed; main the same, test by test. The failure,test_lora_forward, is Inductor's "torch.compile on current platform is not supported for CPU" (flex_attention on this aarch64 CPU).pytest tests/rl/unit_tests/gpu tests/unit_tests/gpu/models/qwen3_5/test_qwen3_5_deltanet.py tests/unit_tests/gpu/models/common/test_kda_attention.py -m "not multi_gpu": 40 passed, 3 skipped. main: 37 passed, 3 failed, 3 skipped. main's 3 failures are warpdrive kernels that fail to compile (AttributeError: 'tuple' object has no attribute 'atom'): GDN paged prefill (test_packed_decode_routing_matches_recurrence), GDN chunk and KDA chunk (the two varlen-vs-documents tests). This PR never picks warpdrive. Skips: nodist_moepackage (1), no local Qwen3.5-0.8B weights (2).rl/README.mdlines this PR does not touch, as on main. Pyrefly, run from a checkout outside.worktrees/(it skips dot folders): the same 1 error as main, in a file this PR does not touch.test_bitwise_parity.py(this box has 1 GPU).