qwen2vl.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # coding=utf-8
  2. # Copyright 2024 The Qwen team, Alibaba Group and the HuggingFace Inc. team.
  3. # All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Qwen2VL model configuration"""
  17. import os
  18. from typing import Union
  19. from transformers import PretrainedConfig
  20. class Qwen2VLVisionConfig(PretrainedConfig):
  21. model_type = "qwen2_vl"
  22. def __init__(
  23. self,
  24. depth=32,
  25. embed_dim=1280,
  26. hidden_size=3584,
  27. hidden_act="quick_gelu",
  28. mlp_ratio=4,
  29. num_heads=16,
  30. in_channels=3,
  31. patch_size=14,
  32. spatial_merge_size=2,
  33. temporal_patch_size=2,
  34. **kwargs,
  35. ):
  36. super().__init__(**kwargs)
  37. self.depth = depth
  38. self.embed_dim = embed_dim
  39. self.hidden_size = hidden_size
  40. self.hidden_act = hidden_act
  41. self.mlp_ratio = mlp_ratio
  42. self.num_heads = num_heads
  43. self.in_channels = in_channels
  44. self.patch_size = patch_size
  45. self.spatial_merge_size = spatial_merge_size
  46. self.temporal_patch_size = temporal_patch_size
  47. @classmethod
  48. def from_pretrained(
  49. cls, pretrained_model_name_or_path: Union[str, os.PathLike], **kwargs
  50. ) -> "PretrainedConfig":
  51. cls._set_token_in_kwargs(kwargs)
  52. config_dict, kwargs = cls.get_config_dict(
  53. pretrained_model_name_or_path, **kwargs
  54. )
  55. if config_dict.get("model_type") == "qwen2_vl":
  56. config_dict = config_dict["vision_config"]
  57. return cls.from_dict(config_dict, **kwargs)
  58. class Qwen2VLConfig(PretrainedConfig):
  59. def __init__(
  60. self,
  61. vocab_size=152064,
  62. hidden_size=8192,
  63. intermediate_size=29568,
  64. num_hidden_layers=80,
  65. num_attention_heads=64,
  66. num_key_value_heads=8,
  67. hidden_act="silu",
  68. max_position_embeddings=32768,
  69. initializer_range=0.02,
  70. rms_norm_eps=1e-05,
  71. use_cache=True,
  72. tie_word_embeddings=False,
  73. rope_theta=1000000.0,
  74. use_sliding_window=False,
  75. sliding_window=4096,
  76. max_window_layers=80,
  77. attention_dropout=0.0,
  78. vision_config=None,
  79. rope_scaling=None,
  80. **kwargs,
  81. ):
  82. if isinstance(vision_config, dict):
  83. self.vision_config = Qwen2VLVisionConfig(**vision_config)
  84. elif vision_config is None:
  85. self.vision_config = Qwen2VLVisionConfig()
  86. self.vocab_size = vocab_size
  87. self.max_position_embeddings = max_position_embeddings
  88. self.hidden_size = hidden_size
  89. self.intermediate_size = intermediate_size
  90. self.num_hidden_layers = num_hidden_layers
  91. self.num_attention_heads = num_attention_heads
  92. self.use_sliding_window = use_sliding_window
  93. self.sliding_window = sliding_window
  94. self.max_window_layers = max_window_layers
  95. # for backward compatibility
  96. if num_key_value_heads is None:
  97. num_key_value_heads = num_attention_heads
  98. self.num_key_value_heads = num_key_value_heads
  99. self.hidden_act = hidden_act
  100. self.initializer_range = initializer_range
  101. self.rms_norm_eps = rms_norm_eps
  102. self.use_cache = use_cache
  103. self.rope_theta = rope_theta
  104. self.attention_dropout = attention_dropout
  105. self.rope_scaling = rope_scaling
  106. # NOTE: the following section from original transformers config
  107. # for Qwen2-VL is commented out to address rope config loading issue
  108. #
  109. # if self.rope_scaling is not None and "type" in self.rope_scaling:
  110. # if self.rope_scaling["type"] == "mrope":
  111. # self.rope_scaling["type"] = "default"
  112. # self.rope_scaling["rope_type"] = self.rope_scaling["type"]
  113. # rope_config_validation(self)
  114. super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)