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

合并lora模型和原始模型出问题

Open kevinuserdd opened this issue 1 year ago • 5 comments

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

详细描述问题

请尽量具体地描述您遇到的问题,必要时给出运行命令。这将有助于我们更快速地定位问题所在。

运行截图或日志

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

必查项目(前三项只保留你要问的)

  • [ ] 基础模型:LLaMA / Alpaca / LLaMA-Plus / Alpaca-Plus
  • [ ] 运行系统:Windows / MacOS / Linux
  • [ ] 问题分类:下载问题 / 模型转换和合并 / 模型训练与精调 / 模型推理问题(🤗 transformers) / 模型量化和部署问题(llama.cpp、text-generation-webui、LlamaChat) / 效果问题 / 其他问题
  • [ ] (必选)由于相关依赖频繁更新,请确保按照Wiki中的相关步骤执行
  • [ ] (必选)我已阅读FAQ章节并且已在Issue中对问题进行了搜索,没有找到相似问题和解决方案
  • [ ] (必选)第三方插件问题:例如llama.cpptext-generation-webuiLlamaChat等,同时建议到对应的项目中查找解决方案 指令微调生成的lora模型,与原始模型进行合并,出错误如下: │ │ │ 272 │ │ │ for k in non_lora_keys: │ │ 273 │ │ │ │ print(f"merging {k}") │ │ 274 │ │ │ │ original_k = k.replace('base_model.model.','') │ │ ❱ 275 │ │ │ │ base_model_sd[original_k].copy_(lora_model_sd[k]) │ │ 276 │ │ │ │ │ 277 │ │ │ for k in lora_keys: │ │ 278 │ │ │ │ print(f"merging {k}") │ ╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ RuntimeError: The size of tensor a (49953) must match the size of tensor b (49954) at non-singleton dimension 0

kevinuserdd avatar May 24 '23 09:05 kevinuserdd

image 试试这个

LiemLin avatar May 24 '23 09:05 LiemLin

image 试试这个

在script/run_clm_sft_with_peft.py文件中,有以下代码功能,resize; if len(tokenizer) != embedding_size: logger.info("resize the embedding size by the size of the tokenizer") model.resize_token_embeddings(len(tokenizer))

if training_args.peft_path is not None:
    logger.info("Peft from pre-trained model")
    model = PeftModel.from_pretrained(model, training_args.peft_path)
else:

我这个是执行指令微调后,将lora和原始文件合并的时候出现的bug

kevinuserdd avatar May 24 '23 10:05 kevinuserdd

从错误信息看是词表大小不对,你贴一下完整的运行命令,并说明是具体合并的是哪个模型。

ymcui avatar May 24 '23 10:05 ymcui

从错误信息看是词表大小不对,你贴一下完整的运行命令,并说明是具体合并的是哪个模型。

第一步:从主页链接下载lora文件,对应这个

Chinese-LLaMA-13B | ziqingyang/chinese-llama-lora-13b

第二步:运行将lora版本和原始llama-13b-hf版本合并,得到合并后的模型如下 python scripts/merge_llama_with_chinese_lora.py --base_model ../llama-13b-hf/ --lora_model ../chinese-llama-lora-13b --output_type huggingface --output_dir chinese-llama-lora-merged-13b

第三步: 进行指令微调训练,代码脚本为scripts/run_sft.sh,生成lora文件 第四步: 合并第三步生成的lora文件与第二步得到的chinese-llama-lora-merged-13b,在这里合并的时候发生了上述错误。

kevinuserdd avatar May 24 '23 11:05 kevinuserdd

第四步的命令贴一下? lora文件夹中是否放入了alpaca-tokenizer?

airaria avatar May 24 '23 12:05 airaria

第四步的命令贴一下? lora文件夹中是否放入了alpaca-tokenizer?

你好,这个是指令微调后lora生成后的文件情况,截图如下: WechatIMG254 还有第二步生成的chinese-llama-lora-merged-13b的截图情况: WechatIMG255 然后使用合并脚本,将他们合并: python scripts/merge_llama_with_chinese_lora.py --base_model ../chinese-llama-merged-13b/ --lora_model ../output_dir --output_type huggingface --output_dir output_model

