-
Notifications
You must be signed in to change notification settings - Fork 264
torch: add the "CUDNN" torch.nn.attention provider (cudnn.torch) #554
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vedaanta
wants to merge
3
commits into
NVIDIA:develop
Choose a base branch
from
vedaanta:vagarwalla/cudnn-torch-provider
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,69 @@ | ||
| # SDPA torch custom ops: `cudnn::sdpa_fwd` / `cudnn::sdpa_bwd` | ||
|
|
||
| PyTorch custom ops (`torch.library`) exposing the full cuDNN SDPA feature | ||
| surface — the features `torch.nn.functional.scaled_dot_product_attention`'s | ||
| aten contract cannot express: | ||
|
|
||
| - **attention sinks** — per-Q-head logits folded into the softmax denominator | ||
| - **sliding window** — `window_left` (cuDNN convention: visible tokens | ||
| *including* self; FA2's `(w, 0)` maps to `window_left = w + 1`) | ||
| - **bottom-right causal alignment** — inference-style diagonals | ||
| - **padded batches** — per-batch actual lengths via `seq_len_q` / `seq_len_kv` | ||
| - **THD / varlen packing** — FlashAttention-style `(T, H, D)` + `cu_seqlens` | ||
|
|
||
| The ops build cuDNN pygraph `sdpa` / `sdpa_backward` nodes; the engine Router | ||
| picks the best serving plan (FROST OSS kernels or cuDNN-backend engines) per | ||
| configuration. Graphs are cached per configuration (bounded, thread-safe; | ||
| cuDNN handles are thread-local). | ||
|
|
||
| ## Usage | ||
|
|
||
| ```python | ||
| import torch | ||
| import cudnn | ||
|
|
||
| _ = cudnn.sdpa_torch # lazy public export: importing registers cudnn::sdpa_fwd / cudnn::sdpa_bwd | ||
|
|
||
| # Dense BHSD with sinks + sliding window | ||
| o, lse = torch.ops.cudnn.sdpa_fwd(q, k, v, scale, is_causal=True, | ||
| window_left=128, sinks=sinks, return_lse=True) | ||
|
|
||
| # THD / varlen (FA-style packed (T, H, D) + cu_seqlens), differentiable: | ||
| q, k, v = (t.requires_grad_(True) for t in (q_thd, k_thd, v_thd)) | ||
| o, lse = torch.ops.cudnn.sdpa_fwd(q, k, v, scale, is_causal=True, | ||
| cu_seqlens_q=cu, cu_seqlens_kv=cu, | ||
| max_seqlen_q=mx, max_seqlen_kv=mx, | ||
| return_lse=True) | ||
| o.backward(grad) # routes through cudnn::sdpa_bwd via register_autograd | ||
|
|
||
| # Or through the python wrapper (same op underneath): | ||
| o = cudnn.sdpa_torch(q, k, v, is_causal=True, cu_seqlens_q=cu, cu_seqlens_kv=cu, | ||
| max_seqlen_q=mx, max_seqlen_kv=mx) | ||
| ``` | ||
|
|
||
| ## Contracts and limits | ||
|
|
||
| - Dense tensors are BHSD `(B, H, S, D)` (any strides; the graph declares the | ||
| actual layout). Varlen tensors are packed `(T, H, D)`; non-contiguous views | ||
| (e.g. K/V slices of a fused `(T, 2, H, D)` KV projection) are declared with | ||
| their true strides. On the varlen path, a non-dense innermost dim or a | ||
| misaligned base pointer is repaired by one copy (warned as slow path); the | ||
| dense path declares the given strides as-is. | ||
| - One io dtype per call (`fp16` or `bf16`); mixed-dtype inputs are rejected. | ||
| - `sdpa_bwd` serves the **THD/varlen** path. Dense backward and sink backward | ||
| (dSink) are follow-ups and raise `NotImplementedError`. It consumes a | ||
| **padded** `(B, H, max_seqlen_q, 1)` fp32 LSE (backend restriction: bprop | ||
| THD rejects ragged LSE on SM8X/SM12X). | ||
| - Autograd (`register_autograd`) requires `return_lse=True` on the forward; | ||
| the glue converts the packed TH1 stats to the padded layout device-side. | ||
| - Both ops ship `register_fake` meta kernels. `cudnn::sdpa_fwd` passes | ||
| `torch.library.opcheck` on the dense and varlen paths, including | ||
| dynamic-shape AOT dispatch (`torch.compile`-ready); the opcheck autograd | ||
| case exercises `cudnn::sdpa_bwd` through the registered backward. | ||
|
|
||
| ## Requirements | ||
|
|
||
| - `nvidia-cudnn-frontend[cutedsl]`, cuDNN backend ≥ 9.6 (THD token-major | ||
| stats), sm80+. | ||
|
|
||
| Tests: `test/python/test_cudnn_sdpa_torch_ops.py`. |
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.