Buckets:
| # Granite Vision | |
| ## Overview | |
| The [Granite Vision](https://www.ibm.com/new/announcements/ibm-granite-3-1-powerful-performance-long-context-and-more) model is a variant of [LLaVA-NeXT](llava_next), leveraging a [Granite](granite) language model alongside a [SigLIP](SigLIP) visual encoder. It utilizes multiple concatenated vision hidden states as its image features, similar to [VipLlava](vipllava). It also uses a larger set of image grid pinpoints than the original LlaVa-NeXT models to support additional aspect ratios. | |
| Tips: | |
| - This model is loaded into Transformers as an instance of LlaVA-Next. The usage and tips from [LLaVA-NeXT](llava_next) apply to this model as well. | |
| - You can apply the chat template on the tokenizer / processor in the same way as well. Example chat format: | |
| ```bash | |
| "<|user|>\nWhat’s shown in this image?\n<|assistant|>\nThis image shows a red stop sign.<|end_of_text|><|user|>\nDescribe the image in more details.\n<|assistant|>\n" | |
| ``` | |
| Sample inference: | |
| ```python | |
| from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration | |
| from accelerate import Accelerator | |
| device = Accelerator().device | |
| model_path = "ibm-granite/granite-vision-3.1-2b-preview" | |
| processor = LlavaNextProcessor.from_pretrained(model_path) | |
| model = LlavaNextForConditionalGeneration.from_pretrained(model_path).to(device) | |
| # prepare image and text prompt, using the appropriate prompt template | |
| url = "https://github.com/haotian-liu/LLaVA/blob/1a91fc274d7c35a9b50b3cb29c4247ae5837ce39/images/llava_v1_5_radar.jpg?raw=true" | |
| conversation = [ | |
| { | |
| "role": "user", | |
| "content": [ | |
| {"type": "image", "url": url}, | |
| {"type": "text", "text": "What is shown in this image?"}, | |
| ], | |
| }, | |
| ] | |
| inputs = processor.apply_chat_template( | |
| conversation, | |
| add_generation_prompt=True, | |
| tokenize=True, | |
| return_dict=True, | |
| return_tensors="pt" | |
| ).to(model.device) | |
| # autoregressively complete prompt | |
| output = model.generate(**inputs, max_new_tokens=100) | |
| print(processor.decode(output[0], skip_special_tokens=True)) | |
| ``` | |
| This model was contributed by [Alexander Brooks](https://huggingface.co/abrooks9944). | |
| ## LlavaNextConfig[[transformers.LlavaNextConfig]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.LlavaNextConfig</name><anchor>transformers.LlavaNextConfig</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/configuration_llava_next.py#L24</source><parameters>[{"name": "vision_config", "val": " = None"}, {"name": "text_config", "val": " = None"}, {"name": "image_token_index", "val": " = 32000"}, {"name": "projector_hidden_act", "val": " = 'gelu'"}, {"name": "vision_feature_select_strategy", "val": " = 'default'"}, {"name": "vision_feature_layer", "val": " = -2"}, {"name": "image_grid_pinpoints", "val": " = None"}, {"name": "tie_word_embeddings", "val": " = False"}, {"name": "image_seq_length", "val": " = 576"}, {"name": "multimodal_projector_bias", "val": " = True"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **vision_config** (`Union[AutoConfig, dict]`, *optional*, defaults to `CLIPVisionConfig`) -- | |
| The config object or dictionary of the vision backbone. | |
| - **text_config** (`Union[AutoConfig, dict]`, *optional*, defaults to `LlamaConfig`) -- | |
| The config object or dictionary of the text backbone. | |
| - **image_token_index** (`int`, *optional*, defaults to 32000) -- | |
| The image token index to encode the image prompt. | |
| - **projector_hidden_act** (`str`, *optional*, defaults to `"gelu"`) -- | |
| The activation function used by the multimodal projector. | |
| - **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"`. If `"default"`, the CLS token is removed from the vision features. | |
| If `"full"`, the full vision features are used. | |
| - **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. | |
| - **image_grid_pinpoints** (`List`, *optional*, defaults to `[[336, 672], [672, 336], [672, 672], [1008, 336], [336, 1008]]`) -- | |
| A list of possible resolutions to use for processing high resolution images. Each item in the list should be a tuple or list | |
| of the form `(height, width)`. | |
| - **tie_word_embeddings** (`bool`, *optional*, defaults to `False`) -- | |
| Whether the model's input and output word embeddings should be tied. | |
| - **image_seq_length** (`int`, *optional*, defaults to 576) -- | |
| Sequence length of one image embedding. | |
| - **multimodal_projector_bias** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to use bias in the multimodal projector.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| This is the configuration class to store the configuration of a [LlavaNextForConditionalGeneration](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextForConditionalGeneration). It is used to instantiate an | |
| Llava-NeXT 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 [llava-hf/llava-v1.6-mistral-7b-hf](https://huggingface.co/llava-hf/llava-v1.6-mistral-7b-hf) | |
| model. | |
| Configuration objects inherit from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) and can be used to control the model outputs. Read the | |
| documentation from [PreTrainedConfig](/docs/transformers/pr_33962/en/main_classes/configuration#transformers.PreTrainedConfig) for more information. | |
| <ExampleCodeBlock anchor="transformers.LlavaNextConfig.example"> | |
| Example: | |
| ```python | |
| >>> from transformers import LlavaNextForConditionalGeneration, LlavaNextConfig, CLIPVisionConfig, LlamaConfig | |
| >>> # Initializing a CLIP-vision config | |
| >>> vision_config = CLIPVisionConfig() | |
| >>> # Initializing a Llama config | |
| >>> text_config = LlamaConfig() | |
| >>> # Initializing a Llava-Next llava-hf/llava-v1.6-mistral-7b-hf style configuration | |
| >>> configuration = LlavaNextConfig(vision_config, text_config) | |
| >>> # Initializing a model from the llava-hf/llava-v1.6-mistral-7b-hf style configuration | |
| >>> model = LlavaNextForConditionalGeneration(configuration) | |
| >>> # Accessing the model configuration | |
| >>> configuration = model.config | |
| ``` | |
| </ExampleCodeBlock> | |
| </div> | |
| ## LlavaNextImageProcessor[[transformers.LlavaNextImageProcessor]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.LlavaNextImageProcessor</name><anchor>transformers.LlavaNextImageProcessor</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/image_processing_llava_next.py#L119</source><parameters>[{"name": "do_resize", "val": ": bool = True"}, {"name": "size", "val": ": typing.Optional[dict[str, int]] = None"}, {"name": "image_grid_pinpoints", "val": ": typing.Optional[list] = None"}, {"name": "resample", "val": ": Resampling = <Resampling.BICUBIC: 3>"}, {"name": "do_center_crop", "val": ": bool = True"}, {"name": "crop_size", "val": ": typing.Optional[dict[str, int]] = None"}, {"name": "do_rescale", "val": ": bool = True"}, {"name": "rescale_factor", "val": ": typing.Union[int, float] = 0.00392156862745098"}, {"name": "do_normalize", "val": ": bool = True"}, {"name": "image_mean", "val": ": typing.Union[float, list[float], NoneType] = None"}, {"name": "image_std", "val": ": typing.Union[float, list[float], NoneType] = None"}, {"name": "do_pad", "val": ": typing.Optional[bool] = True"}, {"name": "do_convert_rgb", "val": ": bool = True"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **do_resize** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to resize the image's (height, width) dimensions to the specified `size`. Can be overridden by | |
| `do_resize` in the `preprocess` method. | |
| - **size** (`dict[str, int]` *optional*, defaults to `{"shortest_edge" -- 224}`): | |
| Size of the image after resizing. The shortest edge of the image is resized to size["shortest_edge"], with | |
| the longest edge resized to keep the input aspect ratio. Can be overridden by `size` in the `preprocess` | |
| method. | |
| - **image_grid_pinpoints** (`List` *optional*, defaults to `[[672, 336], [336, 672], [672, 672], [336, 1008], [1008, 336]]`) -- | |
| A list of possible resolutions to use for processing high resolution images. The best resolution is selected | |
| based on the original size of the image. Can be overridden by `image_grid_pinpoints` in the `preprocess` | |
| method. | |
| - **resample** (`PILImageResampling`, *optional*, defaults to `Resampling.BICUBIC`) -- | |
| Resampling filter to use if resizing the image. Can be overridden by `resample` in the `preprocess` method. | |
| - **do_center_crop** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to center crop the image to the specified `crop_size`. Can be overridden by `do_center_crop` in the | |
| `preprocess` method. | |
| - **crop_size** (`dict[str, int]` *optional*, defaults to 224) -- | |
| Size of the output image after applying `center_crop`. Can be overridden by `crop_size` in the `preprocess` | |
| method. | |
| - **do_rescale** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to rescale the image by the specified scale `rescale_factor`. Can be overridden by `do_rescale` in | |
| the `preprocess` method. | |
| - **rescale_factor** (`int` or `float`, *optional*, defaults to `1/255`) -- | |
| Scale factor to use if rescaling the image. Can be overridden by `rescale_factor` in the `preprocess` | |
| method. | |
| - **do_normalize** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to normalize the image. Can be overridden by `do_normalize` in the `preprocess` method. | |
| - **image_mean** (`float` or `list[float]`, *optional*, defaults to `[0.48145466, 0.4578275, 0.40821073]`) -- | |
| Mean to use if normalizing the image. This is a float or list of floats the length of the number of | |
| channels in the image. Can be overridden by the `image_mean` parameter in the `preprocess` method. | |
| - **image_std** (`float` or `list[float]`, *optional*, defaults to `[0.26862954, 0.26130258, 0.27577711]`) -- | |
| Standard deviation to use if normalizing the image. This is a float or list of floats the length of the | |
| number of channels in the image. Can be overridden by the `image_std` parameter in the `preprocess` method. | |
| Can be overridden by the `image_std` parameter in the `preprocess` method. | |
| - **do_pad** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to pad the image. If `True`, will pad the patch dimension of the images in the batch to the largest | |
| number of patches in the batch. Padding will be applied to the bottom and right with zeros. | |
| - **do_convert_rgb** (`bool`, *optional*, defaults to `True`) -- | |
| Whether to convert the image to RGB.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| Constructs a LLaVa-NeXT image processor. Based on [CLIPImageProcessor](/docs/transformers/pr_33962/en/model_doc/clip#transformers.CLIPImageProcessor) with incorporation of additional techniques | |
| for processing high resolution images as explained in the [LLaVa paper](https://huggingface.co/papers/2310.03744). | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>preprocess</name><anchor>transformers.LlavaNextImageProcessor.preprocess</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/image_processing_llava_next.py#L563</source><parameters>[{"name": "images", "val": ": typing.Union[ForwardRef('PIL.Image.Image'), numpy.ndarray, ForwardRef('torch.Tensor'), list['PIL.Image.Image'], list[numpy.ndarray], list['torch.Tensor']]"}, {"name": "do_resize", "val": ": typing.Optional[bool] = None"}, {"name": "size", "val": ": typing.Optional[dict[str, int]] = None"}, {"name": "image_grid_pinpoints", "val": ": typing.Optional[list] = None"}, {"name": "resample", "val": ": typing.Optional[PIL.Image.Resampling] = None"}, {"name": "do_center_crop", "val": ": typing.Optional[bool] = None"}, {"name": "crop_size", "val": ": typing.Optional[int] = None"}, {"name": "do_rescale", "val": ": typing.Optional[bool] = None"}, {"name": "rescale_factor", "val": ": typing.Optional[float] = None"}, {"name": "do_normalize", "val": ": typing.Optional[bool] = None"}, {"name": "image_mean", "val": ": typing.Union[float, list[float], NoneType] = None"}, {"name": "image_std", "val": ": typing.Union[float, list[float], NoneType] = None"}, {"name": "do_pad", "val": ": typing.Optional[bool] = None"}, {"name": "do_convert_rgb", "val": ": typing.Optional[bool] = None"}, {"name": "return_tensors", "val": ": typing.Union[str, transformers.utils.generic.TensorType, NoneType] = None"}, {"name": "data_format", "val": ": typing.Optional[transformers.image_utils.ChannelDimension] = <ChannelDimension.FIRST: 'channels_first'>"}, {"name": "input_data_format", "val": ": typing.Union[str, transformers.image_utils.ChannelDimension, NoneType] = None"}]</parameters><paramsdesc>- **images** (`ImageInput`) -- | |
| Image to preprocess. Expects a single or batch of images with pixel values ranging from 0 to 255. If | |
| passing in images with pixel values between 0 and 1, set `do_rescale=False`. | |
| - **do_resize** (`bool`, *optional*, defaults to `self.do_resize`) -- | |
| Whether to resize the image. | |
| - **size** (`dict[str, int]`, *optional*, defaults to `self.size`) -- | |
| Size of the image after resizing. Shortest edge of the image is resized to size["shortest_edge"], with | |
| the longest edge resized to keep the input aspect ratio. | |
| - **image_grid_pinpoints** (`List` *optional*, defaults to `self.image_grid_pinpoints`) -- | |
| A list of possible resolutions to use for processing high resolution images. The best resolution is | |
| selected based on the original size of the image. | |
| - **resample** (`int`, *optional*, defaults to `self.resample`) -- | |
| Resampling filter to use if resizing the image. This can be one of the enum `PILImageResampling`. Only | |
| has an effect if `do_resize` is set to `True`. | |
| - **do_center_crop** (`bool`, *optional*, defaults to `self.do_center_crop`) -- | |
| Whether to center crop the image. | |
| - **crop_size** (`dict[str, int]`, *optional*, defaults to `self.crop_size`) -- | |
| Size of the center crop. Only has an effect if `do_center_crop` is set to `True`. | |
| - **do_rescale** (`bool`, *optional*, defaults to `self.do_rescale`) -- | |
| Whether to rescale the image. | |
| - **rescale_factor** (`float`, *optional*, defaults to `self.rescale_factor`) -- | |
| Rescale factor to rescale the image by if `do_rescale` is set to `True`. | |
| - **do_normalize** (`bool`, *optional*, defaults to `self.do_normalize`) -- | |
| Whether to normalize the image. | |
| - **image_mean** (`float` or `list[float]`, *optional*, defaults to `self.image_mean`) -- | |
| Image mean to use for normalization. Only has an effect if `do_normalize` is set to `True`. | |
| - **image_std** (`float` or `list[float]`, *optional*, defaults to `self.image_std`) -- | |
| Image standard deviation to use for normalization. Only has an effect if `do_normalize` is set to | |
| `True`. | |
| - **do_pad** (`bool`, *optional*, defaults to `self.do_pad`) -- | |
| Whether to pad the image. If `True`, will pad the patch dimension of the images in the batch to the largest | |
| number of patches in the batch. Padding will be applied to the bottom and right with zeros. | |
| - **do_convert_rgb** (`bool`, *optional*, defaults to `self.do_convert_rgb`) -- | |
| Whether to convert the image to RGB. | |
| - **return_tensors** (`str` or `TensorType`, *optional*) -- | |
| The type of tensors to return. Can be one of: | |
| - Unset: Return a list of `np.ndarray`. | |
| - `TensorType.PYTORCH` or `'pt'`: Return a batch of type `torch.Tensor`. | |
| - `TensorType.NUMPY` or `'np'`: Return a batch of type `np.ndarray`. | |
| - **data_format** (`ChannelDimension` or `str`, *optional*, defaults to `ChannelDimension.FIRST`) -- | |
| The channel dimension format for the output image. Can be one of: | |
| - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. | |
| - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. | |
| - Unset: Use the channel dimension format of the input image. | |
| - **input_data_format** (`ChannelDimension` or `str`, *optional*) -- | |
| The channel dimension format for the input image. If unset, the channel dimension format is inferred | |
| from the input image. Can be one of: | |
| - `"channels_first"` or `ChannelDimension.FIRST`: image in (num_channels, height, width) format. | |
| - `"channels_last"` or `ChannelDimension.LAST`: image in (height, width, num_channels) format. | |
| - `"none"` or `ChannelDimension.NONE`: image in (height, width) format.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| </div></div> | |
| ## LlavaNextProcessor[[transformers.LlavaNextProcessor]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.LlavaNextProcessor</name><anchor>transformers.LlavaNextProcessor</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/processing_llava_next.py#L51</source><parameters>[{"name": "image_processor", "val": " = None"}, {"name": "tokenizer", "val": " = None"}, {"name": "patch_size", "val": " = None"}, {"name": "vision_feature_select_strategy", "val": " = None"}, {"name": "chat_template", "val": " = None"}, {"name": "image_token", "val": " = '<image>'"}, {"name": "num_additional_image_tokens", "val": " = 0"}, {"name": "**kwargs", "val": ""}]</parameters><paramsdesc>- **image_processor** ([LlavaNextImageProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextImageProcessor), *optional*) -- | |
| The image processor is a required input. | |
| - **tokenizer** ([LlamaTokenizerFast](/docs/transformers/pr_33962/en/model_doc/llama#transformers.LlamaTokenizerFast), *optional*) -- | |
| The tokenizer is a required input. | |
| - **patch_size** (`int`, *optional*) -- | |
| Patch size from the vision tower. | |
| - **vision_feature_select_strategy** (`str`, *optional*) -- | |
| The feature selection strategy used to select the vision feature from the vision backbone. | |
| Should be same as in model's config | |
| - **chat_template** (`str`, *optional*) -- A Jinja template which will be used to convert lists of messages | |
| in a chat into a tokenizable string. | |
| - **image_token** (`str`, *optional*, defaults to `"<image>"`) -- | |
| Special token used to denote image location. | |
| - **num_additional_image_tokens** (`int`, *optional*, defaults to 0) -- | |
| Number of additional tokens added to the image embeddings, such as CLS (+1). If the backbone has no CLS or other | |
| extra tokens appended, no need to set this arg.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| Constructs a LLaVa-NeXT processor which wraps a LLaVa-NeXT image processor and a LLaMa tokenizer into a single processor. | |
| [LlavaNextProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextProcessor) offers all the functionalities of [LlavaNextImageProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextImageProcessor) and [LlamaTokenizerFast](/docs/transformers/pr_33962/en/model_doc/llama#transformers.LlamaTokenizerFast). See the | |
| `__call__()` and [decode()](/docs/transformers/pr_33962/en/main_classes/processors#transformers.ProcessorMixin.decode) for more information. | |
| </div> | |
| ## LlavaNextForConditionalGeneration[[transformers.LlavaNextForConditionalGeneration]] | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>class transformers.LlavaNextForConditionalGeneration</name><anchor>transformers.LlavaNextForConditionalGeneration</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/modeling_llava_next.py#L534</source><parameters>[{"name": "config", "val": ": LlavaNextConfig"}]</parameters><paramsdesc>- **config** ([LlavaNextConfig](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextConfig)) -- | |
| 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()](/docs/transformers/pr_33962/en/main_classes/model#transformers.PreTrainedModel.from_pretrained) method to load the model weights.</paramsdesc><paramgroups>0</paramgroups></docstring> | |
| The LLAVA-NeXT model which consists of a vision backbone and a language model. | |
| This model inherits from [PreTrainedModel](/docs/transformers/pr_33962/en/main_classes/model#transformers.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](https://pytorch.org/docs/stable/nn.html#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. | |
| <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> | |
| <docstring><name>forward</name><anchor>transformers.LlavaNextForConditionalGeneration.forward</anchor><source>https://github.com/huggingface/transformers/blob/vr_33962/src/transformers/models/llava_next/modeling_llava_next.py#L600</source><parameters>[{"name": "input_ids", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "pixel_values", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "image_sizes", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "attention_mask", "val": ": typing.Optional[torch.Tensor] = None"}, {"name": "position_ids", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "past_key_values", "val": ": typing.Optional[transformers.cache_utils.Cache] = None"}, {"name": "inputs_embeds", "val": ": typing.Optional[torch.FloatTensor] = None"}, {"name": "vision_feature_layer", "val": ": typing.Union[int, list[int], NoneType] = None"}, {"name": "vision_feature_select_strategy", "val": ": typing.Optional[str] = None"}, {"name": "labels", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "use_cache", "val": ": typing.Optional[bool] = None"}, {"name": "output_attentions", "val": ": typing.Optional[bool] = None"}, {"name": "output_hidden_states", "val": ": typing.Optional[bool] = None"}, {"name": "cache_position", "val": ": typing.Optional[torch.LongTensor] = None"}, {"name": "logits_to_keep", "val": ": typing.Union[int, torch.Tensor] = 0"}, {"name": "**kwargs", "val": ": typing_extensions.Unpack[transformers.utils.generic.TransformersKwargs]"}]</parameters><paramsdesc>- **input_ids** (`torch.LongTensor` of shape `(batch_size, sequence_length)`, *optional*) -- | |
| Indices of input sequence tokens in the vocabulary. Padding will be ignored by default. | |
| Indices can be obtained using [AutoTokenizer](/docs/transformers/pr_33962/en/model_doc/auto#transformers.AutoTokenizer). See [PreTrainedTokenizer.encode()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.encode) and | |
| [PreTrainedTokenizer.__call__()](/docs/transformers/pr_33962/en/internal/tokenization_utils#transformers.PreTrainedTokenizerBase.__call__) for details. | |
| [What are input IDs?](../glossary#input-ids) | |
| - **pixel_values** (`torch.FloatTensor` of shape `(batch_size, num_channels, image_size, image_size)`, *optional*) -- | |
| The tensors corresponding to the input images. Pixel values can be obtained using | |
| [LlavaNextImageProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextImageProcessor). See [LlavaNextImageProcessor.__call__()](/docs/transformers/pr_33962/en/model_doc/fuyu#transformers.FuyuImageProcessor.__call__) for details ([LlavaNextProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextProcessor) uses | |
| [LlavaNextImageProcessor](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextImageProcessor) for processing images). | |
| - **image_sizes** (`torch.LongTensor` of shape `(batch_size, 2)`, *optional*) -- | |
| The sizes of the images in the batch, being (height, width) for each image. | |
| - **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?](../glossary#attention-mask) | |
| - **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?](../glossary#position-ids) | |
| - **past_key_values** (`~cache_utils.Cache`, *optional*) -- | |
| Pre-computed hidden-states (key and values in the self-attention blocks and in the cross-attention | |
| blocks) that can be used to speed up sequential decoding. This typically consists in the `past_key_values` | |
| returned by the model at a previous stage of decoding, when `use_cache=True` or `config.use_cache=True`. | |
| Only [Cache](/docs/transformers/pr_33962/en/internal/generation_utils#transformers.Cache) instance is allowed as input, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache). | |
| If no `past_key_values` are passed, [DynamicCache](/docs/transformers/pr_33962/en/internal/generation_utils#transformers.DynamicCache) will be initialized by default. | |
| The model will output the same cache format that is fed as input. | |
| If `past_key_values` are used, the user is expected to input only unprocessed `input_ids` (those that don't | |
| have their past key value states given to this model) of shape `(batch_size, unprocessed_length)` instead of all `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], NoneType]`) -- | |
| 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"`. If `"default"`, the CLS token is removed from the vision features. | |
| If `"full"`, the full vision features are used. | |
| - **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]`. | |
| - **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. | |
| - **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. | |
| - **logits_to_keep** (`Union[int, torch.Tensor]`, defaults to `0`) -- | |
| 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).</paramsdesc><paramgroups>0</paramgroups><rettype>`transformers.models.llava_next.modeling_llava_next.LlavaNextCausalLMOutputWithPast` or `tuple(torch.FloatTensor)`</rettype><retdesc>A `transformers.models.llava_next.modeling_llava_next.LlavaNextCausalLMOutputWithPast` 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 ([LlavaNextConfig](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextConfig)) 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** (`Cache`, *optional*, returned when `use_cache=True` is passed or when `config.use_cache=True`) -- It is a [Cache](/docs/transformers/pr_33962/en/internal/generation_utils#transformers.Cache) instance. For more details, see our [kv cache guide](https://huggingface.co/docs/transformers/en/kv_cache). | |
| 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_patches, num_images, sequence_length, hidden_size)`. | |
| image_hidden_states of the model produced by the vision encoder and after projecting the last hidden state.</retdesc></docstring> | |
| The [LlavaNextForConditionalGeneration](/docs/transformers/pr_33962/en/model_doc/granitevision#transformers.LlavaNextForConditionalGeneration) forward method, overrides the `__call__` special method. | |
| <Tip> | |
| 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. | |
| </Tip> | |
| <ExampleCodeBlock anchor="transformers.LlavaNextForConditionalGeneration.forward.example"> | |
| Example: | |
| ```python | |
| >>> from PIL import Image | |
| >>> import requests | |
| >>> from transformers import AutoProcessor, LlavaNextForConditionalGeneration | |
| >>> model = LlavaNextForConditionalGeneration.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf") | |
| >>> processor = AutoProcessor.from_pretrained("llava-hf/llava-v1.6-mistral-7b-hf") | |
| >>> prompt = "[INST] <image>\nWhat is shown in this image? [/INST]" | |
| >>> url = "https://www.ilankelman.org/stopsigns/australia.jpg" | |
| >>> image = Image.open(requests.get(url, stream=True).raw) | |
| >>> inputs = processor(images=image, text=prompt, return_tensors="pt") | |
| >>> # Generate | |
| >>> generate_ids = model.generate(**inputs, max_length=30) | |
| >>> processor.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0] | |
| "[INST] \nWhat is shown in this image? [/INST] The image appears to be a radar chart, which is a type of multi-dimensional plot (...)" | |
| ``` | |
| </ExampleCodeBlock> | |
| </div></div> | |
| <EditOnGithub source="https://github.com/huggingface/transformers/blob/main/docs/source/en/model_doc/granitevision.md" /> |
Xet Storage Details
- Size:
- 33.3 kB
- Xet hash:
- 8db63888b9bb3be363d89259223e9aa779fee1ba7299f98cb0a96e0864068b94
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.