MCPcopy Create free account
hub / github.com/facebookresearch/xformers

github.com/facebookresearch/xformers

Chat with this repo
repository ↗ · DeepWiki ↗ · release v0.0.35 ↗ · + Follow · compare 2 versions
1,808 symbols 4,958 edges 798 files ⚖ BSD-2-Clause 246 documented · 14% updated 11d agov0.0.35 · 2026-02-20★ 10,543364 open issues

Browse by type

Functions 1,445 Types & classes 346 Endpoints 17
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Open in Colab

CircleCI Codecov black

PRs welcome


xFormers - Toolbox to Accelerate Research on Transformers

xFormers is: - Customizable building blocks: Independent/customizable building blocks that can be used without boilerplate code. The components are domain-agnostic and xFormers is used by researchers in vision, NLP and more. - Research first: xFormers contains bleeding-edge components, that are not yet available in mainstream libraries like PyTorch. - Built with efficiency in mind: Because speed of iteration matters, components are as fast and memory-efficient as possible. xFormers contains its own CUDA kernels, but dispatches to other libraries when relevant.

Installing xFormers

  • (RECOMMENDED, linux & win) Install latest stable with pip: Requires PyTorch 2.10.0
# [linux & win] cuda 12.6 version
pip3 install -U xformers --index-url https://download.pytorch.org/whl/cu126
# [linux & win] cuda 12.8 version
pip3 install -U xformers --index-url https://download.pytorch.org/whl/cu128
# [linux & win] cuda 13.0 version
pip3 install -U xformers --index-url https://download.pytorch.org/whl/cu130
# [linux only] (EXPERIMENTAL) rocm 7.1 version
pip3 install -U xformers --index-url https://download.pytorch.org/whl/rocm7.1
  • Development binaries:
# Same requirements as for the stable version above
pip install --pre -U xformers
  • Install from source: If you want to use with another version of PyTorch for instance (including nightly-releases)
# (Optional) Makes the build much faster
pip install ninja
# Set TORCH_CUDA_ARCH_LIST if running and building on different GPU types
# NOTE: pytorch must already be installed!
pip install -v --no-build-isolation -U git+https://github.com/facebookresearch/xformers.git@main#egg=xformers
# (this can take dozens of minutes)

Benchmarks

Memory-efficient MHA Benchmarks for ViTS 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.

(Optional) Testing the installation

This command will provide information on an xFormers installation, and what kernels are built/available:

python -m xformers.info

Using xFormers

Key Features

  1. Optimized building blocks, beyond PyTorch primitives
  2. Memory-efficient exact attention - up to 10x faster
  3. sparse attention
  4. block-sparse attention
  5. fused softmax
  6. fused linear layer
  7. fused layer norm
  8. fused dropout(activation(x+bias))
  9. fused SwiGLU

Install troubleshooting

  • NVCC and the current CUDA runtime match. Depending on your setup, you may be able to change the CUDA runtime with module unload cuda; module load cuda/xx.x, possibly also nvcc
  • the version of GCC that you're using matches the current NVCC capabilities
  • the TORCH_CUDA_ARCH_LIST env variable is set to the architectures that you want to support. A suggested setup (slow to build but comprehensive) is export TORCH_CUDA_ARCH_LIST="6.0;6.1;6.2;7.0;7.2;7.5;8.0;8.6"
  • If the build from source OOMs, it's possible to reduce the parallelism of ninja with MAX_JOBS (eg MAX_JOBS=2)
  • If getting error message Filename longer than 260 characters on Windows, make sure long paths are enabled at OS level, and also execute the command git config --global core.longpaths true

License

xFormers has a BSD-style license, as found in the LICENSE file. It includes code from the triton-lang/kernels repo.

Citing xFormers

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}
}

Credits

The following repositories are used in xFormers, either in close to original form or as an inspiration:

Core symbols most depended-on inside this repo

browse all functions →

Shape

Function 900
Method 545
Class 346
Route 17

Languages

Python80%
C++20%

Modules by API surface

stubs/torch_stub_tests.py121 symbols
xformers/ops/fmha/attn_bias.py116 symbols
tests/test_mem_eff_attention.py82 symbols
tests/test_sparsity24.py60 symbols
xformers/ops/sp24.py47 symbols
xformers/fwbw_overlap.py46 symbols
xformers/profiler/profiler.py34 symbols
xformers/csrc/attention/hip_fmha/ck_tiled_fmha_fwd_setting.h33 symbols
xformers/checkpoint.py33 symbols
xformers/ops/fmha/common.py31 symbols
xformers/csrc/attention/hip_fmha/ck_tiled_fmha_fwd_splitkv_setting.h28 symbols
xformers/ops/swiglu_op.py27 symbols

Dependencies from manifests, versioned

black24.4.2 · 1×
docutils0.17.1 · 1×
fairscale0.4.5 · 1×
flake86.1.0 · 1×
jinja23.1.6 · 1×
mypy1.10.0 · 1×
numpy1.19.5 · 1×
pandas2.2.2 · 1×
pyre-check0.9.16 · 1×
pyre-extensions0.0.29 · 1×
pytest7.2.0 · 1×
pytest-cov2.10.0 · 1×

For agents

$ claude mcp add xformers \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page