configuration_llava.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. """Llava model configuration"""
  14. from typing import Literal
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. from ..auto import CONFIG_MAPPING, AutoConfig
  19. @auto_docstring(checkpoint="llava-hf/llava-1.5-7b-hf")
  20. @strict
  21. class LlavaConfig(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import LlavaForConditionalGeneration, LlavaConfig, CLIPVisionConfig, LlamaConfig
  26. >>> # Initializing a CLIP-vision config
  27. >>> vision_config = CLIPVisionConfig()
  28. >>> # Initializing a Llama config
  29. >>> text_config = LlamaConfig()
  30. >>> # Initializing a Llava llava-1.5-7b style configuration
  31. >>> configuration = LlavaConfig(vision_config, text_config)
  32. >>> # Initializing a model from the llava-1.5-7b style configuration
  33. >>> model = LlavaForConditionalGeneration(configuration)
  34. >>> # Accessing the model configuration
  35. >>> configuration = model.config
  36. ```"""
  37. model_type = "llava"
  38. attribute_map = {
  39. "image_token_id": "image_token_index",
  40. }
  41. sub_configs = {"text_config": AutoConfig, "vision_config": AutoConfig}
  42. vision_config: dict | PreTrainedConfig | None = None
  43. text_config: dict | PreTrainedConfig | None = None
  44. image_token_index: int = 32000
  45. image_seq_length: int = 576
  46. projector_hidden_act: str = "gelu"
  47. vision_feature_select_strategy: Literal["default", "full"] = "default"
  48. vision_feature_layer: int | list[int] = -2
  49. multimodal_projector_bias: bool = True
  50. tie_word_embeddings: bool = False
  51. def __post_init__(self, **kwargs):
  52. if isinstance(self.vision_config, dict):
  53. self.vision_config["model_type"] = self.vision_config.get("model_type", "clip_vision_model")
  54. self.vision_config = CONFIG_MAPPING[self.vision_config["model_type"]](**self.vision_config)
  55. elif self.vision_config is None:
  56. self.vision_config = CONFIG_MAPPING["clip_vision_model"](
  57. intermediate_size=4096,
  58. hidden_size=1024,
  59. patch_size=14,
  60. image_size=336,
  61. num_hidden_layers=24,
  62. num_attention_heads=16,
  63. vocab_size=32000,
  64. projection_dim=768,
  65. )
  66. if isinstance(self.text_config, dict):
  67. self.text_config["model_type"] = self.text_config.get("model_type", "llama")
  68. self.text_config = CONFIG_MAPPING[self.text_config["model_type"]](**self.text_config)
  69. elif self.text_config is None:
  70. self.text_config = CONFIG_MAPPING["llama"]()
  71. # The default value is `False` but this config is used with many model types
  72. # Attr `tie_word_embeddings` was saved in text config for those models, so we
  73. # need an ugly workaround and forward-pass the attr from text config
  74. if not self.tie_word_embeddings and self.text_config.tie_word_embeddings:
  75. self.tie_word_embeddings = self.text_config.tie_word_embeddings
  76. super().__post_init__(**kwargs)
  77. __all__ = ["LlavaConfig"]