zl程序教程

您现在的位置是:首页 >  其他

当前栏目

从 huggingface/transformers 转换scibert模型到PaddleNLP

转换 模型 Transformers
2023-09-11 14:14:28 时间
从 huggingface/transformers 转换scibert模型到PaddleNLP
1、获取 huggingface/transformers repo
git clone https://github.com.cnpmjs.org/huggingface/transformers.git
git checkout v2.5.1 #已验证v2.5.1
git branch #查看当前分支
2、修改 huggingface/transformers 相关代码并运行:
export CONVERT_MODEL=$PWD/CONVERT_MODEL # convert_model所在路径
export HUGGINGFACE=$PWD/transformers # git clone的transformers所在路径
cp $CONVERT_MODEL/modeling_utils.py  $HUGGINGFACE/src/transformers/modeling_utils.py # 在470行后加入了numpy转换与保存代码
cp $CONVERT_MODEL/modeling_bert.py  $HUGGINGFACE/src/transformers/modeling_bert.py # 加入了print(outputs),方便验证对齐转换结果
cp $CONVERT_MODEL/tokenization_bert.py  $HUGGINGFACE/src/transformers/tokenization_bert.py
cp $CONVERT_MODEL/configuration_bert.py  $HUGGINGFACE/src/transformers/configuration_bert.py
cp $CONVERT_MODEL/run_glue.py $HUGGINGFACE/examples/run_glue.py # 去掉数据随机性,换成eval模式,使用pretraining的模型替换下游任务的模型
下载QNLI数据

wget https://dataset.bj.bcebos.com/glue/QNLI.zip
unzip QNLI
python3环境中运行

export CUDA_VISIBLE_DEVICES=0
export DATA_DIR=$PWD/QNLI # 上一步中QNLI所在路径
export MODEL=scibert-scivocab-uncased # 希望转换的模型
export PYTHONPATH=$HUGGINGFACE/src
python $HUGGINGFACE/examples/run_glue.py \
--model_type bert \
--model_name_or_path $MODEL \
--task_name QNLI \
--do_train \
--do_eval \
--data_dir $DATA_DIR \
--max_seq_length 128 \
--per_gpu_eval_batch_size=32   \
--per_gpu_train_batch_size=32   \
--learning_rate 2e-5 \
--num_train_epochs 1 \
--output_dir ./tmp/$MODEL/ \
--logging_steps 500 \
--evaluate_during_training \
--do_lower_case \ # cased模型不要这一行
如果缺少依赖,则pip install相关依赖。

3、修改paddlenlp相关代码并运行
获取PaddleNLP repo

git clone https://github.com.cnpmjs.org/PaddlePaddle/PaddleNLP.git
已验证最新版本PaddleNLP

修改paddlenlp/transformers/bert/modeling.py文件
https://huggingface.co/allenai/scibert_scivocab_uncased/blob/main/config.json
将上述地址里json文件中的内容增加到paddlenlp/transformers/bert/modeling.py 中BertPreTrainedModel的pretrained_init_configuration内,添加时去掉

"model_type": "bert"
"layer_norm_eps": 1e-12
两个参数

修改paddlenlp/transformers/bert/tokenizer.py文件
将scibert词表配置填写到paddlenlp/transformers/bert/tokenizer.py 中BertTokenizer的pretrained_resource_files_map和pretrained_init_configuration内,如下

pretrained_resource_files_map = {
 "vocab_file": {
     "scibert-scivocab-uncased":                       "https://huggingface.co/allenai/scibert_scivocab_uncased/resolve/main/vocab.txt"
...}
pretrained_init_configuration = {
"scibert-scivocab-uncased": {
    "do_lower_case":True,
    ...}}
运行代码

export PDNLP= PaddleNLP repo的本地地址
export PYTHONPATH=$PDNLP
python -u $CONVERT_MODEL/run_glue_pp.py \
 --model_type bert \
 --model_name_or_path $MODEL \
 --task_name QNLI \
 --max_seq_length 128 \
 --batch_size 32   \
 --learning_rate 2e-5 \
 --num_train_epochs 1 \
 --logging_steps 1 \
 --save_steps 500 \
 --output_dir ./tmp/$MODEL/ \
 --n_gpu 1 \ # CPU环境设置为0
 --params_pd_path params.pd
比较输出的tensor数值是否与第2步基本相同,第一个tensor部分不一致是seq length长度不一致,无影响,如无问题,模型转换正确。

将当前目录产生的$MODEL.pdparams模型上传bos,并在paddlenlp/transformers/bert/modeling.py 中BertPreTrainedModel的pretrained_resource_files_map中加入该模型对应的链接。