Skip to content

Commit 6dd0eb1

Browse files
committed
Dispatch via keras.ops.set_attention_override instead of per-layer wrapping
1 parent 429c36e commit 6dd0eb1

6 files changed

Lines changed: 250 additions & 486 deletions

File tree

keras_hub/src/vllm/attention.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,11 @@ def vllm_paged_attention(
117117
cache_index = ctx.layer_index
118118
ctx.layer_index = cache_index + 1
119119
cache = ctx.kv_caches[cache_index]
120+
# Sliding windows can't ride through the attention op's arguments,
121+
# so the adapter probes them per layer at load time and they're
122+
# applied here by layer index.
123+
if sliding_window is None and ctx.per_layer_sliding is not None:
124+
sliding_window = ctx.per_layer_sliding[cache_index]
120125

121126
# vLLM convention: one flattened token dim, channels = heads*head_dim.
122127
query = ops.reshape(query, (-1, num_heads * head_dim))

keras_hub/src/vllm/context.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ class VLLMContext(threading.local):
2626
by `set_vllm_context` at the start of every forward step.
2727
num_kv_heads: The model's true KV head count (from the model config),
2828
used by the bridge to undo GQA head expansion generically.
29+
per_layer_sliding: Optional per-layer sliding-window sizes, aligned
30+
with `kv_caches` order. Sliding windows can't be expressed
31+
through the attention op's arguments (its mask only covers the
32+
new tokens, not the paged history), so the adapter probes them
33+
once at load time and the bridge applies them by layer index.
2934
active: Boolean indicating if the context is currently active.
3035
"""
3136

@@ -42,6 +47,7 @@ def __init__(self) -> None:
4247
self.updated_kv_caches: Optional[list] = None
4348
self.layer_index: int = 0
4449
self.num_kv_heads: Optional[int] = None
50+
self.per_layer_sliding: Optional[list] = None
4551
self.active: bool = False
4652

4753

@@ -57,6 +63,7 @@ def set_vllm_context(
5763
positions: Optional[Any] = None,
5864
kv_caches: Optional[list] = None,
5965
num_kv_heads: Optional[int] = None,
66+
per_layer_sliding: Optional[list] = None,
6067
) -> None:
6168
"""Sets the thread-local vLLM context parameters.
6269
@@ -72,6 +79,7 @@ def set_vllm_context(
7279
kv_caches: Per-layer paged KV caches, in transformer-layer order.
7380
num_kv_heads: The model's true KV head count, for generic GQA
7481
un-expansion in the bridge.
82+
per_layer_sliding: Per-layer sliding-window sizes, in layer order.
7583
"""
7684
_vllm_context.block_tables = block_tables
7785
_vllm_context.slot_mapping = slot_mapping
@@ -85,6 +93,7 @@ def set_vllm_context(
8593
)
8694
_vllm_context.layer_index = 0
8795
_vllm_context.num_kv_heads = num_kv_heads
96+
_vllm_context.per_layer_sliding = per_layer_sliding
8897
_vllm_context.active = True
8998

9099

@@ -100,6 +109,7 @@ def clear_vllm_context() -> None:
100109
_vllm_context.updated_kv_caches = None
101110
_vllm_context.layer_index = 0
102111
_vllm_context.num_kv_heads = None
112+
_vllm_context.per_layer_sliding = None
103113
_vllm_context.active = False
104114

105115

0 commit comments

Comments
 (0)