configuration_glm.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. # Copyright 2024 The GLM & ZhipuAI team and HuggingFace Inc. team. All rights reserved.
  2. #
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  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="THUDM/glm-4-9b-chat")
  20. @strict
  21. class GlmConfig(PreTrainedConfig):
  22. r"""
  23. Example:
  24. ```python
  25. >>> from transformers import GlmModel, GlmConfig
  26. >>> # Initializing a Glm glm-4-9b-chat style configuration
  27. >>> configuration = GlmConfig()
  28. >>> # Initializing a model from the glm-4-9b-chat style configuration
  29. >>> model = GlmModel(configuration)
  30. >>> # Accessing the model configuration
  31. >>> configuration = model.config
  32. ```"""
  33. model_type = "glm"
  34. keys_to_ignore_at_inference = ["past_key_values"]
  35. base_model_tp_plan = {
  36. "layers.*.self_attn.q_proj": "colwise",
  37. "layers.*.self_attn.k_proj": "colwise",
  38. "layers.*.self_attn.v_proj": "colwise",
  39. "layers.*.self_attn.o_proj": "rowwise",
  40. "layers.*.mlp.gate_up_proj": "colwise_gather_output", # we need to replicate here due to the `chunk` operation
  41. "layers.*.mlp.down_proj": "rowwise_split_input", # input is replicated due to the `chunk` operation
  42. }
  43. base_model_pp_plan = {
  44. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  45. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  46. "norm": (["hidden_states"], ["hidden_states"]),
  47. }
  48. vocab_size: int = 151552
  49. hidden_size: int = 4096
  50. intermediate_size: int = 13696
  51. num_hidden_layers: int = 40
  52. num_attention_heads: int = 32
  53. num_key_value_heads: int | None = 2
  54. head_dim: int | None = 128
  55. hidden_act: str = "silu"
  56. attention_dropout: float | int | None = 0.0
  57. max_position_embeddings: int = 131072
  58. initializer_range: float = 0.02
  59. rms_norm_eps: float = 0.00000015625
  60. use_cache: bool = True
  61. tie_word_embeddings: bool = False
  62. rope_parameters: RopeParameters | dict | None = None
  63. pad_token_id: int | None = 151329
  64. eos_token_id: int | list[int] | None = None
  65. bos_token_id: int | None = None
  66. attention_bias: bool = True
  67. def __post_init__(self, **kwargs):
  68. kwargs.setdefault("partial_rotary_factor", 0.5) # assign default for BC
  69. if self.eos_token_id is None:
  70. self.eos_token_id = [151329, 151336, 151338]
  71. super().__post_init__(**kwargs)
  72. __all__ = ["GlmConfig"]