[perf]FlashInfer cuDNN batched prefill attention - #1827
Open
klhhhhh wants to merge 19 commits into
Open
Conversation
Contributor
Merge Protections🔴 1 of 1 protections blocking · waiting on 👀 reviews and 🤖 CI
🔴 PR merge requirementsWaiting for
This rule is failing.
|
Contributor
Pre-commit checks failedHi @klhhhhh, the pre-commit checks have failed. To fix them locally: # Install pre-commit if you haven't already
uv pip install pre-commit
pre-commit install
# Run all checks and auto-fix what's possible
pre-commit run --all-filesCommon fixes:
After fixing, commit and push the changes. The checks will re-run automatically. For future commits, |
klhhhhh
force-pushed
the
flashinfer-cudnn
branch
from
September 8, 2026 01:18
025faf7 to
981a04b
Compare
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.
Dependency
This PR depends on:
PR #1799 introduces the opt-in FlashInfer attention backend for Wan using
single_prefill_with_kv_cache.This PR extends that backend with FlashInfer's batched cuDNN prefill kernel and
should be reviewed and merged after #1799.
Summary
This PR adds an opt-in FlashInfer cuDNN prefill path using:
The implementation processes the complete batch through one packed cuDNN
attention call instead of invoking the single-request kernel once per batch
item.
Enable it before constructing the model:
The existing single-request implementation remains the default:
Therefore, this PR does not change the default behavior introduced by #1799.
Motivation
The FlashInfer backend introduced in #1799 invokes
single_prefill_with_kv_cacheseparately for every item in the batch:This is effective for batch size 1, but larger batches require multiple kernel
launches followed by output stacking.
FlashInfer's cuDNN prefill API accepts packed Q/K/V tensors and batch offsets,
allowing the complete batch to be processed by one batched attention call.
This is particularly beneficial for large-batch GQA cross-attention with short
or medium query lengths and long KV sequences.
Implementation
FlashInfer implementation selection
This PR adds:
The default value is
single.The variable only selects the implementation within the
FLASHINFERbackend.The existing top-level selection remains unchanged:
Batched Q/K/V packing
FastVideo provides attention tensors in BSHD layout:
The cuDNN path packs them as:
It constructs token-unit batch offsets:
The packed tensors are passed to:
The output is then restored to FastVideo's BSHD layout.
Workspace reuse
The cuDNN API requires a workspace buffer. This PR allocates a 128 MiB
workspace and shares it across attention layers on the same CUDA device:
This prevents every Transformer attention layer from allocating its own
workspace.
Availability checks
When cuDNN is selected, CUDA backend initialization verifies that:
cudnn_batch_prefill_with_kv_cacheis available.
This makes an incompatible FlashInfer installation fail during backend
initialization rather than during the first attention forward.
Supported behavior
The cuDNN path currently supports:
Current restrictions:
Unsupported configurations raise explicit errors instead of silently falling
back to another kernel.
Tests
This PR adds CPU/mock coverage for:
It also adds a real CUDA parity test against Torch SDPA covering:
Test command:
Kernel benchmark
The benchmark compares:
The cuDNN workspace and batch offsets are created outside the timed region.
Latency is measured with CUDA events.
The benchmark script is included below for reproducibility only. It is not
added to the repository by this PR.
Benchmark script
Benchmark invocation
The reported large-batch results were produced with:
Representative diffusion workload: B=1, 32K self-attention
To complement the synthetic large-batch GQA sweep below, this benchmark uses a
shape representative of Wan dense self-attention: batch size 1, square Q/KV,
40-head MHA, head dimension 128, BF16, and non-causal attention.
Median kernel latency on NVIDIA GB10:
For this representative batch-size-one workload, the raw cuDNN path is 5.1%
slower than FlashAttention and 6.1% slower than FlashInfer single prefill. The
FastVideo cuDNN path is 5.7% slower than FlashAttention, while the FastVideo
single path is effectively at parity. The maximum absolute difference from
FlashAttention is 2.44e-4 for every FlashInfer path.
This result confirms that the cuDNN API is numerically correct for long
batch-size-one diffusion attention, but does not provide a performance benefit
for this shape. Its advantage in the synthetic sweep appears specifically in
larger-batch regimes, consistent with FlashInfer exposing it as a batched
prefill kernel. Therefore, cuDNN remains opt-in and the single-request path
remains the default.
Synthetic large-batch benchmark environment
Synthetic large-batch results
Median kernel latency:
Synthetic large-batch observations
The cuDNN path performs best for large-batch GQA cross-attention with short or
medium query lengths.
At
B=32, Q=128, KV=4096, cuDNN achieves:At
B=32, Q=256, KV=4096, cuDNN achieves:The benefit increases with batch size and decreases as query length grows.
For small batches, the single-request path remains competitive. For
Q=1024, FlashAttention is still slightly faster.These results support keeping the cuDNN implementation opt-in rather than
making it the universal FlashInfer default.
Limitations
generation.
NVIDIA architecture.
Follow-ups