xformers README

The project's README from github.com/facebookresearch/xformers. As one markdown file: README.md.

Open in Colab
CircleCI Codecov black
PRs welcome


xFormers - Toolbox to Accelerate Research on Transformers

xFormers is:

Installing xFormers

# [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
# Same requirements as for the stable version above
pip install --pre -U xformers
# (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
    1. Memory-efficient exact attention - up to 10x faster
    2. sparse attention
    3. block-sparse attention
    4. fused softmax
    5. fused linear layer
    6. fused layer norm
    7. fused dropout(activation(x+bias))
    8. fused SwiGLU

Install troubleshooting

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:

xformers (facebookresearch/xformers)

Searching Database…

10.6K ·GitHub
这是干什么的?xFormers 是 Meta(Facebook Research)推出的一套可魔改、经过优化的 Transformer 构建模块工具箱,可以自由组合使用——它提供的是可复用的组件,而不是一个大而全的框架。
它是独立应用,还是我导入到自己代码里用的库?是一个 Python 库:提供独立、可定制的构建模块,不需要写样板代码就能用。这些模块不限定领域——研究人员在视觉、自然语言处理等领域都在用。
我需要先装好什么?PyTorch 2.10.0——稳定的 pip 包就是针对这个版本编译的;其他 PyTorch 版本(包括 nightly 版)需要使用开发版二进制文件或从源码构建。
安装时一般会出什么问题?从源码构建是最容易出问题的环节:NVCC 版本必须和你的 CUDA 运行时以及 GCC 匹配,要为目标 GPU 设置 TORCHCUDAARCHLIST,如果构建时内存不足就用 MAXJOBS=2。在 Windows 上,需要启用长路径支持,并执行 git config --global core.longpaths true。
它采用什么许可证?采用 BSD 风格的许可证,详见 LICENSE 文件;代码库中还包含来自 triton-lang/kernels 仓库的代码。
实际使用时必须有 NVIDIA 显卡吗?实际上是的——xFormers 自带 CUDA 内核(在适用时会转而调用其他库),该仓库的 CI 也会为 AMD 的 ROCm 构建版本。
最亮眼的功能是什么?节省内存的精确注意力机制,速度最高可快 10 倍——注意这是精确计算,不是近似算法,通过 xformers.ops 即可使用。官方公布的基准测试测的是在 A100 上用 f16 跑一次前向加反向传播的耗时。
除了注意力机制,工具箱里还有什么?还有稀疏注意力和分块稀疏注意力,以及一组融合算子:融合 softmax、融合线性层、融合层归一化、融合 dropout(activation(x+bias))和融合 SwiGLU——这些都是比 PyTorch 原生基础算子更优化的构建模块。
比起直接用 PyTorch,为什么要用它?它以研究为优先:你能用到 PyTorch 等主流库里还没有的前沿组件,而且速度和内存效率本身就是它明确的设计目标。
我怎么知道自己安装的版本里到底有哪些内核?运行 README 中 'Testing the installation' 一节给出的安装检查命令——它会报告你的 xFormers 安装信息,以及哪些内核已经编译好、可以使用。
Size
42.9 MB
Stars
10.6K
Age
5y
Commits · 90 Days