Skip to content

[Fix] Use cached prefix lengths for FlashInfer full-attention ragged prefill - #40796

Merged
Fridge003 merged 3 commits into
sgl-project:mainfrom
Jiminator:fix/flashinfer-ragged-prefix
Sep 24, 2026
Merged

Fridge003 merged 3 commits into
sgl-project:mainfrom
Jiminator:fix/flashinfer-ragged-prefix

Conversation

@Jiminator

@Jiminator Jiminator commented Sep 22, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

In a mixed sliding-window/full-attention model, FlashInfer ragged prefill plans the full-attention layer's paged read over seq_lens. The new K/V is not written to the cache until after attention, and the ragged wrapper already handles those tokens. Reading their cache slots can therefore mix stale K/V into the result.

Modifications

  • Plan the full-attention paged read over prefix_lens when ragged prefill is active. Use the host prefix total when available, retaining the existing device-sum fallback pattern.
  • Add one deterministic GPU regression to the existing FlashInfer SWA suite. It poisons unwritten extend slots and checks the real attention output. No model checkpoint is needed.

Paged-only execution is unchanged. Two files; eight backend lines added.

Accuracy Tests

On NVIDIA B300, using upstream 7b977ce5dcce6e61f0a5f8ef8b4235e42eb00299, Torch 2.13.0+cu130, FlashInfer 0.6.18, and sglang-kernel 0.4.7+cu130:

  • New regression on unmodified upstream: fails numerically, maximum absolute error 14.0 against tolerance 0.04. The same test passes with the fix.
  • FlashInfer SWA suite: 8 passed. Dense suite: 8 passed. No skips. The regression also passes with the optional host prefix list omitted.
  • Public-model reachability: unsloth/gemma-3-1b-it at 5b11413a10db4e486ef16a20101fd028f8f2499c, BF16, TP1, FlashInfer, page size 1, eager execution. Actual full-attention forwards use the ragged path. With a 384-token prompt and 257 cached tokens, the full wrapper plans 384 cache slots before the fix and 257 afterward.
  • For three frozen prompts below the sliding-window boundary, full-vocabulary next-token KL between cold and cached prefill changes from 3.50e-5 / 8.75e-5 / 4.17e-7 to 1.89e-8 / 2.76e-6 / 6.55e-8. This is numerical cache consistency, not a model-quality evaluation. A separate cold-versus-cached discrepancy above the sliding window remains outside this fix.

Reproduce the regression:

python3 test/registered/attention/unittests/swa/test_flashinfer.py \
  TestFlashInferSWAAttentionBackendCorrectness.test_full_layer_ragged_prefill_reads_cached_prefix_only -v

Changed-file pre-commit hooks pass. The all-files run passes its non-Rust hooks; four Rust hooks could not execute because the local host lacks cargo/rustup. No Rust files are changed.

Speed Tests and Profiling

No throughput improvement is claimed. The normal ragged-prefill path uses the existing CPU prefix lengths without adding a device-to-host scalar read.


CI States

Latest PR Test (Base): ⏳ Run #35834018141
Latest PR Test (Extra): ⚠️ Not enabled -- add run-ci-extra label to opt in.
Latest PR Test (AMD ROCm 10): ❌ Run #35834018015

if extend_prefix_lens_cpu is not None:
paged_kernel_lens_sum = sum(extend_prefix_lens_cpu)
else:
paged_kernel_lens_sum = prefix_lens.sum().item()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 2001 will cause severe performance degradation, since .item() is a synchronized operations. Can we make sure this branch is not hit?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch doesnt get hit in most use-cases, only one that I can see that hits this branch is dllm metadata caller, but removing this .item() or changing this branch requires alot of changes elsewhere in this file, which requires a lot of validation and might be a seperate PR.

@Jiminator
Jiminator marked this pull request as ready for review September 23, 2026 05:14
@Jiminator

Copy link
Copy Markdown
Collaborator Author

/tag-and-rerun-ci

@github-actions github-actions Bot added the run-ci CI: run the baseline test suite on this PR label Sep 23, 2026
@Jiminator Jiminator added highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) labels Sep 24, 2026
@Fridge003
Fridge003 merged commit be495a6 into sgl-project:main Sep 24, 2026
304 of 344 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bypass-fail-fast CI: a failing job no longer aborts its siblings (lint still gates) highest-priority CI: all three control labels, plus never batch-cancelled or stale-closed run-ci CI: run the baseline test suite on this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants