configuration_glm.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. # coding=utf-8
  2. # Copyright 2024 The GLM & ZhipuAI team and HuggingFace Inc. team. All rights reserved.
  3. #
  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. from ...configuration_utils import PretrainedConfig
  17. class GlmConfig(PretrainedConfig):
  18. r"""
  19. This is the configuration class to store the configuration of a [`GlmModel`]. It is used to instantiate an Glm
  20. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  21. defaults will yield a similar configuration to that of the Glm-4-9b-chat.
  22. e.g. [THUDM/glm-4-9b-chat](https://huggingface.co/THUDM/glm-4-9b-chat)
  23. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  24. documentation from [`PretrainedConfig`] for more information.
  25. Args:
  26. vocab_size (`int`, *optional*, defaults to 151552):
  27. Vocabulary size of the Glm model. Defines the number of different tokens that can be represented by the
  28. `inputs_ids` passed when calling [`GlmModel`]
  29. hidden_size (`int`, *optional*, defaults to 4096):
  30. Dimension of the hidden representations.
  31. intermediate_size (`int`, *optional*, defaults to 13696):
  32. Dimension of the MLP representations.
  33. num_hidden_layers (`int`, *optional*, defaults to 40):
  34. Number of hidden layers in the Transformer decoder.
  35. num_attention_heads (`int`, *optional*, defaults to 32):
  36. Number of attention heads for each attention layer in the Transformer decoder.
  37. num_key_value_heads (`int`, *optional*, defaults to 2):
  38. This is the number of key_value heads that should be used to implement Grouped Query Attention. If
  39. `num_key_value_heads=num_attention_heads`, the model will use Multi Head Attention (MHA), if
  40. `num_key_value_heads=1` the model will use Multi Query Attention (MQA) otherwise GQA is used. When
  41. converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
  42. by meanpooling all the original heads within that group. For more details, check out [this
  43. paper](https://huggingface.co/papers/2305.13245). If it is not specified, will default to
  44. `num_attention_heads`.
  45. partial_rotary_factor (`float`, *optional*, defaults to 0.5): The factor of the partial rotary position.
  46. head_dim (`int`, *optional*, defaults to 128):
  47. The attention head dimension.
  48. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  49. The legacy activation function. It is overwritten by the `hidden_activation`.
  50. attention_dropout (`float`, *optional*, defaults to 0.0):
  51. The dropout ratio for the attention probabilities.
  52. max_position_embeddings (`int`, *optional*, defaults to 131072):
  53. The maximum sequence length that this model might ever be used with.
  54. initializer_range (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  56. rms_norm_eps (`float`, *optional*, defaults to 1.5625e-07):
  57. The epsilon used by the rms normalization layers.
  58. use_cache (`bool`, *optional*, defaults to `True`):
  59. Whether or not the model should return the last key/values attentions (not used by all models). Only
  60. relevant if `config.is_decoder=True`.
  61. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  62. Whether to tie weight embeddings
  63. rope_theta (`float`, *optional*, defaults to 10000.0):
  64. The base period of the RoPE embeddings.
  65. pad_token_id (`int`, *optional*, defaults to 151329):
  66. Padding token id.
  67. eos_token_id (`int` | `list`, *optional*, defaults to `[151329, 151336, 151338]`):
  68. End of stream token id.
  69. bos_token_id (`int`, *optional*):
  70. Beginning of stream token id.
  71. attention_bias (`bool`, defaults to `False`, *optional*, defaults to `True`):
  72. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  73. ```python
  74. >>> from transformers import GlmModel, GlmConfig
  75. >>> # Initializing a Glm glm-4-9b-chat style configuration
  76. >>> configuration = GlmConfig()
  77. >>> # Initializing a model from the glm-4-9b-chat style configuration
  78. >>> model = GlmModel(configuration)
  79. >>> # Accessing the model configuration
  80. >>> configuration = model.config
  81. ```"""
  82. model_type = "glm"
  83. keys_to_ignore_at_inference = ["past_key_values"]
  84. base_model_tp_plan = {
  85. "layers.*.self_attn.q_proj": "colwise",
  86. "layers.*.self_attn.k_proj": "colwise",
  87. "layers.*.self_attn.v_proj": "colwise",
  88. "layers.*.self_attn.o_proj": "rowwise",
  89. "layers.*.mlp.gate_up_proj": "colwise_rep", # we need to replicate here due to the `chunk` operation
  90. "layers.*.mlp.down_proj": "rowwise_rep", # we need to replicate here due to the `chunk` operation
  91. }
  92. base_model_pp_plan = {
  93. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  94. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  95. "norm": (["hidden_states"], ["hidden_states"]),
  96. }
  97. def __init__(
  98. self,
  99. vocab_size=151552,
  100. hidden_size=4096,
  101. intermediate_size=13696,
  102. num_hidden_layers=40,
  103. num_attention_heads=32,
  104. num_key_value_heads=2,
  105. partial_rotary_factor=0.5,
  106. head_dim=128,
  107. hidden_act="silu",
  108. attention_dropout=0.0,
  109. max_position_embeddings=131072,
  110. initializer_range=0.02,
  111. rms_norm_eps=0.00000015625,
  112. use_cache=True,
  113. tie_word_embeddings=False,
  114. rope_theta=10000.0,
  115. pad_token_id=151329,
  116. eos_token_id=[151329, 151336, 151338],
  117. bos_token_id=None,
  118. attention_bias=True,
  119. **kwargs,
  120. ):
  121. self.vocab_size = vocab_size
  122. self.max_position_embeddings = max_position_embeddings
  123. self.hidden_size = hidden_size
  124. self.intermediate_size = intermediate_size
  125. self.num_hidden_layers = num_hidden_layers
  126. self.num_attention_heads = num_attention_heads
  127. self.partial_rotary_factor = partial_rotary_factor
  128. self.head_dim = head_dim
  129. self.num_key_value_heads = num_key_value_heads
  130. self.hidden_act = hidden_act
  131. self.initializer_range = initializer_range
  132. self.rms_norm_eps = rms_norm_eps
  133. self.use_cache = use_cache
  134. self.rope_theta = rope_theta
  135. self.attention_bias = attention_bias
  136. self.attention_dropout = attention_dropout
  137. super().__init__(
  138. pad_token_id=pad_token_id,
  139. bos_token_id=bos_token_id,
  140. eos_token_id=eos_token_id,
  141. tie_word_embeddings=tie_word_embeddings,
  142. **kwargs,
  143. )
  144. __all__ = ["GlmConfig"]