-
-
Notifications
You must be signed in to change notification settings - Fork 11.7k
[Misc] Refactor Attention kv transfer methods into decorator #27816
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
NickLucche
merged 7 commits into
vllm-project:main
from
NickLucche:refactor-wait-kv-load
Nov 12, 2025
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
31a2ee3
refactor kv transfer methods into decorator
NickLucche 65d7c91
missed one
NickLucche ad51dd4
fetch layer_name index in args
NickLucche 02f6277
[Attention] Create get_attention_context() helper
markmc e9aab4c
review
NickLucche 7ba7765
fix rebase cruft
NickLucche 839a936
peace treaty with docs builder
NickLucche File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
| import inspect | ||
| from collections.abc import Callable | ||
| from functools import wraps | ||
|
|
||
| from vllm.distributed.kv_transfer import ( | ||
| get_kv_transfer_group, | ||
| has_kv_transfer_group, | ||
| is_v1_kv_transfer_group, | ||
| ) | ||
|
|
||
|
|
||
| def maybe_transfer_kv_layer(func: Callable) -> Callable: | ||
| """Decorator that handles KV layer transfer prior and after execution of | ||
| an attention layer, if enabled. Otherwise, the wrapper is a no-op. | ||
|
|
||
| On entry: waits for the KV layer from the connector. | ||
| On exit: saves the KV layer to the connector. | ||
| """ | ||
| # Import at runtime to avoid circular dependency | ||
| from vllm.attention.layer import get_attention_context | ||
|
|
||
| # Inspect the signature ONCE when the decorator is applied. | ||
| sig = inspect.signature(func) | ||
| param_names = list(sig.parameters.keys()) | ||
|
|
||
| # Find the index of 'layer_name' parameter. | ||
| try: | ||
| layer_name_index = param_names.index("layer_name") | ||
| except ValueError as e: | ||
| raise TypeError( | ||
| f"Function {func.__name__} must have a 'layer_name' parameter" | ||
| ) from e | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs): | ||
| if not has_kv_transfer_group() or not is_v1_kv_transfer_group(): | ||
| return func(*args, **kwargs) | ||
|
|
||
| layer_name: str = args[layer_name_index] | ||
|
|
||
| # Extract attention context (layer-specific metadata, layer, and kv_cache) | ||
| attn_metadata, attn_layer, kv_cache = get_attention_context(layer_name) | ||
| connector = get_kv_transfer_group() | ||
| if attn_metadata is None or not connector.has_connector_metadata(): | ||
| return func(*args, **kwargs) | ||
|
|
||
| # Wait for KV layer on entry | ||
| connector.wait_for_layer_load(layer_name) | ||
|
|
||
| # Execute the function | ||
| result = func(*args, **kwargs) | ||
|
|
||
| # Save KV cache layer on exit | ||
| connector.save_kv_layer(layer_name, kv_cache, attn_metadata) | ||
|
|
||
| return result | ||
|
|
||
| return wrapper |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.