configuration_instructblipvideo.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/instructblipvideo/modular_instructblipvideo.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_instructblipvideo.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2024 HuggingFace Inc. team. All rights reserved.
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...models.auto.modeling_auto import MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
  23. from ...utils import auto_docstring, logging
  24. from ..auto import CONFIG_MAPPING, AutoConfig
  25. logger = logging.get_logger(__name__)
  26. @auto_docstring(checkpoint="Salesforce/instructblip-flan-t5-xl")
  27. @strict
  28. class InstructBlipVideoVisionConfig(PreTrainedConfig):
  29. r"""
  30. Example:
  31. ```python
  32. >>> from transformers import InstructBlipVideoVisionConfig, InstructBlipVideoVisionModel
  33. >>> # Initializing a InstructBlipVideoVisionConfig with Salesforce/instructblip-flan-t5-xl style configuration
  34. >>> configuration = InstructBlipVideoVisionConfig()
  35. >>> # Initializing a InstructBlipVideoVisionModel (with random weights) from the Salesforce/instructblip-flan-t5-xl style configuration
  36. >>> model = InstructBlipVideoVisionModel(configuration)
  37. >>> # Accessing the model configuration
  38. >>> configuration = model.config
  39. ```"""
  40. model_type = "instructblipvideo_vision_model"
  41. base_config_key = "vision_config"
  42. hidden_size: int = 1408
  43. intermediate_size: int = 6144
  44. num_hidden_layers: int = 39
  45. num_attention_heads: int = 16
  46. image_size: int | list[int] | tuple[int, int] = 224
  47. patch_size: int | list[int] | tuple[int, int] = 14
  48. hidden_act: str = "gelu"
  49. layer_norm_eps: float = 1e-6
  50. attention_dropout: float | int = 0.0
  51. initializer_range: float = 1e-10
  52. qkv_bias: bool = True
  53. @auto_docstring(checkpoint="Salesforce/instructblip-flan-t5-xl")
  54. @strict
  55. class InstructBlipVideoQFormerConfig(PreTrainedConfig):
  56. r"""
  57. cross_attention_frequency (`int`, *optional*, defaults to 2):
  58. The frequency of adding cross-attention to the Transformer layers.
  59. encoder_hidden_size (`int`, *optional*, defaults to 1408):
  60. The hidden size of the hidden states for cross-attention.
  61. Examples:
  62. ```python
  63. >>> from transformers import InstructBlipVideoQFormerConfig, InstructBlipVideoQFormerModel
  64. >>> # Initializing a InstructBlipVideo Salesforce/instructblip-flan-t5-xl style configuration
  65. >>> configuration = InstructBlipVideoQFormerConfig()
  66. >>> # Initializing a model (with random weights) from the Salesforce/instructblip-flan-t5-xl style configuration
  67. >>> model = InstructBlipVideoQFormerModel(configuration)
  68. >>> # Accessing the model configuration
  69. >>> configuration = model.config
  70. ```"""
  71. model_type = "instructblipvideo_qformer"
  72. base_config_key = "qformer_config"
  73. vocab_size: int = 30522
  74. hidden_size: int = 768
  75. num_hidden_layers: int = 12
  76. num_attention_heads: int = 12
  77. intermediate_size: int = 3072
  78. hidden_act: str = "gelu"
  79. hidden_dropout_prob: float | int = 0.1
  80. attention_probs_dropout_prob: float | int = 0.1
  81. max_position_embeddings: int = 512
  82. initializer_range: float = 0.02
  83. layer_norm_eps: float = 1e-12
  84. pad_token_id: int | None = 0
  85. cross_attention_frequency: int = 2
  86. encoder_hidden_size: int = 1408
  87. @auto_docstring(checkpoint="Salesforce/instructblip-flan-t5-xl")
  88. @strict
  89. class InstructBlipVideoConfig(PreTrainedConfig):
  90. r"""
  91. qformer_config (`dict`, *optional*):
  92. Dictionary of configuration options used to initialize [`InstructBlipVideoQFormerConfig`].
  93. num_query_tokens (`int`, *optional*, defaults to 32):
  94. The number of query tokens passed through the Transformer.
  95. Example:
  96. ```python
  97. >>> from transformers import (
  98. ... InstructBlipVideoVisionConfig,
  99. ... InstructBlipVideoQFormerConfig,
  100. ... OPTConfig,
  101. ... InstructBlipVideoConfig,
  102. ... InstructBlipVideoForConditionalGeneration,
  103. ... )
  104. >>> # Initializing a InstructBlipVideoConfig with Salesforce/instructblip-flan-t5-xl style configuration
  105. >>> configuration = InstructBlipVideoConfig()
  106. >>> # Initializing a InstructBlipVideoForConditionalGeneration (with random weights) from the Salesforce/instructblip-flan-t5-xl style configuration
  107. >>> model = InstructBlipVideoForConditionalGeneration(configuration)
  108. >>> # Accessing the model configuration
  109. >>> configuration = model.config
  110. >>> # We can also initialize a InstructBlipVideoConfig from a InstructBlipVideoVisionConfig, InstructBlipVideoQFormerConfig and any PreTrainedConfig
  111. >>> # Initializing Instructblipvideo vision, Instructblipvideo Q-Former and language model configurations
  112. >>> vision_config = InstructBlipVideoVisionConfig()
  113. >>> qformer_config = InstructBlipVideoQFormerConfig()
  114. >>> text_config = OPTConfig()
  115. >>> config = InstructBlipVideoConfig(vision_config=vision_config, qformer_config=qformer_config, text_config=text_config)
  116. ```"""
  117. model_type = "instructblipvideo"
  118. attribute_map = {"video_token_id": "video_token_index"}
  119. sub_configs = {
  120. "text_config": AutoConfig,
  121. "qformer_config": InstructBlipVideoQFormerConfig,
  122. "vision_config": InstructBlipVideoVisionConfig,
  123. }
  124. vision_config: dict | PreTrainedConfig | None = None
  125. qformer_config: dict | PreTrainedConfig | None = None
  126. text_config: dict | PreTrainedConfig | None = None
  127. num_query_tokens: int = 32
  128. initializer_factor: float = 1.0
  129. initializer_range: float = 0.02
  130. video_token_index: int | None = None
  131. def __post_init__(self, **kwargs):
  132. if self.text_config is None:
  133. self.text_config = CONFIG_MAPPING["opt"]()
  134. logger.info("text_config is None. Initializing the text config with default values (`OPTConfig`).")
  135. elif isinstance(self.text_config, dict):
  136. text_model_type = self.text_config.get("model_type", "opt")
  137. self.text_config = CONFIG_MAPPING[text_model_type](**self.text_config)
  138. if self.qformer_config is None:
  139. self.qformer_config = InstructBlipVideoQFormerConfig()
  140. logger.info("qformer_config is None. Initializing the InstructBlipVideoQFormerConfig with default values.")
  141. elif isinstance(self.qformer_config, dict):
  142. self.qformer_config = InstructBlipVideoQFormerConfig(**self.qformer_config)
  143. if self.vision_config is None:
  144. self.vision_config = InstructBlipVideoVisionConfig()
  145. logger.info(
  146. "`vision_config` is `None`. initializing the `InstructBlipVideoVisionConfig` with default values."
  147. )
  148. elif isinstance(self.vision_config, dict):
  149. self.vision_config = InstructBlipVideoVisionConfig(**self.vision_config)
  150. self.qformer_config.encoder_hidden_size = self.vision_config.hidden_size
  151. self.use_decoder_only_language_model = self.text_config.model_type in MODEL_FOR_CAUSAL_LM_MAPPING_NAMES
  152. super().__post_init__(**kwargs)
  153. __all__ = ["InstructBlipVideoConfig", "InstructBlipVideoQFormerConfig", "InstructBlipVideoVisionConfig"]