Chinese-LLaMA-Alpaca icon indicating copy to clipboard operation
Chinese-LLaMA-Alpaca copied to clipboard

用lora微调时eval loss为nan

Open heshuguo opened this issue 1 year ago • 10 comments

感谢您使用Issue提问模板,请按照以下步骤提供相关信息。我们将优先处理信息相对完整的Issue,感谢您的配合。

提示:将[ ]中填入x,表示打对钩。提问时删除上面这两行。请只保留符合的选项,删掉其他。

详细描述问题

在用alpaca-lora微调时,loss不太稳定,且eval loss为nan,数据格式为: image

生成的loss截图为: image

运行截图或log

(如有必要)请提供文本log或者运行截图,以便我们更好地了解问题详情。

必查项目

  • [ ] 哪个模型的问题:LLaMA / Alpaca (只保留你要问的)
  • [ ] 问题类型:(只保留你要问的)
    • 下载问题
    • 模型转换和合并
    • 模型推理问题(🤗 transformers)
    • 模型量化和部署问题(llama.cpp、text-generation-webui、LlamaChat)
    • 效果问题
    • 其他问题
  • [ ] 由于相关依赖频繁更新,请确保按照Wiki中的相关步骤执行
  • [ ] 我已阅读FAQ章节并且已在Issue中对问题进行了搜索,没有找到相似问题和解决方案
  • [ ] 第三方插件问题:例如llama.cpptext-generation-webuiLlamaChat等,同时建议到对应的项目中查找解决方案

heshuguo avatar May 08 '23 09:05 heshuguo

@airaria 运行的参数如下: nohup python finetune.py --base_model '/data/llama-test/merge_chinese_lora_alpaca_plus_7b' --data_path './data/concat_datasets.json' --output_dir "./plus-7b-output/alpaca-plus-7b-test-001" --batch_size 96 --micro_batch_size 32 --num_epochs 3 --learning_rate 3e-4 --cutoff_len 512 --val_set_size 5000 --lora_r 8 --lora_alpha 16 --lora_dropout 0.1 --lora_target_modules '[q_proj,k_proj,v_proj,o_proj]' --train_on_inputs --group_by_length >> ./plus-7b-output/alpaca-plus-7b-test-001/stdout_run001.log &

和这个有关吗:

tokenizer.pad_token_id = ( 0 # unk. we want this to be different from the eos token ) tokenizer.padding_side = "left" # Allow batched inference

heshuguo avatar May 08 '23 09:05 heshuguo

tokenizer.padding_side 改成'right',可能和这个有关

airaria avatar May 08 '23 10:05 airaria

tokenizer.padding_side 改成'right',可能和这个有关

@airaria 感谢大佬快速回复,等我们改下再试试

heshuguo avatar May 08 '23 10:05 heshuguo

您好,您也是使用alpaca-plus在alpaca-lora的微调代码上进行的吗?训练完成之后怎么合并的呢?

yuanzhiyong1999 avatar May 08 '23 12:05 yuanzhiyong1999

您好,您也是使用alpaca-plus在alpaca-lora的微调代码上进行的吗?训练完成之后怎么合并的呢 是的,不过还没训练完。合并的话,可以参考这个吧,三个一起弄 python scripts/merge_llama_with_chinese_lora.py
--base_model path_to_original_llama_hf_dir
--lora_model path_to_chinese_llama_plus_lora,path_to_chinese_alpaca_plus_lora
--output_type [pth|huggingface] --output_dir path_to_output_dir

heshuguo avatar May 09 '23 00:05 heshuguo

tokenizer.padding_side 改成'right',可能和这个有关

@airaria 感谢大佬快速回复,等我们改下再试试

改成right后验证集有loss了,但是loss下降的有点慢,最终loss的合理值是多少? image

另外还要请教一个问题,alpaca-lora里面用的是int8,如果将load_in_8bit=True改成False后就会报错,错误如下:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu! model = LlamaForCausalLM.from_pretrained( base_model, load_in_8bit=True, torch_dtype=torch.float16, device_map=device_map, ) model = prepare_model_for_int8_training(model)

@airaria 请问大佬,有什么建议吗?

heshuguo avatar May 09 '23 00:05 heshuguo

这个loss我看来算是正常,至少整个流程上没问题,不同domain的数据的有差别,2以下都是正常的。 如果你还是觉得loss高,那需要靠你自己调模型训练、调超参等“炼丹”技术了

load_in_8bit=False后,model = prepare_model_for_int8_training(model)这句也要删掉吧?

airaria avatar May 09 '23 04:05 airaria

这个loss我看来算是正常,至少整个流程上没问题,不同domain的数据的有差别,2以下都是正常的。 如果你还是觉得loss高,那需要靠你自己调模型训练、调超参等“炼丹”技术了

load_in_8bit=False后,model = prepare_model_for_int8_training(model)这句也要删掉吧?

当时这句也删除了,还是报那个错:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!。 是不是device_map那里要改下,我用的auto,这个需要改下吗?改成cpu或者GPU? @airaria

heshuguo avatar May 09 '23 05:05 heshuguo

这个loss我看来算是正常,至少整个流程上没问题,不同domain的数据的有差别,2以下都是正常的。 如果你还是觉得loss高,那需要靠你自己调模型训练、调超参等“炼丹”技术了 load_in_8bit=False后,model = prepare_model_for_int8_training(model)这句也要删掉吧?

当时这句也删除了,还是报那个错:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!。 是不是device_map那里要改下,我用的auto,这个需要改下吗?改成cpu或者GPU? @airaria load_in_8bit=False,直接去掉device_map即可

iMountTai avatar May 09 '23 05:05 iMountTai

这个loss我看来算是正常,至少整个流程上没问题,不同domain的数据的有差别,2以下都是正常的。 如果你还是觉得loss高,那需要靠你自己调模型训练、调超参等“炼丹”技术了 load_in_8bit=False后,model = prepare_model_for_int8_training(model)这句也要删掉吧?

当时这句也删除了,还是报那个错:Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cpu!。 是不是device_map那里要改下,我用的auto,这个需要改下吗?改成cpu或者GPU? @airaria load_in_8bit=False,直接去掉device_map即可

@iMountTai 非常感谢

heshuguo avatar May 09 '23 06:05 heshuguo

我也遇到类似的问题training loss 变成zero,请问为什么改成padding to right可以解决问题

nps798 avatar Oct 10 '23 04:10 nps798