#
InternVL

The InternVL3 family of Visual Language Models was introduced in InternVL3: Exploring Advanced Training and Test-Time Recipes for Open-Source Multimodal Models.

The abstract from the paper is the following:

We introduce InternVL3, a significant advancement in the InternVL series featuring a native multimodal pre-training paradigm. Rather than adapting a text-only large language model (LLM) into a multimodal large language model (MLLM) that supports visual inputs, InternVL3 jointly acquires multimodal and linguistic capabilities from both diverse multimodal data and pure-text corpora during a single pre-training stage. This unified training paradigm effectively addresses the complexities and alignment challenges commonly encountered in conventional post-hoc training pipelines for MLLMs. To further improve performance and scalability, InternVL3 incorporates variable visual position encoding (V2PE) to support extended multimodal contexts, employs advanced post-training techniques such as supervised fine-tuning (SFT) and mixed preference optimization (MPO), and adopts test-time scaling strategies alongside an optimized training infrastructure. Extensive empirical evaluations demonstrate that InternVL3 delivers superior performance across a wide range of multi-modal tasks. In particular, InternVL3-78B achieves a score of 72.2 on the MMMU benchmark, setting a new state-of-the-art among open-source MLLMs. Its capabilities remain highly competitive with leading proprietary models, including ChatGPT-4o, Claude 3.5 Sonnet, and Gemini 2.5 Pro, while also maintaining strong pure-language proficiency. In pursuit of open-science principles, we will publicly release both the training data and model weights to foster further research and development in next-generation MLLMs.

Overview of InternVL3 models architecture, which is the same as InternVL2.5. Taken from the original checkpoint. Comparison of InternVL3 performance on OpenCompass against other SOTA VLLMs. Taken from the original checkpoint.

This model was contributed by yonigozlan. The original code can be found here.

##
Usage example

###
Inference with Pipeline

Here is how you can use the `image-text-to-text` pipeline to perform inference with the `InternVL3` models in just a few lines of code:

    >>> from transformers import pipeline

    >>> messages = [
    ...     {
    ...         "role": "user",
    ...         "content": [
    ...             {
    ...                 "type": "image",
    ...                 "image": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/bee.jpg",
    ...             },
    ...             {"type": "text", "text": "Describe this image."},
    ...         ],
    ...     },
    ... ]

    >>> pipe = pipeline("image-text-to-text", model="OpenGVLab/InternVL3-1B-hf")
    >>> outputs = pipe(text=messages, max_new_tokens=50, return_full_text=False)
    >>> outputs[0]["generated_text"]
    'The image showcases a vibrant scene of nature, featuring several flowers and a bee. \n\n1. **Foreground Flowers**: \n   - The primary focus is on a large, pink cosmos flower with a prominent yellow center. The petals are soft and slightly r'

###
Inference on a single image

This example demonstrates how to perform inference on a single image with the InternVL models using chat templates.

> [!NOTE] Note that the model has been trained with a specific prompt format for chatting. Use `processor.apply_chat_template(my_conversation_dict)` to correctly format your prompts.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText
    >>> import torch

    >>> torch_device = "cuda"
    >>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

    >>> messages = [
    ...     {
    ...         "role": "user",
    ...         "content": [
    ...             {"type": "image", "url": "http://images.cocodataset.org/val2017/000000039769.jpg"},
    ...             {"type": "text", "text": "Please describe the image explicitly."},
    ...         ],
    ...     }
    ... ]

    >>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)

    >>> generate_ids = model.generate(**inputs, max_new_tokens=50)
    >>> decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)

    >>> decoded_output
    'The image shows two cats lying on a pink blanket. The cat on the left is a tabby with a mix of brown, black, and white fur, and it appears to be sleeping with its head resting on the blanket. The cat on the'

###
Text-only generation

This example shows how to generate text using the InternVL model without providing any image input.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText
    >>> import torch

    >>> torch_device = "cuda"
    >>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

    >>> messages = [
    ...     {
    ...         "role": "user",
    ...         "content": [
    ...             {"type": "text", "text": "Write a haiku"},
    ...         ],
    ...     }
    ... ]

    >>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(torch_device, dtype=torch.bfloat16)

    >>> generate_ids = model.generate(**inputs, max_new_tokens=50)
    >>> decoded_output = processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)

    >>> print(decoded_output)
    "Whispers of dawn,\nSilent whispers of the night,\nNew day's light begins."

