← Back to Pull requests

feat(attention): attention sinks over an NVFP4 KV cache in prefill and decode #6021

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 03, 2026 12:59 UTC
Author

<!-- .github/pull_request_template.md -->

📌 Description

BatchAttentionWithAttentionSinkWrapper cannot be built for an NVFP4 KV cache, and the fa2 decode path has no attention-sink wrapper at all. On GPUs without XQA decode, such as SM8x, a sink model therefore cannot decode from a paged cache with FlashInfer, whatever the KV dtype.

This PR:

  • Builds the sink module's JIT arguments in one place, BatchAttentionWithAttentionSinkWrapper._sink_jit_args, which prefill and decode share.

  • - With kv_data_type=torch.uint8 on fa2 it declares maybe_k_cache_sf and maybe_v_cache_sf, which run(..., kv_cache_sf=...) already fills. fa3 still rejects packed FP4 through the existing check.
    - The module name now carries the KV dtype, head dims, position encoding and fp16 QK reduction. These variants used to share one name, so a process that used two head dims ran the first-built module for both: on SM80, bf16 sinks at head_dim 64 and 128 in one process gave a relative error of 0.6 to 1.2 against a torch reference for whichever was built second.
  • Adds BatchDecodeWithAttentionSinkWrapper, which builds the same module through the tensor-core decode wrapper, so sink decode is planned and captured in CUDA graphs like any decode wrapper. It is called as run(q, paged_kv_cache, sinks, sm_scale, ...), like the prefill wrapper.

  • Gives BatchDecodeWithPagedKVCacheWrapper the jit_kwargs argument the prefill wrappers already have, and passes the NVFP4 block scales to custom modules as the prefill wrapper does.
  • 🔍 Related Issues

    Fixes #5966. #6012 adds the same two scale tensors to the prefill sink wrapper; this PR also covers decode and the module-name collision. The module-name part overlaps #5535, which fixes the same collision through get_batch_prefill_attention_sink_uri; I will rebase onto whichever lands first.

    🚀 Pull Request Checklist

    Thank you for contributing to FlashInfer! Before we review your pull request, please make sure the following items are complete.

    ✅ Pre-commit Checks

  • I have installed pre-commit by running pip install pre-commit (or used your preferred method).

  • I have installed the hooks with pre-commit install.

  • I have run the hooks manually with pre-commit run --all-files and fixed any reported issues.
  • If you are unsure about how to set up pre-commit, see the pre-commit documentation.

    🧪 Tests

  • Tests have been added or updated as needed.

  • All tests are passing (unittest, etc.).
  • test_attention_sink_nvfp4_kv runs decode, and prefill in 33-token chunks (the repack path), over an NVFP4 cache and checks both against the prefill sink wrapper over the dequantized bf16 cache, with and without a sliding window. On SM80, tests/attention/test_attention_sink.py: 490 passed, 560 skipped (fa3), 96 xfailed. tests/utils/test_jit_example.py -k batch_decode: 4 passed.

    Also checked on SM80, outside the test suite:

  • The prefill wrapper's JIT arguments against main over 768 input combinations: only the module name differs, plus the two scale tensors for fa2 with uint8 KV.

  • The decode wrapper against a torch reference: relative error about 1.8e-3 for head_dim 64 and 128, bf16 and NVFP4 KV, split-KV on and off, and identical results under CUDA-graph replay.
  • 🔬 Experimental Track

    <!-- Only for PRs submitted under the experimental policy (CONTRIBUTING.md → "Experimental APIs and Backends").
    Leave this section untouched for normal PRs. -->

  • This PR is experimental: it adds or changes code under flashinfer/experimental/ and/or an @flashinfer_experimental_api. Tracking issue: #

  • - [ ] The tracking issue names an owner, the reason for the experimental path, and a graduation plan with a target release.
    - [ ] Core changes are limited to a thin entry point (signature, shared validation, feature-gate check, backend selection, handoff).
    - [ ] Tests live in tests/experimental/ and were validated on the intended hardware; a runnable example is included.
    - [ ] Nothing is registered in flashinfer/aot.py, and no experimental backend is reachable from backend="auto" without FLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1. (Calling an @flashinfer_experimental_api or naming a backend explicitly is itself the opt-in and needs no environment variable.)
    - [ ] Test scope declared below. The experimental CI lane runs exactly these targets, so keep them as narrow as the change allows.

    <!-- Required for experimental PRs. Replace the commented lines below with your targets.
    Do not delete the fence or change its experimental-tests tag — the experimental-track
    watcher reads it verbatim to decide which targets to ask CI for. -->

    ``experimental-tests

    One target per line: a directory or a file. (A pytest ::selector is not


    supported -- the sharding runner cannot consume one.) Must be under


    tests/experimental/ and must exist. Delete these comment lines and add yours, e.g.


    # tests/experimental/test_my_backend.py


    tests/experimental/my_backend/


    # Declaring the whole tree (tests/experimental/) is allowed but means every


    experimental PR pays for every other feature's tests, in every matrix cell.


    ``

    <!-- This is an auto-generated comment: release notes by coderabbit.ai -->

    Summary by CodeRabbit

    New Features
    Added a public batch decode wrapper for attention-sink workloads, with support for NVFP4-quantized key/value caches.
    Added optional JIT keyword arguments to paged KV-cache wrappers.
    Documentation
    Added API documentation for the attention-sink batch decode wrapper.
    Tests
    * Added coverage comparing quantized-cache decode and prefill results with BF16 reference outputs.
    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: lesj0610 Review duration: 21197m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #6021"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%