configuration_helium.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # coding=utf-8
  2. # Copyright 2024 The Kyutai and HuggingFace Inc. teams. 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 HeliumConfig(PretrainedConfig):
  18. r"""
  19. This is the configuration class to store the configuration of a [`HeliumModel`]. It is used to instantiate an Helium
  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 Helium 2b model.
  22. e.g. [kyutai/helium-2b](https://huggingface.co/kyutai/helium-2b)
  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 48000):
  27. Vocabulary size of the Helium model. Defines the number of different tokens that can be represented by the
  28. `inputs_ids` passed when calling [`HeliumModel`]
  29. hidden_size (`int`, *optional*, defaults to 2560):
  30. Dimension of the hidden representations.
  31. intermediate_size (`int`, *optional*, defaults to 7040):
  32. Dimension of the MLP representations.
  33. num_hidden_layers (`int`, *optional*, defaults to 24):
  34. Number of hidden layers in the Transformer decoder.
  35. num_attention_heads (`int`, *optional*, defaults to 20):
  36. Number of attention heads for each attention layer in the Transformer decoder.
  37. num_key_value_heads (`int`, *optional*, defaults to 20):
  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. head_dim (`int`, *optional*, defaults to 128):
  46. The attention head dimension.
  47. hidden_act (`str` or `function`, *optional*, defaults to `"silu"`):
  48. The legacy activation function. It is overwritten by the `hidden_activation`.
  49. attention_dropout (`float`, *optional*, defaults to 0.0):
  50. The dropout ratio for the attention probabilities.
  51. max_position_embeddings (`int`, *optional*, defaults to 4096):
  52. The maximum sequence length that this model might ever be used with.
  53. initializer_range (`float`, *optional*, defaults to 0.02):
  54. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  55. rms_norm_eps (`float`, *optional*, defaults to 1e-08):
  56. The epsilon used by the rms normalization layers.
  57. use_cache (`bool`, *optional*, defaults to `True`):
  58. Whether or not the model should return the last key/values attentions (not used by all models). Only
  59. relevant if `config.is_decoder=True`.
  60. tie_word_embeddings (`bool`, *optional*, defaults to `False`):
  61. Whether to tie weight embeddings
  62. rope_theta (`float`, *optional*, defaults to 100000.0):
  63. The base period of the RoPE embeddings.
  64. pad_token_id (`int`, *optional*, defaults to 3):
  65. Padding token id.
  66. eos_token_id (`int` | `list`, *optional*, defaults to 2):
  67. End of stream token id.
  68. bos_token_id (`int`, *optional*, defaults to 1):
  69. Beginning of stream token id.
  70. attention_bias (`bool`, *optional*, defaults to `False`):
  71. Whether to use a bias in the query, key, value and output projection layers during self-attention.
  72. mlp_bias (`bool`, *optional*, defaults to `False`):
  73. Whether to use a bias in up_proj, down_proj and gate_proj layers in the MLP layers.
  74. ```python
  75. >>> from transformers import HeliumModel, HeliumConfig
  76. >>> # Initializing a Helium 2b style configuration
  77. >>> configuration = HeliumConfig()
  78. >>> # Initializing a model from the Helium 2b style configuration
  79. >>> model = HeliumModel(configuration)
  80. >>> # Accessing the model configuration
  81. >>> configuration = model.config
  82. ```"""
  83. model_type = "helium"
  84. keys_to_ignore_at_inference = ["past_key_values"]
  85. base_model_tp_plan = {
  86. "layers.*.self_attn.q_proj": "colwise",
  87. "layers.*.self_attn.k_proj": "colwise",
  88. "layers.*.self_attn.v_proj": "colwise",
  89. "layers.*.self_attn.o_proj": "rowwise",
  90. "layers.*.mlp.gate_proj": "colwise",
  91. "layers.*.mlp.up_proj": "colwise",
  92. "layers.*.mlp.down_proj": "rowwise",
  93. }
  94. base_model_pp_plan = {
  95. "embed_tokens": (["input_ids"], ["inputs_embeds"]),
  96. "layers": (["hidden_states", "attention_mask"], ["hidden_states"]),
  97. "norm": (["hidden_states"], ["hidden_states"]),
  98. }
  99. def __init__(
  100. self,
  101. vocab_size=48000,
  102. hidden_size=2560,
  103. intermediate_size=7040,
  104. num_hidden_layers=24,
  105. num_attention_heads=20,
  106. num_key_value_heads=20,
  107. head_dim=128,
  108. hidden_act="silu",
  109. attention_dropout=0.0,
  110. max_position_embeddings=4096,
  111. initializer_range=0.02,
  112. rms_norm_eps=1e-8,
  113. use_cache=True,
  114. tie_word_embeddings=False,
  115. rope_theta=100000.0,
  116. pad_token_id=3,
  117. eos_token_id=2,
  118. bos_token_id=1,
  119. attention_bias=False,
  120. mlp_bias=False,
  121. **kwargs,
  122. ):
  123. self.vocab_size = vocab_size
  124. self.max_position_embeddings = max_position_embeddings
  125. self.hidden_size = hidden_size
  126. self.intermediate_size = intermediate_size
  127. self.num_hidden_layers = num_hidden_layers
  128. self.num_attention_heads = num_attention_heads
  129. self.num_key_value_heads = num_key_value_heads
  130. self.head_dim = head_dim
  131. self.hidden_act = hidden_act
  132. self.initializer_range = initializer_range
  133. self.rms_norm_eps = rms_norm_eps
  134. self.use_cache = use_cache
  135. self.rope_theta = rope_theta
  136. self.attention_bias = attention_bias
  137. self.attention_dropout = attention_dropout
  138. self.mlp_bias = mlp_bias
  139. super().__init__(
  140. pad_token_id=pad_token_id,
  141. bos_token_id=bos_token_id,
  142. eos_token_id=eos_token_id,
  143. tie_word_embeddings=tie_word_embeddings,
  144. **kwargs,
  145. )
  146. __all__ = ["HeliumConfig"]