configuration_cwm.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/cwm/modular_cwm.py.
  3. # Do NOT edit this file manually as any edits will be overwritten by the generation of
  4. # the file from the modular. If any change should be done, please apply the change to the
  5. # modular_cwm.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025
  8. #
  9. # Licensed under the Apache License, Version 2.0 (the "License");
  10. # you may not use this file except in compliance with the License.
  11. # You may obtain a copy of the License at
  12. #
  13. # http://www.apache.org/licenses/LICENSE-2.0
  14. #
  15. # Unless required by applicable law or agreed to in writing, software
  16. # distributed under the License is distributed on an "AS IS" BASIS,
  17. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. # See the License for the specific language governing permissions and
  19. # limitations under the License.
  20. from huggingface_hub.dataclasses import strict
  21. from ...configuration_utils import PreTrainedConfig
  22. from ...utils import auto_docstring
  23. @auto_docstring(checkpoint="facebook/cwm")
  24. @strict
  25. class CwmConfig(PreTrainedConfig):
  26. r"""
  27. ```python
  28. >>> from transformers import CwmModel, CwmConfig
  29. >>> # Initializing a Cwm cwm-7b style configuration
  30. >>> configuration = CwmConfig()
  31. >>> # Initializing a model from the cwm-7b style configuration
  32. >>> model = CwmModel(configuration)
  33. >>> # Accessing the model configuration
  34. >>> configuration = model.config
  35. ```"""
  36. model_type = "cwm"
  37. keys_to_ignore_at_inference = ["past_key_values"]
  38. # Default tensor parallel plan for base model `CwmModel`
  39. base_model_tp_plan = {
  40. "layers.*.self_attn.q_proj": "colwise",
  41. "layers.*.self_attn.k_proj": "colwise",
  42. "layers.*.self_attn.v_proj": "colwise",
  43. "layers.*.self_attn.o_proj": "rowwise",
  44. "layers.*.mlp.gate_proj": "colwise",
  45. "layers.*.mlp.up_proj": "colwise",
  46. "layers.*.mlp.down_proj": "rowwise",
  47. }
  48. base_model_pp_plan = {
  49. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  50. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  51. "norm": (["hidden_states"], ["hidden_states"]),
  52. }
  53. vocab_size: int = 128256
  54. hidden_size: int = 6144
  55. intermediate_size: int = 21504
  56. num_hidden_layers: int = 64
  57. num_attention_heads: int = 48
  58. num_key_value_heads: int = 8
  59. hidden_act: str = "silu"
  60. max_position_embeddings: int = 131072
  61. initializer_range: float = 0.02
  62. rms_norm_eps: float = 1e-5
  63. use_cache: bool = True
  64. pad_token_id: int | None = None
  65. bos_token_id: int = 128000
  66. eos_token_id: int | list[int] | None = None
  67. pretraining_tp: int = 1
  68. tie_word_embeddings: bool = False
  69. rope_parameters: dict | None = None
  70. attention_dropout: float | int = 0.0
  71. mlp_bias: bool = False
  72. head_dim: int = 128
  73. default_theta = 1_000_000.0
  74. sliding_window: int = 8192
  75. layer_types: list[str] | None = None # ["full_attention"|"sliding_attention"] per layer
  76. def __post_init__(self, **kwargs):
  77. if self.rope_parameters is None:
  78. self.rope_parameters = {
  79. "rope_theta": 1_000_000.0,
  80. "factor": 16.0,
  81. "high_freq_factor": 4.0,
  82. "low_freq_factor": 1.0,
  83. "original_max_position_embeddings": 8192,
  84. "rope_type": "llama3",
  85. }
  86. if self.layer_types is None:
  87. # Default pattern: every 4th layer uses full attention, others use sliding attention
  88. window_pattern = 4
  89. self.layer_types = [
  90. ("full_attention" if (i % window_pattern == 0) else "sliding_attention")
  91. for i in range(self.num_hidden_layers)
  92. ]
  93. self.sliding_window = int(self.sliding_window) if self.sliding_window else None
  94. self.layer_types = list(self.layer_types)
  95. self.eos_token_id = self.eos_token_id if self.eos_token_id is not None else [128001, 128008, 128009]
  96. if self.head_dim is None:
  97. self.head_dim = self.hidden_size // self.num_attention_heads
  98. if self.num_key_value_heads is None:
  99. self.num_key_value_heads = self.num_attention_heads
  100. super().__post_init__(**kwargs)
  101. def validate_architecture(self):
  102. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  103. if self.hidden_size % self.num_attention_heads != 0:
  104. raise ValueError(
  105. f"The hidden size ({self.hidden_size}) is not a multiple of the number of attention "
  106. f"heads ({self.num_attention_heads})."
  107. )
  108. __all__ = ["CwmConfig"]