当前位置: 首页 > news >正文

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

网上看了看很多资料都是关于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()

相关文章:

  • 大模型增量训练--基于transformer制作一个大模型聊天机器人
  • AndroidStudio导入so文件
  • ESP32和ESP8266下载方法及下载失败排查
  • Git忽略规则原理和.gitignore文件不生效的原因和解决办法
  • pytorch线性/非线性回归拟合
  • Leetcode 3302. Find the Lexicographically Smallest Valid Sequence
  • 数据库中的表添加uuid字段
  • spring 实用小技巧
  • 编程题 7-12 两个数的简单计算器【PAT】
  • Linux:磁盘管理
  • ps aux | grep smart_webrtc这条指令代表什么意思
  • SQLite3模块使用详解
  • 【Android 14源码分析】Activity启动流程-1
  • 大数据复习知识点5
  • linux服务器部署filebeat
  • php的引用
  • CentOS 7 防火墙操作
  • ComponentOne 2017 V2版本正式发布
  • CSS居中完全指南——构建CSS居中决策树
  • JavaScript的使用你知道几种?(上)
  • javascript数组去重/查找/插入/删除
  • JavaWeb(学习笔记二)
  • js如何打印object对象
  • Vue组件定义
  • 闭包--闭包之tab栏切换(四)
  • 程序员最讨厌的9句话,你可有补充?
  • 每个JavaScript开发人员应阅读的书【1】 - JavaScript: The Good Parts
  • 容器化应用: 在阿里云搭建多节点 Openshift 集群
  • 深度学习入门:10门免费线上课程推荐
  • 实战|智能家居行业移动应用性能分析
  • 写给高年级小学生看的《Bash 指南》
  • 智能网联汽车信息安全
  • Play Store发现SimBad恶意软件,1.5亿Android用户成受害者 ...
  • 曜石科技宣布获得千万级天使轮投资,全方面布局电竞产业链 ...
  • 移动端高清、多屏适配方案
  • ​插件化DPI在商用WIFI中的价值
  • #每天一道面试题# 什么是MySQL的回表查询
  • $(function(){})与(function($){....})(jQuery)的区别
  • (Demo分享)利用原生JavaScript-随机数-实现做一个烟花案例
  • (ISPRS,2023)深度语义-视觉对齐用于zero-shot遥感图像场景分类
  • (Redis使用系列) Springboot 使用redis实现接口幂等性拦截 十一
  • (含笔试题)深度解析数据在内存中的存储
  • (力扣)1314.矩阵区域和
  • (十八)三元表达式和列表解析
  • (十六)视图变换 正交投影 透视投影
  • (十五)、把自己的镜像推送到 DockerHub
  • (十五)Flask覆写wsgi_app函数实现自定义中间件
  • (实测可用)(3)Git的使用——RT Thread Stdio添加的软件包,github与gitee冲突造成无法上传文件到gitee
  • (四十一)大数据实战——spark的yarn模式生产环境部署
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转) RFS+AutoItLibrary测试web对话框
  • .axf 转化 .bin文件 的方法
  • .net dataexcel winform控件 更新 日志
  • .net Stream篇(六)
  • .NETCORE 开发登录接口MFA谷歌多因子身份验证