LORA模型与基座大模型合并并由transformer的AutoModel推理

embedded/2024/10/21 0:25:22/

网上看了看很多资料都是关于Lora后的模型直接由Peft 去读取的 ,具体可以参考:LoRA 模型合并与保存 这里就不再赘述了,大概原理就是

def merge_lora_to_LLM():model_name_or_path = "your_LLM_model_path"adapter_name_or_path = "your_lora_model_path"save_path = "save_model_path"tokenizer = AutoTokenizer.from_pretrained(model_name_or_path,trust_remote_code=True)model = AutoModelForCausalLM.from_pretrained(model_name_or_path,trust_remote_code=True,low_cpu_mem_usage=True,torch_dtype=torch.float16,device_map="auto")model = PeftModel.from_pretrained(model, adapter_name_or_path)model = model.merge_and_unload()tokenizer.save_pretrained(save_path)

这里主要阐述的是Lora与LLM合并后,可以直接由Transformer的AutoModel去加载与推理。具体代码如下:

from peft import PeftModel
from transformers import AutoModel, AutoTokenizer
import os
import shutilmodel_type = "/root/ld/ld_model_pretrained/Minicpmv2_6"  # Local model path or huggingface id
path_to_adapter = "/root/ld/ld_project/minicpmv2_6/MiniCPM-V/finetune/output/output_minicpmv2_lora/checkpoint-30"  # Path to the saved LoRA adapter
merge_path = "/root/ld/ld_project/minicpmv2_6/MiniCPM-V/finetune/output/merge_minicpmv"  # Path to save the merged model# 保证原始模型的各个文件不遗漏保存到merge_path中
def copy_files_not_in_B(A_path, B_path):"""Copies files from directory A to directory B if they exist in A but not in B.:param A_path: Path to the source directory (A).:param B_path: Path to the destination directory (B)."""# 保证路径存在if not os.path.exists(A_path):raise FileNotFoundError(f"The directory {A_path} does not exist.")if not os.path.exists(B_path):os.makedirs(B_path)# 获取路径A中所有非权重文件files_in_A = os.listdir(A_path)files_in_A = set([file for file in files_in_A if not (".bin" in file or "safetensors" in file)])# List all files in directory Bfiles_in_B = set(os.listdir(B_path))# 找到所有A中存在但B中不存在的文件files_to_copy = files_in_A - files_in_B# 将这些文件复制到B路径下for file in files_to_copy:src_file = os.path.join(A_path, file)dst_file = os.path.join(B_path, file)shutil.copy2(src_file, dst_file)# 加载原始模型
model = AutoModel.from_pretrained(model_type,trust_remote_code=True
)# 加载lora模块到原始模型中
lora_model = PeftModel.from_pretrained(model,path_to_adapter,device_map="auto",trust_remote_code=True
).eval()# 将加载的lora模块合并到原始模型中
merge_model = lora_model.merge_and_unload()# 将新合并的模型进行保存
merge_model.save_pretrained(merge_path, safe_serialization=False)# 加载分词器
tokenizer = AutoTokenizer.from_pretrained(model_type, trust_remote_code=True)
tokenizer.save_pretrained(merge_path)copy_files_not_in_B(model_type,merge_path)

之后的保存在merge_path的地址就可以直接用transformer的AutoModel加载,大致如下:

model = AutoModel.from_pretrained(merge_path, trust_remote_code=True, torch_dtype=torch.bfloat16)
model = model.to(device=device)
tokenizer = AutoTokenizer.from_pretrained(merge_path, trust_remote_code=True)
model.eval()

http://www.ppmy.cn/embedded/120224.html

相关文章

【Linux实践】实验六:LINUX系统管理

【Linux实践】实验六:LINUX系统管理 实验目的实验内容实验步骤及结果1. 包管理工具2. VMware Tools3. 修改主机名4. 网络配置① 临时修改② 永久修改 5. 查找文件6. 前后台执行7. 查看进程8. 结束进程 实验目的 4、掌握Linux下软件包管理,包括命令rpm、…

理解互联网链路:从本地ISP到Tier 1 ISP运营商

1. 互联网服务提供商(ISP) 互联网服务提供商(ISP)是指提供互联网接入服务的公司或组织。它们负责将用户连接到互联网,并提供相关的服务,如电子邮件、网站托管和其他在线服务。ISP可以分为不同的层级&#…

keepalived+lvs集群

目录 一、环境 二、配置 1、master 1.在master上安装配置Keepalived 2.在master上修改配置文件 2、backup 1.在backup(192.168.229.12)上安装keepalived 2.在backup上修改配置文件 3、master和backup上启动服务 4、web服务器配置 1.web1和web…

汽车转向系统详细分析

一、汽车转向系统概述 汽车转向系统是用于改变或保持汽车行驶方向的机构,它直接影响着汽车的操纵稳定性、行驶安全性和驾驶舒适性。汽车转向系统主要由转向操纵机构、转向器和转向传动机构三大部分组成。 二、转向操纵机构 转向操纵机构主要包括转向盘、转向轴、转向…

RIP路由(已被淘汰)

一、rip 路由原理 RIP(Routing Information Protocol,路由信息协议)早期的动态路由协议,被广泛应用于TCP/IP网络中,尤其是在中小型网络中。基于距离矢量(Distance-Vector)算法来计算到达目的网络…

Redis入门第一步:认识Redis与快速安装配置

认识Redis与快速安装配置🍃 Redis是什么🐲 1.Redis的背景🎍 Redis(Remote Dictionary Server)译为"远程字典服务",它是一款基于内存实现的键值型 NoSQL 数据库, 通常也被称为数据结…

MKV转MP4丨FFmpeg的简单命令使用——视频格式转换

MKV是一种视频封装格式,很好用,也是OBS的默认推荐录制格式,因为不会突然断电关机而导致整个视频录制文件丢失。 但是MKV无法直接导入PR中剪辑,最直接的方法是将MKV转换为MP4格式,最方便且安全无损的转换方法便是用FFmp…

jmeter-Critical Section Controller逻辑控制器

场景 A接口、B接口为链式接口 B接口需要A接口的返回 A、B接口的响应时间不同 A执行完后、B接口执行、B执行完成后,再循环A接口,实现并发