configuration_persimmon.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # coding=utf-8
  2. # Copyright 2023 Adept AI and the HuggingFace Inc. team. All rights reserved.
  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. """Persimmon model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...modeling_rope_utils import rope_config_validation
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class PersimmonConfig(PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`PersimmonModel`]. It is used to instantiate an
  23. Persimmon model according to the specified arguments, defining the model architecture. Instantiating a
  24. configuration with the defaults will yield a similar configuration to that of the
  25. [adept/persimmon-8b-base](https://huggingface.co/adept/persimmon-8b-base).
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. vocab_size (`int`, *optional*, defaults to 262144):
  30. Vocabulary size of the Persimmon model. Defines the number of different tokens that can be represented by
  31. the `inputs_ids` passed when calling [`PersimmonModel`]
  32. hidden_size (`int`, *optional*, defaults to 4096):
  33. Dimension of the hidden representations.
  34. intermediate_size (`int`, *optional*, defaults to 16384):
  35. Dimension of the MLP representations.
  36. num_hidden_layers (`int`, *optional*, defaults to 36):
  37. Number of hidden layers in the Transformer encoder.
  38. num_attention_heads (`int`, *optional*, defaults to 64):
  39. Number of attention heads for each attention layer in the Transformer encoder.
  40. hidden_act (`str` or `function`, *optional*, defaults to `"relu2"`):
  41. The non-linear activation function (function or string) in the decoder.
  42. max_position_embeddings (`int`, *optional*, defaults to 16384):
  43. The maximum sequence length that this model might ever be used with.
  44. initializer_range (`float`, *optional*, defaults to 0.02):
  45. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  46. layer_norm_eps (`float`, *optional*, defaults to 1e-5):
  47. The epsilon used by the rms normalization layers.
  48. use_cache (`bool`, *optional*, defaults to `True`):
  49. Whether or not the model should return the last key/values attentions (not used by all models). Only
  50. relevant if `config.is_decoder=True`.
  51. tie_word_embeddings(`bool`, *optional*, defaults to `False`):
  52. Whether to tie weight embeddings
  53. rope_theta (`float`, *optional*, defaults to 25000.0):
  54. The base period of the RoPE embeddings.
  55. rope_scaling (`Dict`, *optional*):
  56. Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
  57. and you expect the model to work on longer `max_position_embeddings`, we recommend you to update this value
  58. accordingly.
  59. Expected contents:
  60. `rope_type` (`str`):
  61. The sub-variant of RoPE to use. Can be one of ['default', 'linear', 'dynamic', 'yarn', 'longrope',
  62. 'llama3'], with 'default' being the original RoPE implementation.
  63. `factor` (`float`, *optional*):
  64. Used with all rope types except 'default'. The scaling factor to apply to the RoPE embeddings. In
  65. most scaling types, a `factor` of x will enable the model to handle sequences of length x *
  66. original maximum pre-trained length.
  67. `original_max_position_embeddings` (`int`, *optional*):
  68. Used with 'dynamic', 'longrope' and 'llama3'. The original max position embeddings used during
  69. pretraining.
  70. `attention_factor` (`float`, *optional*):
  71. Used with 'yarn' and 'longrope'. The scaling factor to be applied on the attention
  72. computation. If unspecified, it defaults to value recommended by the implementation, using the
  73. `factor` field to infer the suggested value.
  74. `beta_fast` (`float`, *optional*):
  75. Only used with 'yarn'. Parameter to set the boundary for extrapolation (only) in the linear
  76. ramp function. If unspecified, it defaults to 32.
  77. `beta_slow` (`float`, *optional*):
  78. Only used with 'yarn'. Parameter to set the boundary for interpolation (only) in the linear
  79. ramp function. If unspecified, it defaults to 1.
  80. `short_factor` (`list[float]`, *optional*):
  81. Only used with 'longrope'. The scaling factor to be applied to short contexts (<
  82. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  83. size divided by the number of attention heads divided by 2
  84. `long_factor` (`list[float]`, *optional*):
  85. Only used with 'longrope'. The scaling factor to be applied to long contexts (<
  86. `original_max_position_embeddings`). Must be a list of numbers with the same length as the hidden
  87. size divided by the number of attention heads divided by 2
  88. `low_freq_factor` (`float`, *optional*):
  89. Only used with 'llama3'. Scaling factor applied to low frequency components of the RoPE
  90. `high_freq_factor` (`float`, *optional*):
  91. Only used with 'llama3'. Scaling factor applied to high frequency components of the RoPE
  92. qk_layernorm (`bool`, *optional*, default to `True`):
  93. Whether or not to normalize the Queries and Keys after projecting the hidden states
  94. hidden_dropout (`float`, *optional*, default to 0.0):
  95. The dropout ratio after applying the MLP to the hidden states.
  96. attention_dropout (`float`, *optional*, default to 0.0):
  97. The dropout ratio after computing the attention scores.
  98. partial_rotary_factor (`float`, *optional*, default to 0.5):
  99. Percentage of the query and keys which will have rotary embedding.
  100. Example:
  101. ```python
  102. >>> from transformers import PersimmonModel, PersimmonConfig
  103. >>> # Initializing a Persimmon persimmon-7b style configuration
  104. >>> configuration = PersimmonConfig()
  105. ```"""
  106. model_type = "persimmon"
  107. keys_to_ignore_at_inference = ["past_key_values"]
  108. def __init__(
  109. self,
  110. vocab_size=262144,
  111. hidden_size=4096,
  112. intermediate_size=16384,
  113. num_hidden_layers=36,
  114. num_attention_heads=64,
  115. hidden_act="relu2",
  116. max_position_embeddings=16384,
  117. initializer_range=0.02,
  118. layer_norm_eps=1e-5,
  119. use_cache=True,
  120. tie_word_embeddings=False,
  121. rope_theta=25000.0,
  122. rope_scaling=None,
  123. qk_layernorm=True,
  124. hidden_dropout=0.0,
  125. attention_dropout=0.0,
  126. partial_rotary_factor=0.5,
  127. pad_token_id=None,
  128. bos_token_id=1,
  129. eos_token_id=2,
  130. **kwargs,
  131. ):
  132. self.vocab_size = vocab_size
  133. self.max_position_embeddings = max_position_embeddings
  134. self.hidden_size = hidden_size
  135. self.intermediate_size = intermediate_size
  136. self.num_hidden_layers = num_hidden_layers
  137. self.num_attention_heads = num_attention_heads
  138. self.hidden_act = hidden_act
  139. self.initializer_range = initializer_range
  140. self.layer_norm_eps = layer_norm_eps
  141. self.use_cache = use_cache
  142. self.rope_theta = rope_theta
  143. self.rope_scaling = rope_scaling
  144. self.qk_layernorm = qk_layernorm
  145. self.hidden_dropout = hidden_dropout
  146. self.attention_dropout = attention_dropout
  147. self.partial_rotary_factor = partial_rotary_factor
  148. # Validate the correctness of rotary position embeddings parameters
  149. # BC: if there is a 'type' field, move it to 'rope_type'.
  150. if self.rope_scaling is not None and "type" in self.rope_scaling:
  151. self.rope_scaling["rope_type"] = self.rope_scaling["type"]
  152. rope_config_validation(self)
  153. super().__init__(
  154. pad_token_id=pad_token_id,
  155. bos_token_id=bos_token_id,
  156. eos_token_id=eos_token_id,
  157. tie_word_embeddings=tie_word_embeddings,
  158. **kwargs,
  159. )
  160. __all__ = ["PersimmonConfig"]