swift自定义数据集微调Qwen-7B大模型,转换模型后使用ollama跑起来

server/2024/10/11 11:22:07/

前文:swift微调Qwen-7B大模型-CSDN博客

我详细介绍了swift如何进行微调,但数据集均来自魔搭社区,如何想训练自定义数据集,实际上也很简单。

一、自定义数据集微调

export MKL_THREADING_LAYER=GNU \ 
CUDA_VISIBLE_DEVICES=0,1,2,3 \
NPROC_PER_NODE=4 \
swift sft --model_type qwen2-7b-instruct \--model_id_or_path /root/.cache/modelscope/hub/qwen/Qwen2-7B-Instruct \--sft_type lora \--dtype AUTO \--dataset AI-ModelScope/alpaca-gpt4-data-zh#200 \--self_cognition_sample 3000 \--model_name 阿盛 Master Coder \--model_author 盛世芳华 LLM_ROME \--num_train_epochs 1 \--lora_rank 8 \--lora_alpha 32 \--lora_dropout_p 0.05 \--lora_target_modules ALL \--gradient_checkpointing true \--batch_size 1 \--weight_decay 0.1 \--learning_rate 1e-4 \--gradient_accumulation_steps 16 \--output_dir output

微调时,只需指定--dataset为本地csv文件路径即可,csv文件的格式如下:

instruction是问题,input大概能理解为问题背景,output为答案,数据集准备好以后就可以直接进行训练。 

参考:ms-swift/docs/source/LLM/自定义与拓展.md at main · modelscope/ms-swift (github.com)

前文在训练时很慢,原因是就用了一张卡,多卡训练时一定要记着加上:

export MKL_THREADING_LAYER=GNU \ 
CUDA_VISIBLE_DEVICES=0,1,2,3 \
NPROC_PER_NODE=4

二、推理

CUDA_VISIBLE_DEVICES=0,1,2,3 swift infer --ckpt_dir output/qwen2-7b-instruct/v2-20240826-101129/checkpoint-48

常识用CLI推理,问问它:“贝贝是谁”,可以正确回答出我的答案,说明微调生效。

三、合并Lora

CUDA_VISIBLE_DEVICES=0,1,2,3 swift export --ckpt_dir output/qwen2-7b-instruct/v2-20240826-101129/checkpoint-48 --merge_lora true

执行后,生成./output/qwen2-7b-instruct/v2-20240826-101129/checkpoint-48-merged目录

包含了模型文件。

四、安装ollama

大模型的朋友都认识ollama,它的好处不言而喻,想要把合并后的模型跑在ollama中,就需要将模型转换为ollama需要的模型格式。

curl -fsSL https://ollama.com/install.sh | sh

安装成功后,启动ollama服务

ollama serve

五、下载ollama、llm源码

之所以需要下载ollama的源码,是因为需要源码编译来进行模型的转换。

git clone https://github.com/ollama/ollama.git
cd ollama
git submodule init
git submodule update llm/llama.cpp

5.1安装环境依赖

python -m venv llm/llama.cpp/.venv
source llm/llama.cpp/.venv/bin/activate
pip install -r llm/llama.cpp/requirements.txt

5.2构建量化工具

cd llm/llama.cpp/
make

可参考:llama.cpp/docs/build.md at 1e6f6554aa11fa10160a5fda689e736c3c34169f · ggerganov/llama.cpp (github.com)

编译成功后,会在目录下看到很多工具:

如果编译时报错,请先安装:

apt install cmake
apt install ccache

六、模型转化

用convert-hf-to-gguf.py 转换模型:

python ../ollama/llm/llama.cpp/convert_hf_to_gguf.py output/qwen2-7b-instruct/v2-20240826-101129/checkpoint-48-merged --outtype f16 --outfile converted.bin

其中output/qwen2-7b-instruct/v2-20240826-101129/checkpoint-48-merged就是lora合并后的文件夹路径,--outtype f16是不损失精度,--outfile converted.bin是转换后的文件名。

结束后,得到了converted.bin文件,大小14.2G

七、模型量化

一个7b的模型,12.4G还是有点大,使用模型量化工具进行量化,这里我使用4比特量化。

../ollama/llm/llama.cpp/llama-quantize converted.bin quantized.bin q4_0

最终得到quantized.bin,文件大小4.1G。

八、构建ollama

ollama包可以理解为Dockfile,创建Modelfile文件,文件内容:

