Image-Text-to-Text
Transformers
Safetensors
English
qwen2_5_vl
VLM
Computer-Use-Agent
OS-Agent
GUI
Grounding
conversational
custom_code
text-generation-inference
Instructions to use Adocados/GTA1-32B-vllm with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use Adocados/GTA1-32B-vllm with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="Adocados/GTA1-32B-vllm", trust_remote_code=True) messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("Adocados/GTA1-32B-vllm", trust_remote_code=True) model = AutoModelForMultimodalLM.from_pretrained("Adocados/GTA1-32B-vllm", trust_remote_code=True, device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=40) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use Adocados/GTA1-32B-vllm with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "Adocados/GTA1-32B-vllm" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Adocados/GTA1-32B-vllm", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/Adocados/GTA1-32B-vllm
- SGLang
How to use Adocados/GTA1-32B-vllm with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "Adocados/GTA1-32B-vllm" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Adocados/GTA1-32B-vllm", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "Adocados/GTA1-32B-vllm" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "Adocados/GTA1-32B-vllm", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use Adocados/GTA1-32B-vllm with Docker Model Runner:
docker model run hf.co/Adocados/GTA1-32B-vllm
| # ------------------------------------------------------------------------------ | |
| # OpenCUA‑7B Model | |
| # | |
| # This implementation is adapted from the Qwen2‑VL reference code in | |
| # Hugging Face Transformers v4.53.0: | |
| # https://github.com/huggingface/transformers/tree/v4.53.0/src/transformers/models/qwen2_5_vl | |
| # | |
| # Checkpoint used for weight initialisation: | |
| # "Qwen/Qwen2.5-VL-32B-Instruct" – https://huggingface.co/Qwen/Qwen2.5-VL-7B-Instruct | |
| # | |
| # Key modifications | |
| # ----------------- | |
| # • Replaced Multimodal Rotary Position Embedding (M‑RoPE) with 1‑D RoPE for | |
| # compatibility with OpenCUA training settings. | |
| # • Wrapped vision encoder and language model into a single | |
| # `OpenCUAForConditionalGeneration` class. | |
| # • Simplified weight initialisation — this file targets inference / fine‑tuning, | |
| # not training from scratch. | |
| # | |
| # Copyright (c) 2025 XLANG Lab, The University of Hong Kong | |
| # | |
| # Permission is hereby granted, free of charge, to any person obtaining a copy | |
| # of this software and associated documentation files (the “Software”), to deal | |
| # in the Software without restriction, including without limitation the rights | |
| # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| # copies of the Software, and to permit persons to whom the Software is | |
| # furnished to do so, subject to the following conditions: | |
| # | |
| # The above copyright notice and this permission notice shall be included in all | |
| # copies or substantial portions of the Software. | |
| # | |
| # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| # SOFTWARE. | |
| # | |
| # ------------------------------------------------------------------------------ | |
| # Prohibited Uses & Additional Disclaimer | |
| # --------------------------------------- | |
| # • The Software may **not** be used for any purpose or activity that violates | |
| # applicable laws or regulations in any jurisdiction. | |
| # • The authors, contributors, and copyright holders are **not responsible** | |
| # for any illegal, unethical, or harmful use of the Software, nor for any | |
| # direct or indirect damages resulting from such use. | |
| # • Use of the “OpenCUA” name, logo, or trademarks does **not** imply any | |
| # endorsement or affiliation unless a separate written permission is obtained. | |
| import torch | |
| import torch.nn as nn | |
| from transformers.cache_utils import Cache | |
| from transformers.modeling_utils import PreTrainedModel | |
| from transformers.models.llava.modeling_llava import LlavaCausalLMOutputWithPast | |
| from .configuration_opencua import OpenCUAConfig | |
| from transformers.models.qwen2_5_vl.modeling_qwen2_5_vl import Qwen2_5_VisionTransformerPretrainedModel | |
| from transformers.models.qwen2.modeling_qwen2 import Qwen2ForCausalLM | |
| class OpenCUAPreTrainedModel(PreTrainedModel): | |
| config_class = OpenCUAConfig | |
| base_model_prefix = "model" | |
| _no_split_modules = ["Qwen2_5_VisionTransformerPretrainedModel"] | |
| _skip_keys_device_placement = "past_key_values" | |
| _supports_flash_attn_2 = True | |
| def _init_weights(self, module): | |
| # important: this ported version of Llava isn't meant for training from scratch - only | |
| # inference and fine-tuning - so the proper init weights code has been removed - the original codebase | |
| # https://github.com/haotian-liu/LLaVA/tree/main/llava should serve for that purpose | |
| std = ( | |
| self.config.initializer_range | |
| if hasattr(self.config, "initializer_range") | |
| else self.config.text_config.initializer_range | |
| ) | |
| if hasattr(module, "class_embedding"): | |
| module.class_embedding.data.normal_(mean=0.0, std=std) | |
| if isinstance(module, (nn.Linear, nn.Conv2d)): | |
| module.weight.data.normal_(mean=0.0, std=std) | |
| if module.bias is not None: | |
| module.bias.data.zero_() | |
| elif isinstance(module, nn.Embedding): | |
| module.weight.data.normal_(mean=0.0, std=std) | |
| if module.padding_idx is not None: | |
| module.weight.data[module.padding_idx].zero_() | |
| def _supports_sdpa(self): | |
| """ | |
| Retrieve language_model's attribute to check whether the model supports | |
| SDPA or not. | |
| """ | |
| return self.language_model._supports_sdpa | |
| class OpenCUAForConditionalGeneration(OpenCUAPreTrainedModel): | |
| def __init__(self, config: OpenCUAConfig): | |
| super().__init__(config) | |
| self.vision_tower = Qwen2_5_VisionTransformerPretrainedModel(config.vision_config) | |
| self.language_model = Qwen2ForCausalLM(config.text_config) | |
| self.post_init() | |
| def get_input_embeddings(self): | |
| return self.language_model.get_input_embeddings() | |
| def set_input_embeddings(self, value): | |
| self.language_model.set_input_embeddings(value) | |
| def get_output_embeddings(self): | |
| return self.language_model.get_output_embeddings() | |
| def set_output_embeddings(self, new_embeddings): | |
| self.language_model.set_output_embeddings(new_embeddings) | |
| def set_decoder(self, decoder): | |
| self.language_model.set_decoder(decoder) | |
| def get_decoder(self): | |
| return self.language_model.get_decoder() | |
| def tie_weights(self): | |
| return self.language_model.tie_weights() | |
| def resize_token_embeddings(self, new_num_tokens: int | None = None, pad_to_multiple_of=None) -> nn.Embedding: | |
| model_embeds = self.language_model.resize_token_embeddings( | |
| new_num_tokens, pad_to_multiple_of) | |
| # update vocab size | |
| self.config.text_config.vocab_size = model_embeds.num_embeddings | |
| self.vocab_size = model_embeds.num_embeddings | |
| return model_embeds | |
| def _merge_input_ids_with_image_features( | |
| self, | |
| image_features: torch.Tensor, | |
| feature_lengths: list[int], | |
| inputs_embeds: torch.Tensor, | |
| input_ids: torch.Tensor, | |
| attention_mask: torch.Tensor, | |
| labels: torch.Tensor | None = None): | |
| """ | |
| Args: | |
| image_features (:obj:`torch.Tensor` of shape :obj:`(num_image_tokens, embed_dim)`): | |
| The image features to merge with the input embeddings. | |
| feature_lengths: the length of image feature. | |
| inputs_embeds (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length, embed_dim)`): | |
| The input embeddings. | |
| input_ids (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`): | |
| The input ids. | |
| attention_mask (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`): | |
| The attention mask. | |
| labels (:obj:`torch.Tensor` of shape :obj:`(batch_size, sequence_length)`, *optional*): | |
| The labels. | |
| """ | |
| image_token_index: int = self.config.media_placeholder_token_id | |
| pad_token_id: int = self.config.pad_token_id | |
| ignore_index: int = self.config.ignore_index | |
| _, embed_dim = image_features.shape | |
| batch_size, sequence_length = input_ids.shape | |
| left_padding = not torch.sum( | |
| input_ids[:, -1] == torch.tensor(pad_token_id)) | |
| # 1. Create a mask to know where special image tokens are | |
| _token_occupation_table = torch.ones_like(input_ids.flatten()) | |
| _token_occupation_table[input_ids.flatten() == image_token_index] = \ | |
| torch.tensor(feature_lengths, | |
| dtype=torch.long, device=input_ids.device) | |
| _token_occupation_table = _token_occupation_table.reshape( | |
| input_ids.shape) | |
| max_embed_dim = _token_occupation_table.sum(-1).max().item() | |
| assert max_embed_dim >= sequence_length, ( | |
| f"The maximum embedding dimension ({max_embed_dim}) is less than the sequence length ({sequence_length})" | |
| ) | |
| batch_indices, non_image_indices = torch.where(input_ids != image_token_index) | |
| # 2. Compute the positions where text should be written | |
| # Calculate new positions for text tokens in merged image-text sequence. | |
| new_token_positions = torch.cumsum(_token_occupation_table, -1) - 1 | |
| nb_image_pad = max_embed_dim - 1 - new_token_positions[:, -1] | |
| if left_padding: | |
| new_token_positions += nb_image_pad[:, None] # offset for left padding | |
| text_to_overwrite = new_token_positions[batch_indices, non_image_indices] | |
| # 3. Create the full embedding, already padded to the maximum position | |
| final_embedding = torch.zeros( | |
| batch_size, max_embed_dim, embed_dim, dtype=inputs_embeds.dtype, device=inputs_embeds.device | |
| ) | |
| final_attention_mask = torch.zeros( | |
| batch_size, max_embed_dim, dtype=attention_mask.dtype, device=inputs_embeds.device | |
| ) | |
| if labels is not None: | |
| final_labels = torch.full( | |
| (batch_size, max_embed_dim), ignore_index, dtype=input_ids.dtype, device=input_ids.device | |
| ) | |
| # In case the Vision model or the Language model has been offloaded to CPU, we need to manually | |
| # set the corresponding tensors into their correct target device. | |
| target_device = inputs_embeds.device | |
| batch_indices, non_image_indices, text_to_overwrite = ( | |
| batch_indices.to(target_device), | |
| non_image_indices.to(target_device), | |
| text_to_overwrite.to(target_device), | |
| ) | |
| attention_mask = attention_mask.to(target_device) | |
| # 4. Fill the embeddings based on the mask. | |
| final_embedding[batch_indices, text_to_overwrite] = inputs_embeds[batch_indices, non_image_indices] | |
| final_attention_mask[batch_indices, text_to_overwrite] = attention_mask[batch_indices, non_image_indices] | |
| if labels is not None: | |
| final_labels[batch_indices, text_to_overwrite] = labels[batch_indices, non_image_indices] | |
| # 5. Fill the embeddings corresponding to the images. Anything that is not `text_positions` needs filling (#29835) | |
| image_to_overwrite = torch.full( | |
| (batch_size, max_embed_dim), True, dtype=torch.bool, device=inputs_embeds.device | |
| ) | |
| image_to_overwrite[batch_indices, text_to_overwrite] = False | |
| image_to_overwrite &= image_to_overwrite.cumsum(-1) - 1 >= nb_image_pad[:, None].to(target_device) | |
| if image_to_overwrite.sum() != image_features.shape[:-1].numel(): | |
| raise ValueError( | |
| f"The input provided to the model are wrong. The number of image tokens is {image_to_overwrite.sum()} while" | |
| f" the number of image features given to the model is {image_features.shape[:-1].numel()}. " | |
| "This prevents correct indexing and breaks batch generation." | |
| ) | |
| final_embedding[image_to_overwrite] = image_features.contiguous().reshape(-1, embed_dim).to(target_device) | |
| final_attention_mask |= image_to_overwrite | |
| position_ids = (final_attention_mask.cumsum(-1) - 1).masked_fill_((final_attention_mask == 0), 1) | |
| # 6. Mask out the embedding at padding positions, as we later use the past_key_value value to determine the non-attended tokens. | |
| batch_indices, pad_indices = torch.where(input_ids == pad_token_id) | |
| indices_to_mask = new_token_positions[batch_indices, pad_indices] | |
| final_embedding[batch_indices, indices_to_mask] = 0 | |
| if labels is None: | |
| final_labels = None | |
| return final_embedding, final_attention_mask, final_labels, position_ids | |
| def _extract_image_features(self, | |
| pixel_values: torch.FloatTensor | list[torch.FloatTensor], | |
| grid_thws: torch.FloatTensor, | |
| ): | |
| """ | |
| Args: | |
| pixel_values (:obj:`torch.FloatTensor` of shape :obj:`(sum_num_image_tokens, channels)`): | |
| The pixel values of the images processed by image processor. | |
| grid_thws: (B,3) | |
| Returns: | |
| selected_image_feature (:obj:`torch.FloatTensor` of shape :obj:`(num_image_tokens, embed_dim)`): | |
| The selected image features to use as input to the projector head. | |
| """ | |
| assert len(grid_thws.shape)==2 and grid_thws.shape[1]==3, f"grid_thws must be a 2D tensor with shape (batched, 3), but got {grid_thws.shape}" | |
| if isinstance(pixel_values, list): | |
| pixel_values = torch.cat(pixel_values, dim=0) | |
| image_features_ = self.vision_tower(pixel_values, grid_thw=grid_thws) | |
| image_features_list = [] | |
| start_idx = 0 | |
| for i, grid_thw in enumerate(grid_thws): | |
| end_idx = start_idx + (grid_thw[0] * grid_thw[1] * grid_thw[2]) // 4 | |
| image_features_list.append(image_features_[start_idx:end_idx, :]) | |
| start_idx = end_idx | |
| selected_image_feature = torch.cat(image_features_list, dim=0) | |
| feature_lengths = [x.size(0) for x in image_features_list] | |
| return selected_image_feature, feature_lengths | |
| def forward( | |
| self, | |
| input_ids: torch.LongTensor | None = None, | |
| pixel_values: torch.FloatTensor | list[torch.FloatTensor] | None = None, | |
| grid_thws: torch.Tensor = None, | |
| attention_mask: torch.Tensor | None = None, | |
| position_ids: torch.LongTensor | None = None, | |
| past_key_values: list[torch.FloatTensor] | None = None, | |
| inputs_embeds: torch.FloatTensor | None = None, | |
| labels: torch.LongTensor | None = None, | |
| use_cache: bool | None = None, | |
| output_attentions: bool | None = None, | |
| output_hidden_states: bool | None = None, | |
| return_dict: bool | None = None, | |
| ) -> tuple | LlavaCausalLMOutputWithPast: | |
| r""" | |
| 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]`. | |
| ```""" | |
| output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions | |
| output_hidden_states = ( | |
| output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states | |
| ) | |
| return_dict = return_dict if return_dict is not None else self.config.use_return_dict | |
| if inputs_embeds is None: | |
| # 1. Extra the input embeddings | |
| inputs_embeds = self.get_input_embeddings()(input_ids) | |
| # 2. Merge text and images | |
| if pixel_values is not None and len(pixel_values) > 0 and input_ids.shape[1] != 1: | |
| image_feature, feature_lengths = self._extract_image_features( | |
| pixel_values, grid_thws) | |
| inputs_embeds = inputs_embeds.to(image_feature.dtype) # num_tokens, embed_dim | |
| inputs_embeds, attention_mask, labels, position_ids = \ | |
| self._merge_input_ids_with_image_features(image_feature, feature_lengths, inputs_embeds, input_ids, attention_mask, labels | |
| ) | |
| # In case input_ids.shape[1] == 1 & pixel_values==None & past_key_values != None, we are in the case of | |
| # generation with cache | |
| elif past_key_values is not None and pixel_values is not None and input_ids.shape[1] == 1: | |
| # Retrieve the first layer to inspect the logits and mask out the hidden states | |
| # that are set to 0 | |
| first_layer_past_key_value = past_key_values[0][0][:, :, :, 0] | |
| # Sum all dimensions of head_dim (-2) to avoid random errors such as: https://github.com/huggingface/transformers/pull/28032#issuecomment-1863691941 | |
| batch_index, non_attended_tokens = torch.where(first_layer_past_key_value.float().sum(-2) == 0) | |
| # Get the target length | |
| target_length = input_ids.shape[1] | |
| past_length = first_layer_past_key_value.shape[-1] | |
| extended_attention_mask = torch.ones( | |
| (attention_mask.shape[0], past_length), | |
| dtype=attention_mask.dtype, | |
| device=attention_mask.device, | |
| ) | |
| # Filter out only the tokens that can be un-attended, this can happen | |
| # if one uses Llava + Fused modules where the cache on the | |
| # first iteration is already big enough, or if one passes custom cache | |
| valid_indices = non_attended_tokens < extended_attention_mask.size(-1) | |
| new_batch_index = batch_index[valid_indices] | |
| new_non_attended_tokens = non_attended_tokens[valid_indices] | |
| # Zero-out the places where we don't need to attend | |
| extended_attention_mask[new_batch_index, new_non_attended_tokens] = 0 | |
| attention_mask = torch.cat((extended_attention_mask, attention_mask[:, -target_length:]), dim=1) | |
| position_ids = torch.sum(attention_mask, dim=1).unsqueeze(-1) - 1 | |
| outputs = self.language_model( | |
| attention_mask=attention_mask, | |
| position_ids=position_ids, | |
| past_key_values=past_key_values, | |
| inputs_embeds=inputs_embeds, | |
| use_cache=use_cache, | |
| output_attentions=output_attentions, | |
| output_hidden_states=output_hidden_states, | |
| return_dict=return_dict, | |
| ) | |
| logits = outputs[0] | |
| loss = None | |
| if labels is not None: | |
| # Shift so that tokens < n predict n | |
| if attention_mask is not None: | |
| shift_attention_mask = attention_mask[..., 1:] | |
| shift_logits = logits[..., :-1, :][shift_attention_mask.to(logits.device) != 0].contiguous() | |
| shift_labels = labels[..., 1:][shift_attention_mask.to(labels.device) != 0].contiguous() | |
| else: | |
| shift_logits = logits[..., :-1, :].contiguous() | |
| shift_labels = labels[..., 1:].contiguous() | |
| # Flatten the tokens | |
| loss_fct = nn.CrossEntropyLoss() | |
| loss = loss_fct( | |
| shift_logits.view(-1, shift_logits.size(-1)), shift_labels.view(-1).to(shift_logits.device) | |
| ) | |
| if not return_dict: | |
| output = (logits,) + outputs[1:] | |
| return (loss,) + output if loss is not None else output | |
| return LlavaCausalLMOutputWithPast( | |
| loss=loss, | |
| logits=logits, | |
| past_key_values=outputs.past_key_values, | |
| hidden_states=outputs.hidden_states, | |
| attentions=outputs.attentions, | |
| ) | |
| def prepare_inputs_for_generation( | |
| self, input_ids, past_key_values=None, inputs_embeds=None, pixel_values=None, grid_thws=None, attention_mask=None, **kwargs | |
| ): | |
| if past_key_values is not None: | |
| if isinstance(past_key_values, Cache): | |
| cache_length = past_key_values.get_seq_length() | |
| past_length = past_key_values.seen_tokens | |
| else: | |
| cache_length = past_length = past_key_values[0][0].shape[2] | |
| # Keep only the unprocessed tokens: | |
| # 1 - If the length of the attention_mask exceeds the length of input_ids, then we are in a setting where | |
| # some of the inputs are exclusively passed as part of the cache (e.g. when passing input_embeds as | |
| # input) | |
| if attention_mask is not None and attention_mask.shape[1] > input_ids.shape[1]: | |
| input_ids = input_ids[:, -(attention_mask.shape[1] - past_length) :] | |
| # 2 - If the past_length is smaller than input_ids', then input_ids holds all input tokens. We can discard | |
| # input_ids based on the past_length. | |
| elif past_length < input_ids.shape[1]: | |
| input_ids = input_ids[:, past_length:] | |
| # 3 - Otherwise (past_length >= input_ids.shape[1]), let's assume input_ids only has unprocessed tokens. | |
| elif self.config.media_placeholder_token_id in input_ids: | |
| input_ids = input_ids[:, input_ids.shape[1] - 1 :] | |
| # If the cache has seen more tokens than it can hold, then the cache has a size limit. Let's discard the | |
| # older attention values, as their corresponding values are not part of the input. | |
| if cache_length < past_length and attention_mask is not None: | |
| attention_mask = attention_mask[:, -(cache_length + input_ids.shape[1]) :] | |
| position_ids = kwargs.get("position_ids", None) | |
| if attention_mask is not None and position_ids is None: | |
| # create position_ids on the fly for batch generation | |
| position_ids = attention_mask.long().cumsum(-1) - 1 | |
| position_ids.masked_fill_(attention_mask == 0, 1) | |
| if past_key_values: | |
| position_ids = position_ids[:, -input_ids.shape[1] :] | |
| # if `inputs_embeds` are passed, we only want to use them in the 1st generation step | |
| if inputs_embeds is not None and past_key_values is None: | |
| model_inputs = {"inputs_embeds": inputs_embeds} | |
| else: | |
| model_inputs = {"input_ids": input_ids} | |
| model_inputs.update( | |
| { | |
| "position_ids": position_ids, | |
| "past_key_values": past_key_values, | |
| "use_cache": kwargs.get("use_cache"), | |
| "attention_mask": attention_mask, | |
| "pixel_values": pixel_values, | |
| "grid_thws": grid_thws, | |
| } | |
| ) | |
| return model_inputs | |
| def _reorder_cache(self, *args, **kwargs): | |
| return self.language_model._reorder_cache(*args, **kwargs) | |