configuration_vipllava.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # coding=utf-8
  2. # Copyright 2023 Microsoft Research & University of Wisconsin-Madison and the HuggingFace Inc. team. All rights reserved.
  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. """VipLlava model configuration"""
  15. from ...configuration_utils import PretrainedConfig
  16. from ...utils import logging
  17. from ..auto import CONFIG_MAPPING, AutoConfig
  18. logger = logging.get_logger(__name__)
  19. class VipLlavaConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`VipLlavaForConditionalGeneration`]. It is used to instantiate an
  22. VipLlava model according to the specified arguments, defining the model architecture. Instantiating a configuration
  23. with the defaults will yield a similar configuration to that of the VipLlava-9B.
  24. e.g. [ybelkada/vip-llava-7b-hf](https://huggingface.co/ybelkada/vip-llava-7b-hf)
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vision_config (`VipLlavaVisionConfig`, *optional*):
  29. Custom vision config or dict
  30. text_config (`Union[AutoConfig, dict]`, *optional*):
  31. The config object of the text backbone. Can be any of `LlamaConfig` or `MistralConfig`.
  32. image_token_index (`int`, *optional*, defaults to 32000):
  33. The image token index to encode the image prompt.
  34. projector_hidden_act (`str`, *optional*, defaults to `"gelu"`):
  35. The activation function used by the multimodal projector.
  36. projector_layernorm_eps (`float`, *optional*, defaults to 1e-05):
  37. The layer norm epsilon of the projector layernorm
  38. vision_feature_layers (`Union[int, list[int]]`, *optional*, defaults to `[-2, -5, -8, -11, 6]`):
  39. The vision feature layer, or list of layers to select the vision features from.
  40. image_seq_length (`int`, *optional*, defaults to 576):
  41. Sequence length of one image embedding.
  42. Example:
  43. ```python
  44. >>> from transformers import VipLlavaForConditionalGeneration, VipLlavaConfig, CLIPVisionConfig, LlamaConfig
  45. >>> # Initializing a CLIP-vision config
  46. >>> vision_config = CLIPVisionConfig()
  47. >>> # Initializing a Llama config
  48. >>> text_config = LlamaConfig()
  49. >>> # Initializing a VipLlava vipllava-7b style configuration
  50. >>> configuration = VipLlavaConfig(vision_config, text_config)
  51. >>> # Initializing a model from the vipllava-7b style configuration
  52. >>> model = VipLlavaForConditionalGeneration(configuration)
  53. >>> # Accessing the model configuration
  54. >>> configuration = model.config
  55. ```"""
  56. model_type = "vipllava"
  57. attribute_map = {
  58. "image_token_id": "image_token_index",
  59. }
  60. sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
  61. def __init__(
  62. self,
  63. vision_config=None,
  64. text_config=None,
  65. image_token_index=32000,
  66. projector_hidden_act="gelu",
  67. projector_layernorm_eps=1e-5,
  68. vision_feature_layers=[-2, -5, -8, -11, 6],
  69. image_seq_length=576,
  70. **kwargs,
  71. ):
  72. self.image_token_index = image_token_index
  73. self.projector_hidden_act = projector_hidden_act
  74. self.projector_layernorm_eps = projector_layernorm_eps
  75. self.vision_feature_layers = vision_feature_layers
  76. self.image_seq_length = image_seq_length
  77. self.vision_config = vision_config
  78. if isinstance(self.vision_config, dict):
  79. vision_config["model_type"] = vision_config.get("model_type", "clip_vision_model")
  80. self.vision_config = CONFIG_MAPPING[vision_config["model_type"]](**vision_config)
  81. elif vision_config is None:
  82. self.vision_config = CONFIG_MAPPING["clip_vision_model"](
  83. intermediate_size=4096,
  84. hidden_size=1024,
  85. patch_size=14,
  86. image_size=336,
  87. num_hidden_layers=24,
  88. num_attention_heads=16,
  89. vocab_size=32000,
  90. projection_dim=768,
  91. )
  92. if isinstance(text_config, dict):
  93. text_config["model_type"] = text_config.get("model_type", "llama")
  94. text_config = CONFIG_MAPPING[text_config["model_type"]](**text_config)
  95. elif text_config is None:
  96. text_config = CONFIG_MAPPING["llama"]()
  97. self.text_config = text_config
  98. super().__init__(**kwargs)
  99. __all__ = ["VipLlavaConfig"]