###
Batched image and text inputs

InternVL models also support batched image and text inputs.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText
    >>> import torch

    >>> torch_device = "cuda"
    >>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

    >>> messages = [
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
    ...                 {"type": "text", "text": "Write a haiku for this image"},
    ...             ],
    ...         },
    ...     ],
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://www.ilankelman.org/stopsigns/australia.jpg"},
    ...                 {"type": "text", "text": "Describe this image"},
    ...             ],
    ...         },
    ...     ],
    ... ]

    >>> inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)

    >>> output = model.generate(**inputs, max_new_tokens=25)

    >>> decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
    >>> decoded_outputs
    ["user\n\nWrite a haiku for this image\nassistant\nSilky lake,  \nWooden pier,  \nNature's peace.",
     'user\n\nDescribe this image\nassistant\nThe image shows a street scene with a traditional Chinese archway, known as a "Chinese Gate" or "Chinese Gate of']

###
Batched multi-image input

This implementation of the InternVL models supports batched text-images inputs with different number of images for each text.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText
    >>> import torch

    >>> torch_device = "cuda"
    >>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

    >>> messages = [
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
    ...                 {"type": "text", "text": "Write a haiku for this image"},
    ...             ],
    ...         },
    ...     ],
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"},
    ...                 {"type": "image", "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg"},
    ...                 {"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
    ...             ],
    ...         },
    ...     ],
    >>> ]

    >>> inputs = processor.apply_chat_template(messages, padding=True, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(model.device, dtype=torch.bfloat16)

    >>> output = model.generate(**inputs, max_new_tokens=25)

    >>> decoded_outputs = processor.batch_decode(output, skip_special_tokens=True)
    >>> decoded_outputs
    ["user\n\nWrite a haiku for this image\nassistant\nSilky lake,  \nWooden pier,  \nNature's peace.",
     'user\n\n\nThese images depict two different landmarks. Can you identify them?\nassistant\nYes, these images depict the Statue of Liberty and the Golden Gate Bridge.']

###
Video input

InternVL models can also handle video inputs. Here is an example of how to perform inference on a video input using chat templates.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText, BitsAndBytesConfig

    >>> model_checkpoint = "OpenGVLab/InternVL3-8B-hf"
    >>> quantization_config = BitsAndBytesConfig(load_in_4bit=True)
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, quantization_config=quantization_config)

    >>> messages = [
    ...     {
    ...         "role": "user",
    ...         "content": [
    ...             {
    ...                 "type": "video",
    ...                 "url": "https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4",
    ...             },
    ...             {"type": "text", "text": "What type of shot is the man performing?"},
    ...         ],
    ...     }
    >>> ]
    >>> inputs = processor.apply_chat_template(
    ...     messages,
    ...     return_tensors="pt",
    ...     add_generation_prompt=True,
    ...     tokenize=True,
    ...     return_dict=True,
    ...     num_frames=8,
    >>> ).to(model.device, dtype=torch.float16)

    >>> output = model.generate(**inputs, max_new_tokens=25)

    >>> decoded_output = processor.decode(output[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True)
    >>> decoded_output
    'The man is performing a forehand shot.'

###
Interleaved image and video inputs

This example showcases how to handle a batch of chat conversations with interleaved image and video inputs using chat template.

    >>> from transformers import AutoProcessor, AutoModelForImageTextToText, BitsAndBytesConfig
    >>> import torch

    >>> torch_device = "cuda"
    >>> model_checkpoint = "OpenGVLab/InternVL3-1B-hf"
    >>> processor = AutoProcessor.from_pretrained(model_checkpoint)
    >>> model = AutoModelForImageTextToText.from_pretrained(model_checkpoint, device_map=torch_device, torch_dtype=torch.bfloat16)

    >>> messages = [
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg"},
    ...                 {"type": "image", "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg"},
    ...                 {"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
    ...             ],
    ...         },
    ...     ],
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "video", "url": "https://huggingface.co/datasets/hf-internal-testing/fixtures_videos/resolve/main/tennis.mp4"},
    ...                 {"type": "text", "text": "What type of shot is the man performing?"},
    ...             ],
    ...         },
    ...     ],
    ...     [
    ...         {
    ...             "role": "user",
    ...             "content": [
    ...                 {"type": "image", "url": "https://llava-vl.github.io/static/images/view.jpg"},
    ...                 {"type": "text", "text": "Write a haiku for this image"},
    ...             ],
    ...         },
    ...     ],
    >>> ]
    >>> inputs = processor.apply_chat_template(
    ...     messages,
    ...     padding=True,
    ...     add_generation_prompt=True,
    ...     tokenize=True,
    ...     return_dict=True,
    ...     return_tensors="pt",
    >>> ).to(model.device, dtype=torch.bfloat16)

    >>> outputs = model.generate(**inputs, max_new_tokens=25)

    >>> decoded_outputs = processor.batch_decode(outputs, skip_special_tokens=True)
    >>> decoded_outputs
    ['user\n\n\nThese images depict two different landmarks. Can you identify them?\nassistant\nThe images depict the Statue of Liberty and the Golden Gate Bridge.',
     'user\nFrame1: \nFrame2: \nFrame3: \nFrame4: \nFrame5: \nFrame6: \nFrame7: \nFrame8: \nWhat type of shot is the man performing?\nassistant\nA forehand shot',
     "user\n\nWrite a haiku for this image\nassistant\nSilky lake,  \nWooden pier,  \nNature's peace."]

