configuration_smollm3.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  2. # This file was automatically generated from src/transformers/models/smollm3/modular_smollm3.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_smollm3.py file directly. One of our CI enforces this.
  6. # 🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨
  7. # Copyright 2025 The HuggingFace Inc. team. All rights reserved.
  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 ...modeling_rope_utils import RopeParameters
  23. from ...utils import auto_docstring
  24. @auto_docstring(checkpoint="HuggingFaceTB/SmolLM3-3B")
  25. @strict
  26. class SmolLM3Config(PreTrainedConfig):
  27. r"""
  28. no_rope_layers (`List[int]`, *optional*):
  29. List with at least the same length as the number of layers in the model.
  30. A `1` at an index position indicates that the corresponding layer will use RoPE,
  31. while a `0` indicates that it's a NoPE layer.
  32. no_rope_layer_interval (`int`, *optional*, defaults to 4):
  33. If `no_rope_layers` is `None`, it will be created using a NoPE layer every
  34. `no_rope_layer_interval` layers.
  35. ```python
  36. >>> from transformers import SmolLM3Model, SmolLM3Config
  37. >>> # Initializing a SmolLM3 style configuration
  38. >>> configuration = SmolLM3Config()
  39. >>> # Initializing a model from the SmolLM3 style configuration
  40. >>> model = SmolLM3Model(configuration)
  41. >>> # Accessing the model configuration
  42. >>> configuration = model.config
  43. ```"""
  44. model_type = "smollm3"
  45. keys_to_ignore_at_inference = ["past_key_values"]
  46. default_theta = 2000000.0
  47. base_model_tp_plan = {
  48. "layers.*.self_attn.q_proj": "colwise",
  49. "layers.*.self_attn.k_proj": "colwise",
  50. "layers.*.self_attn.v_proj": "colwise",
  51. "layers.*.self_attn.o_proj": "rowwise",
  52. "layers.*.mlp.gate_proj": "colwise",
  53. "layers.*.mlp.up_proj": "colwise",
  54. "layers.*.mlp.down_proj": "rowwise",
  55. }
  56. base_model_pp_plan = {
  57. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  58. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  59. "norm": (["hidden_states"], ["hidden_states"]),
  60. }
  61. vocab_size: int = 128256
  62. hidden_size: int = 2048
  63. intermediate_size: int = 11008
  64. num_hidden_layers: int = 36
  65. num_attention_heads: int = 16
  66. num_key_value_heads: int | None = 4
  67. hidden_act: str = "silu"
  68. max_position_embeddings: int = 32768
  69. initializer_range: float = 0.02
  70. rms_norm_eps: float = 1e-6
  71. use_cache: bool = True
  72. pad_token_id: int | None = 128004
  73. bos_token_id: int | None = 128000
  74. eos_token_id: int | list[int] | None = 128001
  75. rope_parameters: RopeParameters | dict | None = None
  76. use_sliding_window: bool = False
  77. sliding_window: int | None = None
  78. no_rope_layers: list[int] | None = None
  79. no_rope_layer_interval: int = 4
  80. layer_types: list[str] | None = None
  81. attention_bias: bool = False
  82. attention_dropout: float | int = 0.0
  83. mlp_bias: bool = False
  84. tie_word_embeddings: bool = True
  85. def __post_init__(self, **kwargs):
  86. if self.num_key_value_heads is None:
  87. self.num_key_value_heads = self.num_attention_heads
  88. if self.no_rope_layers is None:
  89. self.no_rope_layers = [
  90. int((layer_idx + 1) % self.no_rope_layer_interval != 0) for layer_idx in range(self.num_hidden_layers)
  91. ]
  92. if self.layer_types is None:
  93. self.layer_types = []
  94. for layer_idx in range(self.num_hidden_layers):
  95. has_rope = self.no_rope_layers[layer_idx]
  96. if self.use_sliding_window and self.sliding_window is not None and not has_rope:
  97. self.layer_types.append("sliding_attention")
  98. else:
  99. self.layer_types.append("full_attention")
  100. super().__post_init__(**kwargs)
  101. __all__ = ["SmolLM3Config"]