feat(attention): add causal + bidirectional-ranges batch prefill #5189
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 555 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
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.378
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #5189"
general
weight: 0.35
lesj0610
<!-- .github/pull_request_template.md -->
📌 Description
FA2 supports custom attention variants, but there is currently no packaged batch-prefill path for sequences that are causal except for spans that attend in both directions. Callers either have to materialize a dense custom mask or use a different backend.
The dense mask is
O(qo_len * kv_len). For example, 16384 scheduled query tokens against a 262144-token context require 4 GiB just for a boolean mask.This PR adds an FA2 paged-prefill wrapper that computes the mask directly from a compact
int32 [total_q, 2]range tensor:Each query row contains an inclusive
[start, end]span in request-local KV positions.(-1, -1)means that the query has no bidirectional span and remains purely causal. At 16384 query tokens, this tensor is 128 KiB regardless of context length.The implementation uses the existing
variant_owns_maskpath inBatchPrefillWithPagedKVCacheWrapper; it does not change the default prefill dispatch.The public API added here is:
| Symbol | Exported from |
|---|---|
|
BatchPrefillWithCausalBidirectionalRangesWrapper|flashinfer,flashinfer.attention||
gen_batch_prefill_bidirectional_ranges_module|flashinfer.jit.attention|The wrapper and generator share one internal spec for the URI and JIT tensor and scalar arguments.
Two kernel details are worth calling out:
window_left = kv_lento keep FA2's normal sliding-window tile pruning from discarding keys that may still belong to a bidirectional range. The causal and range windows are applied inside the variant instead.CTA_TILE_Qlanes are clamped to the last real query row before indexing the range tensor.The wrapper is FA2-only because this path relies on a custom
LogitsMaskbeing evaluated for every KV tile. Its JIT specialization is fixed in the constructor;plan()andworkspace_size()inherit omitted dtype values from that specialization and reject explicit mismatches. Options that conflict with the variant-owned mask are rejected rather than silently ignored.Packed NVFP4 KV caches use the existing
maybe_k_cache_sfandmaybe_v_cache_sfJIT tensor conventions.🔍 Related Issues
None.
🚀 Pull Request Checklist
✅ Pre-commit Checks
pre-commitby runningpip install pre-commit(or used your preferred method).pre-commit install.pre-commit run --all-filesand fixed any reported issues.🧪 Tests
unittest, etc.).The targeted tests cover:
Pre-commit hooks for the changed files also pass.
Reviewer Notes
This is currently JIT-only. Unlike the existing attention-sink AOT entry, it does not have a small fixed set of dtype and head-dimension combinations to precompile.
Testing was limited to SM80. I did not run this on SM90 or SM100, and the FP8 paths were not exercised.
This wrapper is separate from
prefix_len_ptr, which selectsMaskMode.MULTIITEMSCORING; the two mask modes cannot be combined.<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
- Added bidirectional range attention for batch prefill, combining causal masking with per-query key spans.
- Exposed the new attention wrapper through the public API.
- Added support for NVFP4 key-value caches and log-sum-exp results.
- Added validation for range inputs, attention settings, data types, and configuration compatibility.
- Added tensor-valued query, key, and value scaling support for paged prefill.
- Documented causal and bidirectional ranges prefill functionality.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->