大模型训练: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
相关推荐
忘记57816 小时前
下载llama factory
llama
路边草随风16 小时前
llama_index简单使用
人工智能·python·llama
王威振的csdn1 天前
法律大模型微调:基于 LLaMA-Factory 的指令微调方案
llama
java_logo2 天前
LANGFUSE Docker 容器化部署指南
运维·docker·云原生·容器·eureka·llama
weixin_446260852 天前
二、LLaMA Factory 介绍和基本使用
llama
weixin_446260853 天前
三、LLaMA Factory 微调通用设置
llama
木枷4 天前
LLama-factory数据报错
人工智能·机器学习·llama
快乐的钢镚子4 天前
【RAG实战】中医医疗问答系统
langchain·llama
Yeliang Wu6 天前
LLaMA-Factory 模型评估理论与实战:基于 Ubuntu 22.04 的系统化指南
linux·ubuntu·llama·评估·llamafactory