Skip to content

perf(b12x/qsa): cap workspace_q_rows at 2048 and early-exit empty score chunks - #174

Open
avifenesh wants to merge 1 commit into
local-inference-lab:mainfrom
avifenesh:perf/b12x-qsa-early-exit-w2048
Open

avifenesh wants to merge 1 commit into
local-inference-lab:mainfrom
avifenesh:perf/b12x-qsa-early-exit-w2048

Conversation

@avifenesh

Copy link
Copy Markdown

Summary

Eliminate redundant multi-chunk work in QSA group selection while preserving exact bitwise selection results:

  1. Cap workspace_q_rows at min(max_q_rows, 2048) in qsa._contract._scratch_layout:

    • Within the default 128 MiB score workspace budget, uncapped workspace_q_rows = max_q_rows narrows score_workspace_width as prefill chunk size grows: at 262k max context (max_groups = 65536, group_budget = 512), max_q_rows = 4096 required 9 score chunks, 8192 required 19 chunks, and 16384 required 43 chunks.
    • Capping workspace_q_rows at 2048 keeps score_workspace_width = 16384 (5 chunks for 262k context) regardless of max_q_rows, shrinks auxiliary scratch buffers (topk_values, topk_indices, stable_topk, prepared_query), and reuses the existing for row_start in range(0, rows, work_rows) chunk loop in _qsa_decode_impl.
  2. Canonical buffer A + early-exit across empty score chunks:

    • Keep running top-k winners in canonical buffer A (chunk_topk_values, chunk_topk_ids) across score chunks rather than ping-ponging A/B, writing per-chunk radix scratch into buffer B for score_chunk > 0 and scattering final stabilized rows back to buffer A.
    • When a row has no newly eligible groups in a later chunk (group_offset > 0 and chunk_eligible == 0):
      • _stage_topk_carry_kernel exits immediately when eligible <= GROUP_OFFSET.
      • _score_representatives_tiled_kernel (with skip_empty_chunks=True) writes merge_lengths = 0.
      • DSATiledTopkKernel (with skip_leq_topk=True) skips rows with length <= topk because StableSelectionKernel handles length <= budget directly.
      • StableSelectionKernel (with preserve_empty_carry=True, compile spec version 3 -> 4) and _copy_stable_topk_kernel (PRESERVE_EMPTY_CARRY=True) exit without touching canonical buffer A, preserving the prior chunk's exact winners.
    • All new behavior flags (skip_empty_chunks, skip_leq_topk, preserve_empty_carry) default to False on standalone helper entry points so existing callers and unit tests retain unchanged contracts.

Benchmark (NVIDIA GB10 / DGX Spark, SM 12.1)

End-to-end Nsight Systems profile of a 32,768-token Qwen3.8 Flash-Next prefill on GB10 (all 4 selector kernels combined: _score_representatives_tiled, DSATiledTopk, StableSelection, _copy_stable_topk + _stage_topk_carry):

  • 447.5 ms -> 139.0 ms (-308.5 ms, -69.0%) vs stock 4K-chunk baseline.
  • Enables 8K and 16K prefill chunks without selector chunk-count blowup (saves over 830 ms vs uncapped 8K chunks).

Testing

  • Updated test_qsa_score_workspace_bound_sets_selection_chunks in tests/experimental/b12x/attention/test_qsa_contract.py for the 2048-row workspace cap (including max_q_rows=8192).
  • Added test_stable_selection_preserve_empty_carry_keeps_dest_untouched in tests/experimental/b12x/attention/test_qsa_stable_selection.py.
  • Ran on GB10 (SM 12.1):
    • pytest --confcutdir=tests/experimental/b12x tests/experimental/b12x/attention/test_qsa_stable_selection.py tests/experimental/b12x/attention/test_qsa_program_keys.py tests/experimental/b12x/attention/test_qsa_fixed_splits_gpu.py tests/experimental/b12x/attention/test_qsa_contract.py -k "score_workspace_bound or tiled_scores_match_scalar or tiled_representative or multi_chunk or select_only or stable_selection or preserve_empty or fixed_splits" -> 46 passed.
    • pytest --confcutdir=tests/experimental/b12x tests/experimental/b12x/attention/test_paged_prefill_topk_long_context.py -> 11 passed.

…re chunks

Avoid redundant work in multi-chunk QSA group selection while staying bit-exact:

1. Cap _scratch_layout workspace_q_rows at min(max_q_rows, 2048). For max_q_rows >= 4096 within the default 128 MiB score workspace, this keeps score_workspace_width at 16384 (5 chunks for 262k context instead of 9 at 4096, 19 at 8192, or 43 at 16384) and shrinks auxiliary scratch tensors.
2. Keep canonical running top-k winners in buffer A across chunks so chunks with no newly eligible groups (group_offset > 0 and chunk_eligible == 0) early-exit across _stage_topk_carry_kernel, _score_representatives_tiled_kernel (merge_lengths=0), DSATiledTopkKernel (skip_leq_topk=True), StableSelectionKernel (preserve_empty_carry=True), and _copy_stable_topk_kernel.
3. Preserve default behavior (skip_empty_chunks=False, skip_leq_topk=False, preserve_empty_carry=False) on standalone helper entry points so existing unit tests and callers keep unchanged contracts.
@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 19 minutes.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ae7133dd-96ed-4626-aa2a-d55214d50f01
Commits

Reviewing files that changed from the base of the PR and between 335459b and ca6b0cb.

Files selected for processing: 6

  • flashinfer/experimental/b12x/attention/dsa_indexer/tiled_topk.py
  • flashinfer/experimental/b12x/attention/qsa/_contract.py
  • flashinfer/experimental/b12x/attention/qsa/_kernels.py
  • flashinfer/experimental/b12x/attention/qsa/_stable_select_cute.py
  • tests/experimental/b12x/attention/test_qsa_contract.py
  • tests/experimental/b12x/attention/test_qsa_stable_selection.py
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@avifenesh

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Action not completed

Review rate limited.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant