大模型训练: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
相关推荐
DigitalOcean3 小时前
DigitalOcean Gradient AI 推理云平台原生集成 LlamaIndex
llama
ECHO飞跃 0126 天前
Unity2019 本地推理 通义千问0.5-1.5B微调导入
人工智能·深度学习·unity·llama
黑白极客6 天前
ACP大模型认证刷题工具开源,助力高效备考
java·ai·github·llama·认证
迷之程序员6 天前
llama-cpp-python用法,模型加载gpu踩坑全记录
开发语言·python·llama
~kiss~7 天前
Ollama 底层的 llama.cpp 和 GGUF
llama
小雨中_8 天前
4.1 Megatron-LM:千卡级集群预训练的“硬核”框架
人工智能·python·深度学习·机器学习·llama
重生之我要成为代码大佬9 天前
AI框架设计与选型
人工智能·langchain·大模型·llama·qwen
小雨中_9 天前
4.1 LLaMA 系列:从 LLaMA-1 到 LLaMA-3
人工智能·python·深度学习·机器学习·自然语言处理·llama
l1t11 天前
DeepSeek总结的llama.cpp使用说明
llama
爱跑步的程序员~14 天前
SpringBoot集成SpringAI与Ollama本地大模型
java·后端·spring·ai·llama·springai