一、实现指南

llavaVL的示例代码

https://github.com/vllm-project/vllm/blob/main/vllm/model_executor/models/llava.py#L841

vLLM 通过 vllm.multimodal 包为多模态模型提供实验性支持。

多模态输入可以与文本和令牌提示一起传递给 支持的模型,方法是通过 vllm.inputs.PromptInputs 中的 multi_modal_data 字段。

目前,vLLM 仅内置支持图像数据。你可以按照 本指南 扩展 vLLM 以处理其他模态.

1.1 加入已经实现了新模型的添加 

大模型-vllm如何部署新模型-2-CSDN博客

1.2 更新基础 vLLM 模型

假设你已经根据 这些步骤 在 vLLM 中实现了模型。请按照以下步骤进一步更新模型:

  • 实现 SupportsMultiModal 接口。

    + from vllm.model_executor.models.interfaces import SupportsMultiModal
    
    - class YourModelForImage2Seq(nn.Module):
    + class YourModelForImage2Seq(nn.Module, SupportsMultiModal):

    模型类不必命名为 *ForCausalLM。查看 HuggingFace Transformers 文档 获取一些示例

    如果你还没有这样做,请在 forward() 中为每个与多模态输入相对应的输入张量保留一个关键字参数,如下例所示:

  •   def forward(
          self,
          input_ids: torch.Tensor,
          positions: torch.Tensor,
          kv_caches: List[torch.Tensor],
          attn_metadata: AttentionMetadata,
    +     pixel_values: torch.Tensor,
      ) -> SamplerOutput:

1.3 注册输入映射器

  • 对于模型接受的每种模态类型,使用 MULTIMODAL_REGISTRY.register_input_mapper 装饰模型类。此装饰器接受一个函数,该函数将多模态输入映射到你之前在 forward() 中定义的关键字参数。

  •   from vllm.model_executor.models.interfaces import SupportsMultiModal
    + from vllm.multimodal import MULTIMODAL_REGISTRY
    
    + @MULTIMODAL_REGISTRY.register_image_input_mapper()
      class YourModelForImage2Seq(nn.Module, SupportsMultiModal):

    核心 vLLM 库中为每种模态提供了一个默认映射器。如果你没有提供自己的函数,将使用此输入映射器。

  • 1.4 注册多模态令牌的最大数量

  • 对于模型接受的每种模态类型,计算每个数据实例的可能最大令牌数,并通过 INPUT_REGISTRY.register_dummy_data 注册它。

  •   from vllm.inputs import INPUT_REGISTRY
      from vllm.model_executor.models.interfaces import SupportsMultiModal
      from vllm.multimodal import MULTIMODAL_REGISTRY
    
      @MULTIMODAL_REGISTRY.register_image_input_mapper()
    + @MULTIMODAL_REGISTRY.register_max_image_tokens(<your_calculation>)
      @INPUT_REGISTRY.register_dummy_data(<your_dummy_data_factory>)
      class YourModelForImage2Seq(nn.Module, SupportsMultiModal):

    以下是一些示例:

  • 图像输入(静态特征大小):LLaVA-1.5 模型

  • 图Y像输入(动态特征大小):本指南

二、单图像输入

https://www.llamafactory.cn/vllm/models/vlm.html

要将图像传递给模型,请注意 vllm.inputs.PromptInputs 中的以下内容:

  • prompt: 提示应遵循 HuggingFace 上记录的格式。

  • multi_modal_data: 这是一个字典,遵循 vllm.multimodal.MultiModalDataDict 中定义的模式。

  • # Refer to the HuggingFace repo for the correct format to use
    prompt = "USER: <image>\nWhat is the content of this image?\nASSISTANT:"
    
    # Load the image using PIL.Image
    image = PIL.Image.open(...)
    
    # Single prompt inference
    outputs = llm.generate({
        "prompt": prompt,
        "multi_modal_data": {"image": image},
    })
    
    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)
    
    # Inference with image embeddings as input
    image_embeds = torch.load(...) # torch.Tensor of shape (1, image_feature_size, hidden_size of LM)
    outputs = llm.generate({
        "prompt": prompt,
        "multi_modal_data": {"image": image_embeds},
    })
    
    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)
    
    # Batch inference
    image_1 = PIL.Image.open(...)
    image_2 = PIL.Image.open(...)
    outputs = llm.generate(
        [
            {
                "prompt": "USER: <image>\nWhat is the content of this image?\nASSISTANT:",
                "multi_modal_data": {"image": image_1},
            },
            {
                "prompt": "USER: <image>\nWhat's the color of this image?\nASSISTANT:",
                "multi_modal_data": {"image": image_2},
            }
        ]
    )
    
    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)

    三、多图像输入

  • 多图像输入仅支持部分 VLM,如 此处 所示。要为每个文本提示启用多个多模态项,你必须为 LLM 类设置 limit_mm_per_prompt

  • llm = LLM(
        model="microsoft/Phi-3.5-vision-instruct",
        trust_remote_code=True,  # Required to load Phi-3.5-vision
        max_model_len=4096,  # Otherwise, it may not fit in smaller GPUs
        limit_mm_per_prompt={"image": 2},  # The maximum number to accept
    )
    # Refer to the HuggingFace repo for the correct format to use
    prompt = "<|user|>\n<image_1>\n<image_2>\nWhat is the content of each image?<|end|>\n<|assistant|>\n"
    
    # Load the images using PIL.Image
    image1 = PIL.Image.open(...)
    image2 = PIL.Image.open(...)
    
    outputs = llm.generate({
        "prompt": prompt,
        "multi_modal_data": {
            "image": [image1, image2]
        },
    })
    
    for o in outputs:
        generated_text = o.outputs[0].text
        print(generated_text)

    添加新模型 — vLLM

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