FROM quantized.bin# set the temperature to 0.7 [higher is more creative, lower is more coherent]
PARAMETER temperature 0.7
PARAMETER top_p 0.8
PARAMETER repeat_penalty 1.05
TEMPLATE """{{ if .System }}<|im_start|>system
{{ .System }}<|im_end|>
{{ end }}{{ if .Prompt }}<|im_start|>user
{{ .Prompt }}<|im_end|>
{{ end }}<|im_start|>assistant
{{ .Response }}<|im_end|>"""
# set the system message
SYSTEM """
You are a helpful assistant.
"""

最关键的就是第一句:FROM quantized.bin(文件名一定要和你量化后的文件名对的上)

九、构建部署包

ollama create test2 -f Modelfile

然后ollama看一下当前有哪些模型:

ollama list

test1是之前我创建的未经量化的模型,test2是量化后的。

十、运行模型

ollama run test2

速度很快,一切正常。

参考:

Ollama 构建自定义模型包 - 简书 (jianshu.com)

ollama 使用自定义大模型_ollama 自定义模型-CSDN博客

什么是Base模型?什么是chat模型?什么是instruct?什么是4Bit?_instruct模型和base模型的区别-CSDN博客 将 HuggingFace 模型转换为 GGUF 及使用 ollama 运行 —— 以 Qwen2-0.5B 为例_qwen2 gguf-CSDN博客

Ollama运行qwen2:7b 输出乱码_ollama run qwen2:7b-CSDN博客

ollama运行qwen2:7b一直输出大写字母G ·问题 #485 ·QwenLM/Qwen2 (github.com)

Ollama创建微调模型_ollama微调模型-CSDN博客

Ollama 导入模型指南 (zhihu.com)

基于SWIFT微调专属于自己的大模型 - 知乎 (zhihu.com)


http://www.ppmy.cn/server/107671.html

相关文章

一键编译QT5源码脚本(交叉编译arm64、mips64版本)

前言 这几天为了编写国产专用机上的软件&#xff0c;又盘起了交叉编译.. 一开始想使用深度最新的deepin 23正式版做系统&#xff0c;搭建编译环境。然而交叉编译链工具直接安装失败&#xff01; 然后又装了Debian12原版系统&#xff0c;编译环境倒是顺利搭建起来&#xff0c…

39-nacos eureka zookeeper区别

Nacos, Eureka, Zookeeper都是服务发现和配置管理的工具&#xff0c;但是它们之间有一些区别&#xff1a; Nacos 设计目标是以更简单的方式来实现服务发现和配置管理。 支持服务发现和服务元数据的注册与发现。 支持DNS-based服务发现。 支持RPC和服务间调用。 支持配置的…

在 Spring Boot 中为 MyBatis 添加拦截器

在 Spring Boot 中为 MyBatis 添加拦截器以实现分表查询涉及几个步骤。以下是如何在 Spring Boot 应用中配置和使用 MyBatis 拦截器的指南&#xff0c;具体以分表查询为例&#xff1a; 创建拦截器 首先&#xff0c;定义一个自定义的 MyBatis 拦截器&#xff0c;实现 Intercept…

2024最新FL Studio24.1.1.4285破解版中文安装包百度云网盘下载地址

大家好&#xff0c;今天我要给大家介绍一款音乐制作神器——FL Studio 24.1.1.4285中文版。这款软件可是音乐制作界的翘楚&#xff0c;无论是专业人士还是音乐爱好者&#xff0c;都会为它的强大功能和易用性所折服。 我们来看看FL Studio的特点。 这是一款全能型的音乐工作站&…

Spring Cloud全解析:网关之GateWay断言

GateWay断言 断言Predicate gateWay网关中提供了多种断言方式 After断言 Loaded RoutePredicateFactory [After] After匹配在当前日期时间之后发生的请求 spring:cloud:gateway:routes:- id: after_routeuri: https://example.orgpredicates:- After2021-09-06T16:02:25.…

SpringBoot下获取resources目录下文件的常用方法

哈喽&#xff0c;大家好&#xff0c;今天给大家带来SpringBoot获取resources目录下文件的常用方法&#xff0c;示例中的方法是读取resources目录下的txt和xlsx文件&#xff0c;并将xlsx导出到excel的简单写法。完整代码放在最后。 通过this.getClass()方法获取 method1 - met…

Verilog刷题笔记60

题目&#xff1a; Exams/2013 q2bfsm Consider a finite state machine that is used to control some type of motor. The FSM has inputs x and y, which come from the motor, and produces outputs f and g, which control the motor. There is also a clock input called …

【YOLOv8改进[Conv]】 感受野注意力卷积RFAConv(2024.3)| 使用RFAConv改进C2f + 含全部代码和详细修改方式

本文将进行在YOLOv8中使用 感受野注意力卷积RFAConv改进C2f 的实践,助力YOLOv8目标检测效果,文中含全部代码、详细修改方式。助您轻松理解改进的方法。