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
# 创建配置文件(可选清华源加速)
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 和 CUDA 状态
python -c "import torch; print(torch.cuda.get_device_name(0)); print(torch.__version__)"
若需测试推理功能,可下载一个轻量级模型(如 Meta-Llama-3-8B-Instruct)并进行 4bit 量化加载:
import torch
transformers AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig
warnings
warnings.filterwarnings()
torch.cuda.set_device()
device = torch.cuda.is_available()
bnb_config = BitsAndBytesConfig(
load_in_4bit=,
bnb_4bit_compute_dtype=torch.bfloat16,
bnb_4bit_use_double_quant=,
bnb_4bit_quant_type=
)
model_id =
tokenizer = AutoTokenizer.from_pretrained(model_id)
tokenizer.pad_token = tokenizer.eos_token
model = AutoModelForCausalLM.from_pretrained(
model_id,
quantization_config=bnb_config,
torch_dtype=torch.bfloat16,
device_map=device,
trust_remote_code=,
low_cpu_mem_usage=
)
()


