feat(topk): add PreparedTopKRaggedTransform, a ragged top-k with its backend and buffers fixed up front #5860
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
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.388
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #5860"
general
weight: 0.35
lesj0610
<!-- .github/pull_request_template.md -->
📌 Description
top_k_ragged_transformchooses a backend on every call and keeps the scratch that backend needs in a device-wide cache. That suits one call at a time. It does not suit a caller that holds several transforms at once, which then share one cached buffer, or one that runs under a CUDA graph, where an allocation made during capture stays in the graph's private pool.PreparedTopKRaggedTransformfixes the backend when it is built and then runs into buffers the caller owns:The backend comes from
resolve_ragged_transform_backend(), whichtop_k_ragged_transformnow calls too, so the two entry points cannot pick differently for the same shape.workspace_size()answers from the prepared shape without allocating; for CUB it goes through a descriptor form of the existing size query, which takes the same CUB path as the run, so no score tensor is needed. The clusters backend allocates its own output, so a transform that would land on it is refused when it is built.run()checks the shape, dtype, device and layout of everything it is given and refuses a workspace that is too small or not 16-byte aligned. The workspace has to be zeroed once when it is allocated: the radix backend reads its counters before writing them on the first round and leaves them reusable afterwards.🔍 Related Issues
Part of the QSA work 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
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 tests are in
tests/utils/test_topk.pyand run on both the radix and the CUB backend. The prepared transform matchestop_k_ragged_transformon the same scores, offsets and lengths, with and withoutrow_starts, including rows shorter thankand akwider than the rows, and on CUB its descriptor size query returns what the tensor one does; a run after the first allocates nothing, not even a temporary, and a captured run replays to the same result on new scores; and inputs it was not prepared for are refused, as is the clusters backend.On sm_80 all of them pass, and so do the existing ragged-transform tests (306 passed, the rest skipped by their own architecture checks) and the trace consistency tests for the new template. Nothing here 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. -->
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 frombackend="auto"withoutFLASHINFER_ALLOW_EXPERIMENTAL_AUTO_BACKENDS=1. (Calling an@flashinfer_experimental_apior 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-teststag — the experimental-trackwatcher 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.
<!-- This is an auto-generated comment: release notes by coderabbit.ai -->
Summary by CodeRabbit
New Features
Added a prepared Top-K workflow for ragged inputs. Specify the input shape and options once, then reuse the operation with caller-provided output and workspace.
Prepared runs check input and workspace compatibility and support repeated execution, including CUDA graph replay.
Added a workspace-size query based on shape, data type, device, and Top-K settings.
Documentation
Added the prepared ragged Top-K workflow to the Top-K API reference.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->