Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions tests/kernels/attention/test_rocm_triton_attn_dsv4.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ def _launch_sparse_decode_reduce(
part_l: torch.Tensor,
part_acc: torch.Tensor,
adaptive_splits: bool,
positions: torch.Tensor | None = None,
cos_sin_cache: torch.Tensor | None = None,
) -> torch.Tensor:
"""Run the reduce kernel, with the inverse-RoPE epilogue if given positions."""
from vllm.v1.attention.ops import rocm_aiter_mla_sparse as mod

num_queries, num_splits, num_heads = part_m.shape
Expand All @@ -275,20 +278,26 @@ def _launch_sparse_decode_reduce(
part_acc,
attn_sink,
out,
positions,
cos_sin_cache,
out.stride(0),
out.stride(1),
part_m.stride(0),
part_m.stride(1),
part_acc.stride(0),
part_acc.stride(1),
part_acc.stride(2),
cos_sin_cache.stride(0) if cos_sin_cache is not None else 0,
num_heads,
HAS_ATTN_SINK=False,
ADAPTIVE_SPLITS=adaptive_splits,
COMB_DIM=HEAD_DIM,
BLOCK_H=1,
NUM_SPLITS=num_splits,
SPLITS_PAD=1 << (num_splits - 1).bit_length(),
FUSE_INV_ROPE=positions is not None,
NOPE=NOPE_HEAD_DIM,
HALF=ROPE_HEAD_DIM // 2,
num_warps=4,
)
return out
Expand Down Expand Up @@ -1008,6 +1017,46 @@ def test_sparse_attn_decode_gfx950_adaptive_reduce_ignores_stale_scratch() -> No
assert torch.equal(actual, torch.full_like(actual, 2))


@requires_gfx950
@pytest.mark.parametrize("adaptive_splits", [False, True])
@torch.inference_mode()
def test_sparse_attn_decode_reduce_inverse_rope_epilogue(adaptive_splits: bool) -> None:
"""FUSE_INV_ROPE must land where the standalone rotation would.

The epilogue rotates a whole [H, nope + rope] row with one expression by
feeding cos=1/sin=0 to the NoPE lanes, which only holds if the pair split
lands on ``nope_head_dim // 2``; the NoPE lanes therefore have to come
back untouched, not merely close.
"""
from vllm.v1.attention.ops.rocm_aiter_mla_sparse import _fused_inverse_rope_gptj

device = torch.device("cuda")
torch.manual_seed(7)
num_queries, num_splits, num_heads, max_pos = 5, 4, 8, 64
shape = (num_queries, num_splits, num_heads)
part_m = torch.randn(shape, dtype=torch.float32, device=device)
part_l = torch.rand(shape, dtype=torch.float32, device=device) + 0.5
part_acc = torch.randn((*shape, HEAD_DIM), dtype=torch.float32, device=device)
positions = torch.randint(
0, max_pos, (num_queries,), dtype=torch.int64, device=device
)
angle = torch.randn(max_pos, ROPE_HEAD_DIM // 2, device=device)
cos_sin_cache = torch.cat((angle.cos(), angle.sin()), dim=-1).contiguous()

unfused = _launch_sparse_decode_reduce(part_m, part_l, part_acc, adaptive_splits)
expected = _fused_inverse_rope_gptj(
unfused, positions, cos_sin_cache, ROPE_HEAD_DIM
)
actual = _launch_sparse_decode_reduce(
part_m, part_l, part_acc, adaptive_splits, positions, cos_sin_cache
)

torch.testing.assert_close(actual, expected, atol=2e-2, rtol=2e-2)
assert torch.equal(actual[..., :NOPE_HEAD_DIM], unfused[..., :NOPE_HEAD_DIM])
# An epilogue that quietly did nothing would satisfy everything above.
assert not torch.equal(actual[..., NOPE_HEAD_DIM:], unfused[..., NOPE_HEAD_DIM:])


@requires_gfx950
@pytest.mark.parametrize("extra_len", [0, 1, 31, 32, 33, 63, 64, 65])
@torch.inference_mode()
Expand Down
25 changes: 22 additions & 3 deletions vllm/models/deepseek_v41/amd/rocm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from vllm.v1.attention.ops.rocm_aiter_mla_sparse import (
build_ragged_indices_from_dense,
rocm_inv_rope_einsum,
rocm_inverse_rope_rows_,
rocm_sparse_attn_decode,
rocm_sparse_attn_prefill,
)
Expand Down Expand Up @@ -673,6 +674,7 @@ def _o_proj(self, attn_out: torch.Tensor, positions: torch.Tensor) -> torch.Tens
self.n_local_groups,
self.o_lora_rank,
self.wo_a,
inverse_rope=False,
)
zf = z.flatten(1)
if self._wo_b_scale is not None and zf.dim() == 2:
Expand Down Expand Up @@ -747,15 +749,28 @@ def forward_mqa(
attn_metadata=rocm_metadata,
swa_metadata=swa_metadata,
)
rotated = 0
if num_decodes > 0:
self._forward_decode(
rotated = self._forward_decode(
q=q[:num_decode_tokens],
positions=positions[:num_decode_tokens],
kv_cache=self_kv_cache,
swa_metadata=swa_metadata,
attn_metadata=rocm_metadata,
swa_only=swa_only,
output=output[:num_decode_tokens],
)
# Only the decode reduce rotates its own rows, and only the leading
# `rotated` of them; prefill rows and any decode path that did not
# fuse still owe the standalone pass. Settle that here rather than in
# _o_proj: the split is batch-dependent and _o_proj runs compiled,
# where such a value freezes at its trace-time value.
rocm_inverse_rope_rows_(
output[rotated:, : self.n_local_heads, :],
positions[rotated:],
self.rotary_emb.cos_sin_cache,
self.rope_head_dim,
)

def _decode_topk_ragged(
self,
Expand Down Expand Up @@ -796,12 +811,14 @@ def _decode_topk_ragged(
def _forward_decode(
self,
q: torch.Tensor,
positions: torch.Tensor,
kv_cache: torch.Tensor | None,
swa_metadata: DeepseekV4ROCMAiterSparseSWAMetadata,
attn_metadata: DeepseekV4FlashMLAMetadata | None,
swa_only: bool,
output: torch.Tensor,
) -> None:
) -> int:
"""Returns how many leading rows the decode epilogue inverse-RoPE'd."""
num_decodes = swa_metadata.num_decodes
num_decode_tokens = swa_metadata.num_decode_tokens

Expand All @@ -820,7 +837,7 @@ def _forward_decode(
num_decode_tokens=num_decode_tokens,
)

rocm_sparse_attn_decode(
return rocm_sparse_attn_decode(
q=q,
kv_cache=kv_cache,
swa_k_cache=self.swa_cache_layer.kv_cache,
Expand All @@ -839,6 +856,8 @@ def _forward_decode(
nope_head_dim=self.nope_head_dim,
rope_head_dim=self.rope_head_dim,
output=output,
inv_rope_positions=positions,
inv_rope_cos_sin_cache=self.rotary_emb.cos_sin_cache,
extra_cache_nan_free=_trust_dsv4_extra_cache_nan_free(
self.kv_cache_dtype,
self._has_kv_transfer,
Expand Down
Loading
Loading