configuration_vipllava.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # Copyright 2023 Microsoft Research & University of Wisconsin-Madison and the HuggingFace Inc. team. All rights reserved.
  2. # Licensed under the Apache License, Version 2.0 (the "License");
  3. # you may not use this file except in compliance with the License.
  4. # You may obtain a copy of the License at
  5. #
  6. # http://www.apache.org/licenses/LICENSE-2.0
  7. #
  8. # Unless required by applicable law or agreed to in writing, software
  9. # distributed under the License is distributed on an "AS IS" BASIS,
  10. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. # See the License for the specific language governing permissions and
  12. # limitations under the License.
  13. """VipLlava model configuration"""
  14. from huggingface_hub.dataclasses import strict
  15. from ...configuration_utils import PreTrainedConfig
  16. from ...utils import auto_docstring
  17. from ..auto import CONFIG_MAPPING, AutoConfig
  18. @auto_docstring(checkpoint="llava-hf/vip-llava-7b-hf")
  19. @strict
  20. class VipLlavaConfig(PreTrainedConfig):
  21. r"""
  22. projector_layernorm_eps (`float`, *optional*, defaults to 1e-05):
  23. The layer norm epsilon of the projector layernorm
  24. vision_feature_layers (`Union[int, list[int]]`, *optional*, defaults to `[-2, -5, -8, -11, 6]`):
  25. The vision feature layer, or list of layers to select the vision features from.
  26. Example:
  27. ```python
  28. >>> from transformers import VipLlavaForConditionalGeneration, VipLlavaConfig, CLIPVisionConfig, LlamaConfig
  29. >>> # Initializing a CLIP-vision config
  30. >>> vision_config = CLIPVisionConfig()
  31. >>> # Initializing a Llama config
  32. >>> text_config = LlamaConfig()
  33. >>> # Initializing a VipLlava vipllava-7b style configuration
  34. >>> configuration = VipLlavaConfig(vision_config, text_config)
  35. >>> # Initializing a model from the vipllava-7b style configuration
  36. >>> model = VipLlavaForConditionalGeneration(configuration)
  37. >>> # Accessing the model configuration
  38. >>> configuration = model.config
  39. ```"""
  40. model_type = "vipllava"
  41. attribute_map = {
  42. "image_token_id": "image_token_index",
  43. }
  44. sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
  45. vision_config: dict | PreTrainedConfig | None = None
  46. text_config: dict | PreTrainedConfig | None = None
  47. image_token_index: int = 32000
  48. projector_hidden_act: str = "gelu"
  49. projector_layernorm_eps: float = 1e-5
  50. vision_feature_layers: int | list[int] | tuple[int, ...] = (-2, -5, -8, -11, 6)
  51. image_seq_length: int = 576
  52. tie_word_embeddings: bool = False
  53. def __post_init__(self, **kwargs):
  54. if isinstance(self.vision_config, dict):
  55. self.vision_config["model_type"] = self.vision_config.get("model_type", "clip_vision_model")
  56. self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
  57. elif self.vision_config is None:
  58. self.vision_config = CONFIG_MAPPING["clip_vision_model"](
  59. intermediate_size=4096,
  60. hidden_size=1024,
  61. patch_size=14,
  62. image_size=336,
  63. num_hidden_layers=24,
  64. num_attention_heads=16,
  65. vocab_size=32000,
  66. projection_dim=768,
  67. )
  68. if isinstance(self.text_config, dict):
  69. self.text_config["model_type"] = self.text_config.get("model_type", "llama")
  70. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  71. elif self.text_config is None:
  72. self.text_config = CONFIG_MAPPING["llama"]()
  73. super().__post_init__(**kwargs)
  74. __all__ = ["VipLlavaConfig"]