configuration_ovis2.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. # Copyright 2025 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
  17. from ..qwen2.configuration_qwen2 import Qwen2Config
  18. @auto_docstring(checkpoint="thisisiron/Ovis2-1B-hf")
  19. @strict
  20. class Ovis2VisionConfig(PreTrainedConfig):
  21. r"""
  22. hidden_stride (`int`, *optional*, defaults to 1):
  23. The stride of the hidden layer in the Vision Transformer.
  24. num_visual_indicator_tokens (`int`, *optional*, defaults to 5):
  25. Number of visual indicator tokens.
  26. tokenize_function (`str`, *optional*, defaults to `"softmax"`):
  27. The function used to tokenize the visual indicator tokens.
  28. """
  29. base_config_key = "vision_config"
  30. hidden_size: int = 1024
  31. intermediate_size: int = 2816
  32. num_hidden_layers: int = 24
  33. num_attention_heads: int = 8
  34. num_channels: int = 3
  35. image_size: int | list[int] | tuple[int, int] = 224
  36. patch_size: int | list[int] | tuple[int, int] = 14
  37. rms_norm_eps: float = 1e-5
  38. attention_dropout: float | int = 0.0
  39. qkv_bias: bool = False
  40. mlp_bias: bool = False
  41. hidden_act: str = "silu"
  42. vocab_size: int = 16384
  43. hidden_stride: int = 1
  44. num_visual_indicator_tokens: int = 5
  45. initializer_range: float = 0.02
  46. tokenize_function: str = "softmax"
  47. @auto_docstring(checkpoint="thisisiron/Ovis2-1B-hf")
  48. @strict
  49. class Ovis2Config(PreTrainedConfig):
  50. r"""
  51. visual_indicator_token_ids (`List[int]`, *optional*, defaults to `[151666, 151667, 151668, 151669, 151670]`):
  52. The visual indicator token ids to encode the image prompt.
  53. ```python
  54. >>> from transformers import Ovis2ForConditionalGeneration, Ovis2Config
  55. >>> # Initializing a Ovis2 style configuration
  56. >>> configuration = Ovis2Config()
  57. >>> # Initializing a model from the Ovis2-2B style configuration
  58. >>> model = Ovis2ForConditionalGeneration(configuration)
  59. >>> # Accessing the model configuration
  60. >>> configuration = model.config
  61. ```
  62. """
  63. model_type = "ovis2"
  64. sub_configs = {"text_config": Qwen2Config, "vision_config": Ovis2VisionConfig}
  65. vision_config: dict | PreTrainedConfig | None = None
  66. text_config: dict | PreTrainedConfig | None = None
  67. image_token_id: int = 151665
  68. visual_indicator_token_ids: list[int] | tuple[int, ...] = (151666, 151667, 151668, 151669, 151670)
  69. vocab_size: int = 151643
  70. hidden_size: int = 1536
  71. tie_word_embeddings: bool = True
  72. def __post_init__(self, **kwargs):
  73. if isinstance(self.vision_config, dict):
  74. self.vision_config = Ovis2VisionConfig(**self.vision_config)
  75. if self.vision_config is None:
  76. self.vision_config = Ovis2VisionConfig(num_visual_indicator_tokens=len(self.visual_indicator_token_ids))
  77. if isinstance(self.text_config, dict):
  78. self.text_config = Qwen2Config(**self.text_config)
  79. elif self.text_config is None:
  80. self.text_config = Qwen2Config()
  81. super().__post_init__(**kwargs)
  82. __all__ = ["Ovis2VisionConfig", "Ovis2Config"]