Skip to content
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

[Test] Add xformer and flash attn tests #3961

Merged
merged 4 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions tests/basic_correctness/test_basic_correctness.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@pytest.mark.parametrize("dtype", ["half"])
@pytest.mark.parametrize("max_tokens", [5])
@pytest.mark.parametrize("enforce_eager", [False, True])
@pytest.mark.parametrize("attn_backend", ["XFORMER", "FLASH"])
def test_models(
hf_runner,
vllm_runner,
Expand All @@ -22,7 +23,10 @@ def test_models(
dtype: str,
max_tokens: int,
enforce_eager: bool,
attn_backend: str,
monkeypatch,
) -> None:
monkeypatch.setenv("VLLM_ATTENTION_BACKEND", attn_backend)
rkooo567 marked this conversation as resolved.
Show resolved Hide resolved
hf_model = hf_runner(model, dtype=dtype)
hf_outputs = hf_model.generate_greedy(example_prompts, max_tokens)
del hf_model
Expand Down
16 changes: 15 additions & 1 deletion vllm/attention/selector.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import enum
import os
from functools import lru_cache
from typing import Type

Expand All @@ -10,6 +11,8 @@

logger = init_logger(__name__)

VLLM_ATTENTION_BACKEND = "VLLM_ATTENTION_BACKEND"


class _Backend(enum.Enum):
rkooo567 marked this conversation as resolved.
Show resolved Hide resolved
FLASH_ATTN = enum.auto()
Expand Down Expand Up @@ -75,4 +78,15 @@ def _which_attn_to_use(dtype: torch.dtype) -> _Backend:
"Cannot use FlashAttention backend because the flash_attn package "
"is not found. Please install it for better performance.")
return _Backend.XFORMERS
return _Backend.FLASH_ATTN

backend_by_env_var = os.getenv(VLLM_ATTENTION_BACKEND)
if backend_by_env_var == "XFORMER":
return _Backend.XFORMERS
elif backend_by_env_var == "FLASH":
return _Backend.FLASH_ATTN
elif backend_by_env_var is None:
# Default case.
simon-mo marked this conversation as resolved.
Show resolved Hide resolved
return _Backend.FLASH_ATTN
else:
raise AssertionError(
f"{VLLM_ATTENTION_BACKEND}={backend_by_env_var} is not supported.")
Loading