configuration_git.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. # coding=utf-8
  2. # Copyright 2022 The HuggingFace Inc. team. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from ...configuration_utils import PretrainedConfig
  16. from ...utils import logging
  17. logger = logging.get_logger(__name__)
  18. class GitVisionConfig(PretrainedConfig):
  19. r"""
  20. This is the configuration class to store the configuration of a [`GitVisionModel`]. It is used to instantiate a GIT
  21. vision encoder according to the specified arguments, defining the model architecture. Instantiating a configuration
  22. with the defaults will yield a similar configuration to that of the vision encoder of the GIT
  23. [microsoft/git-base](https://huggingface.co/microsoft/git-base) architecture.
  24. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  25. documentation from [`PretrainedConfig`] for more information.
  26. Args:
  27. hidden_size (`int`, *optional*, defaults to 768):
  28. Dimensionality of the encoder layers and the pooler layer.
  29. intermediate_size (`int`, *optional*, defaults to 3072):
  30. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  31. num_hidden_layers (`int`, *optional*, defaults to 12):
  32. Number of hidden layers in the Transformer encoder.
  33. num_attention_heads (`int`, *optional*, defaults to 12):
  34. Number of attention heads for each attention layer in the Transformer encoder.
  35. image_size (`int`, *optional*, defaults to 224):
  36. The size (resolution) of each image.
  37. patch_size (`int`, *optional*, defaults to 16):
  38. The size (resolution) of each patch.
  39. hidden_act (`str` or `function`, *optional*, defaults to `"quick_gelu"`):
  40. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  41. `"relu"`, `"selu"` and `"gelu_new"` `"quick_gelu"` are supported.
  42. layer_norm_eps (`float`, *optional*, defaults to 1e-5):
  43. The epsilon used by the layer normalization layers.
  44. attention_dropout (`float`, *optional*, defaults to 0.0):
  45. The dropout ratio for the attention probabilities.
  46. initializer_range (`float`, *optional*, defaults to 0.02):
  47. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  48. Example:
  49. ```python
  50. >>> from transformers import GitVisionConfig, GitVisionModel
  51. >>> # Initializing a GitVisionConfig with microsoft/git-base style configuration
  52. >>> configuration = GitVisionConfig()
  53. >>> # Initializing a GitVisionModel (with random weights) from the microsoft/git-base style configuration
  54. >>> model = GitVisionModel(configuration)
  55. >>> # Accessing the model configuration
  56. >>> configuration = model.config
  57. ```"""
  58. model_type = "git_vision_model"
  59. base_config_key = "vision_config"
  60. def __init__(
  61. self,
  62. hidden_size=768,
  63. intermediate_size=3072,
  64. num_hidden_layers=12,
  65. num_attention_heads=12,
  66. num_channels=3,
  67. image_size=224,
  68. patch_size=16,
  69. hidden_act="quick_gelu",
  70. layer_norm_eps=1e-5,
  71. attention_dropout=0.0,
  72. initializer_range=0.02,
  73. **kwargs,
  74. ):
  75. super().__init__(**kwargs)
  76. self.hidden_size = hidden_size
  77. self.intermediate_size = intermediate_size
  78. self.num_hidden_layers = num_hidden_layers
  79. self.num_attention_heads = num_attention_heads
  80. self.num_channels = num_channels
  81. self.patch_size = patch_size
  82. self.image_size = image_size
  83. self.initializer_range = initializer_range
  84. self.attention_dropout = attention_dropout
  85. self.layer_norm_eps = layer_norm_eps
  86. self.hidden_act = hidden_act
  87. class GitConfig(PretrainedConfig):
  88. r"""
  89. This is the configuration class to store the configuration of a [`GitModel`]. It is used to instantiate a GIT model
  90. according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  91. defaults will yield a similar configuration to that of the GIT
  92. [microsoft/git-base](https://huggingface.co/microsoft/git-base) architecture.
  93. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  94. documentation from [`PretrainedConfig`] for more information.
  95. Args:
  96. vision_config (`dict`, *optional*):
  97. Dictionary of configuration options used to initialize [`GitVisionConfig`].
  98. vocab_size (`int`, *optional*, defaults to 30522):
  99. Vocabulary size of the GIT model. Defines the number of different tokens that can be represented by the
  100. `inputs_ids` passed when calling [`GitModel`].
  101. hidden_size (`int`, *optional*, defaults to 768):
  102. Dimensionality of the encoder layers and the pooler layer.
  103. num_hidden_layers (`int`, *optional*, defaults to 6):
  104. Number of hidden layers in the Transformer encoder.
  105. num_attention_heads (`int`, *optional*, defaults to 12):
  106. Number of attention heads for each attention layer in the Transformer encoder.
  107. intermediate_size (`int`, *optional*, defaults to 3072):
  108. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  109. hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
  110. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  111. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  112. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  113. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  114. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  115. The dropout ratio for the attention probabilities.
  116. max_position_embeddings (`int`, *optional*, defaults to 1024):
  117. The maximum sequence length that this model might ever be used with. Typically set this to something large
  118. just in case (e.g., 512 or 1024 or 2048).
  119. initializer_range (`float`, *optional*, defaults to 0.02):
  120. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  121. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  122. The epsilon used by the layer normalization layers.
  123. position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
  124. Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
  125. positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
  126. [Self-Attention with Relative Position Representations (Shaw et al.)](https://huggingface.co/papers/1803.02155).
  127. For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
  128. with Better Relative Position Embeddings (Huang et al.)](https://huggingface.co/papers/2009.13658).
  129. use_cache (`bool`, *optional*, defaults to `True`):
  130. Whether or not the model should return the last key/values attentions (not used by all models).
  131. num_image_with_embedding (`int`, *optional*):
  132. The number of temporal embeddings to add, in case the model is used for video captioning/VQA.
  133. Examples:
  134. ```python
  135. >>> from transformers import GitConfig, GitModel
  136. >>> # Initializing a GIT microsoft/git-base style configuration
  137. >>> configuration = GitConfig()
  138. >>> # Initializing a model (with random weights) from the microsoft/git-base style configuration
  139. >>> model = GitModel(configuration)
  140. >>> # Accessing the model configuration
  141. >>> configuration = model.config
  142. ```"""
  143. model_type = "git"
  144. sub_configs = {"vision_config": GitVisionConfig}
  145. def __init__(
  146. self,
  147. vision_config=None,
  148. vocab_size=30522,
  149. hidden_size=768,
  150. num_hidden_layers=6,
  151. num_attention_heads=12,
  152. intermediate_size=3072,
  153. hidden_act="gelu",
  154. hidden_dropout_prob=0.1,
  155. attention_probs_dropout_prob=0.1,
  156. max_position_embeddings=1024,
  157. initializer_range=0.02,
  158. layer_norm_eps=1e-12,
  159. pad_token_id=0,
  160. position_embedding_type="absolute",
  161. use_cache=True,
  162. tie_word_embeddings=False,
  163. bos_token_id=101,
  164. eos_token_id=102,
  165. num_image_with_embedding=None,
  166. **kwargs,
  167. ):
  168. super().__init__(bos_token_id=bos_token_id, eos_token_id=eos_token_id, pad_token_id=pad_token_id, **kwargs)
  169. if vision_config is None:
  170. vision_config = {}
  171. logger.info("vision_config is None. initializing the GitVisionConfig with default values.")
  172. self.vision_config = GitVisionConfig(**vision_config)
  173. self.vocab_size = vocab_size
  174. self.hidden_size = hidden_size
  175. self.num_hidden_layers = num_hidden_layers
  176. self.num_attention_heads = num_attention_heads
  177. self.hidden_act = hidden_act
  178. self.intermediate_size = intermediate_size
  179. self.hidden_dropout_prob = hidden_dropout_prob
  180. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  181. self.max_position_embeddings = max_position_embeddings
  182. self.initializer_range = initializer_range
  183. self.layer_norm_eps = layer_norm_eps
  184. self.position_embedding_type = position_embedding_type
  185. self.use_cache = use_cache
  186. self.tie_word_embeddings = tie_word_embeddings
  187. self.num_image_with_embedding = num_image_with_embedding
  188. self.bos_token_id = bos_token_id
  189. self.eos_token_id = eos_token_id
  190. __all__ = ["GitConfig", "GitVisionConfig"]