configuration_mpt.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. # Copyright 2023 HuggingFace Inc. team and MosaicML NLP team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Mpt configuration"""
  15. from typing import Literal
  16. from huggingface_hub.dataclasses import strict
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="mosaicml/mpt-7b")
  20. @strict
  21. class MptAttentionConfig(PreTrainedConfig):
  22. r"""
  23. attn_type (`str`, *optional*, defaults to `"multihead_attention"`):
  24. type of attention to use. Options: `"multihead_attention"`, `"multiquery_attention"`.
  25. attn_pdrop (`float`, *optional*, defaults to `0.0`):
  26. The dropout probability for the attention layers.
  27. attn_impl (`str`, *optional*, defaults to `"torch"`):
  28. The attention implementation to use. One of `"torch"`, `"flash"`, or `"triton"`.
  29. clip_qkv (`float`, *optional*):
  30. If not `None`, clip the queries, keys, and values in the attention layer to this value.
  31. softmax_scale (`float`, *optional*):
  32. If not `None`, scale the softmax in the attention layer by this value. If `None`, will default to
  33. `1/sqrt(hidden_size)`.
  34. prefix_lm (`bool`, *optional*, defaults to `False`):
  35. Whether the model should operate as a Prefix LM. This requires passing an extra `prefix_mask` argument
  36. which indicates which tokens belong to the prefix. Tokens in the prefix can attend to one another
  37. bi-directionally. Tokens outside the prefix use causal attention.
  38. qk_ln (`bool`, *optional*, defaults to `False`):
  39. Whether to apply layer normalization to the queries and keys in the attention layer.
  40. attn_uses_sequence_id (`bool`, *optional*, defaults to `False`):
  41. Whether to restrict attention to tokens that have the same token_type_ids. When the model is in `train`
  42. mode, this requires passing an extra *token_type_ids* argument which indicates which sub-sequence each
  43. token belongs to. Defaults to `False` meaning any provided *token_type_ids* will be ignored.
  44. alibi (`bool`, *optional*, defaults to `True`):
  45. Whether or not to use the alibi bias instead of positional embedding.
  46. alibi_bias_max (`int`, *optional*, defaults to 8):
  47. The maximum value of the alibi bias.
  48. """
  49. base_config_key = "attn_config"
  50. attn_type: Literal["multihead_attention", "multiquery_attention"] = "multihead_attention"
  51. attn_pdrop: int = 0
  52. attn_impl: str = "torch"
  53. clip_qkv: float | None = None
  54. softmax_scale: float | None = None
  55. prefix_lm: bool = False
  56. qk_ln: bool = False
  57. attn_uses_sequence_id: bool = False
  58. alibi: bool = True
  59. alibi_bias_max: int = 8
  60. @auto_docstring(checkpoint="mosaicml/mpt-7b")
  61. @strict
  62. class MptConfig(PreTrainedConfig):
  63. r"""
  64. expansion_ratio (`int`, *optional*, defaults to 4):
  65. The ratio of the up/down scale in the MLP.
  66. max_seq_len (`int`, *optional*, defaults to 2048):
  67. The maximum sequence length of the model.
  68. layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
  69. The epsilon to use in the layer normalization layers.
  70. learned_pos_emb (`bool`, *optional*, defaults to `True`):
  71. Whether to use learned positional embeddings.
  72. attn_config (`dict`, *optional*):
  73. A dictionary used to configure the model's attention module.
  74. init_device (`str`, *optional*, defaults to `"cpu"`):
  75. The device to use for parameter initialization. Defined for backward compatibility
  76. logit_scale (`float`, *optional*):
  77. If not None, scale the logits by this value.
  78. no_bias (`bool`, *optional*, defaults to `True`):
  79. Whether to use bias in all linear layers.
  80. embedding_fraction (`float`, *optional*, defaults to 1.0):
  81. The fraction to scale the gradients of the embedding layer by.
  82. norm_type (`str`, *optional*, defaults to `"low_precision_layernorm"`):
  83. Type of layer norm to use. All MPT models uses the same layer norm implementation. Defined for backward
  84. compatibility.
  85. Example:
  86. ```python
  87. >>> from transformers import MptConfig, MptModel
  88. >>> # Initializing a Mpt configuration
  89. >>> configuration = MptConfig()
  90. >>> # Initializing a model (with random weights) from the configuration
  91. >>> model = MptModel(configuration)
  92. >>> # Accessing the model configuration
  93. >>> configuration = model.config
  94. ```
  95. """
  96. model_type = "mpt"
  97. sub_configs = {"attn_config": MptAttentionConfig}
  98. attribute_map = {
  99. "num_attention_heads": "n_heads",
  100. "hidden_size": "d_model",
  101. "num_hidden_layers": "n_layers",
  102. }
  103. d_model: int = 2048
  104. n_heads: int = 16
  105. n_layers: int = 24
  106. expansion_ratio: int = 4
  107. max_seq_len: int = 2048
  108. vocab_size: int = 50368
  109. resid_pdrop: float | int = 0.0
  110. layer_norm_epsilon: float = 1e-5
  111. emb_pdrop: float | int = 0.0
  112. learned_pos_emb: bool = True
  113. attn_config: dict | MptAttentionConfig | None = None
  114. init_device: str = "cpu"
  115. logit_scale: float | str | None = None
  116. no_bias: bool = True
  117. embedding_fraction: float = 1.0
  118. norm_type: str = "low_precision_layernorm"
  119. use_cache: bool = False
  120. initializer_range: float = 0.02
  121. tie_word_embeddings: bool = True
  122. pad_token_id: int | None = None
  123. bos_token_id: int | None = None
  124. eos_token_id: int | list[int] | None = None
  125. def __post_init__(self, **kwargs):
  126. if self.attn_config is None:
  127. self.attn_config = MptAttentionConfig()
  128. elif isinstance(self.attn_config, dict):
  129. self.attn_config = MptAttentionConfig(**self.attn_config)
  130. super().__post_init__(**kwargs)
  131. __all__ = ["MptConfig"]