← Back to Pull requests

feat(qsa_ops): add Qwen4Exp quantized sparse attention #5862

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Oct 01, 2026 09:41 UTC
Author

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

📌 Description

Qwen4Exp's quantized sparse attention (QSA) for SM80 and newer, as flashinfer.qsa_ops, with the two pieces of common code it needs.

A QSA layer scores a compressed index cache, keeps the top blocks per query, and attends over those tokens where they sit in the paged KV cache. The package has the per-step kernels (pre-indexer, paged scorer, route expansion and mapping, output gate) and three objects that run them out of caller-owned workspaces, so a captured CUDA graph can replay a whole step: QSASelection, QSAAttention and QSA. qsa_capabilities() tells a serving stack what the build carries.

Two changes to existing code support it:

  • BlockSparseAttentionWrapper runs on a paged KV cache (plan(..., kv_cache_page_size=...); dense, FP8 or NVFP4; NHD or HND) and reports its workspace size before planning, so several live plans can share one arena. It also fixes the default FP8 scales, which were sized by the block width instead of the KV-head count.

  • top_k_ragged_transform takes out, workspace and a resolved backend, so a caller can run it without allocating and with the backend fixed up front. Calls without them are unchanged.
  • The kernels run on SM80 and every later architecture: the JIT and AOT builds target 8.x through 12.x, and there are no architecture-specific paths, so newer GPUs run the same mma.sync and cp.async code rather than tuned variants. SM7x is excluded because the scorer's m16n8k16 arrived with SM80. The pre-indexer, scorer and route are one JIT module; the gate is a second one built without fast math, so a subnormal logistic is not flushed to zero. cp_async.cuh gains one L1-caching copy and mma.cuh the m16n8k16 helpers; existing functions are untouched.

    #5859, #5860 and #5861 were folded into this PR and closed.

    🔍 Related Issues

    Tracked in #5669.

    🚀 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.).
  • Each kernel is checked against a torch reference; the selection, the attention and a whole step against torch, including workspace lifetimes, plan freezing and CUDA-graph replay. The paged block-sparse route is checked against dense attention over the same entries for bf16, FP8 and NVFP4 caches, and the top-k caller-buffer path against the per-call transform. On sm_80 everything in tests/qsa_ops, tests/attention/test_block_sparse.py, tests/utils/test_topk.py and the trace tests passes; the SM90-only FP8 default-scale test skips. Nothing has run on SM90 or newer.

    🔬 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.


    `

    Reviewer Notes

    The only changes to shared paths are paged_kv_t, which gains a storage_page_size field and a resolve_slot step on every paged kernel's address computation (a uniform branch that is a no-op when the logical block is the page), and the three keyword arguments on top_k_ragged_transform. Everything else is new code under flashinfer/qsa_ops, include/flashinfer/attention/qsa and csrc/qsa_*`. The paged route is served by the FA2 kernel on every architecture; FA3 and trtllm-gen do not take a paged route here, so on SM90 and newer QSA attention runs FA2. Nothing has run on SM90 or newer.

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: lesj0610 Review duration: 24276m
    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 #5862"
    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%