PaddleNLP icon indicating copy to clipboard operation
PaddleNLP copied to clipboard

PaddleNLP/examples/model_compression/ofa/export_model.py / 原始官方代码,运行报错

Open AI-Mart opened this issue 2 years ago • 6 comments

Traceback (most recent call last): File "./export_model_ofa.py", line 371, in do_train(args) File "./export_model_ofa.py", line 299, in do_train mapping_name = name.replace('.'+str(idx)+'.', '.'+str(kept_layers_index[idx])+'.') UnboundLocalError: local variable 'kept_layers_index' referenced before assignment

原始官方代码,运行报错,报错信息如上,请核查原始代码是否有问题

python -u ./export_model_ofa.py --model_type bert \ --model_name_or_path ../ernie_gram_zh/ofa_cls_36Kr_bert_base_chinese/model_0.6666666666666666 \ --max_seq_length 512 \ --sub_model_output_dir ../ernie_gram_zh/ofa_cls_36Kr_bert_base_chinese/model_0.6666666666666666/dynamic_model \ --static_sub_model ../ernie_gram_zh/ofa_cls_36Kr_bert_base_chinese/model_0.6666666666666666/static_model \ --device gpu \ --width_mult 0.6666666666666666

AI-Mart avatar Nov 14 '21 03:11 AI-Mart

您好! 目前已经在develop分支修复了这个bug,感谢您指出错误! 目前这份导出代码只支持bert,如果需要支持bert以外的模型,需要自己重新定义你的模型(例如ErnieModel)的forward函数:

  • 需要在原有的XXModel的forward增加这一行,https://github.com/PaddlePaddle/PaddleNLP/blob/54bef3dd03c1295f1befc2e57f2d0929086daadb/examples/model_compression/ofa/export_model.py#L44
  • 并且后面需要用到wtype:https://github.com/PaddlePaddle/PaddleNLP/blob/54bef3dd03c1295f1befc2e57f2d0929086daadb/examples/model_compression/ofa/export_model.py#L48

LiuChiachi avatar Nov 17 '21 05:11 LiuChiachi

好的,感谢,我验证下。为了支持PaddleNlp我其实是用ernie-gram训练的分类模型是实体识别模型,但是部署上效率需要做优化,想到模型压缩,所以参考了这个ofa框架,但是目前这个框架只支持BERT,我也就把之前的预训练模型改成BERT重新训练了一遍分类模型和实体识别模型。 用其他的预训练模型,这个需要重新自定义forward函数感觉比较麻烦,毕竟自带的预训练模型这个forwar函数都是底层配置好了,希望后面这个框架能做成兼容就好了,可以在现有的框架下,替换个预训练模型改个名字就能跑起来。

AI-Mart avatar Nov 17 '21 06:11 AI-Mart

  • (input_ids == self.pad_token_id).astype(wtype) * -1e9, axis=[1, 2])

我在调用trainer.evaluate(test_ds, batch_size=16)执行我自己的训练数据(长度为512的中文文本,三种label)时,也报了这个错误:

UnboundLocalError Traceback (most recent call last) /tmp/ipykernel_2214/3588254174.py in 9 trainer.evaluate(test_ds, batch_size=16)

~/anaconda3/envs/paddle/lib/python3.7/site-packages/paddlehub/finetune/trainer.py in evaluate(self, eval_dataset, batch_size, num_workers, collate_fn) 317 # print avg metrics and loss 318 print_msg = '[Evaluation result]' --> 319 if loss: 320 avg_loss /= num_samples 321 print_msg += ' avg_loss={:.4f}'.format(avg_loss)

UnboundLocalError: local variable 'loss' referenced before assignment

我修改了/paddlenlp/transformers/ernie/modeling.py的ERNIEModel的forward函数后,依然报错。是不是我修改的位置不对?

zoeChen119 avatar Jul 26 '22 07:07 zoeChen119

  • (input_ids == self.pad_token_id).astype(wtype) * -1e9, axis=[1, 2])

我在调用trainer.evaluate(test_ds, batch_size=16)执行我自己的训练数据(长度为512的中文文本,三种label)时,也报了这个错误:

UnboundLocalError Traceback (most recent call last) /tmp/ipykernel_2214/3588254174.py in 9 trainer.evaluate(test_ds, batch_size=16) ~/anaconda3/envs/paddle/lib/python3.7/site-packages/paddlehub/finetune/trainer.py in evaluate(self, eval_dataset, batch_size, num_workers, collate_fn) 317 # print avg metrics and loss 318 print_msg = '[Evaluation result]' --> 319 if loss: 320 avg_loss /= num_samples 321 print_msg += ' avg_loss={:.4f}'.format(avg_loss) UnboundLocalError: local variable 'loss' referenced before assignment

我修改了/paddlenlp/transformers/ernie/modeling.py的ERNIEModel的forward函数后,依然报错。是不是我修改的位置不对?

想请问下您也是在做模型压缩吗,还是只是训练一个分类模型?您是怎样修改 forward 函数的呢?另外请问下您的 paddlenlp 版本是?

LiuChiachi avatar Jul 26 '22 12:07 LiuChiachi

好的,感谢,我验证下。为了支持PaddleNlp我其实是用ernie-gram训练的分类模型是实体识别模型,但是部署上效率需要做优化,想到模型压缩,所以参考了这个ofa框架,但是目前这个框架只支持BERT,我也就把之前的预训练模型改成BERT重新训练了一遍分类模型和实体识别模型。 用其他的预训练模型,这个需要重新自定义forward函数感觉比较麻烦,毕竟自带的预训练模型这个forwar函数都是底层配置好了,希望后面这个框架能做成兼容就好了,可以在现有的框架下,替换个预训练模型改个名字就能跑起来。

感谢您的关注,压缩API 即将更新,会有更加简洁和通用的使用体验~

LiuChiachi avatar Jul 26 '22 12:07 LiuChiachi

只是分类模型,后来我查看https://paddlenlp.readthedocs.io/zh/latest/model_zoo/index.html 这里的教程顺利完成了微调。 我的paddle版本是2.3.1

zoeChen119 avatar Jul 27 '22 02:07 zoeChen119

This issue is stale because it has been open for 60 days with no activity. 当前issue 60天内无活动,被标记为stale。

github-actions[bot] avatar Jan 31 '23 00:01 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale. 当前issue 被标记为stale已有14天,即将关闭。

github-actions[bot] avatar Feb 15 '23 00:02 github-actions[bot]