LLaMA-Factory微调Qwen2.5-7B模型
本次实验环境使用的魔塔社区的机子https://i-blog.csdnimg.cn/direct/2dc2d96abc12423cb2af9c96e4f65544.png
准备LLaMA-Factory环境
git clone https://github.com/hiyouga/LLaMA-Factory.git
https://i-blog.csdnimg.cn/direct/700339dbda3c4f32bf3170a22cef4916.png
创建假造环境
一样平常的可以使用anaconda ,miniconda 这个免费的机器有限我就使用python的venv来创建假造环境
root@eais-bjmklatuw19ea8k68zju-0:/mnt/workspace# python --version
Python 3.10.14
root@eais-bjmklatuw19ea8k68zju-0:/mnt/workspace# python -m venv llama_factory https://i-blog.csdnimg.cn/direct/29963b48acbf4658a36e21352353747b.png
激活环境,下载依靠
root@eais-bjmklatuw19ea8k68zju-0:/mnt/workspace# source llama_factory/bin/activate
(llama_factory) root@eais-bjmklatuw19ea8k68zju-0:/mnt/workspace# cd LLaMA-Factory/
(llama_factory) root@eais-bjmklatuw19ea8k68zju-0:/mnt/workspace/LLaMA-Factory# pip install -e "."
Looking in indexes: https://mirrors.cloud.aliyuncs.com/pypi/simple
Obtaining file:///mnt/workspace/LLaMA-Factory
Installing build dependencies ... done
Checking if build backend supports build_editable ... done
Getting requirements to build editable ... done
Preparing editable metadata (pyproject.toml) ... done
Collecting gradio<=5.12.0,>=4.38.0
Downloading https://mirrors.cloud.aliyuncs.com/pypi/packages/7a/70/fb8611fabeb432d05946ef89d7acc6fde6c7e85ca9a05d39626b4cdf1a17/gradio-5.12.0-py3-none-any.whl (57.6 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 57.6/57.6 MB 32.9 MB/s eta 0:00:00
Collecting pydantic... source llama_factory/bin/activate #激活环境
cd LLaMA-Factory/#切换目录
pip install -e "." #下载依赖
pip install bitsandbytes>=0.39.0 #下载bitsandbytes
使用SwanLab记录实验数据
去官网创建一个账号,在假造环境登录
swanlab login
粘贴复制的API token
https://i-blog.csdnimg.cn/direct/5501c4ff08514138b4e852917280e9b7.pnghttps://i-blog.csdnimg.cn/direct/35dac0b1c65645b19085a983e446ab3f.png
下载模型
在modescope当中寻找相应的模型,这里使用ModelScope下载
https://i-blog.csdnimg.cn/direct/edb99c1d079f41a3bcc415754db06242.png
主意路径和放在/mnt/workspace/下
modelscope download --model Qwen/Qwen2.5-7B-Instruct https://i-blog.csdnimg.cn/direct/135c9a602b7c4196a011727a7ee855b9.png
下载数据集
需要的格式为Apache格式,在数据集当中寻找你需要的
https://i-blog.csdnimg.cn/direct/baadb6f56d794832841e17a1fff41f9b.png
使用什么下载都ok,末了要将数据文件存放在/mnt/workspace/LLaMA-Factory/data下
编辑训练文件
/mnt/workspace/LLaMA-Factory/examples/train_qlora/下复制一份llama3_lora_sft_awq.yaml为qwen_lora_sft_bitsandbytes.yaml然后编辑这个文件
AWQ
[*] 含义:AWQ 是 Activation-aware Weight Quantization 的缩写,是一种量化技术。
[*] 用途:用于存储经过 AWQ 量化处置惩罚的模型文件。AWQ 是一种高效的量化方法,能够在保持模型性能的同时显著减少模型大小和推理时间。
[*] 特点:AWQ 通常会根据激活值(activation)的分布动态调整权重量化策略,从而在低精度下保持较高的模型精度。
bnb_npu
[*] 含义:bnb 是 BitsandBytes 的缩写,而 npu 指的是 Neural Processing Unit(神经网络处置惩罚单位)。
[*] 用途:bnb_npu BitsandBytes 库针对 NPU 优化的量化工具或方法。
[*] 特点:
[*] BitsandBytes 是一个盛行的量化库,支持 8 位和 4 位量化。
[*] npu 表示该方法针对 NPU(如华为 Ascend、高通 Hexagon 等)举行了优化,以进步在这些硬件上的推理服从
### model
#修改为你模型的位置
model_name_or_path: /mnt/workspace/Qwen
quantization_bit: 4
# trust_remote_code: true
### method
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 8
lora_target: all
### dataset
dataset: law
template: qwen
cutoff_len: 2048
max_samples: 1000
overwrite_cache: true
preprocessing_num_workers: 16
### output
output_dir: saves/qwen2.5-7b/lora/sft
logging_steps: 10
save_steps: 500
plot_loss: true
overwrite_output_dir: true
### train
per_device_train_batch_size: 1
gradient_accumulation_steps: 8
learning_rate: 1.0e-4
num_train_epochs: 3.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
bf16: true
ddp_timeout: 180000000
# 配置swanlab ,前提要登录
use_swanlab: true
swanlab_project: llamafactory
swanlab_run_name: Qwen2.5-7B-Instruct
### eval
val_size: 0.1
per_device_eval_batch_size: 1
eval_strategy: steps
eval_steps: 500
Model 部分
[*] model_name_or_path: 指定预训练模型的路径或名称。这里路径是 /mnt/workspace/Qwen,表示模型存储在本地目次。
[*] quantization_bit: 量化位数,这里设置为 4,表示使用 4 位量化来减少模型的内存占用和计算量。
[*] trust_remote_code: 假如模型包罗自定义代码(如 Hugging Face 的 transformers 库中的自定义模型),需要设置为 true。这里被注释掉了,表示不启用。
Method 部分
[*] stage: 当前任务的阶段,sft 表示监督微调(Supervised Fine-Tuning)。
[*] do_train: 是否举行训练,设置为 true 表示启用训练。
[*] finetuning_type: 微调类型,这里使用 lora,表示使用 LoRA 技术举行微调。
[*] lora_rank: LoRA 的秩(rank),这里设置为 8。秩越低,参数目越少,但可能会影响模型性能。
[*] lora_target: 指定 LoRA 应用于哪些层,all 表示应用于所有层。
Dataset 部分
[*] dataset: 使用的数据集名称,这里为 law,可能是一个法律相关的数据集。
[*] template: 数据模板,这里为 qwen,表示使用 Qwen 模型的默认模板。
[*] cutoff_len: 输入序列的最大长度,凌驾此长度的部分会被截断。这里设置为 2048。
[*] max_samples: 最大样本数,这里设置为 1000,表示只使用数据会合的前 1000 个样本。
[*] overwrite_cache: 是否覆盖缓存,设置为 true 表示每次运行都会重新生成缓存。
[*] preprocessing_num_workers: 数据预处置惩罚的并行工作线程数,这里设置为 16。
Output 部分
[*] output_dir: 模型和日记的输出目次,这里为 saves/qwen2.5-7b/lora/sft。
[*] logging_steps: 每隔多少步记录一越日记,这里设置为 10。
[*] save_steps: 每隔多少步保存一次模型,这里设置为 500。
[*] plot_loss: 是否绘制损失曲线,设置为 true 表示启用。
[*] overwrite_output_dir: 是否覆盖输出目次,设置为 true 表示假如目次已存在则会覆盖。
Train 部分
[*] per_device_train_batch_size: 每个设备的训练批次大小,这里设置为 1。
[*] gradient_accumulation_steps: 梯度累积步数,这里设置为 8。通过累积梯度来模拟更大的批次大小。
[*] learning_rate: 学习率,这里设置为 1.0e-4。
[*] num_train_epochs: 训练的总轮数,这里设置为 3.0。
[*] lr_scheduler_type: 学习率调理器类型,这里为 cosine,表示使用余弦退火调理器。
[*] warmup_ratio: 学习率预热比例,这里设置为 0.1,表示在前 10% 的训练步骤中逐步增长学习率。
[*] bf16: 是否使用 bfloat16 精度,设置为 true 表示启用,可以减少内存占用并加快训练。
[*] ddp_timeout: 分布式数据并行(DDP)的超时时间,这里设置为 180000000 毫秒。
SwanLab 设置
[*] use_swanlab: 是否使用 SwanLab 举行实验跟踪,设置为 true 表示启用。
[*] swanlab_project: SwanLab 项目名称,这里为 llamafactory。
[*] swanlab_run_name: 当前运行的名称,这里为 Qwen2.5-7B-Instruct。
Eval 部分
[*] val_size: 验证集的比例,这里设置为 0.1,表示使用 10% 的数据作为验证集。
[*] per_device_eval_batch_size: 每个设备的验证批次大小,这里设置为 1。
[*] eval_strategy: 验证策略,这里为 steps,表示每隔肯定步数举行验证。
[*] eval_steps: 每隔多少步举行一次验证,这里设置为 500。
数据与文件对应
https://i-blog.csdnimg.cn/direct/56cb94933cc1483e9ea6bdcc2d56cea3.png
https://i-blog.csdnimg.cn/direct/d6886e27352943ce94e0c7d9761c65a4.png
这几个地方要对应上
https://i-blog.csdnimg.cn/direct/e6107b8e3497475b81fa4ee9cec6cd7c.png
训练模型
在 LLaMA-Factory 目次下,输入以下下令启动微调脚本
llamafactory-cli train examples/train_qlora/qwen_lora_sft_bitsandbytes.yaml
https://i-blog.csdnimg.cn/direct/5598ce8881f54d5ea1c784f0a6638cc9.png
https://i-blog.csdnimg.cn/direct/bbdc8d6980714e98ae08a1cf573ac59b.png
https://i-blog.csdnimg.cn/direct/4e6a5b9836ab4f90b9b9da2240417131.png
末了效果
https://i-blog.csdnimg.cn/direct/a6e5c5ffc32b4f23aed0ef4ec9fdc496.pnghttps://i-blog.csdnimg.cn/direct/936e73857b8e431db7970d39e88c8987.pnghttps://i-blog.csdnimg.cn/direct/3095502d1985455290b9c42ed2e75ef7.png
推理测试
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]