configuration_qwen3.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. """Qwen3 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/Qwen3-8B")
  20. @strict
  21. class Qwen3Config(PreTrainedConfig):
  22. r"""
  23. ```python
  24. >>> from transformers import Qwen3Model, Qwen3Config
  25. >>> # Initializing a Qwen3 style configuration
  26. >>> configuration = Qwen3Config()
  27. >>> # Initializing a model from the Qwen3-8B style configuration
  28. >>> model = Qwen3Model(configuration)
  29. >>> # Accessing the model configuration
  30. >>> configuration = model.config
  31. ```
  32. """
  33. model_type = "qwen3"
  34. keys_to_ignore_at_inference = ["past_key_values"]
  35. # Default tensor parallel plan for base model `Qwen3`
  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.q_norm": "replicated_with_grad_allreduce",
  41. "layers.*.self_attn.k_norm": "replicated_with_grad_allreduce",
  42. "layers.*.self_attn.o_proj": "rowwise",
  43. "layers.*.mlp.gate_proj": "colwise",
  44. "layers.*.mlp.up_proj": "colwise",
  45. "layers.*.mlp.down_proj": "rowwise",
  46. }
  47. base_model_pp_plan = {
  48. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  49. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  50. "norm": (["hidden_states"], ["hidden_states"]),
  51. }
  52. vocab_size: int = 151936
  53. hidden_size: int = 4096
  54. intermediate_size: int = 22016
  55. num_hidden_layers: int = 32
  56. num_attention_heads: int = 32
  57. num_key_value_heads: int | None = 32
  58. head_dim: int = 128
  59. hidden_act: str = "silu"
  60. max_position_embeddings: int = 32768
  61. initializer_range: float = 0.02
  62. rms_norm_eps: float = 1e-6
  63. use_cache: bool = True
  64. tie_word_embeddings: bool = False
  65. rope_parameters: RopeParameters | dict | None = None
  66. attention_bias: bool = False
  67. use_sliding_window: bool = False
  68. sliding_window: int | None = 4096
  69. max_window_layers: int = 28
  70. layer_types: list[str] | None = None
  71. attention_dropout: float | int = 0.0
  72. pad_token_id: int | None = None
  73. bos_token_id: int | None = None
  74. eos_token_id: int | list[int] | None = None
  75. def __post_init__(self, **kwargs):
  76. self.sliding_window = self.sliding_window if self.use_sliding_window else None
  77. if self.num_key_value_heads is None:
  78. self.num_key_value_heads = self.num_attention_heads
  79. if self.layer_types is None:
  80. self.layer_types = [
  81. "sliding_attention"
  82. if self.sliding_window is not None and i >= self.max_window_layers
  83. else "full_attention"
  84. for i in range(self.num_hidden_layers)
  85. ]
  86. super().__post_init__(**kwargs)
  87. __all__ = ["Qwen3Config"]