Skip to content
31 changes: 23 additions & 8 deletions vllm/attention/layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
SlidingWindowSpec,
)

if current_platform.is_rocm():
from vllm.platforms.rocm import on_gfx9
else:
on_gfx9 = lambda *args, **kwargs: False


FP8_DTYPE = current_platform.fp8_dtype()
logger = init_logger(__name__)
USE_XFORMERS_OPS = None
Expand Down Expand Up @@ -93,14 +99,22 @@ def check_upstream_fa_availability(dtype: torch.dtype):

def maybe_get_vit_flash_attn_backend(
attn_backend: _Backend, use_upstream_fa: bool
) -> tuple[_Backend, Callable]:
if (
attn_backend != _Backend.FLASH_ATTN
and attn_backend != _Backend.ROCM_AITER_FA
and check_upstream_fa_availability(torch.get_default_dtype())
):
attn_backend = _Backend.FLASH_ATTN
use_upstream_fa = True
) -> tuple[_Backend, Callable | None]:
if current_platform.is_rocm():
if envs.VLLM_ROCM_USE_AITER and envs.VLLM_ROCM_USE_AITER_MHA and on_gfx9():
Comment thread
JartX marked this conversation as resolved.
attn_backend = _Backend.ROCM_AITER_FA
elif on_gfx9():

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This condition was crafted for ROCm platform (on_gfx9()).

        if (
            attn_backend != _Backend.FLASH_ATTN
            and attn_backend != _Backend.ROCM_AITER_FA
            and check_upstream_fa_availability(torch.get_default_dtype())
        ):

On on_gfx9(), we will always attempt to use flash_attn, but if it does not support, then we fallback to _Backend.TORCH_SDPA

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tjtanaa The first step was simply to check if it was rocm and if it was on_gfx1x and if so, return _Backend.TORCH_SDPA, None directly. Would you like to leave it like this with a comment indicating why? I've only seen it affect RDNA3 with the upstream flow. I think this way we could mark the required changes as resolved. If not, please tell me how the code should be :)

attn_backend = _Backend.FLASH_ATTN
else:
return _Backend.TORCH_SDPA, None
else:
if (

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned above, this condition and its content is also applicable to ROCm on gfx9.

attn_backend != _Backend.FLASH_ATTN
and attn_backend != _Backend.ROCM_AITER_FA
and check_upstream_fa_availability(torch.get_default_dtype())
):
attn_backend = _Backend.FLASH_ATTN
use_upstream_fa = True
Comment thread
JartX marked this conversation as resolved.

if current_platform.is_rocm() and attn_backend == _Backend.FLASH_ATTN:
use_upstream_fa = True
Expand Down Expand Up @@ -534,6 +548,7 @@ def forward(
value = torch.repeat_interleave(value, num_repeat, dim=2)

if self.is_flash_attn_backend:
assert self._flash_attn_varlen_func is not None
cu_seqlens_q = torch.arange(
0, (bsz + 1) * q_len, step=q_len, dtype=torch.int32, device=query.device
)
Expand Down