liguodongiot / llm-action

本项目旨在分享大模型相关技术原理以及实战经验(大模型工程化、大模型应用落地)
https://www.zhihu.com/column/c_1456193767213043713
Apache License 2.0
9.95k stars 979 forks source link

模型不在huggingface中本地的预训练模型如何微调 #22

Closed c-pupil closed 3 months ago

c-pupil commented 3 months ago

您好,首先感谢您出色的工作和讲解,另外我有下面的问题希望您可以解惑: 我自己从本地中加载已训练好的模型

model=Mymodel()
model.load_state_dict(torch.load(args.net_path))

config = LoraConfig(
    r=16,
    lora_alpha=16,
    target_modules=["qkv", "proj"],
    lora_dropout=0.1,
    bias="none",
)
model = get_peft_model(model, config)
model.train()

请问在训练的过程中mode.train()之后不在target_modules的其他层的权重会自动冻结么,还有这个保存权重和推理时adapter以及在pth中冻结的权重如何加载。是通过以下方式么(PeftModel.from_pretrained是会将pth中微调的权重自动覆盖掉是么)

model.evlal()
model.load_state_dict(torch.load(args.net_path))
model = PeftModel.from_pretrained(model, peft_model_id)

如果能得到您的回复将不胜感激。

liguodongiot commented 3 months ago

会自动冻结。

lora 推理不合并权重的话,类似这样加载即可。

 model = LlamaForCausalLM.from_pretrained(
            base_model,
            load_in_8bit=load_8bit,
            torch_dtype=torch.float16,
            device_map="auto",
        )
        model = PeftModel.from_pretrained(
            model,
            lora_weights,
            torch_dtype=torch.float16,
        )