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

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

就记录这么多。

相关推荐
小草cys1 天前
在树莓派集群上部署 Distributed Llama (Qwen 3 14B) 详细指南
python·llama·树莓派·qwen
咕咚-萌西1 天前
联邦学习论文分享:Towards Building the Federated GPT:Federated Instruction Tuning
llama·联邦学习·指令微调
relis3 天前
解密llama.cpp中的batch与ubatch:深度学习推理优化的内存艺术
深度学习·batch·llama
relis3 天前
解密llama.cpp:Prompt Processing如何实现高效推理?
prompt·llama
GEO_JYB4 天前
BERT家族进化史:从BERT到LLaMA,每一次飞跃都源于对“学习”的更深理解
学习·bert·llama
AI大模型4 天前
大模型开发实战:使用 LLaMA Factory 微调与量化模型并部署至 Ollama
程序员·llm·llama
relis4 天前
解密llama.cpp:从Prompt到Response的完整技术流程剖析
prompt·llama
我不是小upper5 天前
Llama v3 中的低秩自适应 (LoRA)
llama
df007df11 天前
【RAGFlow代码详解-10】文本处理和查询处理
人工智能·ocr·embedding·llama
liliangcsdn11 天前
llama.cpp reranking源码分析
llama