Skip to content

Commit e883075

Browse files
committed
fix rope kv_cache for RocmAiterUnifiedAttentionImpl
1 parent 03d26d7 commit e883075

2 files changed

Lines changed: 62 additions & 17 deletions

File tree

vllm/model_executor/models/gpt_oss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ def forward(
137137
qkv, _ = self.qkv_proj(hidden_states)
138138
q, k, v = qkv.split([self.q_size, self.kv_size, self.kv_size], dim=-1)
139139
# q, k = self.rotary_emb(positions, q, k)
140-
v = v.contiguous()
140+
# v = v.contiguous()
141141
attn_output = self.attn(q, k, v, positions=positions)
142142
output, _ = self.o_proj(attn_output)
143143
return output

vllm/v1/attention/backends/rocm_aiter_unified_attn.py

Lines changed: 61 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from vllm import _custom_ops as ops
88
from vllm.attention.backends.abstract import AttentionType
99
from vllm.logger import init_logger
10+
from vllm.platforms import current_platform
1011
from vllm.model_executor.layers.quantization.utils.quant_utils import (
1112
QuantKey,
1213
kFp8StaticTensorSym,
@@ -20,6 +21,10 @@
2021

2122
logger = init_logger(__name__)
2223

24+
if current_platform.is_rocm():
25+
from vllm._aiter_ops import rocm_aiter_ops
26+
if rocm_aiter_ops.is_enabled():
27+
from aiter.ops.triton.fused_kv_cache import fused_qk_rope_reshape_and_cache
2328

2429
class RocmAiterUnifiedAttentionBackend(RocmAttentionBackend):
2530
accept_output_buffer: bool = True
@@ -102,6 +107,7 @@ def forward(
102107
output: torch.Tensor | None = None,
103108
output_scale: torch.Tensor | None = None,
104109
output_block_scale: torch.Tensor | None = None,
110+
positions: torch.Tensor | None = None,
105111
) -> torch.Tensor:
106112
"""Forward pass with FlashAttention.
107113
@@ -145,23 +151,62 @@ def forward(
145151
# key and value may be None in the case of cross attention. They are
146152
# calculated once based on the output from the encoder and then cached
147153
# in KV cache.
148-
if (
149-
self.kv_sharing_target_layer_name is None
150-
and key is not None
151-
and value is not None
152-
):
153-
# Reshape the input keys and values and store them in the cache.
154-
# Skip this if sharing KV cache with an earlier attention layer.
155-
ops.reshape_and_cache_flash(
156-
key,
157-
value,
158-
key_cache,
159-
value_cache,
160-
attn_metadata.slot_mapping,
161-
self.kv_cache_dtype,
162-
layer._k_scale,
163-
layer._v_scale,
154+
155+
if positions is not None and query.shape[0] <= 256 and rocm_aiter_ops.is_enabled():
156+
assert self.kv_sharing_target_layer_name is None
157+
cos_sin_cache = self.rotary_emb.cos_sin_cache
158+
is_neox = self.rotary_emb.is_neox_style
159+
cos, sin = cos_sin_cache.chunk(2, dim=-1)
160+
is_fp8_kv_cache = self.kv_cache_dtype.startswith("fp8")
161+
if is_fp8_kv_cache:
162+
key_cache = key_cache.view(current_platform.fp8_dtype())
163+
value_cache = value_cache.view(current_platform.fp8_dtype())
164+
query, key, key_cache, value_cache, output = (
165+
fused_qk_rope_reshape_and_cache(
166+
query,
167+
key,
168+
value,
169+
key_cache,
170+
value_cache,
171+
attn_metadata.slot_mapping,
172+
positions,
173+
cos,
174+
sin,
175+
layer._k_scale,
176+
layer._v_scale,
177+
is_neox,
178+
flash_layout=True,
179+
apply_scale=is_fp8_kv_cache,
180+
offs=None,
181+
q_out=query,
182+
k_out=key,
183+
output_zeros=True,
184+
zeros_out=output,
185+
)
164186
)
187+
else:
188+
if positions is not None:
189+
if current_platform.is_rocm():
190+
query, key = self.rotary_emb.forward_cuda(positions, query, key)
191+
else:
192+
query, key = self.rotary_emb(positions, query, key)
193+
if (
194+
self.kv_sharing_target_layer_name is None
195+
and key is not None
196+
and value is not None
197+
):
198+
# Reshape the input keys and values and store them in the cache.
199+
# Skip this if sharing KV cache with an earlier attention layer.
200+
ops.reshape_and_cache_flash(
201+
key,
202+
value,
203+
key_cache,
204+
value_cache,
205+
attn_metadata.slot_mapping,
206+
self.kv_cache_dtype,
207+
layer._k_scale,
208+
layer._v_scale,
209+
)
165210

166211
if self.kv_cache_dtype.startswith("fp8"):
167212
key_cache = key_cache.view(self.fp8_dtype)

0 commit comments

Comments
 (0)