##
InternVLVisionConfig

###
class transformers.InternVLVisionConfig

< source >

( hidden_size = 1024 num_hidden_layers = 24 num_attention_heads = 16 attention_bias = False use_qk_norm = False intermediate_size = 4096 hidden_act = 'gelu' hidden_dropout_prob = 0.0 attention_dropout = 0.0 projection_dropout = 0.0 initializer_range = 0.02 norm_type = 'layer_norm' layer_norm_eps = 1e-06 image_size = [448, 448] patch_size = [14, 14] num_channels = 3 use_mask_token = False use_absolute_position_embeddings = True layer_scale_init_value = 0.1 use_mean_pooling = True **kwargs )

Parameters

- hidden_size (`int`, optional, defaults to 1024) — Dimensionality of the encoder layers and the pooler layer.
- num_hidden_layers (`int`, optional, defaults to 24) — Number of hidden layers in the Transformer encoder.
- num_attention_heads (`int`, optional, defaults to 16) — Number of attention heads for each attention layer in the Transformer encoder.
- attention_bias (`bool`, optional, defaults to `False`) — Whether to add a bias to the queries, keys and values.
- use_qk_norm (`bool`, optional, defaults to `False`) — Whether to apply normalization to the queries and keys before the attention operation.
- intermediate_size (`int`, optional, defaults to 4096) — Dimensionality of the “intermediate” (i.e., feed-forward) layer in the Transformer encoder.
- hidden_act (`str` or `function`, optional, defaults to `"gelu"`) — The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`, `"relu"`, `"selu"` and `"gelu_new"` are supported.
- hidden_dropout_prob (`float`, optional, defaults to 0.0) — The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
- attention_dropout (`float`, optional, defaults to 0.0) — Dropout probability for attention weights.
- projection_dropout (`float`, optional, defaults to 0.0) — Dropout probability for the projection layer.
- initializer_range (`float`, optional, defaults to 0.02) — The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
- norm_type (`str`, optional, defaults to `"layer_norm"`) — The type of normalization to use in the encoder. Can be `"layer_norm"` or `"rms_norm"`.
- layer_norm_eps (`float`, optional, defaults to 1e-06) — The epsilon used by the layer normalization layers.
- image_size (`int` or `list[int]`, optional, defaults to `[448, 448]`) — The size (resolution) of each image.
- patch_size (`int` or `list[int]`, optional, defaults to `[14, 14]`) — The size (resolution) of each patch.
- num_channels (`int`, optional, defaults to 3) — The number of input channels.
- use_mask_token (`bool`, optional, defaults to `False`) — Whether to use a mask token for masked image modeling.
- use_absolute_position_embeddings (`bool`, optional, defaults to `True`) — Whether to use BERT-style absolute position embeddings.
- layer_scale_init_value (`float`, optional, defaults to 0.1) — Scale to use in the self-attention layers. 0.1 for base, 1e-5 for large. Set 0 to disable layer scale.
- use_mean_pooling (`bool`, optional, defaults to `True`) — Whether to mean pool the final hidden states of the patches instead of using the final hidden state of the CLS token, before applying the classification head.

This is the configuration class to store the configuration of a InternVLVisionModel. It is used to instantiate an InternVLVisionModel model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of the InternVL3-1B. e.g. OpenGVLab/InternVL3-1B-hf

Example:

    >>> from transformers import InternVLVisionConfig, InternVLVisionModel

    >>> # Initializing a InternVLVisionModel OpenGVLab/InternVL3-1B-hf style configuration
    >>> configuration = InternVLVisionConfig()

    >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration
    >>> model = InternVLVisionModel(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config

##
InternVLConfig

###
class transformers.InternVLConfig

< source >

( vision_config = None text_config = None image_token_id = 151667 image_seq_length = 256 downsample_ratio = 0.5 projector_hidden_act = 'gelu' vision_feature_layer = -1 vision_feature_select_strategy = 'default' **kwargs )

Parameters

- vision_config (`Union[AutoConfig, dict]`, optional, defaults to `InternVisonConfig`) — The config object or dictionary of the vision backbone.
- text_config (`Union[AutoConfig, dict]`, optional, defaults to `Qwen2Config`) — The config object or dictionary of the text backbone.
- image_token_id (`int`, optional, defaults to 151667) — The image token index to encode the image prompt.
- image_seq_length (`int`, optional, defaults to 256) — Number of image tokens to use per image patch.
- downsample_ratio (`float`, optional, defaults to 0.5) — Factor by which to downsample the image.
- projector_hidden_act (`str` or `function`, optional, defaults to `"gelu"`) — The non-linear activation function (function or string) in the projector.
- vision_feature_layer (`int`, optional, defaults to -1) — The index of the layer to use as the image features.
- vision_feature_select_strategy (`str`, optional, defaults to `"default"`) — The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`.

This is the configuration class to store the configuration of a InternVLForConditionalGeneration. It is used to instantiate a InternVL model according to the specified arguments, defining the model architecture. Instantiating a configuration with the defaults will yield a similar configuration to that of InternVL3-1B. e.g. OpenGVLab/InternVL3-1B-hf

Configuration objects inherit from PretrainedConfig and can be used to control the model outputs. Read the documentation from PretrainedConfig for more information.

    >>> from transformers import InternVLForConditionalGeneration, InternVLConfig

    >>> # Initializing a InternVL style configuration
    >>> configuration = InternVLConfig()

    >>> # Initializing a model (with random weights) from the OpenGVLab/InternVL3-1B-hf configuration
    >>> model = InternVLForConditionalGeneration(configuration)

    >>> # Accessing the model configuration
    >>> configuration = model.config

##
InternVLVisionModel

###
class transformers.InternVLVisionModel

< source >

( config: InternVLVisionConfig )

Parameters

- config (InternVLVisionConfig) — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the from_pretrained() method to load the model weights.

The bare InternVLVision Model transformer outputting raw hidden-states without any specific head on top. This model is a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.

####
forward

< source >

( pixel_values: Tensor bool_masked_pos: typing.Optional[torch.BoolTensor] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None ) →
`transformers.models.internvl.modeling_internvl.InternVLVisionModelOutputWithPooling` or `tuple(torch.FloatTensor)`

Parameters

- pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, height, width)`) — Pixel values. Pixel values can be obtained using AutoImageProcessor. See `InternVLVisionImageProcessor.__call__` for details.
- output_attentions (`bool`, optional) — Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail.
- output_hidden_states (`bool`, optional) — Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail.
- return_dict (`bool`, optional) — Whether or not to return a ModelOutput instead of a plain tuple.
- bool_masked_pos (`torch.BoolTensor` of shape `(batch_size, num_patches)`, optional) — Boolean masked positions. Indicates which patches are masked (1) and which aren’t (0).

Returns

`transformers.models.internvl.modeling_internvl.InternVLVisionModelOutputWithPooling` or `tuple(torch.FloatTensor)`

A `transformers.models.internvl.modeling_internvl.InternVLVisionModelOutputWithPooling` or a tuple of `torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various elements depending on the configuration (InternVLVisionConfig) and inputs.

- last_hidden_state (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`) — Sequence of hidden-states at the output of the last layer of the model.

- pooler_output (`torch.FloatTensor` of shape `(batch_size, hidden_size)`) — Average of the last layer hidden states of the patch tokens (excluding the [CLS] token) if config.use_mean_pooling is set to True. If set to False, then the final hidden state of the [CLS] token will be returned.

- hidden_states (`tuple(torch.FloatTensor)`, optional, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) — Tuple of `torch.FloatTensor` (one for the output of the embeddings + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

  Hidden-states of the model at the output of each layer plus the initial embedding outputs.

- attentions (`tuple(torch.FloatTensor)`, optional, returned when `output_attentions=True` is passed or when `config.output_attentions=True`) — Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`.

  Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.

The InternVLVisionModel forward method, overrides the `__call__` special method.

Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.

Example:

    >>> from transformers import AutoImageProcessor, InternVLVisionModel
    >>> import torch
    >>> from datasets import load_dataset

    >>> dataset = load_dataset("huggingface/cats-image", trust_remote_code=True)
    >>> image = dataset["test"]["image"][0]

    >>> image_processor = AutoImageProcessor.from_pretrained("OpenGVLab/InternVL3-1B-hf")
    >>> model = InternVLVisionModel.from_pretrained("OpenGVLab/InternVL3-1B-hf")

    >>> inputs = image_processor(image, return_tensors="pt")

    >>> with torch.no_grad():
    ...     outputs = model(**inputs)

    >>> last_hidden_states = outputs.last_hidden_state
    >>> list(last_hidden_states.shape)
    [1, 197, 768]

##
InternVLForConditionalGeneration

###
class transformers.InternVLForConditionalGeneration

< source >

( config: InternVLConfig )

Parameters

- config (InternVLConfig or InternVLVisionConfig) — Model configuration class with all the parameters of the model. Initializing with a config file does not load the weights associated with the model, only the configuration. Check out the from_pretrained() method to load the model weights.

The INTERNVL model which consists of a vision backbone and a language model. This model inherits from PreTrainedModel. Check the superclass documentation for the generic methods the library implements for all its model (such as downloading or saving, resizing the input embeddings, pruning heads etc.)

This model is also a PyTorch torch.nn.Module subclass. Use it as a regular PyTorch Module and refer to the PyTorch documentation for all matter related to general usage and behavior.

####
forward

< source >

( input_ids: typing.Optional[torch.LongTensor] = None pixel_values: typing.Optional[torch.FloatTensor] = None attention_mask: typing.Optional[torch.Tensor] = None position_ids: typing.Optional[torch.LongTensor] = None past_key_values: typing.Optional[typing.List[torch.FloatTensor]] = None inputs_embeds: typing.Optional[torch.FloatTensor] = None vision_feature_layer: typing.Union[int, typing.List[int], NoneType] = None vision_feature_select_strategy: typing.Optional[str] = None labels: typing.Optional[torch.LongTensor] = None use_cache: typing.Optional[bool] = None output_attentions: typing.Optional[bool] = None output_hidden_states: typing.Optional[bool] = None return_dict: typing.Optional[bool] = None cache_position: typing.Optional[torch.LongTensor] = None logits_to_keep: typing.Union[int, torch.Tensor] = 0 image_sizes: typing.Optional[torch.Tensor] = None **lm_kwargs ) →
`transformers.models.internvl.modeling_internvl.InternVLCausalLMOutputWithPast` or `tuple(torch.FloatTensor)`

Parameters

- input_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`) — Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it.

  Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.

  What are input IDs?

- pixel_values (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)) -- The tensors corresponding to the input images. Pixel values can be obtained using [AutoImageProcessor](/docs/transformers/main/en/model_doc/auto#transformers.AutoImageProcessor). See [CLIPImageProcessor.__call__()](/docs/transformers/main/en/model_doc/vilt#transformers.ViltFeatureExtractor.__call__) for details ([]`InternVLProcessor`] uses CLIPImageProcessor for processing images).
- attention_mask (`torch.Tensor` of shape `(batch_size, sequence_length)`, optional) — Mask to avoid performing attention on padding token indices. Mask values selected in `[0, 1]`:
  - 1 for tokens that are not masked,
  - 0 for tokens that are masked.

  What are attention masks?

  Indices can be obtained using AutoTokenizer. See PreTrainedTokenizer.encode() and PreTrainedTokenizer.call() for details.

  If `past_key_values` is used, optionally only the last `decoder_input_ids` have to be input (see `past_key_values`).

  If you want to change padding behavior, you should read `modeling_opt._prepare_decoder_attention_mask` and modify to your needs. See diagram 1 in the paper for more information on the default strategy.

  - 1 indicates the head is not masked,
  - 0 indicates the head is masked.
- position_ids (`torch.LongTensor` of shape `(batch_size, sequence_length)`, optional) — Indices of positions of each input sequence tokens in the position embeddings. Selected in the range `[0, config.n_positions - 1]`. What are position IDs?
- past_key_values (`tuple(tuple(torch.FloatTensor))`, optional, returned when `use_cache=True` is passed or when `config.use_cache=True`) — Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`) and 2 additional tensors of shape `(batch_size, num_heads, encoder_sequence_length, embed_size_per_head)`.

  Contains pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.

  If `past_key_values` are used, the user can optionally input only the last `decoder_input_ids` (those that don’t have their past key value states given to this model) of shape `(batch_size, 1)` instead of all `decoder_input_ids` of shape `(batch_size, sequence_length)`.

- inputs_embeds (`torch.FloatTensor` of shape `(batch_size, sequence_length, hidden_size)`, optional) — Optionally, instead of passing `input_ids` you can choose to directly pass an embedded representation. This is useful if you want more control over how to convert `input_ids` indices into associated vectors than the model’s internal embedding lookup matrix.
- vision_feature_layer (`Union[int, List[int]], *optional*, defaults to -2`) — The index of the layer to select the vision feature. If multiple indices are provided, the vision feature of the corresponding indices will be concatenated to form the vision features.
- vision_feature_select_strategy (`str`, optional, defaults to `"default"`) — The feature selection strategy used to select the vision feature from the vision backbone. Can be one of `"default"` or `"full"`.
- use_cache (`bool`, optional) — If set to `True`, `past_key_values` key value states are returned and can be used to speed up decoding (see `past_key_values`).
- output_attentions (`bool`, optional) — Whether or not to return the attentions tensors of all attention layers. See `attentions` under returned tensors for more detail.
- output_hidden_states (`bool`, optional) — Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for more detail.
- return_dict (`bool`, optional) — Whether or not to return a ModelOutput instead of a plain tuple.
- cache_position (`torch.LongTensor` of shape `(sequence_length)`, optional) — Indices depicting the position of the input sequence tokens in the sequence. Contrarily to `position_ids`, this tensor is not affected by padding. It is used to update the cache in the correct position and to infer the complete sequence length.
- Args — labels (`torch.LongTensor` of shape `(batch_size, sequence_length)`, optional): Labels for computing the masked language modeling loss. Indices should either be in `[0, ..., config.vocab_size]` or -100 (see `input_ids` docstring). Tokens with indices set to `-100` are ignored (masked), the loss is only computed for the tokens with labels in `[0, ..., config.vocab_size]`.

  logits_to_keep (`int` or `torch.Tensor`, optional): If an `int`, compute logits for the last `logits_to_keep` tokens. If `0`, calculate logits for all `input_ids` (special case). Only last token logits are needed for generation, and calculating them only for that token can save memory, which becomes pretty significant for long sequences or large vocabulary size. If a `torch.Tensor`, must be 1D corresponding to the indices to keep in the sequence length dimension. This is useful when using packed tensor format (single dimension for batch and sequence length).

Returns

`transformers.models.internvl.modeling_internvl.InternVLCausalLMOutputWithPast` or `tuple(torch.FloatTensor)`

A `transformers.models.internvl.modeling_internvl.InternVLCausalLMOutputWithPast` or a tuple of `torch.FloatTensor` (if `return_dict=False` is passed or when `config.return_dict=False`) comprising various elements depending on the configuration (InternVLConfig) and inputs.

- loss (`torch.FloatTensor` of shape `(1,)`, optional, returned when `labels` is provided) — Language modeling loss (for next-token prediction).

- logits (`torch.FloatTensor` of shape `(batch_size, sequence_length, config.vocab_size)`) — Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).

- past_key_values (`tuple(tuple(torch.FloatTensor))`, optional, returned when `use_cache=True` is passed or when `config.use_cache=True`) — Tuple of `tuple(torch.FloatTensor)` of length `config.n_layers`, with each tuple having 2 tensors of shape `(batch_size, num_heads, sequence_length, embed_size_per_head)`)

  Contains pre-computed hidden-states (key and values in the self-attention blocks) that can be used (see `past_key_values` input) to speed up sequential decoding.

- hidden_states (`tuple(torch.FloatTensor)`, optional, returned when `output_hidden_states=True` is passed or when `config.output_hidden_states=True`) — Tuple of `torch.FloatTensor` (one for the output of the embeddings, if the model has an embedding layer, + one for the output of each layer) of shape `(batch_size, sequence_length, hidden_size)`.

  Hidden-states of the model at the output of each layer plus the optional initial embedding outputs.

