Skip to content

perf(dsa): compute indexer scores with the cuDNN kernel - #8154

Open
JackRao123 wants to merge 3 commits into
NVIDIA:mainfrom
JackRao123:jackrao/upstream-dsa-cudnn-chunked-indexer-scores
Open

JackRao123 wants to merge 3 commits into
NVIDIA:mainfrom
JackRao123:jackrao/upstream-dsa-cudnn-chunked-indexer-scores

Conversation

@JackRao123

@JackRao123 JackRao123 commented Oct 12, 2026 •

Copy link
Copy Markdown

What

_compute_indexer_scores_chunk_with_global_rows computed indexer scores with a per-head fp32 PyTorch loop: bmm, then relu_, mul_ and add_ for each indexer head, all on SIMT rather than tensor cores. It serves two callers:

  • _indexer_topk_from_score_chunks, whenever a chunk doesn't cover every row or carries explicit lengths: long causal calls that need chunking, and calls with varlen bounds;
  • the full-score varlen branch of _indexer_topk_bshd.

This PR computes the scores with indexer_forward_wrapper instead, under the same name, and deletes the loop:

  • Plain causal chunks: pass q_causal_offsets=row_start, which is the reference mask ((row_start + i + 1) // ratio).
  • Chunks with explicit lengths: run with the tightest causal offset that covers every row, computed on device with no host sync. The exact per-row prefix is then applied, so masked positions match the reference exactly.

q_causal_offsets is available in the pinned nvidia-cudnn-frontend==1.29.0.

Numerics

  • New test test_dsa_cudnn_indexer_scores.py: compares against a direct einsum reference over 9 cases: causal at ratio 1 and 4, unaligned row offsets, pooled bounds, empty rows, non-monotonic lengths, lengths beyond sk, packed resets.
  • Result: the -inf mask is identical in every case; max abs diff ≤ 1.2e-4 on scores up to ~300.
  • Environment: this branch on GB300 with nvidia-cudnn-frontend==1.29.0, 9/9 passing.

Performance

Measured on a downstream fork with a model whose indexer always passes explicit bounds (a 4:1 pooled-key indexer). 262k tokens on 8x GB300, CP4 EP8:

  • Forward/backward step: 50.7 s → 32.7 s. The fp32 loop was about 15 s of it.
  • Over 6 steps, loss and grad norm match the reference path within run-to-run noise.

Upstream models gain wherever they hit chunked scoring with explicit bounds, long causal calls, or the full-score varlen branch; I haven't benchmarked an upstream model.

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

_compute_indexer_scores_chunk_with_global_rows computed indexer scores with a
per-head fp32 PyTorch loop that runs on SIMT instead of tensor cores. It
serves chunked top-k whenever a chunk does not cover every row or carries
explicit lengths, and the full-score varlen branch of _indexer_topk_bshd.

Compute the scores with the cuDNN indexer kernel instead. Plain causal chunks
pass q_causal_offsets=row_start, which is the reference mask. Chunks with
explicit lengths run with the tightest causal offset that covers every row,
computed on device, then apply the exact per-row prefix, so masked positions
match the reference.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jack Rao <jack.rao@baseten.co>
@JackRao123
JackRao123 force-pushed the jackrao/upstream-dsa-cudnn-chunked-indexer-scores branch from 1de576b to e4ee673 Compare October 13, 2026 18:30
@JackRao123 JackRao123 changed the title perf(dsa): score chunked indexer rows with the cuDNN kernel perf(dsa): compute indexer scores with the cuDNN kernel Oct 13, 2026
JackRao123 and others added 2 commits October 22, 2026 09:23
…unks

cuDNN frontend >= 1.27 rejects seqlen_q > seqlen_k * ratio. Pooled-key
chunks (for example 2312 query rows against 578 pooled keys at ratio 1)
hit this check when explicit seq_lens are passed to
_compute_indexer_scores_chunk_with_global_rows.

Explicit seq_lens replace the kernel's causal mask through the
masked_fill_ that follows the kernel call, so any ratio that covers them
is valid. Raise the ratio to ceil(sq / sk) before the causal offset is
computed from it: the offset (seq_lens * ratio - row - 1) then still
makes row i keep at least seq_lens[i] keys under the ratio the kernel
actually uses.

Add test cases with more query rows than pooled keys * ratio, and make
the number of rows a test parameter.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jack Rao <jack.rao@baseten.co>
The cuDNN indexer fake kernels in test_dsa_native_parity.py did not accept
the q_causal_offsets argument that the chunked score path now passes, so
seven tests failed with a TypeError or wrong selections.

Add a shared fake that emulates the kernel's ratio causal mask from
q_causal_offsets, and use it where the old stubs either ignored the
offsets or asserted that the kernel is never called: the explicit
seq_lens chunk path now scores with the kernel and applies seq_lens
afterwards. Stubs that return fixed scores just accept the new argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Jack Rao <jack.rao@baseten.co>
@JackRao123

Copy link
Copy Markdown
Author

Two follow-up commits:

  • 6853e28e1: cuDNN frontend >= 1.27 rejects seqlen_q > seqlen_k * ratio, which pooled-key chunks with explicit seq_lens hit (e.g. 2312 query rows vs 578 keys at ratio 1). The ratio is now raised to ceil(sq / sk) before the causal offset is computed; seq_lens are still applied by the masked_fill_ after the kernel. Adds test cases with more rows than keys * ratio (they fail without the fix on cudnn-frontend 1.29.1).
  • 5d2044ca8: updates the fake cuDNN indexer stubs in test_dsa_native_parity.py for the new q_causal_offsets argument.

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

community-request waiting-on-maintainers Waiting on maintainers to respond

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants