configuration_aria.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/aria/modular_aria.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_aria.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2024 The Rhymes-AI Teams Authors and The HuggingFace Inc. team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...modeling_rope_utils import RopeParameters
  23. from ...utils import auto_docstring
  24. from ...utils.type_validators import interval
  25. from ..auto import CONFIG_MAPPING, AutoConfig
  26. @auto_docstring(checkpoint="rhymes-ai/Aria")
  27. @strict
  28. class AriaTextConfig(PreTrainedConfig):
  29. r"""
  30. moe_num_experts (`int`, *optional*, defaults to 8):
  31. The number of experts in the MoE layer.
  32. moe_topk (`int`, *optional*, defaults to 2):
  33. The number of top experts to route to for each token.
  34. moe_num_shared_experts (`int`, *optional*, defaults to 2):
  35. The number of shared experts.
  36. """
  37. model_type = "aria_text"
  38. keys_to_ignore_at_inference = ["past_key_values"]
  39. base_model_tp_plan = {
  40. "layers.*.self_attn.q_proj": "colwise",
  41. "layers.*.self_attn.k_proj": "colwise",
  42. "layers.*.self_attn.v_proj": "colwise",
  43. "layers.*.self_attn.o_proj": "rowwise",
  44. "layers.*.mlp.shared_experts.gate_proj": "colwise",
  45. "layers.*.mlp.shared_experts.up_proj": "colwise",
  46. "layers.*.mlp.shared_experts.down_proj": "rowwise",
  47. }
  48. base_model_pp_plan = {
  49. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  50. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  51. "norm": (["hidden_states"], ["hidden_states"]),
  52. }
  53. vocab_size: int = 32000
  54. hidden_size: int = 4096
  55. intermediate_size: int = 4096
  56. num_hidden_layers: int = 32
  57. num_attention_heads: int = 32
  58. num_key_value_heads: int | None = None
  59. hidden_act: str = "silu"
  60. max_position_embeddings: int = 2048
  61. initializer_range: float = interval(min=0.0, max=1.0)(default=0.02)
  62. rms_norm_eps: float = 1e-6
  63. use_cache: bool = True
  64. pad_token_id: int | None = 2
  65. bos_token_id: int | None = 1
  66. eos_token_id: int | list[int] | None = 2
  67. pretraining_tp: int | None = 1
  68. tie_word_embeddings: bool = False
  69. rope_parameters: RopeParameters | dict | None = None
  70. attention_bias: bool = False
  71. attention_dropout: int | float | None = 0.0
  72. mlp_bias: bool = False
  73. head_dim: int | None = None
  74. base_config_key = "text_config"
  75. moe_num_experts: int = 8
  76. moe_topk: int = 2
  77. moe_num_shared_experts: int = 2
  78. def __post_init__(self, **kwargs):
  79. if self.head_dim is None:
  80. self.head_dim = self.hidden_size // self.num_attention_heads
  81. if self.num_key_value_heads is None:
  82. self.num_key_value_heads = self.num_attention_heads
  83. super().__post_init__(**kwargs)
  84. def validate_architecture(self):
  85. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  86. if self.hidden_size % self.num_attention_heads != 0:
  87. raise ValueError(
  88. f"The hidden size ({self.hidden_size}) is not a multiple of the number of attention "
  89. f"heads ({self.num_attention_heads})."
  90. )
  91. @auto_docstring(checkpoint="rhymes-ai/Aria")
  92. @strict
  93. class AriaConfig(PreTrainedConfig):
  94. r"""
  95. projector_patch_to_query_dict (`dict`, *optional*):
  96. Mapping of patch sizes to query dimensions.
  97. """
  98. model_type = "aria"
  99. attribute_map = {
  100. "image_token_id": "image_token_index",
  101. }
  102. sub_configs = {"text_config": AriaTextConfig, "vision_config": AutoConfig}
  103. vision_config: dict | PreTrainedConfig | None = None
  104. text_config: dict | AriaTextConfig | None = None
  105. vision_feature_layer: int | list[int] = -1
  106. projector_patch_to_query_dict: dict | None = None
  107. image_token_index: int = 9
  108. initializer_range: float = 0.02
  109. tie_word_embeddings: bool = False
  110. def __post_init__(self, **kwargs):
  111. # Convert the keys and values of projector_patch_to_query_dict to integers
  112. # This ensures consistency even if they were provided as strings
  113. if self.projector_patch_to_query_dict is None:
  114. self.projector_patch_to_query_dict = {
  115. 1225: 128,
  116. 4900: 256,
  117. }
  118. self.projector_patch_to_query_dict = {int(k): int(v) for k, v in self.projector_patch_to_query_dict.items()}
  119. self.max_value_projector_patch_to_query_dict = max(self.projector_patch_to_query_dict.values())
  120. if isinstance(self.vision_config, dict):
  121. self.vision_config["model_type"] = "idefics3_vision"
  122. self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
  123. elif self.vision_config is None:
  124. self.vision_config = CONFIG_MAPPING["idefics3_vision"]()
  125. if isinstance(self.text_config, dict) and "model_type" in self.text_config:
  126. self.text_config = AriaTextConfig(**self.text_config)
  127. elif self.text_config is None:
  128. self.text_config = AriaTextConfig()
  129. super().__post_init__(**kwargs)
  130. __all__ = ["AriaConfig", "AriaTextConfig"]