Skip to content

Commit

Permalink
Remove AriaVisionModel by just using Idefics3
Browse files Browse the repository at this point in the history
  • Loading branch information
aymeric-roucher committed Oct 25, 2024
1 parent ae19ca6 commit 0c8aa0a
Show file tree
Hide file tree
Showing 9 changed files with 263 additions and 732 deletions.
6 changes: 4 additions & 2 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@
"models.aria": [
"AriaConfig",
"AriaTextConfig",
"AriaVisionConfig",
],
"models.audio_spectrogram_transformer": [
"ASTConfig",
Expand Down Expand Up @@ -2461,6 +2460,8 @@
"Idefics3Model",
"Idefics3PreTrainedModel",
"Idefics3Processor",
"Idefics3VisionTransformer",
"Idefics3VisionConfig",
]
)
_import_structure["models.imagegpt"].extend(
Expand Down Expand Up @@ -5022,7 +5023,6 @@
from .models.aria import (
AriaConfig,
AriaTextConfig,
AriaVisionConfig,
)
from .models.audio_spectrogram_transformer import (
ASTConfig,
Expand Down Expand Up @@ -7178,6 +7178,8 @@
Idefics3Model,
Idefics3PreTrainedModel,
Idefics3Processor,
Idefics3VisionTransformer,
Idefics3VisionConfig,
)
from .models.imagegpt import (
ImageGPTForCausalImageModeling,
Expand Down
5 changes: 2 additions & 3 deletions src/transformers/models/aria/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@


_import_structure = {
"configuration_aria": ["AriaConfig", "AriaForCausalLM", "AriaTextConfig", "AriaVisionConfig"],
"configuration_aria": ["AriaConfig", "AriaForCausalLM", "AriaTextConfig"],
"modeling_aria": ["AriaForConditionalGeneration", "AriaPreTrainedModel"],
"processing_aria": ["AriaProcessor"],
}
Expand All @@ -41,13 +41,12 @@
]
_import_structure["configuration_aria"] = [
"AriaConfig",
"AriaVisionConfig",
"AriaTextConfig",
]


if TYPE_CHECKING:
from .configuration_aria import AriaConfig, AriaTextConfig, AriaVisionConfig
from .configuration_aria import AriaConfig, AriaTextConfig

try:
if not is_torch_available():
Expand Down
122 changes: 11 additions & 111 deletions src/transformers/models/aria/configuration_aria.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,113 +4,11 @@
# the file from the modular. If any change should be done, please apply the change to the
# modular_aria.py file directly. One of our CI enforces this.
# 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
import os
from typing import Union


from ...configuration_utils import PretrainedConfig
from ...modeling_rope_utils import rope_config_validation
from ...utils import logging


logger = logging.get_logger(__name__)


class AriaVisionConfig(PretrainedConfig):
r"""
This is the configuration class to store the configuration of a [`AriaVisionModel`]. It is used to instantiate a
Aria vision encoder according to the specified arguments, defining the model architecture. Instantiating a
configuration with the defaults will yield a similar configuration to that of the vision encoder of the Aria
[google/aria-base-patch16-224](https://huggingface.co/google/aria-base-patch16-224) architecture.
Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
documentation from [`PretrainedConfig`] for more information.
Args:
hidden_size (`int`, *optional*, defaults to 768):
Dimensionality of the encoder layers and the pooler layer.
intermediate_size (`int`, *optional*, defaults to 3072):
Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
num_hidden_layers (`int`, *optional*, defaults to 12):
Number of hidden layers in the Transformer encoder.
num_attention_heads (`int`, *optional*, defaults to 12):
Number of attention heads for each attention layer in the Transformer encoder.
num_channels (`int`, *optional*, defaults to 3):
Number of channels in the input images.
image_size (`int`, *optional*, defaults to 224):
The size (resolution) of each image.
patch_size (`int`, *optional*, defaults to 16):
The size (resolution) of each patch.
hidden_act (`str` or `function`, *optional*, defaults to `"gelu_pytorch_tanh"`):
The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
`"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
layer_norm_eps (`float`, *optional*, defaults to 1e-06):
The epsilon used by the layer normalization layers.
attention_dropout (`float`, *optional*, defaults to 0.0):
The dropout ratio for the attention probabilities.
Example:
```python
>>> from transformers import AriaVisionConfig, AriaVisionModel
>>> # Initializing a AriaVisionConfig with google/aria-base-patch16-224 style configuration
>>> configuration = AriaVisionConfig()
>>> # Initializing a AriaVisionModel (with random weights) from the google/aria-base-patch16-224 style configuration
>>> model = AriaVisionModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
```
Configuration class for AriaVisionModel."""

model_type = "aria_vision_model"

def __init__(
self,
hidden_size=768,
intermediate_size=3072,
num_hidden_layers=12,
num_attention_heads=12,
num_channels=3,
image_size=224,
patch_size=16,
hidden_act="gelu_pytorch_tanh",
layer_norm_eps=1e-6,
attention_dropout=0.0,
**kwargs,
):
super().__init__(**kwargs)

self.hidden_size = hidden_size
self.intermediate_size = intermediate_size
self.num_hidden_layers = num_hidden_layers
self.num_attention_heads = num_attention_heads
self.num_channels = num_channels
self.patch_size = patch_size
self.image_size = image_size
self.attention_dropout = attention_dropout
self.layer_norm_eps = layer_norm_eps
self._supports_sdpa = False
self.hidden_act = hidden_act

@classmethod
def from_pretrained(cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs) -> "PretrainedConfig":
cls._set_token_in_kwargs(kwargs)

config_dict, kwargs = cls.get_config_dict(pretrained_model_name_or_path, **kwargs)

# get the vision config dict if we are loading from AriaConfig
if config_dict.get("model_type") == "aria":
config_dict = config_dict["vision_config"]

if "model_type" in config_dict and hasattr(cls, "model_type") and config_dict["model_type"] != cls.model_type:
logger.warning(
f"You are using a model of type {config_dict['model_type']} to instantiate a model of type "
f"{cls.model_type}. This is not supported for all configurations of models and can yield errors."
)

return cls.from_dict(config_dict, **kwargs)
from ..auto import CONFIG_MAPPING


class AriaTextConfig(PretrainedConfig):
Expand Down Expand Up @@ -245,7 +143,6 @@ def __init__(
image_token_index=32000,
**kwargs,
):
super().__init__(**kwargs)
self.ignore_index = ignore_index
self.image_token_index = image_token_index

Expand All @@ -257,17 +154,20 @@ def __init__(
4900: 256,
}
self.projector_patch_to_query_dict = {int(k): int(v) for k, v in projector_patch_to_query_dict.items()}
if vision_config is None:
vision_config = AriaVisionConfig()
if text_config is None:
text_config = AriaTextConfig()

if isinstance(vision_config, dict) and "model_type" in vision_config:
vision_config = AriaVisionConfig(**vision_config)
if isinstance(vision_config, dict):
vision_config["model_type"] = "idefics3_vision"
vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config)
elif vision_config is None:
vision_config = CONFIG_MAPPING["idefics3_vision"]()

self.vision_config = vision_config

if isinstance(text_config, dict) and "model_type" in text_config:
text_config = AriaTextConfig(**text_config)
elif text_config is None:
text_config = AriaTextConfig()

self.text_config = text_config

super().__init__(**kwargs)
Loading

0 comments on commit 0c8aa0a

Please sign in to comment.