如何使用 LLaMA-Factory 微调 LLaMA3

【LLaMa3微调】使用 LLaMA-Factory 微调LLaMA3

  1. 实验环境
    1.1 机器
    • 操作系统:Windows 10 或 Ubuntu
    • PyTorch 版本:2.1.0
    • Python 版本:3.10(针对Ubuntu 22.04)
    • Cuda 版本:12.1
    • GPU 配置:p100 (16GB) * 2
    • CPU 配置:12 vCPU Intel® Xeon® Platinum 8352V CPU @ 2.10GHz

1.2 基座模型

  • 基于中文数据训练过的 LLaMA3 8B 模型:shenzhi-wang/Llama3-8B-Chinese-Chat

  • 可选配置:hf 国内镜像站,使用以下命令安装和配置:

    bash 复制代码
    pip install -U huggingface_hub
    export HF_ENDPOINT=https://hf-mirror.com
    huggingface-cli download --resume-download shenzhi-wang/Llama3-8B-Chinese-Chat --local-dir /root/autodl-tmp/models/Llama3-8B-Chinese-Chat1
  1. LLaMA-Factory 框架

2.1 安装

  • 使用以下命令克隆并安装LLaMA-Factory:

    bash 复制代码
    git clone https://github.com/hiyouga/LLaMA-Factory.git
    cd LLaMA-Factory
    pip install -e .

2.2 准备训练数据

  • 训练数据示例:fruozhiba_qaswift_train.json,可以去魔搭社区下载

  • 将训练数据放置在 LLaMA-Factory/data/ 目录下,例如:LLaMA-Factory/data/ruozhiba_qaswift_train.json

  • 修改数据注册文件:LLaMA-Factory/data/dataset_info.json,添加数据集的配置信息,例如:

    json 复制代码
    "fintech": {
      "file_name": "ruozhiba_qaswift_train.json",
      "columns": {
        "prompt": "instruction",
        "query": "input",
        "response": "output",
        "history": "history"
      }
    }

2.3 启动 Web UI

在 LLaMA-Factory 目录下执行以下命令启动 Web UI:
bash cd LLaMA-Factory llamafactory-cli webui

2.4 微调模型

使用 Web UI 或命令行进行微调。

命令行微调示例:构建配置文件 cust/train_llama3_lora_sft.yaml,内容示例如下:

复制代码
 cutoff_len: 1024
 dataset: ruozhiba_qaswift_train
 dataset_dir: data
 do_train: true
 finetuning_type: lora
 flash_attn: auto
 fp16: true
 gradient_accumulation_steps: 8
 learning_rate: 0.0002
 logging_steps: 5
 lora_alpha: 16
 lora_dropout: 0
 lora_rank: 8
 lora_target: q_proj,v_proj
 lr_scheduler_type: cosine
 max_grad_norm: 1.0
 max_samples: 1000
 model_name_or_path: /root/autodl-tmp/models/Llama3-8B-Chinese-Chat
 num_train_epochs: 10.0
 optim: adamw_torch
 output_dir: saves/LLaMA3-8B-Chinese-Chat/lora/train_XXXX-XX-XX-XX-XX-XX
 packing: false
 per_device_train_batch_size: 2
 plot_loss: true
 preprocessing_num_workers: 16
 report_to: none
 save_steps: 100
 stage: sft
 template: llama3
 use_unsloth: true
 warmup_steps: 0

使用以下命令执行微调

复制代码
 ```bash
 llamafactory-cli train cust/train_llama3_lora_sft.yaml
 ```
相关推荐
赵庆明老师7 小时前
RH7.6安装 llama.cpp(普通用户,无sudo权限)
llama
七牛云行业应用9 小时前
llama.cpp 本地部署完全指南:从安装到 OpenAI 兼容 API 服务
人工智能·大模型·llama
DO_Community11 小时前
AI 应用成本怎么算?从推理费用到完整 TCO 拆解
人工智能·gpt·agent·ai编程·llama·kimi
bosins11 小时前
Ubuntu 24.04部署llama.cpp下载资源一直中断的解决方案
ubuntu·eureka·llama
苏打水com1 天前
《llama.cpp Server实战:把本地模型变成OpenAI兼容接口》
llama
苏打水com2 天前
《llama.cpp从零编译教程:Windows + Linux完整环境搭建》
linux·windows·llama
维核科技4 天前
Llama 3.3 vs Qwen2.5 vs DeepSeek-R1
ai·私有化部署·llama·大模型部署·私有化大模型部署
AI78407 天前
开源模型改写AI格局:Llama、通义千问、Mistral如何挑战闭源巨头
人工智能·开源·llama
leoZ2318 天前
本地跑大模型实战(七):llama.cpp 性能调优,让推理更快更省
java·人工智能·spring·生成对抗网络·语言模型·自然语言处理·llama
leoZ2318 天前
实战复盘:用 Claude Code 从零搭一个 GitHub PR 统计工具
java·人工智能·python·深度学习·自然语言处理·github·llama