在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之类,然后指定导出文件的目录,然后就可以导出了。

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

就记录这么多。

相关推荐
大模型教程1 天前
小白学大模型:从零搭建LLaMA
程序员·llm·llama
Jina AI3 天前
让 llama.cpp 支持多模态向量模型
llama
wyw00003 天前
大模型微调之LLaMA-Factory实战
llama
2202_756749693 天前
LLM大模型-大模型微调(常见微调方法、LoRA原理与实战、LLaMA-Factory工具部署与训练、模型量化QLoRA)
人工智能·深度学习·llama
JoannaJuanCV3 天前
大模型训练框架:LLaMA-Factory框架
llama·大模型训练·llama factory
骑士9991116 天前
llama_factory 安装以及大模型微调
llama
周小码6 天前
llama-stack实战:Python构建Llama应用的可组合开发框架(8k星)
开发语言·python·llama
blackoon889 天前
DeepSeek R1大模型微调实战-llama-factory的模型下载与训练
llama
johnny2339 天前
大模型微调理论、实战:LLaMA-Factory、Unsloth
llama
闲看云起9 天前
从 GPT 到 LLaMA:解密 LLM 的核心架构——Decoder-Only 模型
gpt·架构·llama