configuration_janus.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/janus/modular_janus.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_janus.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 Deepseek AI and The HuggingFace 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 ...utils import auto_docstring, logging
  23. from ..auto import CONFIG_MAPPING, AutoConfig
  24. logger = logging.get_logger(__name__)
  25. @auto_docstring(checkpoint="deepseek-community/Janus-Pro-1B")
  26. @strict
  27. class JanusVisionConfig(PreTrainedConfig):
  28. r"""
  29. projection_dropout (`float`, *optional*, defaults to 0.0):
  30. Dropout probability for the projection layer.
  31. num_image_tokens (`int`, *optional*, defaults to 576):
  32. Number of image tokens.
  33. """
  34. model_type = "janus_vision_model"
  35. base_config_key = "vision_config"
  36. hidden_size: int = 1024
  37. num_hidden_layers: int = 24
  38. num_attention_heads: int = 16
  39. num_channels: int = 3
  40. image_size: int | list[int] | tuple[int, int] = 384
  41. patch_size: int | list[int] | tuple[int, int] = 16
  42. hidden_act: str = "gelu"
  43. layer_norm_eps: float = 1e-6
  44. attention_dropout: float | int = 0.0
  45. mlp_ratio: float | int = 4.0
  46. attention_bias: bool = True
  47. hidden_dropout_rate: float | int = 0.0
  48. projection_dim: int = 2048
  49. projection_dropout: float | int = 0.0
  50. use_qk_norm: bool = False
  51. initializer_range: float = 0.02
  52. depth: int = 2
  53. num_image_tokens: int = 576
  54. @auto_docstring(checkpoint="deepseek-community/Janus-Pro-1B")
  55. @strict
  56. class JanusVQVAEConfig(PreTrainedConfig):
  57. r"""
  58. base_channels (`int`, *optional*, defaults to 128):
  59. Base channel count.
  60. channel_multiplier (`list[int]`, *optional*, defaults to `[1, 1, 2, 2, 4]`):
  61. Channel multipliers for each resolution.
  62. num_res_blocks (`int`, *optional*, defaults to 2):
  63. Number of residual blocks.
  64. num_patches (`int`, *optional*, defaults to 32):
  65. Num of patches the input images can be divided into.
  66. out_channels (`int`, *optional*, defaults to 3):
  67. Number of out channels.
  68. image_token_embed_dim (`int`, *optional*, defaults to 2048):
  69. Dimension of image embeddings. It should be same as the dimensionality of text embeddings.
  70. """
  71. model_type = "janus_vqgan"
  72. base_config_key = "vq_config"
  73. embed_dim: int = 8
  74. num_embeddings: int = 16384
  75. double_latent: bool = False
  76. latent_channels: int = 256
  77. in_channels: int = 3
  78. base_channels: int = 128
  79. channel_multiplier: list[int] | tuple[int, ...] = (1, 1, 2, 2, 4)
  80. num_res_blocks: int = 2
  81. dropout: float | int = 0.0
  82. initializer_range: float = 0.02
  83. num_patches: int = 32
  84. out_channels: int = 3
  85. projection_dim: int = 2048
  86. num_hidden_layers: int = 2
  87. hidden_act: str = "gelu"
  88. image_token_embed_dim: int = 2048
  89. @auto_docstring(checkpoint="deepseek-community/Janus-Pro-1B")
  90. @strict
  91. class JanusConfig(PreTrainedConfig):
  92. r"""
  93. Example:
  94. ```python
  95. >>> from transformers import JanusForConditionalGeneration, JanusConfig, JanusVisionConfig, JanusVQVAEConfig, LlamaConfig
  96. >>> # Initializing a Janus vision config
  97. >>> vision_config = JanusVisionConfig()
  98. >>> # Initializing a Llama config
  99. >>> text_config = LlamaConfig()
  100. >>> # Initializing a VQ config
  101. >>> vq_config = JanusVQVAEConfig()
  102. >>> # Initializing a Janus Pro 1B style configuration
  103. >>> configuration = JanusConfig(vision_config=vision_config, text_config=text_config, vq_config=vq_config)
  104. >>> # Initializing a model from the Janus Pro 1B style configuration
  105. >>> model = JanusForConditionalGeneration(configuration)
  106. >>> # Accessing the model configuration
  107. >>> configuration = model.config
  108. ```"""
  109. model_type = "janus"
  110. sub_configs = {
  111. "text_config": AutoConfig,
  112. "vision_config": JanusVisionConfig,
  113. "vq_config": JanusVQVAEConfig,
  114. }
  115. text_config: dict | PreTrainedConfig | None = None
  116. vision_config: dict | PreTrainedConfig | None = None
  117. vq_config: dict | PreTrainedConfig | None = None
  118. image_token_id: int = 100581
  119. def __post_init__(self, **kwargs):
  120. if isinstance(self.text_config, dict):
  121. self.text_config["model_type"] = self.text_config.get("model_type", "llama")
  122. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  123. elif self.text_config is None:
  124. logger.info("`text_config` is None. Initializing with default values")
  125. self.text_config = CONFIG_MAPPING["llama"]()
  126. if self.vision_config is None:
  127. logger.info("`vision_config` is None. Initializing with default JanusVisionConfig values")
  128. self.vision_config = JanusVisionConfig()
  129. elif isinstance(self.vision_config, dict):
  130. self.vision_config = JanusVisionConfig(**self.vision_config)
  131. if self.vq_config is None:
  132. logger.info("`vq_config` is None. Initializing with default JanusVQVAEConfig values")
  133. self.vq_config = JanusVQVAEConfig()
  134. elif isinstance(self.vq_config, dict):
  135. self.vq_config = JanusVQVAEConfig(**self.vq_config)
  136. # This dimension is required when decoding discrete image tokens to continuous input.
  137. self.vq_config.num_patches = self.vision_config.image_size // self.vision_config.patch_size
  138. super().__post_init__(**kwargs)
  139. __all__ = ["JanusVQVAEConfig", "JanusVisionConfig", "JanusConfig"]