configuration_idefics.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. # Copyright 2022 EleutherAI and the HuggingFace Inc. team. All rights reserved.
  2. #
  3. # This code is based on EleutherAI's GPT-NeoX library and the GPT-NeoX
  4. # and OPT implementations in this library. It has been modified from its
  5. # original forms to accommodate minor architectural differences compared
  6. # to GPT-NeoX and OPT used by the Meta AI team that trained the model.
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. """Idefics model configuration"""
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...utils import auto_docstring
  23. @auto_docstring(checkpoint="HuggingFaceM4/idefics-9b")
  24. @strict
  25. class IdeficsVisionConfig(PreTrainedConfig):
  26. model_type = "idefics_vision"
  27. attribute_map = {"hidden_size": "embed_dim"}
  28. embed_dim: int = 768
  29. image_size: int | list[int] | tuple[int, int] = 224
  30. intermediate_size: int = 5120
  31. patch_size: int | list[int] | tuple[int, int] = 14
  32. num_hidden_layers: int = 32
  33. num_attention_heads: int = 16
  34. num_channels: int = 3
  35. hidden_act: str = "gelu"
  36. layer_norm_eps: float = 1e-5
  37. attention_dropout: float | int = 0.0
  38. initializer_range: float = 0.02
  39. initializer_factor: float = 1.0
  40. @auto_docstring(checkpoint="HuggingFaceM4/idefics-9b")
  41. @strict
  42. class IdeficsPerceiverConfig(PreTrainedConfig):
  43. r"""
  44. use_resampler (`bool`, *optional*, defaults to `False`):
  45. Whether or not to use the resampler
  46. resampler_n_latents (`int`, *optional*, defaults to 64):
  47. Number of latent embeddings to resample ("compress") the input sequence to (usually < 128).
  48. resampler_depth (`int`, *optional*, defaults to 6):
  49. Depth of the Perceiver Resampler (Transformer w/ cross attention). Should be shallow (< 3).
  50. resampler_n_heads (`int`, *optional*, defaults to 16):
  51. Number of heads in each Transformer block (for multi-headed self-attention).
  52. resampler_head_dim (`int`, *optional*, defaults to 96):
  53. Dimensionality of each head projection in the Transformer block.
  54. qk_layer_norms_perceiver (`bool`, *optional*, defaults to `False`):
  55. Whether or not to use qk layer norms in perceiver
  56. """
  57. model_type = "idefics_perciever"
  58. use_resampler: bool = False
  59. resampler_n_latents: int = 64
  60. resampler_depth: int = 6
  61. resampler_n_heads: int = 16
  62. resampler_head_dim: int = 96
  63. qk_layer_norms_perceiver: bool = False
  64. @auto_docstring(checkpoint="HuggingFaceM4/idefics-9b")
  65. @strict
  66. class IdeficsConfig(PreTrainedConfig):
  67. r"""
  68. additional_vocab_size (`int`, *optional*, defaults to 0):
  69. Additional vocabulary size of the model, typically for the special "<img>" token. Additional vocab tokens
  70. are always trainable whereas regular vocab tokens can be frozen or not.
  71. alpha_initializer (`str`, *optional*, defaults to `"zeros"`):
  72. Initialization type for the alphas.
  73. alphas_initializer_range (`float`, *optional*, defaults to 0.0):
  74. The standard deviation of the truncated_normal_initializer for initializing the alphas in the Gated Cross
  75. Attention.
  76. alpha_type (`str`, *optional*, defaults to `"float"`):
  77. Whether the gating alphas should be vectors or single floats.
  78. cross_layer_interval (`int`, *optional*, default to 1):
  79. Interval for cross attention (from text to image) layers.
  80. qk_layer_norms (`bool`, *optional*, defaults to `False`):
  81. Whether to add layer norm after q and k
  82. freeze_text_layers (`bool`, *optional*, defaults to `True`):
  83. Whether to freeze text layers
  84. freeze_text_module_exceptions (`bool`, *optional*, defaults to `[]`):
  85. Exceptions to freezing text layers when `freeze_text_layers` is `True`
  86. freeze_lm_head (`bool`, *optional*, defaults to `False`):
  87. Whether to freeze lm head
  88. freeze_vision_layers (`bool`, *optional*, defaults to `True`):
  89. Whether to freeze vision layers
  90. freeze_vision_module_exceptions (`bool`, *optional*, defaults to `[]`):
  91. Exceptions to freezing vision layers when `freeze_vision_layers` is `True`
  92. use_resampler (`bool`, *optional*, defaults to `False`):
  93. Whether to use the Resampler
  94. perceiver_config (`IdeficsPerceiverConfig`, *optional*):
  95. Custom perceiver config or dict
  96. Example:
  97. ```python
  98. >>> from transformers import IdeficsModel, IdeficsConfig
  99. >>> # Initializing a Idefics idefics-9b style configuration
  100. >>> configuration = IdeficsConfig()
  101. >>> # Initializing a model from the idefics-9b style configuration
  102. >>> model = IdeficsModel(configuration)
  103. >>> # Accessing the model configuration
  104. >>> configuration = model.config
  105. ```"""
  106. model_type = "idefics"
  107. sub_configs = {"perceiver_config": IdeficsPerceiverConfig, "vision_config": IdeficsVisionConfig}
  108. vocab_size: int = 32000
  109. additional_vocab_size: int = 0
  110. hidden_size: int = 4096
  111. intermediate_size: int = 11008
  112. num_hidden_layers: int = 32
  113. num_attention_heads: int = 32
  114. dropout: float | int = 0.0
  115. hidden_act: str = "silu"
  116. initializer_range: float = 0.02
  117. alpha_initializer: str = "zeros"
  118. alphas_initializer_range: float = 0.0
  119. alpha_type: str = "float"
  120. rms_norm_eps: float = 1e-6
  121. use_cache: bool = True
  122. pad_token_id: int | None = 0
  123. bos_token_id: int | None = 1
  124. eos_token_id: int | list[int] | None = 2
  125. tie_word_embeddings: bool = False
  126. cross_layer_interval: int = 1
  127. qk_layer_norms: bool = False
  128. freeze_text_layers: bool = True
  129. freeze_text_module_exceptions: list | tuple = ()
  130. freeze_lm_head: bool = False
  131. freeze_vision_layers: bool = True
  132. freeze_vision_module_exceptions: list | tuple = ()
  133. use_resampler: bool = False
  134. vision_config: dict | PreTrainedConfig | None = None
  135. perceiver_config: dict | PreTrainedConfig | None = None
  136. def __post_init__(self, **kwargs):
  137. if self.perceiver_config is None:
  138. self.perceiver_config = IdeficsPerceiverConfig()
  139. elif isinstance(self.perceiver_config, dict):
  140. self.perceiver_config = IdeficsPerceiverConfig(**self.perceiver_config)
  141. if self.vision_config is None:
  142. self.vision_config = IdeficsVisionConfig()
  143. elif isinstance(self.vision_config, dict):
  144. self.vision_config = IdeficsVisionConfig(**self.vision_config)
  145. super().__post_init__(**kwargs)
  146. __all__ = ["IdeficsConfig"]