configuration_qwen2.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # Copyright 2024 The Qwen team, Alibaba Group and 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. """Qwen2 model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...modeling_rope_utils import RopeParameters
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="Qwen/Qwen2-7B")
  20. @strict
  21. class Qwen2Config(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import Qwen2Model, Qwen2Config
  26. >>> # Initializing a Qwen2 style configuration
  27. >>> configuration = Qwen2Config()
  28. >>> # Initializing a model from the Qwen2-7B style configuration
  29. >>> model = Qwen2Model(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```"""
  33. model_type = "qwen2"
  34. keys_to_ignore_at_inference = ["past_key_values"]
  35. # Default tensor parallel plan for base model `Qwen2`
  36. base_model_tp_plan = {
  37. "layers.*.self_attn.q_proj": "colwise",
  38. "layers.*.self_attn.k_proj": "colwise",
  39. "layers.*.self_attn.v_proj": "colwise",
  40. "layers.*.self_attn.o_proj": "rowwise",
  41. "layers.*.mlp.gate_proj": "colwise",
  42. "layers.*.mlp.up_proj": "colwise",
  43. "layers.*.mlp.down_proj": "rowwise",
  44. }
  45. base_model_pp_plan = {
  46. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  47. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  48. "norm": (["hidden_states"], ["hidden_states"]),
  49. }
  50. vocab_size: int = 151936
  51. hidden_size: int = 4096
  52. intermediate_size: int = 22016
  53. num_hidden_layers: int = 32
  54. num_attention_heads: int = 32
  55. num_key_value_heads: int | None = 32
  56. hidden_act: str = "silu"
  57. max_position_embeddings: int = 32768
  58. initializer_range: float = 0.02
  59. rms_norm_eps: float = 1e-6
  60. use_cache: bool = True
  61. tie_word_embeddings: bool = False
  62. rope_parameters: RopeParameters | dict | None = None
  63. use_sliding_window: bool = False
  64. sliding_window: int | None = 4096
  65. max_window_layers: int = 28
  66. layer_types: list[str] | None = None
  67. attention_dropout: float | int = 0.0
  68. pad_token_id: int | None = None
  69. bos_token_id: int | None = None
  70. eos_token_id: int | list[int] | None = None
  71. def __post_init__(self, **kwargs):
  72. self.sliding_window = self.sliding_window if self.use_sliding_window else None
  73. if self.num_key_value_heads is None:
  74. self.num_key_value_heads = self.num_attention_heads
  75. if self.layer_types is None:
  76. self.layer_types = [
  77. "sliding_attention"
  78. if self.sliding_window is not None and i >= self.max_window_layers
  79. else "full_attention"
  80. for i in range(self.num_hidden_layers)
  81. ]
  82. super().__post_init__(**kwargs)
  83. __all__ = ["Qwen2Config"]