AI and LLM integration · Troubleshooting walkthrough
flash-attn will not install with pip
Short answer
pip install flash-attn breaks when setup.py cannot import torch, when no prebuilt wheel matches your setup, or when the source build finds no usable nvcc. flash-attn 2.8.3.post2 ships wheels only for Linux x86_64, CUDA 12 builds of torch 2.4 to 2.8, and Python 3.9 to 3.13. Install torch first, then run pip install flash-attn --no-build-isolation.
pip install flash-attn fails for three different reasons that look alike at first. Its setup.py imports torch before pip has installed anything, no ready-made wheel matches your exact Python, torch and CUDA combination so pip compiles from source, and the compile needs a working nvcc and plenty of memory. The log tells you which stage you are in.
The project's README recommends pip install flash-attn --no-build-isolation and says it needs Linux, CUDA 12.0 or newer, PyTorch 2.2 or newer, and a working ninja. FlashAttention-2 targets Ampere, Ada and Hopper GPUs. Check that your card is one of these before spending an afternoon on the build.
The text you see
ModuleNotFoundError: No module named 'torch'Precompiled wheel not found. Building from source...flash_attn was requested, but nvcc was not found. Are you sure your environment has nvcc available?RuntimeError: FlashAttention is only supported on CUDA 11.7 and above. Note: make sure nvcc has a supported version by running nvcc -V.
Stage 1
Collect the facts the installer uses
setup.py builds a wheel file name from your Python tag, the torch major and minor version, the CUDA version torch was built with, a C++ ABI flag and the platform, then looks for that name among the project's release files. Print the same values, plus the compiler version.
Python, platform, torch, CUDA and nvcc
python -c "import sys, platform, torch; print(sys.version_info[:2], platform.machine(), torch.__version__, torch.version.cuda, torch._C._GLIBCXX_USE_CXX11_ABI)"
nvcc -VIf the first command raises No module named 'torch', go to the next section. Otherwise read the log of the failed install for Precompiled wheel not found.
Stage 2
No module named 'torch' during the build
By default pip builds a package in a throw-away environment that contains only the declared build requirements. flash-attn imports torch at the top of setup.py, and your installed torch is invisible in that environment. --no-build-isolation tells pip to build in your own environment, which also means you must have the helper packages there yourself.
Install helpers first, then turn isolation off
python -m pip install packaging psutil ninja wheel
python -m pip install flash-attn --no-build-isolationStage 3
No wheel for your combination
The release page's files are named like flash_attn-2.8.3.post2+cu12torch2.4cxx11abiFALSE-cp310-cp310-linux_x86_64.whl: CUDA 12, torch 2.4, ABI flag, Python 3.10, Linux x86_64. A setup outside the published names, for example a different platform or a newer torch than any wheel lists, falls through to Precompiled wheel not found. Building from source.... Open the release's file list and compare it with the values you printed.
You then have two routes. Match a published wheel by choosing a torch version and Python that appear in the list, or accept a source build and prepare the toolchain.
The two routes
# route A: align torch and Python with a published wheel name, then install
python -m pip install flash-attn --no-build-isolation
# route B: compile locally against a toolkit with the same major version as torch
export CUDA_HOME=/usr/local/cuda-12.8
python -m pip install flash-attn --no-build-isolationStage 4
nvcc missing or too old
A source build stops at nvcc was not found when no compiler is on the path. Install a CUDA toolkit whose major version matches the CUDA torch was built with, and set CUDA_HOME to the folder that has bin/nvcc. An old toolkit gives FlashAttention is only supported on CUDA 11.7 and above; the README asks for 12.0 or newer. Newer GPU families also need newer compilers, so a target the toolkit cannot compile is dropped, and if nothing is left the architecture error appears.
Building inside NVIDIA's PyTorch container avoids most of this, because the toolkit and compiler come with it, and the README recommends it.
Check the compiler, then build for one GPU family
nvcc -V | grep release
export PATH="$CUDA_HOME/bin:$PATH"
FLASH_ATTN_CUDA_ARCHS=90 python -m pip install flash-attn --no-build-isolationStage 5
The build eats the RAM or takes hours
The README warns that on a machine with less than 96 GB of RAM and many cores, ninja may start too many parallel jobs and exhaust memory. Limit them with MAX_JOBS. It also says that without a working ninja compiling can take about two hours, because only one core is used. Check that ninja works by looking at its exit code.
Fix ninja, cap the parallel jobs
ninja --version; echo $?
python -m pip uninstall -y ninja && python -m pip install ninja
MAX_JOBS=4 python -m pip install flash-attn --no-build-isolationStage 6
Confirm it runs on the GPU
python -c "import flash_attn; print(flash_attn.__version__)"prints a version.- A tiny half-precision call on the GPU returns a tensor, proving the compiled kernels load.
- Save the wheel you built if you will repeat the install, so the next machine skips the compile.
A smoke test on the GPU
python -c "import torch; from flash_attn import flash_attn_func; q = torch.randn(1, 8, 2, 64, device='cuda', dtype=torch.float16); print(flash_attn_func(q, q, q).shape)"If you only keep one thing
- Use --no-build-isolation and install torch plus the helper packages first.
- Compare your Python, torch and CUDA with the release's wheel names; if none match, you compile.
- A compile needs nvcc, CUDA 12 or newer, a working ninja and a MAX_JOBS cap on small machines.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.