LLaMA-Factory 简介
Llama-Factory 是基于 transformers 库开发的训练、微调、推理一体化平台,支持预训练、指令监督微调(SFT)、奖励模型训练、PPO、DPO、KTO、ORPO 等多种训练范式。它允许使用 Accelerate 或 DeepSpeed 作为训练加速后端。
使用 Llama-Factory 进行微调的核心优势在于其强大的数据处理与配置能力。只要按照官方文档配置好环境,直接运行对应的脚本即可启动流程。
环境部署
容器安装
推荐使用 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
# 创建配置目录
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
# 卸载旧版本依赖并重新安装 torch 等核心库
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 工具可用,再检查 GPU 和 CUDA 环境是否正常。
# 确认安装正常
llamafactory-cli train -h
# 确认 GPU 和 CUDA 环境正常
python
import torch
torch.cuda.current_device()
torch.cuda.get_device_name(0)
torch.__version__
推理测试
在正式微调前,建议先跑通一个推理 Demo,确保模型加载无误。


