vLLM 部署 InternVL2_5

vllm 中文文档
OpenAI 兼容服务器部署参数

模型下载

模型下载的渠道很多,这里使用 modelscope 进行下载,InternVL2_5-1B首页,

  • 安装 modelscope
pip install modelscope
  • 下载模型
from modelscope import snapshot_download
model_dir = snapshot_download('OpenGVLab/InternVL2_5-1B', local_dir="xxx/OpenGVLab/InternVL2_5-1B")

服务部署与请求

OpenGVLab/InternVL2_5-1B/config.json 中给定了初始化的超参数,例如temperaturetop_ptop_k等,简单部署命令如下,默认使用 8000 端口

vllm serve OpenGVLab/InternVL2_5-1B
or
python -m vllm.entrypoints.openai.api_server --model=OpenGVLab/InternVL2_5-1B

http://127.0.0.1:8000/docs 可以看到各种路由信息

请求脚本如下

import base64
import requests
from openai import OpenAI

# Modify OpenAI's API key and API base to use vLLM's API server.
openai_api_key = "EMPTY"
openai_api_base = "http://localhost:8000/v1"

client = OpenAI(
    # defaults to os.environ.get("OPENAI_API_KEY")
    api_key=openai_api_key,
    base_url=openai_api_base,
)

models = client.models.list()
model = models.data[0].id


def encode_image_base64_from_url(image_url: str) -> str:
    """Encode an image retrieved from a remote url to base64 format."""

    with requests.get(image_url) as response:
        response.raise_for_status()
        result = base64.b64encode(response.content).decode('utf-8')

    return result


def image_to_base64(image_path):
    with open(image_path, "rb") as image_file:
        image_data = image_file.read()
        base64_str = base64.b64encode(image_data).decode('utf-8')
        return base64_str  # 添加MIME类型前缀[7](@ref)


def single_image_call(image_path):
    image_base64 = image_to_base64(image_path=image_path)
    chat_completion_from_base64 = client.chat.completions.create(
        messages=[{
            "role":
                "user",
            "content": [
                {
                    "type": "text",
                    "text": "What’s in this image?"
                },
                {
                    "type": "image_url",
                    "image_url": {
                        "url": f"data:image/jpeg;base64,{image_base64}"
                    },
                },
            ],
        }],
        model=model,
        max_tokens=8192,
        top_p=0.9,
        temperature=0.0,
    )
    return chat_completion_from_base64.choices[0].message.content


total_result = []
for i in range(20):
    result = single_image_call("demo.jpg")
    total_result.append(result)

# 验证多次推理结果是否相同
if len(set(total_result)) == 1:
    print(True)
else:
    print(False)
  • 请求脚本使用 temperature=0.0 保证每次推理结果相同
  • vllm 服务刚启动了,前几个请求始终会出现差异,可能是 bug
  • –trust-remote-code 加载用户自己训练的模型,需要该参数
  • –port 8765 指定端口号
  • –tensor-parallel-size 张量并行数,部署服务需要的显卡数量
  • –seed 42 指定随机种子,使用 temperature=0.0,无需该参数也能保证每次推理结果相同

vLLM 示例命令

vllm serve xxx/checkpoint-yyy --port 8567 --trust-remote-code --max-num-batched-tokens 8192 --seed 42 --tensor-parallel-size 8
### 如何使用vLLM部署Qwen_vL模型 #### 准备工作 为了成功部署Qwen_vL模型,需要先准备好运行环境并下载所需的模型文件。确保安装了必要的依赖库以及配置好Python虚拟环境或者Conda环境。 #### 下载与验证模型 获取Qwen_vL模型后,应当对其进行完整性检验以确认文件未损坏。这可以通过计算文件的SHA-256哈希值并与官方提供的哈希值对比来完成[^3]。 ```bash sha256sum model-00001-of-00003.safetensors # 对比官方发布的哈希值(需查看模型卡) ``` #### 设置vLLM服务 激活预先创建好的名为`qwen_vl`的Conda环境,并启动vLLM服务。在此过程中指定了本地存储的模型权重路径以及其他一些用于优化性能的关键参数,比如数据类型的精度设置为bfloat16,限制每条提示的最大内存占用量等。 ```bash conda activate qwen_vl vllm serve --model /path/to/Qwen2.5-VL-7B-Instruct \ --dtype bfloat16 \ --limit-mm-per-prompt image=4 ``` #### 测试API接口 一旦服务器端口开启监听,默认情况下会开放HTTP RESTful API供外部调用。可以利用curl命令查询已加载到内存中的可用模型列表,以此验证部署是否成功[^2]。 ```bash curl http://localhost:8000/v1/models ``` #### 参数调整建议 对于追求更高效率的应用场景来说,合理地调节各项参数能够显著改善推理过程的速度表现。具体而言,在不影响最终效果的前提下适当降低图像分辨率、减少批处理大小或是启用异步执行模式均有助于加快响应时间[^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值