ymcui / Chinese-LLaMA-Alpaca

中文LLaMA&Alpaca大语言模型+本地CPU/GPU训练部署 (Chinese LLaMA & Alpaca LLMs)
https://github.com/ymcui/Chinese-LLaMA-Alpaca/wiki
Apache License 2.0
17.98k stars 1.84k forks source link

关于该项目在zero3模式下lora权重保存存在问题的情况(已查明) #835

Closed xiaoqi25478 closed 10 months ago

xiaoqi25478 commented 11 months ago

提交前必须检查以下项目

问题类型

模型训练与精调

基础模型

LLaMA-13B

操作系统

Linux

详细描述问题

作者自己写了一个SavePeftModelCallback中断函数来实现lora部分的权重保存,这个函数在zero2模式下是没有问题的,因为zero2模式不涉及到模型拆分到不同的gpu或者cpu上,保存的权重参数其状态都是可获取的,但是在zero3模式下,模型的权重会被拆分到不同的gpu以及offload到cpu上,该模式下直接强制的保存权重参数会存在一些问题,如issues广泛反馈的zero3模式下adapter_model.bin文件只有几十k的问题,因此此时获取的权重文件只有key,没有value。

class SavePeftModelCallback(transformers.TrainerCallback):
    def save_model(self, args, state, kwargs):
        if state.best_model_checkpoint is not None:
            checkpoint_folder = os.path.join(state.best_model_checkpoint, "sft_lora_model")
        else:
            checkpoint_folder = os.path.join(args.output_dir, f"{PREFIX_CHECKPOINT_DIR}-{state.global_step}")

        peft_model_path = os.path.join(checkpoint_folder, "sft_lora_model")
        kwargs["model"].save_pretrained(peft_model_path)
        kwargs["tokenizer"].save_pretrained(peft_model_path)

    def on_save(self, args, state, control, **kwargs):
        self.save_model(args, state, kwargs)
        return control

    def on_train_end(self, args, state, control, **kwargs):
        peft_model_path = os.path.join(args.output_dir, "sft_lora_model")
        kwargs["model"].save_pretrained(peft_model_path)
        kwargs["tokenizer"].save_pretrained(peft_model_path)

其中transformers里面有自带的针对zero3模式下的权重保存代码,如下: image 只不过此时保存的权重是所有的参数,不仅仅是lora权重。 在项目的merge_llama_with_chinese_lora_low_mem.py代码中,会对base_model当中的参数用lora_model当中的参数进行优先替换,因此不需要涉及到merge_llama_with_chinese_lora_low_mem.py的修复。 因此本项目如果需要支持zero3的话,需要对模型权重保存的方式进行修复。修复的方式也很简单,注释掉作者自己添加的SavePeftModelCallback函数,直接使用官方的权重保存函数即可。

依赖情况(代码类问题务必提供)

# 请在此处粘贴依赖情况

运行日志或截图

# 请在此处粘贴运行日志
zhengsipeng commented 11 months ago

但是保存完之后要怎么再load回去呢?

xiaoqi25478 commented 11 months ago

但是保存完之后要怎么再load回去呢?

保存后的权重带有lora权重 需要merge merge过后的权重就是正常的权重

github-actions[bot] commented 10 months ago

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] commented 10 months ago

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