当前位置: 首页 > news >正文

大模型-Vllm 启用多模态数据-3

一、实现指南

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_REGISTRYfrom vllm.model_executor.models.interfaces import SupportsMultiModalfrom 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].textprint(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].textprint(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].textprint(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-visionmax_model_len=4096,  # Otherwise, it may not fit in smaller GPUslimit_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].textprint(generated_text)

    添加新模型 — vLLM

http://www.dtcms.com/a/618502.html

相关文章:

  • 集团网站建设定制网站建设中企动力是怎么建设网站的
  • Shell脚本猜数字,使用判断提示用户比目标数字是大还是小
  • 【开题答辩全过程】以 基于安卓的校园二手物品为例,包含答辩的问题和答案
  • 内测检测vs第三方软件检测的差异解析
  • 【科研绘图系列】R语言绘制多组条形图图(barplot)
  • Linux-线程
  • 最专业网站建设公备案域名是什么意思
  • SQL 约束
  • 创立一个网站要多少钱上海公司做网站的
  • MoE算法深度解析:从理论架构到行业实践
  • 【2025CVPR 异常检测方向】DFM: Differentiable Feature Matching for Anomaly Detection
  • 北京西站地铁是几号线做网站贵
  • 数据库第六次作业
  • 西宁大型网站建设网站如何做电脑和手机软件
  • 【Linux】Shell脚本
  • qt显示类控件---QProgressBar
  • 复式记账的“借”与“贷”
  • 设备健康管理诊断报告生成:工业智能化的“决策引擎”与效率革命​
  • 淘客网站是怎么做的成都网站排名生客seo怎么样
  • vscode插件开发记录
  • 做淘宝代理哪个网站好淘宝网店页面设计
  • 【Linux系统编程】进程控制
  • day2江协科技-3 GPIO
  • Photoshop文字设计基础知识
  • 自己做的网站项目怎样卖微信支付 企业网站
  • Java 大视界 -- Java 大数据机器学习模型在自然语言处理中的少样本学习与迁移学习融合
  • 全椒县城乡建设局网站centos。wordpress
  • 南京华夏商务网做网站怎么样腾讯qq网页版
  • 网站怎么做白色字网站建设和执纪监督
  • Docker使用MinerU