环境:
LLaMA-Factory
vllm0.8.5
Qwen3-8b
问题描述:
LLaMA-Factory微调Qwen3模型完了,怎么直接用vllm推理模型?
解决方案:
一、合并 LoRA 权重与基础模型
vLLM 需要完整的模型文件(含合并后的权重),而非单独的 LoRA 适配器。需先合并权重:
- 修改LLaMA-Factory合并配置文件
训练配置文件
### model
model_name_or_path: /mnt/program/LLaMA-Factory/LLaMA-Factory/Qwen/Qwen3-8B
trust_remote_code: true
### method
stage: sft
do_train: true
finetuning_type: lora
lora_rank: 16
lora_target: all
### dataset
dataset: sjj_train
template: qwen
cutoff_len: 2048
max_samples: 8000
overwrite_cache: true
preprocessing_num_workers: 16
dataloader_num_workers: 8
### output
output_dir: saves/Qwen3-8b/lora/sft
logging_steps: 1000
save_steps: 5000
plot_loss: true
overwrite_output_dir: true
save_only_model: false
report_to: none # choices: [none, wandb, tensorboard, swanlab, mlflow]
### train
per_device_train_batch_size: 8
gradient_accumulation_steps: 8
learning_rate: 2.0e-4
num_train_epochs: 2.0
lr_scheduler_type: cosine
warmup_ratio: 0.1
fp16: true
ddp_timeout: 180000000
resume_from_checkpoint: null
### eval
#val_size: 0.2
#per_device_eval_batch_size: 2
#eval_strategy: steps
#eval_steps: 5
合并配置文件内容
目录文件merge_lora/qwen3-8b_lora_sft.yaml
### Note: DO NOT use quantized model or quantization_bit when merging lora adapters
### model
model_name_or_path: /mnt/program/LLaMA-Factory/LLaMA-Factory/Qwen/Qwen3-8B
adapter_name_or_path: saves/qwen3-8b/lora/sft/checkpoint-186
template: qwen
trust_remote_code: true
### export
export_dir: output/Qwen3_8b_lora_sft
export_size: 5
export_device: gpu:1
export_legacy_format: false
- 执行合并命令
CUDA_VISIBLE_DEVICES=1 llamafactory-cli export examples/merge_lora/qwen3-8b_lora_sft.yaml
查看合并后的文件夹,包含 model.safetensors
和配置文件
ubuntu@VM-0-2-ubuntu:~$ ls /mnt/program/LLaMA-Factory/LLaMA-Factory/output/Qwen3_8b_lora_sft
added_tokens.json generation_config.json model-00001-of-00004.safetensors model-00003-of-00004.safetensors Modelfile special_tokens_map.json tokenizer.json
config.json merges.txt model-00002-of-00004.safetensors model-00004-of-00004.safetensors model.safetensors.index.json tokenizer_config.json vocab.json
二、配置 vLLM 推理服务
使用合并后的完整模型启动 vLLM 服务:
-
安装 vLLM 依赖
确保已安装 vLLM 支持包:pip install -e '.[vllm]' # 在 LLaMA-Factory 项目目录下执行
-
启动 vLLM API 服务
通过命令行部署兼容 OpenAI 的 API:
CUDA_VISIBLE_DEVICES=0,1 nohup sh -c "VLLM_USE_MODELSCOPE=true VLLM_ALLOW_LONG_MAX_MODEL_LEN=1 vllm serve /mnt/program/LLaMA-Factory/LLaMA-Factory/output/Qwen3_8b_lora_sft --host 0.0.0.0 --port 8700 --gpu-memory-utilization 0.8 --max-num-seqs 200 --served-model-name Qwen3-8b --tensor-parallel-size 2 --enable-auto-tool-choice --tool-call-parser hermes --rope-scaling '{\"rope_type\":\"yarn\",\"factor\":4.0,\"original_max_position_embeddings\":32768}' --max-model-len 24096" > vllm.log 2>&1 &
``
关键参数说明:
--tensor-parallel-size
:多卡推理时需匹配 GPU 数量。--gpu-memory-utilization
:建议设为0.9
避免 OOM。
三、验证服务可用性
通过 curl
或 Python 测试 API:
from openai import OpenAI
client = OpenAI(base_url="http://localhost:8700/v1", api_key="sk-xxx")
response = client.chat.completions.create(
model="你的模型名称", # 与 --served-model-name 一致
messages=[{"role": "user", "content": "你好!"}]
)
print(response.choices[0].message.content)
⚠️ 常见问题与优化
-
显存不足
• 降低--tensor-parallel-size
(如单卡设为1
)。• 减小
--max-model-len
或启用--quantization
(如awq
)。 -
多卡部署
通过CUDA_VISIBLE_DEVICES
指定 GPU:CUDA_VISIBLE_DEVICES=0,1,2,3 vllm serve --tensor-parallel-size 4 ...
-
性能优化
• 使用--enforce-eager
模式避免内核编译错误(牺牲部分速度)。• 监控 GPU 利用率调整
--batch-size
。
合并后的模型可直接被 vLLM 加载,无需额外转换。若需进一步量化(如 GPTQ),可在合并时配置
export_quantization_bit
参数,但需注意量化可能影响精度。