大模型训练:LLaMA-Factory快速上手

第1步:安装环境

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

# 推荐使用 conda
conda create -n llama-factory python=3.10
conda activate llama-factory

pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install -r requirements.txt
pip install -e ".[torch,metrics]"

⚠️ 注意:推荐使用 CUDA 环境,GPU 显存建议 ≥24GB(如 3090/4090)


第2步:准备数据

支持多种格式,最常用的是 Alpaca 格式 JSON 文件

json 复制代码
[
  {
    "instruction": "写一首关于春天的诗",
    "input": "",
    "output": "春风拂面花自开,柳绿桃红映山川..."
  },
  {
    "instruction": "解释什么是机器学习",
    "input": "",
    "output": "机器学习是让计算机从数据中自动学习规律的技术..."
  }
]

📌 存为 my_data.json,放在 data/ 目录下。


第3步:启动训练-多GPU(以 LoRA 微调 Qwen-7B 为例)

参考examples/accelerate 下面的文件,准备自己的yaml文件

c 复制代码
compute_environment: LOCAL_MACHINE
debug: false
distributed_type: MULTI_GPU
downcast_bf16: 'no'
gpu_ids: 0,1,2,3
machine_rank: 0
main_training_function: main
mixed_precision: fp16
num_machines: 1
num_processes: 4
rdzv_backend: static
same_network: true
tpu_env: []
tpu_use_cluster: false
tpu_use_sudo: false
use_cpu: false
main_process_port: 29503

启用多GPU训练

bash 复制代码
#!/bin/bash

CUDA_VISIBLE_DEVICES=4,5,6,7 accelerate launch --config_file config.yaml ../src/train.py \
    --stage sft \
    --do_train True\
    --template qwen3 \
    --finetuning_type lora \
    --model_name_or_path ../Qwen/Qwen3-0.6B \
    --dataset_dir ./vehicle_control_dataset \
    --dataset my_data \
    --output_dir ./saves \
    --overwrite_cache \
    --overwrite_output_dir \
    --cutoff_len 1024 \
    --per_device_train_batch_size 1 \
    --per_device_eval_batch_size 1 \
    --gradient_accumulation_steps 8 \
    --lr_scheduler_type cosine \
    --max_grad_norm 1.0 \
    --logging_steps 10 \
    --save_steps 200 \
    --warmup_steps 0 \
    --learning_rate 5e-5 \
    --num_train_epochs 3.0 \
    --max_samples 100000 \
    --fp16 True \
    --lora_rank 8 \
    --lora_dropout 0.1 \
    --lora_target all \
    --plot_loss True
相关推荐
忧郁的橙子.3 天前
07-大模型微调-LLama Factor微调Qwen -- 局部微调/训练医疗问答模型
llama·llama factor·微调qwen
南宫乘风4 天前
LLaMA-Factory 给 Qwen1.5 做 LoRA 微调 实战
人工智能·深度学习·llama
华农DrLai4 天前
什么是自动Prompt优化?为什么需要算法来寻找最佳提示词?
人工智能·算法·llm·nlp·prompt·llama
jjinl4 天前
1.1 llama.cpp 编译
llama
serve the people4 天前
macbook m4 LLaMA-Factory入门级微调
llama
WiSirius5 天前
LLM:基于 AgentScope + Streamlit 的 AI Agent脑暴室
人工智能·深度学习·自然语言处理·大模型·llama
掘金安东尼6 天前
llama.cpp、Ollama、LM Studio:背后是谁在做?为什么会出现?要什么机器才能跑?
llama
海天一色y6 天前
LLaMA-Factory PPO 训练实战:从 SFT 到 RLHF 完整指南
llama
接着奏乐接着舞。6 天前
5分钟本地跑起大模型
人工智能·llama
liuze4086 天前
Ollama安装
llama