1 . Fairseq 简介
Fairseq 是一个用 PyTorch 编写的序列建模工具包,它可以为翻译、摘要、语言建模和其他文本生成任务训练自定义模型。
特点:
- 多GPU训练
- 使用多种搜索算法在 CPU 和 GPU 上快速生成
- 在单个 GPU 上,gradient accumulation可以使用大的小批量进行训练
- 可扩展:轻松注册新 models , criterions , tasks , optimizers and learning rate schedulers
- 混合精度训练
- full parameter and optimizer state sharding
- offloading parameters to CPU
2 . Fairseq 安装
2.1 环境准备
fairseq 0.9.0
python 3.7.4
pytorch 1.6.0+cu101
cuda 10.1
gcc 5.4.0
apex 0.1
2.2 安装
本文系统为 CentOS Linux release 7.9.2009 (Core)
2.2.1 pytorch安装
pip install torch==1.6.0+cu101 torchvision==0.7.0+cu101 -f https://download.pytorch.org/whl/torch_stable.html
验证安装
python -c "import torch;print(torch.__version__, torch.version.cuda)"
显示
1.6.0+cu101 10.1
2.2.2 安装 fairseq
git 下载 fairseq 到本地,版本需要回退到 2497a9dc6ebcd58b410e7d8e265219cf0b0dbfbb
commit 2497a9dc6ebcd58b410e7d8e265219cf0b0dbfbb
Author: Xu Song <xusong.vip@gmail.com>
Date: Thu Aug 13 21:19:09 2020 -0700
运行:
git clone https://github.com/pytorch/fairseq.git
cd fairseq
git reset --hard 2497a9dc6ebcd58b410e7d8e265219cf0b0dbfbb
pip install --editable ./
验证安装
python -c "import fairseq;print(fairseq.__version__)"
显示
0.9.0
2.2.3 安装 apex(可选)
(1) 简介
apex 是NVIDIA为Pytorch开发的混合精度训练库,在多卡训练、半精度训练的过程中可以带来更快的训练效率。安装apex的时候需要注意,由于安装过程会编译CUDA代码,且需要与pytorch使用同一版本的CUDA编译,因此要先安装与pytorch一致的CUDA。
(2) 安装
git 下载 apex库到本地,回退版本 3fe10b5597ba14a748ebb271a6ab97c09c5701ac
commit 3fe10b5597ba14a748ebb271a6ab97c09c5701ac
Author: Burc Eryilmaz <sberyilm@gmail.com>
Date: Thu Dec 3 21:16:22 2020 -0800
运行
git clone https://github.com/NVIDIA/apex
cd apex
git reset --hard 3fe10b5597ba14a748ebb271a6ab97c09c5701ac
pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" \
--global-option="--deprecated_fused_adam" --global-option="--xentropy" \
--global-option="--fast_multihead_attn" ./
验证安装
python -c "import fairseq;print(fairseq.utils.multi_tensor_l2norm_available)"
显示
True
3. 总结
安装时,一定要注意版本一致。