在Windows上用Llama Factory微调Llama 3的基本操作

这篇博客参考了一些文章,例如:教程:利用LLaMA_Factory微调llama3:8b大模型_llama3模型微调保存-CSDN博客

也可以参考Llama Factory的Readme:GitHub - hiyouga/LLaMA-Factory: Unify Efficient Fine-Tuning of 100+ LLMsUnify Efficient Fine-Tuning of 100+ LLMs. Contribute to hiyouga/LLaMA-Factory development by creating an account on GitHub.https://github.com/hiyouga/LLaMA-Factory?tab=readme-ov-file#installation首先将Llama Factory clone到本地:GitHub - hiyouga/LLaMA-Factory: Unify Efficient Fine-Tuning of 100+ LLMs

其次创建一个conda环境:

python 复制代码
conda create -n llama_factory python=3.10

激活环境后首先安装pytorch,具体参考这个页面:Start Locally | PyTorch,例如:

bash 复制代码
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

而后进入到LLaMA-Factory文件夹,参考其Readme,运行:

bash 复制代码
pip install -e .[torch,metrics]

同时,按照其Readme,在Windows系统上还需要运行:

bash 复制代码
pip install https://github.com/jllllll/bitsandbytes-windows-webui/releases/download/wheels/bitsandbytes-0.41.2.post2-py3-none-win_amd64.whl

具体原因我就不展开讲了。然后依次运行:

bash 复制代码
Set CUDA_VISIBLE_DEVICES=0
Set GRADIO_SHARE=1
llamafactory-cli webui

就可以看到其webui了。不过这时候还没有模型参数文件,对于国内用户而言,可以在这里https://modelscope.cn/organization/LLM-Researchhttps://modelscope.cn/organization/LLM-Research

进行下载,例如可以下载Llama3中文版本(如果没有git lfs可以用前两个命令安装):

bash 复制代码
conda install git-lfs
git-lfs install
git lfs clone https://www.modelscope.cn/LLM-Research/Llama3-8B-Chinese-Chat.git

下载好之后,可以构造自己的微调数据集,具体而言,按照这里的介绍:

https://github.com/hiyouga/LLaMA-Factory/tree/main/data

Llama Factory支持alpaca and sharegpt的格式,前者类似于这种格式:

复制代码
[
  {
    "instruction": "human instruction (required)",
    "input": "human input (optional)",
    "output": "model response (required)",
    "system": "system prompt (optional)",
    "history": [
      ["human instruction in the first round (optional)", "model response in the first round (optional)"],
      ["human instruction in the second round (optional)", "model response in the second round (optional)"]
    ]
  }
]

我们构造数据集的时候,最简单的方法就是只构造instruction和output。把生成的json文件放到LLaMA-Factory\data目录下,然后打开dataset_info.json文件,增加这个文件名记录即可,例如我这里增加:

"private_train": {

"file_name": "private_train.json"

},

选择自己的私有数据集,可以预览一下,然后就可以开始训练了。

训练完成后切换到Export,然后在上面的"微调方法"------"检查点路径"中选择刚才存储的目录Train_2024_xxxx之类,然后指定导出文件的目录,然后就可以导出了。

导出之后我们可以加载微调之后的模型并测试了。当然,如果训练数据集比较小的话,测试的效果也不会太好。如果大家只是想对微调效果和特定问题进行展示,可以训练模型到过拟合,呵呵呵。

就记录这么多。

相关推荐
CHPCWWHSU1 天前
初识llama.cpp - 轻量级推理引擎
llm·llama·cpp·cudatoolkit
l1t1 天前
在ARM64 KyLin计算机上安装llama.cpp
大数据·llama·kylin
l1t1 天前
在华为arm64 kylin计算机上安装docker编译llama.cpp的步骤
华为·docker·llama·kylin
小超同学你好2 天前
Transformer 12. LLaMA 架构介绍以及与 Transformer 架构对比
人工智能·语言模型·transformer·llama
l1t2 天前
在Windows11利用llama.cpp调用Qwen3.5量化模型测试
人工智能·llama·qwen
HyperAI超神经2 天前
在线教程丨免费CPU资源快速部署,覆盖Qwen3.5/DeepSeek-R1/Gemma 3/Llama 3.2等热门开源模型
人工智能·深度学习·学习·机器学习·ai编程·llama·vllm
乾元2 天前
本地大模型:如何在内网部署 Llama/Qwen 等安全增强模型
运维·网络·人工智能·安全·机器学习·llama·安全架构
jialan752 天前
llama-cli 核心命令详解
llama
Thomas.Sir2 天前
Qwen2:阿里开源大模型理论剖析以及本地部署实战
开源·llama·qwen2·ai 大模型
l1t2 天前
在Android设备上利用Termux安装llama.cpp并启动webui
android·llama