LLaMA-Factory微调Qwen3模型完了,怎么直接用vllm推理模型?_llama factory合并权重
环境:
LLaMA-Factory
vllm0.8.5
Qwen3-8b
问题描述:
LLaMA-Factory微调Qwen3模型完了,怎么直接用vllm推理模型?
解决方案:
一、合并 LoRA 权重与基础模型
vLLM 需要完整的模型文件(含合并后的权重),而非单独的 LoRA 适配器。需先合并权重:
- 修改LLaMA-Factory合并配置文件
训练配置文件
### modelmodel_name_or_path: /mnt/program/LLaMA-Factory/LLaMA-Factory/Qwen/Qwen3-8Btrust_remote_code: true### methodstage: sftdo_train: truefinetuning_type: loralora_rank: 16lora_target: all### datasetdataset: sjj_traintemplate: qwencutoff_len: 2048max_samples: 8000overwrite_cache: truepreprocessing_num_workers: 16dataloader_num_workers: 8### outputoutput_dir: saves/Qwen3-8b/lora/sftlogging_steps: 1000save_steps: 5000plot_loss: trueoverwrite_output_dir: truesave_only_model: falsereport_to: none # choices: [none, wandb, tensorboard, swanlab, mlflow]### trainper_device_train_batch_size: 8gradient_accumulation_steps: 8learning_rate: 2.0e-4num_train_epochs: 2.0lr_scheduler_type: cosinewarmup_ratio: 0.1fp16: trueddp_timeout: 180000000resume_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### modelmodel_name_or_path: /mnt/program/LLaMA-Factory/LLaMA-Factory/Qwen/Qwen3-8Badapter_name_or_path: saves/qwen3-8b/lora/sft/checkpoint-186template: qwentrust_remote_code: true### exportexport_dir: output/Qwen3_8b_lora_sftexport_size: 5export_device: gpu:1export_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_sftadded_tokens.json generation_config.json model-00001-of-00004.safetensors model-00003-of-00004.safetensors Modelfile special_tokens_map.json tokenizer.jsonconfig.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 OpenAIclient = 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
参数,但需注意量化可能影响精度。