本文将介绍如下内容:
- 一、Lora 合并的环境依赖
- 二、Lora 合并代码实现
一、Lora 合并的环境依赖
autoawq 0.2.5
peft 0.14.0
transformers 4.46.3
二、Lora 合并代码实现
import torch
from peft import PeftModel
from transformers import AutoTokenizer, AutoModelForCausalLM, LlamaTokenizer
from transformers.generation.utils import GenerationConfig
def apply_lora(model_name_or_path, output_path, lora_path):
print(f"Loading the base model from {model_name_or_path}")
base_tokenizer = AutoTokenizer.from_pretrained(model_name_or_path, use_fast=False, trust_remote_code=True)
# base = AutoModelForCausalLM.from_pretrained(model_name_or_path, torch_dtype=torch.bfloat16, trust_remote_code=True)
base = AutoModelForCausalLM.from_pretrained(model_name_or_path, torch_dtype=torch.float32, trust_remote_code=True)
print(f"Loading the LoRA adapter from {lora_path}")
lora_model = PeftModel.from_pretrained(
base,
lora_path,
torch_dtype=torch.float32,
)
print("Applying the LoRA")
model = lora_model.merge_and_unload()
print(f"Saving the target model to {output_path}")
model.save_pretrained(output_path)
base_tokenizer.save_pretrained(output_path)
if __name__ == "__main__":
model_name_or_path = "/nasdata/zhanjie/models/Qwen2.5-32B-Instruct"
output_path = "./output"
lora_path = "/nasdata/zhanjie/models/qwen2_5-32B-lora-1-0-7"
apply_lora(model_name_or_path, output_path, lora_path)
print("Done")