Transformers: FlashAttention2 has been toggled on, but … flash_attn seems to be not installed (FlashAttention missing)
The script wants to load the model with FlashAttention 2, but the Python package flash-attn is not installed in your environment.
The message
ImportError: FlashAttention2 has been toggled on, but it cannot be used due to the following error: the package flash_attn seems to be not installed. Please refer to the documentation of https://huggingface.co/docs/transformers/perf_infer_gpu_one#flashattention-2 to install Flash Attention 2.
Affects: Python
Why is this happening?
With attn_implementation="flash_attention_2", the code requires a fast attention calculation that comes from the extra package flash-attn. This package has to be built to match CUDA and PyTorch, is mainly intended for Linux, and only runs on newer NVIDIA cards (Ampere, Ada, Hopper, so around an RTX 3090 or 4090). On Windows, the developers say compilation is still not very well tested.
How to fix it
- The easiest path: use the version built into PyTorch. Change
attn_implementation="flash_attention_2"in the code toattn_implementation="sdpa"or choose this option in the program. The model will run normally with that. - Update Transformers with the kernels add-on in your active venv:
pip install -U "transformers[kernels]". Newer versions automatically fall back to a ready-made FlashAttention kernel from the Hugging Face Hub whenflash_attention_2is missing. - You can also request the Hub kernel directly:
attn_implementation="kernels-community/flash-attn2". That way you do not have to installflash-attnyourself at all. - Check your graphics card: FlashAttention 2 supports Ampere, Ada, and Hopper (for example RTX 3090, RTX 4090). Older cards like the RTX 20 series or T4 are not supported by the package; use
sdpathere. - If you want the real package, install it on Linux or in WSL: requirements are CUDA 12.0 or later, PyTorch 2.2 or later, and
ninja(check withninja --version). Thenpip install flash-attn --no-build-isolation. If you have little RAM, useMAX_JOBS=4 pip install flash-attn --no-build-isolation.
Better not: Do not just start building flash-attn on Windows and hope for the best: without ninja, compilation can take around two hours, and sdpa works right away.
Terms for this
Similar errors
- pip: error: externally-managed-environment (Linux/WSL)
- A module that was compiled using NumPy 1.x cannot be run in NumPy 2.x – a package does not match the installed NumPy version
- A1111: INCOMPATIBLE PYTHON VERSION / Couldn't install torch
- ImportError: DLL load failed while importing …: The specified module could not be found – a Python package is missing a Windows DLL
- No matching distribution found for torch (wrong Python version)
Sources
- https://huggingface.co/docs/transformers/attention_interface
- https://huggingface.co/docs/transformers/kernel_doc/loading_kernels
- https://github.com/Dao-AILab/flash-attention
Checked on Oct 27, 2026. Didn't help? Ask in the forum and name the graphics card, program, and the full message.