Skip to content

[models] GDN/KDA chunk kernels: always fused - #6294

Draft
felipemello1 wants to merge 6 commits into
mainfrom
felipemello1/gdn-kda-fused-chunk-kernels
Draft

felipemello1 wants to merge 6 commits into
mainfrom
felipemello1/gdn-kda-fused-chunk-kernels

Conversation

@felipemello1

@felipemello1 felipemello1 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Summary (human-written)

Agent details

TLDR: chunk_kernel_options now 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.

On main, the cuDNN GDN chunk kernel scores one token of a 78,147-token sequence -1.91 in the full sequence and -10.35 in its own prefix, while the fused kernel this PR always picks gives -0.0146 both ways (fp32 reference -0.016).

The image predates main's rename of the backend "cudnn" to "warpdrive".

# before: warpdrive for packed (batch 1) fp16/bf16 on SM100/SM103, outside deterministic and batch-invariant modes
return KernelOptions(backend="warpdrive" if use_warpdrive else "fused")
# after
return KernelOptions(backend="fused")

Problem

  1. On main, chunk_kernel_options picks 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 the cudnn names. The trainer packs documents into one sequence, so its bf16 steps there run warpdrive. The RL generator's GDN prefill calls the same function.
  2. A causal model's logprob at position p can't depend on tokens after p. Scoring the full sequence and scoring only its prefix must agree, up to float noise.
  3. On one 78k-token sequence, the same backend under its old name (cuDNN, attn_gym 0.0.20) breaks that at one token, by 8.4 nats with main's training attention (Results).
  4. A wrong trainer logprob gives a wrong PPO ratio and policy gradient for that token.

Solution

  1. chunk_kernel_options returns backend="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.
  2. A TODO says when to bring warpdrive back: once it is causally consistent at long packed lengths. It keeps the cuDNN numbers and speed-up below, and asks for an Attention Gym issue with this repro.
  3. KDA's warpdrive kernel was not measured, so it stays fused too.
  4. The RL README now says GDN runs the fused chunk kernels.
  5. Unchanged where main already used fused: Hopper, other dtypes, deterministic and batch-invariant modes.

Results

Numerics: not bitwise vs main on SM100/SM103 (another kernel). Bitwise unchanged elsewhere (same kernel as main).

Qwen3.8-27B, one 78,147-token sequence (prompt + 1,024 sampled tokens), 1 GB300, TorchTitan trainer model,
attn_gym 0.0.20, torch 2.16.0.dev20261107, cuDNN 9.27. Logprob of the generators' token at completion position 562:
GDN chunk kernel   full attention                              full sequence   prefix ending at 562
cuDNN              FA4 (what main trains with at head dim 256)  -1.91           -10.35
cuDNN              cuDNN (main's default in no-grad forwards)   -11.55          -0.0002
fused              FA4                                         -0.0146         -0.0146
reference: transformers fp32 (fp32 weights and activations, SDPA EFFICIENT, TF32 off) -0.016,
TorchTitan's vLLM generator -0.0025, vLLM's own Qwen3.5 model -0.0001
All rows are no-grad forwards. The cuDNN GDN forward runs the same 3 kernels with and without grad,
with bitwise-equal output (chunk_gdn alone, GB300), so the first row carries over to training.

The cuDNN GDN kernel alone (GB300, attn_gym 0.0.20). Prefix check: max |out(first P tokens) - out(all T)[:P]|
random inputs, T = 77,685 / 131,072 / 262,144, P = T - 1      0 (fused: 0)
random inputs, T = 78,147, P = 77,685 (the failing pair)      0 (fused: 0)
48 GDN layers' real inputs, T = 78,147, P = T - 1             0 at every layer
48 GDN layers' real inputs, T = 78,147, P = 77,685            nonzero at all 48 layers: 0.0625 at layer 0 (|v| up to 48.5),
                                                              <= 0.002 elsewhere (~1 bf16 ulp of the output)
48 GDN layers' real inputs, cuDNN vs fused                    within bf16 rounding (per-token median 0.06-0.54%)
cuDNN in GDN layers 0-11 / 12-23 / 24-47 only, fused elsewhere   token not flipped; layers 0-23 together flip it
32,768 sampled tokens (temperature 1, 55k-249k contexts)     |log ratio| > ln 5: 1 token with either kernel set

GDN fwd+bwd per layer, cuDNN vs fused (16 q/k and 48 v heads; median of 5):
T = 77,685: 11.8 vs 22.4 ms (1.90x)   131,072: 19.6 vs 37.2 ms (1.90x)   262,144: 38.6 vs 74.5 ms (1.93x)
TP2 heads (8 q/k, 24 v): 131,072: 2.02x   262,144: 2.08x
  • So on real inputs, the cuDNN kernel's output at a position depends on how many tokens follow it, by about 1 bf16 ulp per layer. Over layers 0-23 that adds up and flips a fragile greedy token.
  • The fused kernel's prefix check on real inputs was not run. At model level it scores the token the same in the full sequence and the prefix.
  • These checks ran the attn_gym 0.0.20 release, where the backend was named "cudnn" (see the first NOTE).
  • Qwen3.8-27B SWE-rebench V2.1 runs on 6 GB300 nodes train and generate with this function (fused for every input in the trainer and in the generator's GDN prefill).
  • In sampled text it is rare. On those 32,768 tokens, main's no-grad kernels (cuDNN GDN + cuDNN attention) and fused GDN + Flash give the same rate, mean gap and KL. Position 562 is a greedy token that the fp32 reference gives p = 0.98.

You may ask:

  • Why not a config field? chunk_kernel_options has no config today, and no known input marks the cuDNN kernel safe. The TODO keeps the function as the single switch.
  • Why not deterministic mode? It also picks fused, but it changes other kernels too.
  • Why not keep warpdrive, now that main renamed the backend? Nobody has run the full-vs-prefix check on it. Also, with nvidia-cutlass-dsl 4.7.1 its kernels fail to compile (Test plan), though Attention Gym's pin allows that version.

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: no dist_moe package (1), no local Qwen3.5-0.8B weights (2).
  • pre-commit on the changed files: every hook passes except lychee, which times out on 3 links in rl/README.md lines 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.
  • The full-vs-prefix checks above (at an earlier head).
  • Not run: the 2-GPU test_bitwise_parity.py (this box has 1 GPU).

Felipe Mello and others added 2 commits November 20, 2026 11:59
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.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Meta Open Source bot. label Nov 20, 2026
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.
@felipemello1

Copy link
Copy Markdown
Contributor Author

Reply from claude: @claude please review

@claude

claude Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Claude finished @felipemello1's task in 2m 30s —— View job


Needs discussion -- review posted.

@claude claude Bot 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.

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

  1. 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.
  2. linear_attention.py:63 -- TODO with no upstream issue link (inline).

Should consider

  • linear_attention.py:67 -- del q leaves 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 call chunk_kernel_options. Batch-invariant RL parity (test_bitwise_parity.py) already used fused, so it is unaffected.
  • Removing the is_in_batch_invariant_mode import doesn't break any test monkeypatch. test_gdn_graph_metadata.py:98 patches the vLLM linear_attention module, 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/.

Comment on lines +63 to +66
# 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.

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.

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

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.

q is now unused, and so is the warpdrive plumbing that only this switch could reach:

  • torchtitan/rl/generator/attention/gdn.py:130-137 and kda.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:50 and kda.py:54 still register ChunkGdnWarpdrivePacked / 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.

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

ciflow/rl CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant