configuration_bridgetower.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # Copyright 2023 The Intel Labs Team Authors, The Microsoft Research Team Authors and HuggingFace Inc. team. All rights reserved.
  2. #
  3. # Licensed under the Apache License=, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing=, software
  10. # distributed under the License is distributed on an "AS IS" BASIS=,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND=, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """BridgeTower model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring, logging
  18. logger = logging.get_logger(__name__)
  19. @auto_docstring(checkpoint="BridgeTower/bridgetower-base")
  20. @strict
  21. class BridgeTowerVisionConfig(PreTrainedConfig):
  22. r"""
  23. stop_gradient (`bool`, *optional*, defaults to `False`):
  24. Whether to stop gradient for training.
  25. share_layernorm (`bool`, *optional*, defaults to `True`):
  26. Whether LayerNorm layers are shared.
  27. remove_last_layer (`bool`, *optional*, defaults to `False`):
  28. Whether to remove the last layer from the vision encoder.
  29. Example:
  30. ```python
  31. >>> from transformers import BridgeTowerVisionConfig
  32. >>> # Initializing a BridgeTower BridgeTower/bridgetower-base style configuration for the vision model
  33. >>> configuration = BridgeTowerVisionConfig()
  34. >>> # Accessing the configuration
  35. >>> configuration
  36. ```"""
  37. model_type = "bridgetower_vision_model"
  38. base_config_key = "vision_config"
  39. hidden_size: int = 768
  40. num_hidden_layers: int = 12
  41. num_channels: int = 3
  42. patch_size: int | list[int] | tuple[int, int] = 16
  43. image_size: int | list[int] | tuple[int, int] = 288
  44. initializer_factor: float | int = 1
  45. layer_norm_eps: float = 1e-05
  46. stop_gradient: bool = False
  47. share_layernorm: bool = True
  48. remove_last_layer: bool = False
  49. @auto_docstring(checkpoint="BridgeTower/bridgetower-base")
  50. @strict
  51. class BridgeTowerTextConfig(PreTrainedConfig):
  52. r"""
  53. Example:
  54. ```python
  55. >>> from transformers import BridgeTowerTextConfig
  56. >>> # Initializing a BridgeTower BridgeTower/bridgetower-base style configuration for the text model
  57. >>> configuration = BridgeTowerTextConfig()
  58. >>> # Accessing the configuration
  59. >>> configuration
  60. ```"""
  61. model_type = "bridgetower_text_model"
  62. base_config_key = "text_config"
  63. vocab_size: int = 50265
  64. hidden_size: int = 768
  65. num_hidden_layers: int = 12
  66. num_attention_heads: int = 12
  67. initializer_factor: float | int = 1
  68. intermediate_size: int = 3072
  69. hidden_act: str = "gelu"
  70. hidden_dropout_prob: float | int = 0.1
  71. attention_probs_dropout_prob: float | int = 0.1
  72. max_position_embeddings: int = 514
  73. type_vocab_size: int = 1
  74. layer_norm_eps: float = 1e-05
  75. pad_token_id: int | None = 1
  76. bos_token_id: int | None = 0
  77. eos_token_id: int | list[int] | None = 2
  78. use_cache: bool = True
  79. is_decoder: bool = False
  80. add_cross_attention: bool = False
  81. @auto_docstring(checkpoint="BridgeTower/bridgetower-base")
  82. @strict
  83. class BridgeTowerConfig(PreTrainedConfig):
  84. r"""
  85. share_cross_modal_transformer_layers (`bool`, *optional*, defaults to `True`):
  86. Whether cross modal transformer layers are shared.
  87. share_link_tower_layers (`bool`, *optional*, defaults to `False`):
  88. Whether the bride/link tower layers are shared.
  89. link_tower_type (`str`, *optional*, defaults to `"add"`):
  90. Type of the bridge/link layer.
  91. init_layernorm_from_vision_encoder (`bool`, *optional*, defaults to `False`):
  92. Whether to init LayerNorm from the vision encoder.
  93. Example:
  94. ```python
  95. >>> from transformers import BridgeTowerModel, BridgeTowerConfig
  96. >>> # Initializing a BridgeTower BridgeTower/bridgetower-base style configuration
  97. >>> configuration = BridgeTowerConfig()
  98. >>> # Initializing a model from the BridgeTower/bridgetower-base style configuration
  99. >>> model = BridgeTowerModel(configuration)
  100. >>> # Accessing the model configuration
  101. >>> configuration = model.config
  102. ```"""
  103. model_type = "bridgetower"
  104. sub_configs = {"text_config": BridgeTowerTextConfig, "vision_config": BridgeTowerVisionConfig}
  105. share_cross_modal_transformer_layers: bool = True
  106. hidden_act: str = "gelu"
  107. hidden_size: int = 768
  108. initializer_factor: float | int = 1
  109. layer_norm_eps: float = 1e-05
  110. share_link_tower_layers: bool = False
  111. link_tower_type: str = "add"
  112. num_attention_heads: int = 12
  113. num_hidden_layers: int = 6
  114. tie_word_embeddings: bool = False
  115. init_layernorm_from_vision_encoder: bool = False
  116. text_config: dict | PreTrainedConfig | None = None
  117. vision_config: dict | PreTrainedConfig | None = None
  118. def __post_init__(self, **kwargs):
  119. # TODO: remove this once the Hub files are updated.
  120. _ = kwargs.pop("text_config_dict", None)
  121. _ = kwargs.pop("vision_config_dict", None)
  122. if self.text_config is None:
  123. self.text_config = BridgeTowerTextConfig()
  124. logger.info("`text_config` is `None`. initializing the `BridgeTowerTextConfig` with default values.")
  125. elif isinstance(self.text_config, dict):
  126. self.text_config = BridgeTowerTextConfig(**self.text_config)
  127. if self.vision_config is None:
  128. self.vision_config = BridgeTowerVisionConfig()
  129. logger.info("`vision_config` is `None`. initializing the `BridgeTowerVisionConfig` with default values.")
  130. elif isinstance(self.vision_config, dict):
  131. self.vision_config = BridgeTowerVisionConfig(**self.vision_config)
  132. super().__post_init__(**kwargs)
  133. __all__ = ["BridgeTowerConfig", "BridgeTowerTextConfig", "BridgeTowerVisionConfig"]