如何使用 FP16 混合精度训练 Spacy3 项目

时间:2021-03-07 12:09:25

标签: nlp pytorch spacy huggingface-transformers spacy-3

目标是以 FP16 混合精度运行 python -m spacy train,以便在有限的 VRAM 中使用大型转换器(roberta-largealbert-large 等) (RTX 2080ti 11 GB)。

用于训练的新 Spacy3 project.yml approach 直接使用通过 Huggingface-transformers models 加载的 Spacy-transformers v1.0。只需添加 --fp16 标志 (as described here),即可以混合精度运行 Huggingface 模型。

spacy 配置是使用 python -m spacy init config --lang en --pipeline ner --optimize efficiency --gpu -F default.cfg 生成的,并由 python -m spacy init fill-config default.cfg config.cfg --diff 检查是否完整。然而没有找到 FP16/混合精度。

重现

使用 init-config 中已更改的 project.ymlspaCy Project: Named Entity Recognition (WikiNER) 以使用 GPU 和转换器(默认为 roberta-base):

commands:
  -
    name: init-config
    help: "Generate a transformer English NER config"
    script:
      - "python -m spacy init config --lang en --pipeline ner --gpu -F --optimize efficiency -C configs/${vars.config}.cfg"

测试了什么

  • --fp16 添加到 python -m spacy project run
  • --fp16 添加到 python -m spacy train
  • 在各个部分 (fp16 = true) 中将 default.cfg 添加到 [components.transformer], [components.transformer.model], [training], [initialize]

逻辑 transformers 在 FP16 中运行如下:

from transformers import TrainingArguments
TrainingArguments(..., fp16=True, ...)

软件堆栈细节

 - spacy              3.0.3
 - spacy-transformers 1.0.1
 - transformers       4.2.2
 - torch              1.6.0+cu101

0 个答案:

没有答案