从 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中加入该模型对应的链接。
相关文章
- 【项目经验】DataTable与JSON之间的转换
- PHP 变量类型的强制转换 & 创建空对象
- Java实现 蓝桥杯 算法训练 大小写转换
- Kubernetes 与 OpenYurt 无缝转换(命令式)
- matlab在处理图像时为什么把数据转换为double型?
- data gurad物理备份方式中的failover转换
- MATLAB实现将图像转换为素描(简笔画)风格
- Atitit 加强学生就业的规划与艾提拉的治学理念 目录 1. 思路的转换1 1.1. 发展内需为主模型 vs 外贸模式1 1.2. 批发模式vs 零售模式vs1 1.3. 天堂模式vs地狱模
- Atitit 网络协议概论 艾提拉著作 目录 1. 有的模型分七层,有的分四层。我觉得1 1.1. 三、链接层 确定了0和1的分组方式1 1.2. 网络层(ip mac转换层3 1.3. 传输
- flutter 字节和模型的转换
- java-Unicode与中文的转换
- 后缀表达式的转换(栈的运用)
- 【CSS3】CSS3 2D 转换 - rotate 旋转 ② ( 使用 rotate 旋转绘制三角形 )
- Postgresql分页Limit,如何将Mysql的分页转换为Postgresql的分页
- tf 强制转换数据类型
- 基类与派生类的转换
- 史上最简SLAM零基础解读(8.1) - 旋转矩阵、旋转向量、欧拉角推导与相互转换
- pytorch 33 使用mmdeploy将mmdetection模型转换为onnx,并基于mmdeploy-sdk实现c++部署