- attentions (`tuple(torch.FloatTensor)`, optional, returned when `output_attentions=True` is passed or when `config.output_attentions=True`) — Tuple of `torch.FloatTensor` (one for each layer) of shape `(batch_size, num_heads, sequence_length, sequence_length)`.

  Attentions weights after the attention softmax, used to compute the weighted average in the self-attention heads.

- image_hidden_states (`torch.FloatTensor`, optional) — A `torch.FloatTensor` of size (batch_size, num_images, sequence_length, hidden_size)`. image_hidden_states of the model produced by the vision encoder and after projecting the last hidden state.

The InternVLForConditionalGeneration forward method, overrides the `__call__` special method.

Although the recipe for forward pass needs to be defined within this function, one should call the `Module` instance afterwards instead of this since the former takes care of running the pre and post processing steps while the latter silently ignores them.

Example:

    >>> import torch
    >>> from transformers import AutoProcessor, AutoModelForImageTextToText

    >>> torch_device = "cuda"
    >>> processor = AutoProcessor.from_pretrained("OpenGVLab/InternVL3-1B-hf")
    >>> model = AutoModelForImageTextToText.from_pretrained(
    ...     "OpenGVLab/InternVL3-1B-hf", torch_dtype=torch.bfloat16, device_map=torch_device
    ... )

    >>> messages = [
    ...     {
    ...         "role": "user",
    ...         "content": [
    ...             {
    ...                 "type": "image",
    ...                 "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg",
    ...             },
    ...             {
    ...                 "type": "image",
    ...                 "url": "https://thumbs.dreamstime.com/b/golden-gate-bridge-san-francisco-purple-flowers-california-echium-candicans-36805947.jpg",
    ...             },
    ...             {"type": "text", "text": "These images depict two different landmarks. Can you identify them?"},
    ...         ],
    ...     },
    ... ]

    >>> inputs = processor.apply_chat_template(messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt").to(torch_device)
    >>> generate_ids = model.generate(**inputs, max_new_tokens=200)
    >>> print(processor.decode(generate_ids[0, inputs["input_ids"].shape[1] :], skip_special_tokens=True))
    The images depict the Statue of Liberty and the Golden Gate Bridge.

##
InternVLProcessor

###
class transformers.InternVLProcessor

< source >

( image_processor = None tokenizer = None image_seq_length: int = 256 chat_template = None **kwargs )

Parameters

- image_processor (AutoImageProcessor, optional) — The image processor is a required input.
- tokenizer ([`PreTrainedTokenizer`, `PreTrainedTokenizerFast`], optional) — The tokenizer is a required input.
- image_seq_length (`int`, optional, defaults to 256) — The number of image token to use per image patch. it should be set so that: image_seq_length = (config.image_size // config.patch_size) 2 * (config.scale_factor2)
- chat_template (`str`, optional) — A Jinja template which will be used to convert lists of messages in a chat into a tokenizable string.

Constructs a InternVL processor which wraps a AutoImageProcessor and `PretrainedTokenizerFast` tokenizer into a single processor that inherits both the image processor and tokenizer functionalities. See the `__call__()` and decode() for more information.

####
batch_decode

< source >

( *args **kwargs )

This method forwards all its arguments to PreTrainedTokenizerFast’s batch_decode(). Please refer to the docstring of this method for more information.

####
decode

< source >

( *args **kwargs )

This method forwards all its arguments to PreTrainedTokenizerFast’s decode(). Please refer to the docstring of this method for more information.

####
sample_indices_fn

< source >

( metadata: VideoMetadata num_frames: int = None initial_shift: typing.Union[bool, float, int] = True ) →
`np.ndarray`

Parameters

- metadata (`VideoMetadata`) — `VideoMetadata` object containing metadat about the video, such as “total_num_frames” or “fps”.
- num_frames (`int`, optional) — Number of frames to sample uniformly. If None, all frames are sampled.
- initial_shift (`bool`, `float` or `int`, defaults to `0`) — The initial shift to apply when sampling frames. If `True`, the shift is set so that frames are sampled from the middle of the video.

Returns

`np.ndarray`

Array of frame indices to sample.

The function to generate indices of frames to sample from a video.

< > Update on GitHub