The project's README from github.com/facebookresearch/xformers. As one markdown file: README.md.
xFormers is:
xFormers is pure Python, and runs on any PyTorch 2.10+. Its attention kernels come from the mslk package.
# Replace cu132 with the CUDA (e.g. cu130) or ROCm (e.g. rocm7.14) version of your PyTorch.
# flash-attn-3 is optional: it speeds up attention on Ampere and Hopper GPUs. It is only
# available with CUDA (on Windows: only with CUDA 12.x!), so leave it out otherwise.
pip install -U xformers mslk flash-attn-3 --index-url https://download.pytorch.org/whl/cu132
Each mslk release targets a specific PyTorch version: for mslk's native kernels to work (some ROCm backends need them), install versions of torch and mslk from the same row of its compatibility table.
# Install mslk from the PyTorch index matching your PyTorch build, as above
pip install mslk --index-url https://download.pytorch.org/whl/cu132
pip install -v -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
Memory-efficient MHA
Setup: A100 on f16, measured total time for a forward+backward pass
Note that this is exact attention, not an approximation, just by calling xformers.ops.memory_efficient_attention
More benchmarks
xFormers provides many components, and more benchmarks are available in BENCHMARKS.md.
This command will provide information on an xFormers installation, and what kernels are built/available:
python -m xformers.info
xformers.ops warns that mslk could not be imported, or that its native library could not be loaded, install mslk from the PyTorch index matching your PyTorch build (see above). If mslk's native library can't be loaded, xFormers falls back to mslk's python-only mode, in which the default attention backends still work on CUDA.mslk builds, but you can install mslk's python-only build from source instead (nothing is compiled). The default attention backends work with it::: Replace cu126 with the CUDA version of your PyTorch. In PowerShell, use: $env:MSLK_PYTHON_ONLY = "1"
pip install torch flash-attn-3 --index-url https://download.pytorch.org/whl/cu126
set MSLK_PYTHON_ONLY=1
pip install https://github.com/meta-pytorch/MSLK/archive/refs/heads/main.zip
pip install -U xformers --index-url https://download.pytorch.org/whl/cu126
xFormers has a BSD-style license, as found in the LICENSE file. It includes code from the triton-lang/kernels repo.
If you use xFormers in your publication, please cite it by using the following BibTeX entry.
@Misc{xFormers2022,
author = {Benjamin Lefaudeux and Francisco Massa and Diana Liskovich and Wenhan Xiong and Vittorio Caggiano and Sean Naren and Min Xu and Jieru Hu and Marta Tintore and Susan Zhang and Patrick Labatut and Daniel Haziza and Luca Wehrstedt and Jeremy Reizenstein and Grigory Sizov},
title = {xFormers: A modular and hackable Transformer modelling library},
howpublished = {\url{https://github.com/facebookresearch/xformers}},
year = {2022}
}
The following repositories are used in xFormers, either in close to original form or as an inspiration: