configuration_kosmos2.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # Copyright 2023 Microsoft Research and 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. """KOSMOS-2 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="microsoft/kosmos-2-patch14-224")
  20. @strict
  21. class Kosmos2TextConfig(PreTrainedConfig):
  22. model_type = "kosmos_2_text_model"
  23. base_config_key = "text_config"
  24. keys_to_ignore_at_inference = ["past_key_values"]
  25. attribute_map = {
  26. "num_attention_heads": "attention_heads",
  27. "hidden_size": "embed_dim",
  28. "num_hidden_layers": "layers",
  29. }
  30. vocab_size: int = 65037
  31. max_position_embeddings: int = 2048
  32. embed_dim: int = 2048
  33. layers: int = 24
  34. ffn_dim: int = 8192
  35. attention_heads: int = 32
  36. activation_function: str = "gelu"
  37. dropout: float | int = 0.1
  38. attention_dropout: float | int = 0.1
  39. activation_dropout: float | int = 0.0
  40. layerdrop: float | int = 0.0
  41. layer_norm_eps: float = 1e-5
  42. init_std: float = 0.02
  43. scale_embedding: bool = True
  44. use_cache: bool = True
  45. pad_token_id: int | None = 1
  46. bos_token_id: int | None = 0
  47. eos_token_id: int | list[int] | None = 2
  48. add_cross_attention: bool = False
  49. @auto_docstring(checkpoint="microsoft/kosmos-2-patch14-224")
  50. @strict
  51. class Kosmos2VisionConfig(PreTrainedConfig):
  52. model_type = "kosmos_2_vision_model"
  53. base_config_key = "vision_config"
  54. hidden_size: int = 1024
  55. intermediate_size: int = 4096
  56. num_hidden_layers: int = 24
  57. num_attention_heads: int = 16
  58. num_channels: int = 3
  59. image_size: int | list[int] | tuple[int, int] = 224
  60. patch_size: int | list[int] | tuple[int, int] = 14
  61. hidden_act: str = "quick_gelu"
  62. layer_norm_eps: float = 1e-5
  63. attention_dropout: float | int = 0.0
  64. initializer_range: float = 0.02
  65. initializer_factor: float = 1.0
  66. @auto_docstring(checkpoint="microsoft/kosmos-2-patch14-224")
  67. @strict
  68. class Kosmos2Config(PreTrainedConfig):
  69. r"""
  70. latent_query_num (`int`, *optional*, defaults to 64):
  71. The number of latent query tokens that represent the image features used in the text decoder component.
  72. Example:
  73. ```python
  74. >>> from transformers import Kosmos2Config, Kosmos2Model
  75. >>> # Initializing a Kosmos-2 kosmos-2-patch14-224 style configuration
  76. >>> configuration = Kosmos2Config()
  77. >>> # Initializing a model (with random weights) from the kosmos-2-patch14-224 style configuration
  78. >>> model = Kosmos2Model(configuration)
  79. >>> # Accessing the model configuration
  80. >>> configuration = model.config
  81. ```"""
  82. model_type = "kosmos-2"
  83. sub_configs = {"text_config": Kosmos2TextConfig, "vision_config": Kosmos2VisionConfig}
  84. text_config: dict | PreTrainedConfig | None = None
  85. vision_config: dict | PreTrainedConfig | None = None
  86. latent_query_num: int = 64
  87. tie_word_embeddings: bool = True
  88. def __post_init__(self, **kwargs):
  89. if self.text_config is None:
  90. self.text_config = Kosmos2TextConfig()
  91. logger.info("`text_config` is `None`. initializing the `Kosmos2TextConfig` with default values.")
  92. elif isinstance(self.text_config, dict):
  93. self.text_config = Kosmos2TextConfig(**self.text_config)
  94. if self.vision_config is None:
  95. self.vision_config = Kosmos2VisionConfig()
  96. logger.info("`vision_config` is `None`. initializing the `Kosmos2VisionConfig` with default values.")
  97. elif isinstance(self.vision_config, dict):
  98. self.vision_config = Kosmos2VisionConfig(**self.vision_config)
  99. super().__post_init__(**kwargs)
  100. __all__ = ["Kosmos2Config"]