configuration_blip.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. """Blip 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="Salesforce/blip-vqa-base")
  20. @strict
  21. class BlipTextConfig(PreTrainedConfig):
  22. r"""
  23. label_smoothing (float, *optional*):
  24. A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets
  25. become a mixture of the original ground truth and a uniform distribution as described in
  26. `Rethinking the Inception Architecture for Computer Vision <https://huggingface.co/papers/1512.00567>`__. Default: :math:`0.0`.
  27. Example:
  28. ```python
  29. >>> from transformers import BlipTextConfig, BlipTextModel
  30. >>> # Initializing a BlipTextConfig with Salesforce/blip-vqa-base style configuration
  31. >>> configuration = BlipTextConfig()
  32. >>> # Initializing a BlipTextModel (with random weights) from the Salesforce/blip-vqa-base style configuration
  33. >>> model = BlipTextModel(configuration)
  34. >>> # Accessing the model configuration
  35. >>> configuration = model.config
  36. ```"""
  37. model_type = "blip_text_model"
  38. base_config_key = "text_config"
  39. vocab_size: int = 30524
  40. hidden_size: int = 768
  41. encoder_hidden_size: int = 768
  42. intermediate_size: int = 3072
  43. projection_dim: int = 768
  44. num_hidden_layers: int = 12
  45. num_attention_heads: int = 8
  46. max_position_embeddings: int = 512
  47. hidden_act: str = "gelu"
  48. layer_norm_eps: float = 1e-12
  49. hidden_dropout_prob: float | int = 0.0
  50. attention_probs_dropout_prob: float | int = 0.0
  51. initializer_range: float = 0.02
  52. bos_token_id: int | None = 30522
  53. eos_token_id: int | list[int] | None = 2
  54. pad_token_id: int | None = 0
  55. sep_token_id: int | None = 102
  56. is_decoder: bool = True
  57. use_cache: bool = True
  58. label_smoothing: float = 0.0
  59. @auto_docstring(checkpoint="Salesforce/blip-vqa-base")
  60. @strict
  61. class BlipVisionConfig(PreTrainedConfig):
  62. r"""
  63. Example:
  64. ```python
  65. >>> from transformers import BlipVisionConfig, BlipVisionModel
  66. >>> # Initializing a BlipVisionConfig with Salesforce/blip-vqa-base style configuration
  67. >>> configuration = BlipVisionConfig()
  68. >>> # Initializing a BlipVisionModel (with random weights) from the Salesforce/blip-vqa-base style configuration
  69. >>> model = BlipVisionModel(configuration)
  70. >>> # Accessing the model configuration
  71. >>> configuration = model.config
  72. ```"""
  73. model_type = "blip_vision_model"
  74. base_config_key = "vision_config"
  75. hidden_size: int = 768
  76. intermediate_size: int = 3072
  77. projection_dim: int = 512
  78. num_hidden_layers: int = 12
  79. num_attention_heads: int = 12
  80. image_size: int | list[int] | tuple[int, int] = 384
  81. patch_size: int | list[int] | tuple[int, int] = 16
  82. hidden_act: str = "gelu"
  83. layer_norm_eps: float = 1e-5
  84. attention_dropout: float | int = 0.0
  85. initializer_range: float = 1e-10
  86. @auto_docstring(checkpoint="Salesforce/blip-vqa-base")
  87. @strict
  88. class BlipConfig(PreTrainedConfig):
  89. r"""
  90. image_text_hidden_size (`int`, *optional*, defaults to 256):
  91. Dimensionality of the hidden state of the image-text fusion layer.
  92. label_smoothing (float, *optional*):
  93. A float in [0.0, 1.0]. Specifies the amount of smoothing when computing the loss, where 0.0 means no smoothing. The targets
  94. become a mixture of the original ground truth and a uniform distribution as described in
  95. `Rethinking the Inception Architecture for Computer Vision <https://huggingface.co/papers/1512.00567>`__. Default: :math:`0.0`.
  96. Example:
  97. ```python
  98. >>> from transformers import BlipConfig, BlipModel
  99. >>> # Initializing a BlipConfig with Salesforce/blip-vqa-base style configuration
  100. >>> configuration = BlipConfig()
  101. >>> # Initializing a BlipPModel (with random weights) from the Salesforce/blip-vqa-base style configuration
  102. >>> model = BlipModel(configuration)
  103. >>> # Accessing the model configuration
  104. >>> configuration = model.config
  105. >>> # We can also initialize a BlipConfig from a BlipTextConfig and a BlipVisionConfig
  106. >>> # Initializing a BLIPText and BLIPVision configuration
  107. >>> config_text = BlipTextConfig()
  108. >>> config_vision = BlipVisionConfig()
  109. >>> config = BlipConfig(text_config=config_text, vision_config=config_vision)
  110. ```"""
  111. model_type = "blip"
  112. sub_configs = {"text_config": BlipTextConfig, "vision_config": BlipVisionConfig}
  113. text_config: dict | PreTrainedConfig | None = None
  114. vision_config: dict | PreTrainedConfig | None = None
  115. projection_dim: int = 512
  116. logit_scale_init_value: float = 2.6592
  117. image_text_hidden_size: int = 256
  118. label_smoothing: float = 0.0
  119. tie_word_embeddings: bool = True
  120. initializer_factor: float = 1.0
  121. initializer_range: float = 0.02
  122. def __post_init__(self, **kwargs):
  123. if self.text_config is None:
  124. self.text_config = BlipTextConfig()
  125. logger.info("`text_config` is `None`. Initializing the `BlipTextConfig` with default values.")
  126. elif isinstance(self.text_config, dict):
  127. self.text_config = BlipTextConfig(**self.text_config)
  128. if self.vision_config is None:
  129. self.vision_config = BlipVisionConfig()
  130. logger.info("`vision_config` is `None`. initializing the `BlipVisionConfig` with default values.")
  131. elif isinstance(self.vision_config, dict):
  132. self.vision_config = BlipVisionConfig(**self.vision_config)
  133. self.text_config.encoder_hidden_size = self.vision_config.hidden_size
  134. super().__post_init__(**kwargs)
  135. __all__ = ["BlipConfig", "BlipTextConfig", "BlipVisionConfig"]