kevinuserdd avatar May 25 '23 02:05 kevinuserdd

从错误信息看是词表大小不对,你贴一下完整的运行命令,并说明是具体合并的是哪个模型。

能帮忙看下吗? 下面的回复我贴了截图,第一步合并我没报错,我是在指令精调后的模型与前一步合并的时候报错的

kevinuserdd avatar May 25 '23 05:05 kevinuserdd

同错

sszyl avatar May 25 '23 07:05 sszyl

同错

难不成很多人都没有操作这一步。。。不操作的话直接加载lora也是可以推理。。但是微调后合并不成功啊关键

kevinuserdd avatar May 25 '23 07:05 kevinuserdd

不训头和emb就可以了

sszyl avatar May 25 '23 08:05 sszyl

不训头和emb就可以了

什么意思? 代码哪里需要修改? 我打印了tokenizer的长度,也是49953,不知道为啥peftmodel加载的时候就49954不匹配

kevinuserdd avatar May 25 '23 08:05 kevinuserdd

不训头和emb就可以了

什么意思? 代码哪里需要修改? 我打印了tokenizer的长度,也是49953,不知道为啥peftmodel加载的时候就49954不匹配

如果机器的显存比较紧张,可以删去脚本中的--modules_to_save ${modules_to_save} , 即不训练embed_tokens和lm_head(这两部分参数量较大),只训练LoRA参数,以节省显存 如果是在Alpaca-LoRA基础上继续微调,需要修改peft_path下的adapter_config.json文件,改为"modules_to_save": null 如果执行上一步操作后程序有报错,请删除--gradient_checkpointing \再尝试

sszyl avatar May 25 '23 08:05 sszyl

不训头和emb就可以了

什么意思? 代码哪里需要修改? 我打印了tokenizer的长度,也是49953,不知道为啥peftmodel加载的时候就49954不匹配

如果机器的显存比较紧张,可以删去脚本中的--modules_to_save ${modules_to_save} , 即不训练embed_tokens和lm_head(这两部分参数量较大),只训练LoRA参数,以节省显存 如果是在Alpaca-LoRA基础上继续微调,需要修改peft_path下的adapter_config.json文件,改为"modules_to_save": null 如果执行上一步操作后程序有报错,请删除--gradient_checkpointing \再尝试

你是这样可以解决? 我这会儿再重新检查发现,在运行scripts/run_clm_sft_with_peft.py 这个代码,指令精调的时候,中间有这个情况: WechatIMG256 这步会增加一个token,导致最终加载的时候不匹配。奇怪

kevinuserdd avatar May 25 '23 08:05 kevinuserdd

从错误信息看是词表大小不对,你贴一下完整的运行命令,并说明是具体合并的是哪个模型。

WechatIMG256 和这个有关吗? 运行scripts/run_sft.sh的时候

kevinuserdd avatar May 25 '23 08:05 kevinuserdd

从错误信息看是词表大小不对,你贴一下完整的运行命令,并说明是具体合并的是哪个模型。

WechatIMG256 和这个有关吗? 运行scripts/run_sft.sh的时候

https://github.com/ymcui/Chinese-LLaMA-Alpaca/issues/138

sszyl avatar May 25 '23 09:05 sszyl

基于Chinese-LLaMA指令微调后,如果保存了embed_tokens,合并时会比Chinese-LLaMA多一个pad token。把LoRA模型文件下面的special_tokens_map.json改一下就好了(可以直接copy Chinese-Alpaca下面的)

yyqi17 avatar May 29 '23 12:05 yyqi17

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your consideration.

github-actions[bot] avatar Jun 05 '23 22:06 github-actions[bot]

Closing the issue, since no updates observed. Feel free to re-open if you need any further assistance.

github-actions[bot] avatar Jun 08 '23 22:06 github-actions[bot]