Repository navigation
Prefer the cuDNN SDPA backend on Hopper - #48163
qgallouedec wants to merge 7 commits into
Conversation
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
1c34ecd to
4ca643d
Compare
vasqu
left a comment
There was a problem hiding this comment.
I'm not too opposed to this because in a sense it's a free perf gain. I have some initial questions first tho
Still a bit surprised about the blackwell results ngl but hopper shouldnt be changing anymore so these should be consistent improvements
| Hopper only, because that is where cuDNN wins: measured forward+backward at 8K-128K sequence | ||
| lengths, cuDNN is 1.7-2.3x faster on H100 and H200, but 0.82x on A100 and 0.88x on Blackwell | ||
| (RTX PRO 6000, sm120). sm100 was not measured. Benchmark and numbers: | ||
| https://github.com/huggingface/transformers/pull/48163 |
There was a problem hiding this comment.
I'm a bit surprised that blackwell is losing quite a bit as well, potentially a big reason why this is still seen as quite experimental.
Another thing I'm concerned about is the shipped cuda version you tested these on: Cudnn implementations may vary would be my initial intuition
| # Expressing a preference costs ~18us per call (it saves and restores global backend flags), so it is | ||
| # only worth it when the attention call itself is substantial. During decoding `q_length == 1` and that | ||
| # overhead would dominate, so keep the default backend there. | ||
| if q_length == 1 or not torch.cuda.is_available() or not _prefer_cudnn_sdpa(torch.cuda.current_device()): |
There was a problem hiding this comment.
Could we not cache the cuda availability (torch.cuda.is_available()) into _prefer_cudnn_sdpa?
|
|
||
|
|
||
| @lru_cache | ||
| def _prefer_cudnn_sdpa(device: int) -> bool: |
There was a problem hiding this comment.
Honestly we could keep them public as well
CI recapDashboard: View test results in Grafana |
|
Thanks for the review. Answering the CUDA version question turned up something that says this PR should not land 🫠
PyTorch already does exactly this.
The builds where this PR does change something are the ones PyTorch excludes on purpose. That threshold comes from pytorch/pytorch#138522 making cuDNN opt-in after the 2.5 bugs (pytorch/pytorch#139298 among them), and the comment above it points at pytorch/pytorch#169849, a seq_len not divisible by 128 fix that only landed in cuDNN 9.15.1. Forcing it there overrides a deliberate guard rather than winning anything. Closing. For context: I only saw a win because our cluster puts the system CUDA on |
|
Woah I did expect something re cuda but that's super interesting and TIL as well. A bit sad but it's something very good having it documented somewhere and on how to setup on our clusters, appreciate it. |


torch.nn.functional.scaled_dot_product_attentionpicks its backend by a fixed priority that puts FlashAttention first. On Hopper the cuDNN attention kernels are consistently 1.7–2.3× faster for training shapes, at the same numerical accuracy. This adds a backend preference (not a restriction) on Hopper, so shapes cuDNN cannot serve fall back exactly as before.Confirmed that the default really is FlashAttention — profiling Qwen3-0.6B at 16K on
mainshowspytorch_flash::flash_bwd_.../flash_fwd_kerneltaking 335 ms of a 677 ms step.Measured
One GPU, pure torch, no transformers involved: forward+backward through
scaled_dot_product_attentiononce per backend (script at the end of this section).cuDNN is ahead at every sequence length and at every head shape shipped by the models people are training
today, from 1.34x (GLM-5.2, 64 heads x 192) to 1.83x. The gradients land at the same distance from an fp64
reference, so this is a free change numerically.
Confirmed that the default really is FlashAttention: profiling Qwen3-0.6B at 16K on
mainshowspytorch_flash::flash_bwd_.../flash_fwd_kerneltaking 335 ms of a 677 ms step.repro_cudnn_sdpa.pyImplementation
wrapping the single
scaled_dot_product_attentioncall insdpa_attention_forward.Design notes, each of which I measured the hard way:
q_length == 1gate is load-bearing. Expressing a preference costs ~18 us because it saves and restores global backend flags. That is nothing next to a training attention call, but a decode-shaped call is only ~32 us, so wrapping it ungated made 64-token generation 13% slower (2872 ms vs 2542 ms). With the gate, generation is back to baseline.sdpa_attention_forwardand therefore re-selects the same backend. Wrapping at a coarser granularity that does not cover the backward givesCheckpointError: Recomputed values ... have different metadata.set_priority=Trueexpresses an order, so shapes cuDNN cannot serve fall through to flash/efficient/math (verified withhead_dim=72, which cuDNN refuses).End-to-end effect (steady-state median step time, gradient checkpointing, Qwen3-0.6B unless noted)
mainLosses match to 5 decimal places in every case (e.g. 11.42171 vs 11.42174). The gain grows with how much of the step is attention, and there is no configuration in which this is slower.
Caveats / questions for maintainers
is_causal and attention_mask is Nonewould still capture nearly all of the training win.If you would rather not take this
I'd understand if you decide not to move forward: it changes a default for everyone
Nothing is blocked either way, users can select the backend themselves, but it's not super clean:
It has to wrap forward and backward, since checkpoint recompute must pick the same backend. This is what I
used for every long-context run here.
So the question is really whether the default should do this for people who will never read a benchmark. I
think yes, but happy to close it and document the workaround instead.