configuration_git.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2022 The 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. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PreTrainedConfig
  16. from ...utils import auto_docstring, logging
  17. logger = logging.get_logger(__name__)
  18. @auto_docstring(checkpoint="microsoft/git-base")
  19. @strict
  20. class GitVisionConfig(PreTrainedConfig):
  21. r"""
  22. Example:
  23. ```python
  24. >>> from transformers import GitVisionConfig, GitVisionModel
  25. >>> # Initializing a GitVisionConfig with microsoft/git-base style configuration
  26. >>> configuration = GitVisionConfig()
  27. >>> # Initializing a GitVisionModel (with random weights) from the microsoft/git-base style configuration
  28. >>> model = GitVisionModel(configuration)
  29. >>> # Accessing the model configuration
  30. >>> configuration = model.config
  31. ```"""
  32. model_type = "git_vision_model"
  33. base_config_key = "vision_config"
  34. hidden_size: int = 768
  35. intermediate_size: int = 3072
  36. num_hidden_layers: int = 12
  37. num_attention_heads: int = 12
  38. num_channels: int = 3
  39. image_size: int | list[int] | tuple[int, int] = 224
  40. patch_size: int | list[int] | tuple[int, int] = 16
  41. hidden_act: str = "quick_gelu"
  42. layer_norm_eps: float = 1e-5
  43. attention_dropout: float | int = 0.0
  44. initializer_range: float = 0.02
  45. @auto_docstring(checkpoint="microsoft/git-base")
  46. @strict
  47. class GitConfig(PreTrainedConfig):
  48. r"""
  49. num_image_with_embedding (`int`, *optional*):
  50. The number of temporal embeddings to add, in case the model is used for video captioning/VQA.
  51. Examples:
  52. ```python
  53. >>> from transformers import GitConfig, GitModel
  54. >>> # Initializing a GIT microsoft/git-base style configuration
  55. >>> configuration = GitConfig()
  56. >>> # Initializing a model (with random weights) from the microsoft/git-base style configuration
  57. >>> model = GitModel(configuration)
  58. >>> # Accessing the model configuration
  59. >>> configuration = model.config
  60. ```"""
  61. model_type = "git"
  62. sub_configs = {"vision_config": GitVisionConfig}
  63. vision_config: dict | GitVisionConfig | None = None
  64. vocab_size: int = 30522
  65. hidden_size: int = 768
  66. num_hidden_layers: int = 6
  67. num_attention_heads: int = 12
  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 = 1024
  73. initializer_range: float = 0.02
  74. layer_norm_eps: float = 1e-12
  75. pad_token_id: int | None = 0
  76. use_cache: bool = True
  77. tie_word_embeddings: bool = False
  78. bos_token_id: int | None = 101
  79. eos_token_id: int | list[int] | None = 102
  80. num_image_with_embedding: int | None = None
  81. def __post_init__(self, **kwargs):
  82. if self.vision_config is None:
  83. self.vision_config = GitVisionConfig()
  84. logger.info("vision_config is None. initializing the GitVisionConfig with default values.")
  85. elif isinstance(self.vision_config, dict):
  86. self.vision_config = GitVisionConfig(**self.vision_config)
  87. super().__post_init__(**kwargs)
  88. __all__ = ["GitConfig", "GitVisionConfig"]