序列模型的快速高效实现
fastseq的Python项目详细描述
快速序列
简介
FastSeq为文本生成、摘要和翻译任务提供了高性能的流行序列模型的高效实现。它可以通过简单的import fastseq
自动优化pupular NLP工具包(例如FairSeq)的性能。在
支持的模型
fairseq中支持的模型
- [x] ProphetNet
- [x] BART
- [x] Scaling Neural Machine Translation (Ott et al., 2018)
- [x] Mixture Models for Diverse Machine Translation: Tricks of the Trade (Shen et al., 2019)
- [x] Pay Less Attention with Lightweight and Dynamic Convolutions (Wu et al., 2019)
HuggingFace-Transformers中支持的模型
基准
先知网
- 在
CNN每日邮报val数据,NVIDIA-V100-16GB
在BatchSize 32 64 128 prophetnet 2.7 samples/s 3.1 samples/s OOM prophetnet + fastseq 5.5 samples/s 8.4 samples/s 10.3 samples/s
带设置:
$ fastseq-generate-for-fairseq \ cnn_dm_bert.1k/len-1024.bin \ --path prophetnet/model.pt \ --fp16 \ --task translation_prophetnet \ --batch-size BATCH_SIZE \ --beam 4\ --num-workers 4\ --min-len 55\ --max-len-b 140\ --no-repeat-ngram-size 3\ --lenpen 2.0 \ --remove-bpe \ --gen-subset valid \
来自Fairseq的巴特
- 在
CNN每日邮报val数据,NVIDIA-V100-16GB
^{tb2}$ 在
带设置:
^{pr2}$要获得基线fairseq的速度值,将fastseq-generate-for-fairseq
替换为fairseq-generate
。在
变形金刚的巴特
- 在
CNN每日邮报val数据,NVIDIA-V100-16GB
^{3}$ 在
(2.11.0的编号需要根据docker env进行更新。)
带设置:
$ fastseq-generate-for-transformers \ facebook/bart-large-cnn \ cnn_dm.1k/val.source \ out.summary \ --reference_path cnn_dm/val.target \ --device cuda \ --bs 128\ --fp16 \ --score_path out.score \ --task summarization
为了得到变压器的基准速度值,我们可以添加选项--without_fastseq_opt
,或者使用transformers GitHub存储库中提供的tool。在
来自Fairseq的WMT
- 在
在BatchSize 256 512 1024 fairseq-0.9.0 84 samples/s OOM OOM above + fastseq 129 samples/s 131 samples/s 135 samples/s
设置方式:
$ fastseq-generate-for-fairseq \ wmt14.en-fr.joined-dict.newstest2014/ \ --path wmt14.en-fr.joined-dict.transformer/model.pt \ --beam 4\ --lenpen 0.6 \ --remove-bpe \ --batch-size 32
要获得fairseq的速度值,将fastseq-generate-for-fairseq
替换为fairseq-generate
。在
安装
要求
- Python版本>;=3.6
- torch>;=1.4.0
- fairseq>;=0.9.0
- transformers>;=3.0.2
- requets>;=2.24.0
- absl-py>;=0.9.0
- rouge-score
如果您使用fairseq或transformers,则只需安装其中一个即可。如果您同时使用两者,则需要同时安装两者。在
Python包
fastseq
Python包可以使用pip直接安装
$ pip install fastseq
从源安装
$ git clone https://github.com/microsoft/fastseq
$ cd fastseq
$ pip install --editable ./
使用
示例
使用FastSeq
提供的优化只需要一行代码更改。在
# import fastseq at the beginning of your programimportfastseqimporttorch# Download bart.large.cnnbart=torch.hub.load('pytorch/fairseq','bart.large.cnn')bart.cuda()# use GPUbart.eval()# disable dropout for evaluationbart.half()slines=['FastSeq provides efficient implementations of the popular sequence models. Please visit https://github.com/microsoft/fastseq for more details.']hypotheses=bart.sample(slines,beam=4,lenpen=2.0,max_len_b=140,min_len=55,no_repeat_ngram_size=3)print(hypotheses)
fairseq模型的命令行工具
示例
$ fastseq-generate-for-fairseq \ cnn_dnn/bin \ --path bart.large.cnn/model.pt \ --fp16 \ --task translation \ --batch-size 128\ --gen-subset valid \ --truncate-source \ --bpe gpt2 \ --beam 4\ --num-workers 4\ --min-len 55\ --max-len-b 140\ --no-repeat-ngram-size 3\ --lenpen 2.0
变压器模型的命令行工具
示例
$ fastseq-generate-for-transformers \ facebook/bart-large-cnn \ cnn_dm/val.source \ out.summary \ --reference_path cnn_dm/val.target \ --device cuda \ --bs 128\ --fp16 \ --score_path out.score \ --task summarization
运行测试
# run a single test. $ python tests/optimizer/fairseq/test_fairseq_optimizer.py # run benchmark. $ python tests/optimizer/fairseq/benchmark_fairseq_optimizer.py # run all the tests. $ python -m unittest discover -s tests/ -p '*.py'# run all the benchmarks. $ cd benchmarks && bash run_all_benchmarks.sh
建造
# build package
$ python setup.py sdist bdist_wheel
代码样式
Python编码风格
对Python代码的更改应符合PEP 8。yapf
可用于帮助格式化python代码,并使用pylint
检查python更改。在
# format the code by yapf $ yapf --style pep8 -i -r PYTHON_FILE/PACKAGE # run pylint check $ pylint --rcfile=.pylintrc PYTHON_FILE/PACKAGE
贡献
这个项目欢迎大家的贡献和建议。大多数投稿需要你同意 贡献者许可协议(CLA)声明您有权并实际上授予我们 使用你的贡献的权利。有关详细信息,请访问https://cla.opensource.microsoft.com。在
当您提交请求时,CLA bot将自动确定您是否需要提供 一个CLA并适当地装饰PR(例如状态检查、评论)。只需按照说明操作即可 由bot提供。您只需使用我们的CLA在所有回购中执行一次。在
这个项目采用了Microsoft Open Source Code of Conduct。 有关详细信息,请参阅Code of Conduct FAQ或 如有任何其他问题或意见,请联系opencode@microsoft.com。在
- 项目
标签: