LLaMA-Factory 简介
LLaMA-Factory 是一个基于 transformers 库开发的训练、微调、推理一体化平台。它支持预训练、指令监督微调(SFT)、奖励模型训练以及 PPO、DPO、KTO、ORPO 等多种训练范式,并兼容 Accelerate 或 DeepSpeed 作为后端加速。
其核心优势在于强大的数据处理与配置能力。只要环境配置得当,直接运行脚本即可完成大部分工作,极大降低了大模型微调的门槛。
环境搭建
容器化部署
推荐使用 Docker 快速构建环境,避免依赖冲突:
git clone https://github.com/hiyouga/LlamaFactory.git
cd LlamaFactory
cd docker/docker-cuda/
# 构建镜像
docker build -f ./docker/docker-cuda/Dockerfile \
--build-arg PIP_INDEX=https://pypi.org/simple \
--build-arg EXTRAS=metrics \
-t llamafactory:latest .
# 启动容器
docker run -dit --ipc=host --gpus=all \
-p 7860:7860 -p 8000:8000 \
--name llamafactory llamafactory:latest
# 进入容器
docker exec -it llamafactory bash
源码编译安装
若需更灵活的控制,可直接在宿主机安装:
cd workspace
git clone https://github.com/hiyouga/LlamaFactory.git
# 配置 pip 源(可选)
mkdir -p ~/.pip
cat > ~/.pip/pip.conf <<EOF
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
EOF
# 安装依赖
pip uninstall -y torch torchvision torchaudio nvidia-cublas nvidia-cudnn-cu12
pip install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple --index-url https://download.pytorch.org/whl/cu130
pip install --upgrade nvidia-cublas nvidia-cudnn-cu13
cd LlamaFactory
pip install -e '.[torch,metrics]'
验证环境
安装完成后,建议先进行基础测试:
# 检查 CLI 帮助
llamafactory-cli train -h
# 确认 GPU 状态
python -c "import torch; print(torch.cuda.get_device_name(0))"
数据集准备
LLaMA-Factory 主要支持 Alpaca 和 Sharegpt 两种格式。我们可以从魔搭社区等数据源获取角色扮演的数据集来测试效果。
- 下载数据集文件(如
huanhuan.json),确保符合 Alpaca 格式。


