Skip to content

vllm.model_executor.models.blip2

Classes:

Attributes:

Blip2ImageInputs = Blip2ImagePixelInputs | Blip2ImageEmbeddingInputs module-attribute

Alias for supported BLIP-2 image input types.

Blip2ForConditionalGeneration

Bases: Module, SupportsLoRA, SupportsMultiModal, SupportsPP, SupportsQuant

Methods:

  • forward –

    Run forward pass for BLIP-2.

forward(input_ids, positions, intermediate_tensors=None, inputs_embeds=None, **kwargs)

Run forward pass for BLIP-2.

One key thing to understand is the input_ids already accounts for the positions of the to-be-inserted image embeddings.

Concretely, consider a text prompt: "Question: What's the content of the image? Answer:".

Tokenizer outputs: [2, 45641, 35, 653, 18, 5, 1383, 9, 5, 2274, 116, 31652, 35].

To reserve space in KV cache, we have to insert placeholder tokens before they are inputted to the model, so the input processor prepends dummy tokens (denoted as 50265), resulting in: [50265, ..., 50265, 2, 45641, 35, ..., 31652, 35].

We insert 32 tokens since it corresponds to the number of query embeddings outputted by the Q-Former and inputted to the language model.

This way, the positions and attn_metadata are consistent with the input_ids.

Parameters:

  • input_ids

    (Tensor | None) –

    Flattened (concatenated) input_ids corresponding to a batch.

  • positions

    (Tensor) –

    Flattened (concatenated) position ids corresponding to a batch.

  • intermediate_tensors

    (IntermediateTensors | None, default: None ) –

    Intermediate tensors from prior forward pass.

  • inputs_embeds

    (Tensor | None, default: None ) –

    Optional tensor of input embeddings.

Info

Blip2ImageInputs

Blip2ImageEmbeddingInputs

Bases: TensorSchema

Dimensions: - bn: Batch size * number of images - f: Image feature size - h: Hidden size (must match the hidden size of language model backbone)

Blip2ImagePixelInputs

Bases: TensorSchema

Dimensions: - bn: Batch size * number of images - c: Number of channels (3) - h: Height of each image - w: Width of each image