← Back to Pull requests

perf(attention): group tiny-KV FP8 context for uniform Q1-3 #9018

Closed
unknown closed this pull request without merging on Nov 09, 2026 06:12 UTC
View on GitHub
unknown unknown commented on Nov 06, 2026 21:56 UTC
Author

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

📌 Description

The tiny-KV optimization added in #8736 only admitted BF16 Q/KV and two query tokens. FP8-E4M3 Q/KV with BF16 output therefore continued to use the Context path, whose latency grows sharply with batch size for the workload in #7965.

Extend the existing grouped route to equal BF16 or FP8-E4M3 Q/KV with BF16 output and uniform query lengths 1, 2, or 3. Keep the SM103, batch 4–256, declared KV length 1–32, 16:1 query/KV heads, head dimension 128, page size 128, packed HND layout, and existing feature/stride restrictions. Preserve Context workspace and no-split behavior; excluded inputs retain their existing dispatch.

B300 SM103 / CUDA 13.0 measurements for FP8 Q/KV → BF16, Q2/KV2, H16:1, D128, page128, causal attention and PDL enabled:

| Batch | Native before → after (µs) | Speedup | Wrapper before → after (µs) | Speedup |
| ---: | ---: | ---: | ---: | ---: |
| 8 | 6.337 → 5.543 | 1.14× | 6.327 → 5.533 | 1.14× |
| 16 | 8.554 → 5.539 | 1.54× | 8.548 → 5.538 | 1.54× |
| 24 | 10.971 → 5.588 | 1.96× | 10.943 → 5.578 | 1.96× |
| 32 | 13.008 → 5.595 | 2.32× | 13.012 → 5.599 | 2.32× |
| 48 | 17.642 → 5.687 | 3.10× | 17.648 → 5.694 | 3.10× |
| 64 | 21.538 → 5.811 | 3.71× | 21.570 → 5.683 | 3.80× |

Matched baseline–candidate–candidate–baseline runs compare the unchanged Context launcher at e9735b0239f834fa2f46a633e7ab76be49a2cc06 with this patch, using identical represented FP8 inputs, unit scales, preallocated output/workspace/counters and separate JIT caches. Warm CUDA graphs contain 100 calls; each run uses three rounds of ten CUDA-event samples, excluding allocation, wrapper planning and JIT warmup. Row order is reversed in the latter pair. Values are aggregate medians; all twelve rows exceed the predefined 0.3 µs and 5% improvement margins. Actual kernel traces confirm grouped dispatch, including the separate Q3 kernel family.

🔍 Related Issues

Related to https://github.com/flashinfer-ai/flashinfer/issues/7965, especially the FP8 reproducer. Follow-up to https://github.com/flashinfer-ai/flashinfer/pull/8736. Exact GB300 confirmation remains pending; this PR does not close the issue.

🚀 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.
  • All applicable hooks passed for the two changed files. The repository-wide --all-files command was not run.

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

  • This PR requires full-scope CI, not targeted tests (expensive; justify below).

  • Reason: Targeted attention coverage is appropriate; no full-suite run is requested.

    Suggested CI test targets (optional; paths under tests/, whitespace-separated — a starting
    point for @flashinfer-bot run <targets> / /bot run <targets>, reviewers may adjust): tests/attention/test_trtllm_gen_attention_prefill.py

    Targeted B300 validation:

  • python3 -m pytest tests/attention/test_trtllm_gen_attention_prefill.py -q -k tiny_kv --maxfail=5: 345 passed, 14,906 deselected.

  • Paired compatibility and boundary matrices: 72 and 48 rows passed, covering both APIs, BF16/FP8, Q1–3, short/zero KV, masked writes and graph replay. BF16 and excluded-shape fallback controls passed.

  • Known inherited FP8 reference misses were reproduced on unchanged Context. The two graph-update fixtures require bitwise equality to a separate Context execution and retain the independent reference check everywhere except the one documented coordinate. Six deliberate-error checks confirm this exception rejects drift, additional errors and nonfinite values. The historical batch-24 benchmark case also requires exact baseline parity; these discrepancies are not numerical fixes.

  • Changed-file hooks and independent diff/evidence review passed. The full repository suite was not run.
  • 🔬 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

    This remains a draft pending exact GB300/SM103 CUDA13 validation. B300 results establish preliminary kernel behavior and warm-graph performance, not the reporter's target-system or end-to-end serving result. The bounded cold-cache result remains inconclusive. FlashAttention parity, the full reporter shape census, and the full repository suite are unverified.

    Review focus: admission/fallback boundaries, Q3 dispatch, scale/masking/output semantics, and containment of the inherited numerical exceptions.

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

    <!-- coderabbit-ai-generated:v1 -->

    Summary by CodeRabbit

    New Features
    Expanded support for tiny-KV attention with BF16 and FP8 inputs across a wider range of query lengths and batch sizes.
    Bug Fixes
    Improved output correctness for empty requests, fully masked rows, and varied scaling settings.
    * Improved reliability when replaying captured graphs after input scales or KV-length metadata changes.

    <!-- end of auto-generated comment: release notes by coderabbit.ai -->

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: qsang-nv Review duration: 3376m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). 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.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    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.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #9018"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% 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: 60.5% · Lower = rushed or shallow review
    60.5%
    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: 20.5% · Safe to merge
    20.5%