当前位置:   article > 正文

openNMT-py CTranslate2性能对比流程

ctranslate2

https://github.com/OpenNMT/OpenNMT-py

GitHub - OpenNMT/CTranslate2: Fast inference engine for Transformer models

安装软件包:

pip install OpenNMT-py
pip install ctranslate2

基于CTranslate2和openNMT-py对比性能(非基于官方镜像的方式)

参考OpenNMT-py github主页链接下载Pretrained models

基于WMT训练的English-German - Transformer

这个模型里面包含两个文件

averaged-10-epoch.pt和sentencepiece.model

前者是保存的模型,后者是sentencepiece tokenizer的模型

CTranslate2模型转换和量化参考:

https://github.com/OpenNMT/CTranslate2/blob/master/docs/quantization.md

下载测试数据(写在CTranslate2/tools/benchmark/benchmark_all.py)

  1. import sacrebleu
  2. # Benchmark configuration
  3. test_set = "wmt14"
  4. langpair = "en-de"
  5. print("Downloading the test files...")
  6. source_file = sacrebleu.get_source_file(test_set, langpair=langpair)
  7. target_file = sacrebleu.get_reference_files(test_set, langpair=langpair)[0]
  8. print("source_file:", source_file)
  9. print("target_file:", target_file)

会下载翻译前后的原始文本,分别为en-de.en, en-de.de。

bench:参考CTranslate2/tools/benchmark/opennmt_ende_wmt14/目录的ctranslate2和opennmt_py两个目录,里面分别有tokenize.sh和translate.sh。

先对于opennmt_py目录,可以根据tokenize.sh创建custom_tokenize.py,内容如下:

  1. import pyonmttok
  2. sp_model_path = "transformer-ende-wmt-pyOnmt/sentencepiece.model"
  3. src_file = "sacrebleu/wmt14/en-de.en"
  4. tgt_file = src_file + ".tok"
  5. pyonmttok.Tokenizer("none", sp_model_path=sp_model_path).tokenize_file(src_file, tgt_file)

执行后为输入文本生成对应的tokenized文本

然后执行翻译脚本为上一步tokenized输入文本生成翻译后的tokenized文本

修改translate.sh:

  1. #!/bin/bash
  2. # EXTRA_ARGS=""
  3. # if [ $DEVICE = "GPU" ]; then
  4. # EXTRA_ARGS+=" -gpu 0"
  5. # fi
  6. # if [ ${INT8:-0} = "1" ]; then
  7. # EXTRA_ARGS+=" -int8"
  8. # fi
  9. model_path=transformer-ende-wmt-pyOnmt/averaged-10-epoch.pt
  10. src_file=sacrebleu//wmt14/en-de.en.tok
  11. out_file=${src_file}.onmt.out
  12. # onmt_translate \
  13. python -m onmt.bin.translate \
  14. -model ${model_path} \
  15. -src ${src_file} \
  16. -out ${out_file} \
  17. -batch_size 32 \
  18. -beam_size 4 -gpu 0

最后进行detokenize,可以参考detokenize.sh编写custom_detokenize.py:

  1. import sys;
  2. import pyonmttok;
  3. sp_model_path = "transformer-ende-wmt-pyOnmt/sentencepiece.model"
  4. src_file = "sacrebleu/wmt14/en-de.en.tok.onmt.out"
  5. tgt_file = src_file + ".detok"
  6. pyonmttok.Tokenizer("none", sp_model_path=sp_model_path).detokenize_file(src_file, tgt_file)

执行后会为翻译后的tokenized文本生成detokenize的文本文件,结果跟en-de.de应该相似。

ctranslate2执行跟上面流程除了translate步骤其他一样。

benchmark/ctranslate2/translate.sh里面使用了translate二进制可执行文件,但直接pip安装的ctranslate2并没有这个translate文件。

如果不使用镜像或自己构建镜像,直接使用Pip包推理,则应该自己读入文件调用python接口推理。为了公平的比较,openNMT-py的推理可以改成同样的方式并加warmup。

ctranslate2 python接口使用参考:

CTranslate2/python.md at master · OpenNMT/CTranslate2 · GitHub

这里采用自己编写ctans_custom_translate.py脚本读入文件推理:

  1. import ctranslate2
  2. import time
  3. src_file = "sacrebleu/wmt14/en-de.en.tok"
  4. tgt_file = src_file + ".ctrans.out"
  5. model_path = "transformer-ende-wmt-pyOnmt/ende_ctranslate2/"
  6. device = "cuda" # "cpu", "cuda"
  7. max_batch_size = 32
  8. beam_size = 4
  9. with open(src_file, "r") as f:
  10. lines = f.readlines()
  11. lines = [line.strip('\n').split(" ") for line in lines]
  12. translator = ctranslate2.Translator(model_path, device=device)
  13. # warmup
  14. translator.translate_batch(lines[:max_batch_size * 4], max_batch_size=max_batch_size, beam_size=beam_size)
  15. time1 = time.time()
  16. trans_results = translator.translate_batch(lines, max_batch_size=max_batch_size, beam_size=beam_size)
  17. time2 = time.time()
  18. print("ctranslate2 translate time:", time2 - time1)
  19. result_lines = [" ".join(result.hypotheses[0]) + "\n" for result in trans_results]
  20. with open(tgt_file, "w") as f:
  21. f.writelines(result_lines)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/550875
推荐阅读
相关标签
  

闽ICP备14008679号