Skip to content

[M8 1/3] Extract attention contracts and add automatic backend selection - #46

Merged
bjf-frz merged 3 commits into
ThinkFlowLab:mainfrom
Levius-Fubuki:codex/m8-attention
Oct 8, 2026
Merged

bjf-frz merged 3 commits into
ThinkFlowLab:mainfrom
Levius-Fubuki:codex/m8-attention

Conversation

@Levius-Fubuki

@Levius-Fubuki Levius-Fubuki commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

Backend dispatch and synchronous attention metadata currently live inside KVCacheManager, and the first M8 increment inferred packed-prefill support from the FlashAttention generation. This PR extracts those contracts into vllm_rlt/attention and adds an opt-in auto backend selection mode for local inference and serving.

Auto tries compatible FA4, FA3, FA2, Triton, then Torch. It checks hardware, dtype, head dimension, page size and actual implementation imports. For example, SM 8.9 with 16-token pages skips FA2's 256-token page requirement and selects Triton if installed. Explicit choices keep their validation/import failures. Selection diagnostics include the requested backend, selected implementation and reason in the startup log and attention_info.

Each kernel adapter declares its packed-prefill, CUDA Graph and GPU async capabilities. The engine selects a compatible adapter before memory profiling and passes that adapter to KVCacheManager. Execution requirements filter auto candidates, so graph/async/UVA configurations fail at startup if no compatible backend exists.

The host metadata planner owns page-table padding, causal context lengths and packed query grouping. It integrates with the current multi-depth preparation path: depth-independent views remain shared, and metadata is staged in two pinned host tensors with nonblocking copies. M4 retains allocation/depth/prefix validity; M5 retains static, async, UVA and graph buffer/stream lifetimes. The speculative runner routes packed prefill through declared capabilities. CLI changes are documented in the Command-line inference section of docs/launching.md; this PR has no main README changes. The new benchmark scripts and walkthrough have been removed.

Validation of the current revision on local CPU:

  • Full suite: 455 passed, 141 skipped.
  • Ruff lint, formatting and diff checks passed.
  • CPU toy CLI with --attention-backend auto generated outputs and reported Torch selection.
  • Tests simulate hardware/package boundaries for FA2/3/4, Triton, ROCm, configuration constraints, missing/broken imports, explicit failures and graph/async/packed-prefill requirements. CPU auto and explicit Torch produce identical token/depth results and release KV resources.

Additional regressions cover host-only metadata planning, two staging calls across multiple depth assignments, shared metadata views and capability-based speculative routing. GPU execution, kernel numerics and performance have not been revalidated on this revision.

Integrated upstream main: 24c433bc2ea3b0fd5b9f266571300a9386caa5eb. Refs #32.

BuckMulligan99 added a commit to BuckMulligan99/vllm-rlt that referenced this pull request Sep 24, 2026
…on views

KVCacheManager combines logical allocation, device storage, prepared-batch
metadata and attention dispatch, and the scheduler, preemption manager, PD
worker and model runner reached into its private state: _refs to price
prefix hits, _get_allocation for held pages, leases and block tables, raw
key_cache page copies for preemption, and duplicated written-state
bookkeeping for the asynchronous finalize kernel.

Add docs/kv_cache_walkthrough.md with the pinned baseline, caller
inventory, allocation lifecycle, state ownership, function contracts,
call chains, findings, the migration plan aligned with ThinkFlowLab#46 and ThinkFlowLab#50, and
the M3 refactoring-category checklist applied to M4. Add allocation views
(has_allocation, allocated_blocks, plane_block_tables, has_transfer_lease,
exclusive_prefix_blocks), a written-state contract (token_written,
mark_finalized) shared with finalize_token, and KVSnapshot with
snapshot/restore for preemption. Migrate the M3 and M9 callers and
ModelRunner.finalize_many to them.

One explicit behavioral fix (F6): _claim evicted prefix-cache entries
before discovering that the claim still could not be satisfied, so a
failed growth, resumption or PD reservation discarded reusable prefixes.
It now refuses before evicting when num_free_blocks is insufficient;
successful claims are unchanged. Covered by a regression test that fails
on the old code.

Validation is stricter in restore, snapshot and the finalize
precondition, none reachable through existing callers. The remaining
private callers are the M5 prepared-batch builders, the model and the
speculative runner, deferred to the next increment.

Refs ThinkFlowLab#32.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: BuckMulligan99 <65453204+BuckMulligan99@users.noreply.github.com>
BuckMulligan99 added a commit to BuckMulligan99/vllm-rlt that referenced this pull request Sep 24, 2026
…on views

KVCacheManager combines logical allocation, device storage, prepared-batch
metadata and attention dispatch, and the scheduler, preemption manager, PD
worker and model runner reached into its private state: _refs to price
prefix hits, _get_allocation for held pages, leases and block tables, raw
key_cache page copies for preemption, and duplicated written-state
bookkeeping for the asynchronous finalize kernel.

Add docs/kv_cache_walkthrough.md with the pinned baseline, caller
inventory, allocation lifecycle, state ownership, function contracts,
call chains, findings, the migration plan aligned with ThinkFlowLab#46 and ThinkFlowLab#50, and
the M3 refactoring-category checklist applied to M4. Add allocation views
(has_allocation, allocated_blocks, plane_block_tables, has_transfer_lease,
exclusive_prefix_blocks), a written-state contract (token_written,
mark_finalized) shared with finalize_token, and KVSnapshot with
snapshot/restore for preemption. Migrate the M3 and M9 callers and
ModelRunner.finalize_many to them.

One explicit behavioral fix (F6): _claim evicted prefix-cache entries
before discovering that the claim still could not be satisfied, so a
failed growth, resumption or PD reservation discarded reusable prefixes.
It now refuses before evicting when num_free_blocks is insufficient;
successful claims are unchanged. Covered by a regression test that fails
on the old code.

Validation is stricter in restore, snapshot and the finalize
precondition, none reachable through existing callers. The remaining
private callers are the M5 prepared-batch builders, the model and the
speculative runner, deferred to the next increment.

Refs ThinkFlowLab#32.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Signed-off-by: BuckMulligan99 <65453204+BuckMulligan99@users.noreply.github.com>
@bjf-frz

bjf-frz commented Sep 30, 2026

Copy link
Copy Markdown
Collaborator

Please remove benchmarks dir and walkthrough.md

Comment thread vllm_rlt/attention/backend.py Outdated

def create_backend(
backend: str, device: torch.device, dtype: torch.dtype, head_dim: int, block_size: int
):

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.

Could we add an auto selection mode here that considers the current hardware, installed backend packages, and configuration constraints such as dtype, head dimension, and block size? Explicit backend selection should remain supported and fail clearly when incompatible. Please also report the selected backend and the selection reason.

Comment thread vllm_rlt/attention/backend.py Outdated


def backend_capabilities(implementation) -> BackendCapabilities:
return BackendCapabilities(packed_prefill=getattr(implementation, "generation", None) == 4)

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.

Could each backend or its adapter explicitly declare its capabilities instead of inferring packed_prefill from generation == 4? This still couples the capability interface to FlashAttention versioning and does not accommodate other implementations that support packed prefill. Callers should route through the declared capabilities.

@Levius-Fubuki Levius-Fubuki changed the title [M8 1/3] Extract attention backend and metadata contracts [M8 1/3] Extract attention contracts and add automatic backend selection Sep 30, 2026
@Levius-Fubuki

Copy link
Copy Markdown
Contributor Author

@bjf-frz cc,thanks

@bjf-frz

bjf-frz commented Oct 8, 2026

Copy link
Copy Markdown
Collaborator
  1. Please resolve the merge conflicts.
  2. No changes to the main README are needed. Please document the CLI changes in the relevant CLI documentation section.
  3. others lgtm

@Levius-Fubuki

Copy link
Copy Markdown
Contributor Author

Resolved the merge conflicts and moved the CLI documentation to docs/launching.md, with no README changes. Local CPU validation: 455 passed, 141 skipped; Ruff and formatting checks passed.

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.

2 participants