查阅文献: gordicaleksa.medium.com/eli5-flash-…
什么是flash_attn
“FlashAttention:是具有IO 感知能力的、 且快速的、 内存高效的、 精确的注意力
结论是 FlashAttention 是:
- 快速——论文摘录:“我们训练 BERT-large(序列长度 512)比 MLPerf 1.1 中的训练速度记录快 15%,GPT2(序列长度 1K)比 HuggingFace 和 Megatron-LM 的基线实现快 3 倍,和远程竞技场(序列长度 1K-4K)比基线快 2.4 倍。”
-
- Fast — excerpt from the paper: “We train BERT-large (seq. length 512) 15% faster than the training speed record in MLPerf 1.1, GPT2 (seq. length 1K) 3x faster than baseline implementations from HuggingFace and Megatron-LM, and long-range arena (seq. length 1K-4K) 2.4x faster than baselines.”
- 内存效率高——与序列长度O(N²)的普通注意力相比,该方法在 N ( O(N) )中是次二次/线性的。稍后我们会看到原因和方式。
-
- Memory-efficient — compared to vanilla attention, which is quadratic in sequence length, O(N²) , this method is sub-quadratic/linear in N (O(N) ). We’ll see later why & how.
- 精确——这意味着它不是注意力机制的近似(例如稀疏或低秩矩阵近似方法)——它的输出与“普通”注意力机制中的相同。
-
- Exact — meaning it’s not an approximation of the attention mechanism (like e.g. sparse, or low-rank matrix approximation methods) — its outputs are the same as in the “vanilla” attention mechanism.
- IO 感知——与普通注意力相比,Flash 注意力是有感知的。
-
- IO aware — compared to vanilla attention, flash attention is sentient.
如何在环境中安装flash_attn
以我的经验,先安装最新版flash-attn,再安装指定版本的flash-attn,会避免掉很多错误。
第一步:安装最新版的flash-attn
-
将GitHub上的flash-attn最新版git clone 到本地
git clone https://github.com/Dao-AILab/flash-attention.git
至于为什么要 git clone 下载请看以下网址zhuanlan.zhihu.com/p/651322412
-
进入 flash-attention 目录,执行
python setup.py install
的方式来安装最新版的flash-attn,安装时间在1个小时左右。
第二步:安装指定版本的flash-attn
-
如果你想安装的flash-attn版本不是最新版,那就先安装最新版flash-attn,再通过
pip uninstall flash-attn
卸载掉最新版。 -
此时再使用pip install flash-attn==1.0.4 (或其他指定版本),就能顺利安装!
其他意外情况:
若卸载最新版flash-attn后,依旧不能成功安装指定版本flash-attn ,则使用以下命令安装:
pip install flash-attn==1.0.4 --no-build-isolation
--no-build-isolation
是一个选项,表示禁用构建隔离机制。这个隔离机制通常被用于保证安装操作在一个干净的环境中进行,避免与其他项目的依赖冲突。但是,在某些情况下,例如使用特定版本的 C/C++ 编译器等情况下,需要禁用构建隔离机制以确保正确的编译和链接。
等待安装完毕
运行flash_attn基准测试程序 检查flash_attn是否安装成功
flash_attn==1.0.1安装成功以后,打开其Github的官方仓库(github.com/Dao-AILab/f… )。
然后,下载历史版本为1.0.1的flash_attn。
解压后,进入其根目录下:
最后,打开其在pypi的官方仓库(pypi.org/project/fla… ),找到基准测试程序的测试代码。
python benchmarks/benchmark_flash_attention.py
终端执行,等待结果:
输出上述结果,则安装成功!