如何在本地部署Qwen3-VL-8B-Instruct-FP8?vLLM与SGLang完整教程
·
如何在本地部署Qwen3-VL-8B-Instruct-FP8?vLLM与SGLang完整教程
Qwen3-VL-8B-Instruct-FP8是Qwen3-VL-8B-Instruct模型的FP8量化版本,采用细粒度fp8量化方法,块大小为128,其性能指标与原始BF16模型几乎相同。本文将为新手和普通用户提供使用vLLM与SGLang在本地部署该模型的完整教程。
模型简介
Qwen3-VL是Qwen系列中迄今为止最强大的视觉语言模型,在文本理解与生成、视觉感知与推理、上下文长度、空间和视频动态理解以及代理交互能力等方面进行了全面升级。
主要增强功能
- 视觉代理:操作PC/移动GUI——识别元素、理解功能、调用工具、完成任务。
- 视觉编码增强:从图像/视频生成Draw.io/HTML/CSS/JS。
- 高级空间感知:判断物体位置、视点和遮挡;提供更强的2D定位,并支持3D定位以进行空间推理和具身AI。
- 长上下文和视频理解:原生256K上下文,可扩展至1M;处理书籍和数小时长视频,具有完整的回忆和秒级索引。
- 增强的多模态推理:在STEM/数学方面表现出色——因果分析和基于证据的逻辑答案。
- 升级的视觉识别:更广泛、更高质量的预训练能够“识别一切”——名人、动漫、产品、地标、动植物等。
- 扩展的OCR:支持32种语言(从19种增加);在低光、模糊和倾斜情况下表现稳健;更好地处理稀有/古代字符和行话;改进的长文档结构解析。
- 与纯LLM相当的文本理解:无缝的文本-视觉融合,实现无损、统一的理解。
模型架构更新
- Interleaved-MRoPE:通过强大的位置嵌入在时间、宽度和高度上进行全频率分配,增强长视野视频推理。
- DeepStack:融合多级ViT特征,捕捉细粒度细节并锐化图像-文本对齐。
- Text–Timestamp Alignment:超越T-RoPE,实现精确的、基于时间戳的事件定位,增强视频时间建模。
部署准备
目前,🤗 Transformers不支持直接加载这些权重。我们建议使用vLLM或SGLang部署模型,下面提供了示例启动命令。有关运行环境和部署的详细信息,请参考相关部署指南。
克隆仓库
首先,克隆Qwen3-VL-8B-Instruct-FP8仓库:
git clone https://gitcode.com/hf_mirrors/Qwen/Qwen3-VL-8B-Instruct-FP8
cd Qwen3-VL-8B-Instruct-FP8
vLLM部署步骤
vLLM是一个高性能的LLM服务库,支持快速部署和服务Qwen3-VL-8B-Instruct-FP8模型。
安装vLLM
pip install vllm
vLLM推理代码
以下是使用vLLM在本地运行Qwen3-VL推理的代码片段:
# -*- coding: utf-8 -*-
import torch
from qwen_vl_utils import process_vision_info
from transformers import AutoProcessor
from vllm import LLM, SamplingParams
import os
os.environ['VLLM_WORKER_MULTIPROC_METHOD'] = 'spawn'
def prepare_inputs_for_vllm(messages, processor):
text = processor.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# qwen_vl_utils 0.0.14+ reqired
image_inputs, video_inputs, video_kwargs = process_vision_info(
messages,
image_patch_size=processor.image_processor.patch_size,
return_video_kwargs=True,
return_video_metadata=True
)
mm_data = {}
if image_inputs is not None:
mm_data['image'] = image_inputs
if video_inputs is not None:
mm_data['video'] = video_inputs
return {
'prompt': text,
'multi_modal_data': mm_data,
'mm_processor_kwargs': video_kwargs
}
if __name__ == '__main__':
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "receipt.png", # 替换为本地图片路径
},
{"type": "text", "text": "Read all the text in the image."},
],
}
]
# 设置模型路径为本地克隆的仓库路径
checkpoint_path = "./"
processor = AutoProcessor.from_pretrained(checkpoint_path)
inputs = [prepare_inputs_for_vllm(message, processor) for message in [messages]]
llm = LLM(
model=checkpoint_path,
trust_remote_code=True,
gpu_memory_utilization=0.70,
enforce_eager=False,
tensor_parallel_size=torch.cuda.device_count(),
seed=0
)
sampling_params = SamplingParams(
temperature=0,
max_tokens=1024,
top_k=-1,
stop_token_ids=[],
)
outputs = llm.generate(inputs, sampling_params=sampling_params)
for i, output in enumerate(outputs):
generated_text = output.outputs[0].text
print(f"Generated text: {generated_text!r}")
SGLang部署步骤
SGLang是另一个高效的LLM部署框架,也可用于部署Qwen3-VL-8B-Instruct-FP8模型。
安装SGLang
pip install sglang
SGLang推理代码
以下是使用SGLang在本地运行Qwen3-VL推理的代码片段:
import time
from PIL import Image
from sglang import Engine
from qwen_vl_utils import process_vision_info
from transformers import AutoProcessor, AutoConfig
import torch
if __name__ == "__main__":
# 设置模型路径为本地克隆的仓库路径
checkpoint_path = "./"
processor = AutoProcessor.from_pretrained(checkpoint_path)
messages = [
{
"role": "user",
"content": [
{
"type": "image",
"image": "receipt.png", # 替换为本地图片路径
},
{"type": "text", "text": "Read all the text in the image."},
],
}
]
text = processor.apply_chat_template(
messages,
tokenize=False,
add_generation_prompt=True
)
image_inputs, _ = process_vision_info(messages, image_patch_size=processor.image_processor.patch_size)
llm = Engine(
model_path=checkpoint_path,
enable_multimodal=True,
mem_fraction_static=0.8,
tp_size=torch.cuda.device_count(),
attention_backend="fa3"
)
start = time.time()
sampling_params = {"max_new_tokens": 1024}
response = llm.generate(prompt=text, image_data=image_inputs, sampling_params=sampling_params)
print(f"Response costs: {time.time() - start:.2f}s")
print(f"Generated text: {response['text']}")
生成超参数配置
视觉-语言任务
export greedy='false'
export top_p=0.8
export top_k=20
export temperature=0.7
export repetition_penalty=1.0
export presence_penalty=1.5
export out_seq_length=16384
文本任务
export greedy='false'
export top_p=1.0
export top_k=40
export repetition_penalty=1.0
export presence_penalty=2.0
export temperature=1.0
export out_seq_length=32768
总结
通过本教程,你已经了解了如何使用vLLM和SGLang在本地部署Qwen3-VL-8B-Instruct-FP8模型。这两种部署方式都能高效地运行模型,你可以根据自己的需求和环境选择合适的方式。希望这篇教程对你有所帮助,祝你使用愉快!
更多推荐
所有评论(0)