IDEA-CCNL / Fengshenbang-LM

Fengshenbang-LM(封神榜大模型)是IDEA研究院认知计算与自然语言研究中心主导的大模型开源体系,成为中文AIGC和认知智能的基础设施。
Apache License 2.0
4k stars 375 forks source link

DeBertaV2模型重复实验不可复现问题(loss有差异) #126

Closed HUSTHY closed 1 year ago

HUSTHY commented 2 years ago

使用DeBertaV2做分类任务,采用Erlangshen-DeBERTa-v2-97M-Chinese中文预训练权重 环境如下:cuda11.2 torch 1.8.1+cu111 python 3.7.7 transformers 4.21.1 运行同样的代码2次结果不一样,同样的环境和参数,设置了随机种子 日志信息如下: `(hy_py37_torch) [root@localhost ccf_fewshot_classification]# python train_patent_bert_kfold.py /home/kedu/opt/anaconda3/envs/hy_py37_torch/lib/python3.7/site-packages/sklearn/utils/validation.py:37: DeprecationWarning: distutils Version classes are deprecated. Use packaging.version instead. LARGE_SPARSE_SUPPORTED = LooseVersion(scipy_version) >= '0.14.0' 2022-09-08 17:21:05,479 train_patent_bert_kfold.py [line:95] INFO submit_path------submit/submit_title_abstract_ernie_5fold_integrate_logit_2022-09-08_20.csv 2022-09-08 17:21:05,479 train_patent_bert_kfold.py [line:97] INFO Namespace(accumulation_steps=1, adversarial_type='PGD', batch_size=16, bert_type='deberta', data_type='title_abstract', device='0', duplicate=1, epochs=5, integrate_type='logit', is_adversarial=True, is_masklm=False, is_prompt=False, lr=2e-05, max_len=460, model_out='./output/patent/', pretrained='./pretrained_models/torch/Erlangshen-DeBERTa-v2-97M-Chinese', prompt_text='[SEP]专利类别[MASK]', random_seed=100, test_file='./data/testA.json', train_file='./data/train.json') 2022-09-08 17:21:05,479 train_patent_bert_kfold.py [line:98] INFO data_type--------title_abstract 2022-09-08 17:21:05,480 train_patent_bert_kfold.py [line:99] INFO patentBert---------./pretrained_models/torch/Erlangshen-DeBERTa-v2-97M-Chinese 2022-09-08 17:21:05,544 train_patent_bert_kfold.py [line:352] INFO test_datas: 20839 2022-09-08 17:21:05,546 train_patent_bert_kfold.py [line:357] INFO train_datas: 958 tokenization: 20839it [00:15, 1306.00it/s] 2022-09-08 17:21:21,505 train_patent_bert_kfold.py [line:125] INFO ================fold 0=============== 2022-09-08 17:21:21,505 train_patent_bert_kfold.py [line:128] INFO save_path---------./output/patent/deberta_186M_title_abstract_2022-09-08_fold_0 Some weights of the model checkpoint at ./pretrained_models/torch/Erlangshen-DeBERTa-v2-97M-Chinese were not used when initializing PatentDeBertaV2: ['cls.predictions.decoder.bias', 'cls.predictions.transform.LayerNorm.weight', 'cls.predictions.decoder.weight', 'cls.predictions.transform.LayerNorm.bias', 'cls.predictions.transform.dense.weight', 'cls.predictions.bias', 'cls.predictions.transform.dense.bias']

代码在附件中

GGGGGGXY commented 2 years ago

极大概率是learning rate的问题,我把schuduler改成constanst以后,最后的结果相差不会太大,另外为了做验证,我把dataloader里面的shuffle也关了

image image
GGGGGGXY commented 2 years ago

一般情况下,我们验证集也是把shuffle关掉的

HUSTHY commented 2 years ago

验证集到无所谓shuffle;主要是loss有波动,最后的结果有差异的,每次都可能不一样,要是上线或者比赛对复现性要求比较高的就不太友好了。 主要是我现在不知道那里出问题了,我感觉模型权重不可能有问题,torch框架没有问题其他的模型比如Bert能重复实验,那应该就是transformers关于DebertaV2的实现问题。 如果是我的代码问题,我没有发现问题在哪里。。。

GGGGGGXY commented 2 years ago

一开始怀疑是模型精度有问题,因为我们开源的权重是fp16, 结果转换成fp32后依旧能复现这个问题,更具体的表现是、模型的前向loss一致,但是backward以后再前向loss就不一致了, 这里也怀疑是不是优化器的问题,尝试更换optimizer后依旧浮现,极有可能是transformers库里面实现的deberta的问题,这里暂时先mark住,有结论后进一步更新。

GGGGGGXY commented 2 years ago

怀疑是GPU针对单精度计算有优化导致误差,可以确定的是,在训练的第一步opt.step后,两次执行的模型权重已经不一致了。 但是差异很小,几乎是小数点后五六位、七八位,但是这一点误差慢慢累积就会导致最后结果不稳定。 export NVIDIA_TF32_OVERRIDE=0 关闭TF32优化,实验结果依旧不稳定。

更加神奇的是,如果不使用GPU计算,device = 'cpu', 实验结果是可以复现的,同时产生的模型权重完全一致,这个需要继续定位一下在GPU上到底做了啥优化导致精度丢失。

GGGGGGXY commented 2 years ago

根据这个回答 https://www.zhihu.com/question/22373937 强烈怀疑是gpu单精度计算的精度问题,在roberta、bert类模型上因为矩阵运算没有那么多,所以单步积累的误差没有到特别明显的程度