|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
"""EMOVA model configuration""" |
|
from transformers.configuration_utils import PretrainedConfig |
|
from transformers.utils import logging |
|
from transformers.models.auto import CONFIG_MAPPING |
|
|
|
from .configuration_qwen2vit import Qwen2VLVisionConfig |
|
|
|
logger = logging.get_logger(__name__) |
|
|
|
|
|
class EMOVAConfig(PretrainedConfig): |
|
r""" |
|
This is the configuration class to store the configuration of a [`EMOVAForConditionalGeneration`]. It is used to instantiate an |
|
EMOVA model according to the specified arguments, defining the model architecture. |
|
|
|
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the |
|
documentation from [`PretrainedConfig`] for more information. |
|
|
|
Args: |
|
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. |
|
ignore_index (`int`, *optional*, defaults to -100): |
|
The ignore index for the loss function. |
|
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 (`int`, *optional*, defaults to -2): |
|
The index of the layer to select the vision feature. |
|
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. |
|
|
|
Example: |
|
|
|
```python |
|
>>> from transformers import EMOVAForConditionalGeneration, EMOVAConfig, CLIPVisionConfig, LlamaConfig |
|
|
|
>>> # Initializing a CLIP-vision config |
|
>>> vision_config = CLIPVisionConfig() |
|
|
|
>>> # Initializing a Llama config |
|
>>> text_config = LlamaConfig() |
|
|
|
>>> # Initializing a EMOVA style configuration |
|
>>> configuration = EMOVAConfig(vision_config, text_config) |
|
|
|
>>> # Initializing a model from the style configuration |
|
>>> model = EMOVAForConditionalGeneration(configuration) |
|
|
|
>>> # Accessing the model configuration |
|
>>> configuration = model.config |
|
```""" |
|
|
|
model_type = "emova" |
|
is_composition = False |
|
|
|
def __init__( |
|
self, |
|
vision_config=None, |
|
mm_projector_config=None, |
|
text_config=None, |
|
ignore_index=-100, |
|
image_token_index=32000, |
|
vision_feature_select_strategy="default", |
|
vision_feature_layer=-2, |
|
is_native_resolution=True, |
|
image_grid_pinpoints=None, |
|
use_image_newline_parameter=False, |
|
tie_word_embeddings=False, |
|
**kwargs, |
|
): |
|
self.ignore_index = ignore_index |
|
self.image_token_index = image_token_index |
|
|
|
if vision_feature_select_strategy not in ["default", "full"]: |
|
raise ValueError( |
|
"vision_feature_select_strategy should be one of 'default', 'full'." |
|
f"Got: {vision_feature_select_strategy}" |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if isinstance(vision_config, dict): |
|
vision_config = Qwen2VLVisionConfig(**vision_config) |
|
elif vision_config is None: |
|
vision_config = Qwen2VLVisionConfig( |
|
depth=32, |
|
embed_dim=1280, |
|
hidden_act="quick_gelu", |
|
hidden_size=3584, |
|
in_channels=3, |
|
in_chans=3, |
|
mlp_ratio=4, |
|
model_type="qwen2_vl", |
|
num_heads=16, |
|
patch_size=14, |
|
spatial_merge_size=2, |
|
spatial_patch_size=14, |
|
temporal_patch_size=2, |
|
initializer_range=0.02, |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.vision_config = vision_config |
|
self.mm_projector_config = mm_projector_config |
|
if isinstance(text_config, dict): |
|
text_config["model_type"] = text_config["model_type"] if "model_type" in text_config else "llama" |
|
text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config) |
|
elif text_config is None: |
|
text_config = CONFIG_MAPPING["llama"]() |
|
|
|
self.text_config = text_config |
|
super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs) |
|
|