| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | |
| 4 | import importlib.machinery |
| 5 | import os |
| 6 | import sys |
| 7 | import types |
| 8 | |
| 9 | # In symlink mode (VLLM_FLASH_ATTN_SRC_DIR), cute/ is a symlink to the real |
| 10 | # source tree and its files use `flash_attn.cute.*` imports (not rewritten). |
| 11 | # Register a virtual `flash_attn` package so those imports resolve. |
| 12 | _cute_dir = os.path.join(os.path.dirname(__file__), "cute") |
| 13 | if os.path.islink(_cute_dir) and "flash_attn" not in sys.modules: |
| 14 | _fa_mod = types.ModuleType("flash_attn") |
| 15 | _fa_mod.__path__ = [os.path.dirname(os.path.realpath(_cute_dir))] |
| 16 | _fa_mod.__package__ = "flash_attn" |
| 17 | _fa_mod.__spec__ = importlib.machinery.ModuleSpec( |
| 18 | "flash_attn", None, is_package=True |
| 19 | ) |
| 20 | _fa_mod.__spec__.submodule_search_locations = _fa_mod.__path__ |
| 21 | sys.modules["flash_attn"] = _fa_mod |
| 22 | |
| 23 | from vllm.vllm_flash_attn.flash_attn_interface import ( # noqa: E402 |
| 24 | FA2_AVAILABLE, |
| 25 | FA3_AVAILABLE, |
| 26 | compile_flash_attn_varlen_func_from_specs, |
| 27 | fa_version_unsupported_reason, |
| 28 | flash_attn_varlen_func, |
| 29 | get_scheduler_metadata, |
| 30 | is_fa_version_supported, |
| 31 | ) |
| 32 | |
| 33 | if not (FA2_AVAILABLE or FA3_AVAILABLE): |
| 34 | raise ImportError( |
| 35 | "vllm.vllm_flash_attn requires the CUDA flash attention extensions " |
| 36 | "(_vllm_fa2_C or _vllm_fa3_C). On ROCm, use upstream flash_attn." |
| 37 | ) |
| 38 | |
| 39 | __all__ = [ |
| 40 | "compile_flash_attn_varlen_func_from_specs", |
| 41 | "fa_version_unsupported_reason", |
| 42 | "flash_attn_varlen_func", |
| 43 | "get_scheduler_metadata", |
| 44 | "is_fa_version_supported", |
| 45 | ] |