大模型训练: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
相关推荐
SLD_Allen9 小时前
Purple Llama:Meta开源的LLM安全“紫队”工具箱
安全·开源·llama
俊俊谢15 小时前
从零搭建:本地LangChain Agent调用远程LLaMA-Factory模型服务
langchain·llama
liming49516 小时前
Ubuntu + Docker + NVIDIA 显卡 上部署 Ollama
llama
heroboyluck2 天前
AI工程师第四课 - 深度学习入门
人工智能·python·深度学习·llama
Token炼金师4 天前
引擎四强:vLLM、SGLang、TensorRT-LLM 与 llama.cpp —— 推理引擎选型对决
人工智能·llm·llama·vllm·tensorrt-llm·sglang
尼米棕熊5 天前
大模型学习8上-推理部署框架llama.cpp与Ollama使用指北
学习·llama
Day(AKA Elin)5 天前
【Day】MTP(Multi Token Prediction)技术学习
python·深度学习·学习·llama
染指11106 天前
50.llama_index-文档分割器(文本)
llama·rag·llamaindex
Scabbards_8 天前
经典大语言模型框架整理(gpt/llama/chatglm/qwen/deepseek)
gpt·语言模型·llama
染指11109 天前
46.llama_index-提示词模板(富提示词模板、jinja2静态和动态模板)
llama·rag·llamaindex