LLaVA#

LMDeploy 支持以下 llava 系列模型,详细信息如下表所示:

模型

大小

支持的推理引擎

llava-hf/Llava-interleave-qwen-7b-hf

7B

TurboMind, PyTorch

llava-hf/llava-1.5-7b-hf

7B

TurboMind, PyTorch

llava-hf/llava-v1.6-mistral-7b-hf

7B

PyTorch

llava-hf/llava-v1.6-vicuna-7b-hf

7B

PyTorch

liuhaotian/llava-v1.6-mistral-7b

7B

TurboMind

liuhaotian/llava-v1.6-vicuna-7b

7B

TurboMind

下一章将演示如何使用LMDeploy部署一个Llava模型,以llava-hf/llava-interleave为例。

注意

PyTorch引擎在v0.6.4之后移除了对原始llava模型的支持。请使用它们对应的transformers模型代替,可以在https://huggingface.co/llava-hf找到。

安装#

请按照安装指南安装LMDeploy。

或者,您可以使用官方的docker镜像:

docker pull openmmlab/lmdeploy:latest

离线推理#

以下示例代码展示了VLM管道的基本用法。有关详细信息,请参阅VLM离线推理管道

from lmdeploy import GenerationConfig, TurbomindEngineConfig, pipeline
from lmdeploy.vl import load_image


pipe = pipeline("llava-hf/llava-interleave-qwen-7b-hf", backend_config=TurbomindEngineConfig(cache_max_entry_count=0.5),
    gen_config=GenerationConfig(max_new_tokens=512))

image = load_image('https://qianwen-res.oss-cn-beijing.aliyuncs.com/Qwen-VL/assets/demo.jpeg')
prompt = 'Describe the image.'
print(f'prompt:{prompt}')
response = pipe((prompt, image))
print(response)

更多示例如下:

multi-image multi-round conversation, combined images
from lmdeploy import pipeline, GenerationConfig

pipe = pipeline('llava-hf/llava-interleave-qwen-7b-hf', log_level='INFO')
messages = [
    dict(role='user', content=[
        dict(type='text', text='Describe the two images in detail.'),
        dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Beijing_Small.jpeg')),
        dict(type='image_url', image_url=dict(url='https://raw.githubusercontent.com/QwenLM/Qwen-VL/master/assets/mm_tutorial/Chongqing_Small.jpeg'))
    ])
]
out = pipe(messages, gen_config=GenerationConfig(top_k=1))

messages.append(dict(role='assistant', content=out.text))
messages.append(dict(role='user', content='What are the similarities and differences between these two images.'))
out = pipe(messages, gen_config=GenerationConfig(top_k=1))

在线服务#

你可以通过lmdeploy serve api_server CLI启动服务器:

lmdeploy serve api_server llava-hf/llava-interleave-qwen-7b-hf

你也可以使用上述构建的docker镜像来启动服务:

docker run --runtime nvidia --gpus all \
    -v ~/.cache/huggingface:/root/.cache/huggingface \
    --env "HUGGING_FACE_HUB_TOKEN=<secret>" \
    -p 23333:23333 \
    --ipc=host \
    openmmlab/lmdeploy:latest \
    lmdeploy serve api_server llava-hf/llava-interleave-qwen-7b-hf

Docker compose 是另一种选择。在 lmdeploy 项目的根目录下创建一个 docker-compose.yml 配置文件,如下所示:

version: '3.5'

services:
  lmdeploy:
    container_name: lmdeploy
    image: openmmlab/lmdeploy:latest
    ports:
      - "23333:23333"
    environment:
      HUGGING_FACE_HUB_TOKEN: <secret>
    volumes:
      - ~/.cache/huggingface:/root/.cache/huggingface
    stdin_open: true
    tty: true
    ipc: host
    command: lmdeploy serve api_server llava-hf/llava-interleave-qwen-7b-hf
    deploy:
      resources:
        reservations:
          devices:
            - driver: nvidia
              count: "all"
              capabilities: [gpu]

然后,您可以执行如下启动命令:

docker-compose up -d

如果你在运行docker logs -f lmdeploy后看到以下日志,这意味着服务已成功启动。

HINT:    Please open  http://0.0.0.0:23333   in a browser for detailed api usage!!!
HINT:    Please open  http://0.0.0.0:23333   in a browser for detailed api usage!!!
HINT:    Please open  http://0.0.0.0:23333   in a browser for detailed api usage!!!
INFO:     Started server process [2439]
INFO:     Waiting for application startup.
INFO:     Application startup complete.
INFO:     Uvicorn running on  http://0.0.0.0:23333  (Press CTRL+C to quit)

lmdeploy serve api_server 的参数可以通过 lmdeploy serve api_server -h 详细查看。

有关api_server的更多信息以及如何访问该服务,可以从这里找到