configuration_mvp.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # coding=utf-8
  2. # Copyright 2022 The Fairseq Authors 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. """MVP model configuration"""
  16. import warnings
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class MvpConfig(PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`MvpModel`]. It is used to instantiate a MVP model
  23. according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  24. defaults will yield a similar configuration to that of the MVP [RUCAIBox/mvp](https://huggingface.co/RUCAIBox/mvp)
  25. architecture.
  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 50267):
  30. Vocabulary size of the MVP model. Defines the number of different tokens that can be represented by the
  31. `inputs_ids` passed when calling [`MvpModel`].
  32. d_model (`int`, *optional*, defaults to 1024):
  33. Dimensionality of the layers and the pooler layer.
  34. encoder_layers (`int`, *optional*, defaults to 12):
  35. Number of encoder layers.
  36. decoder_layers (`int`, *optional*, defaults to 12):
  37. Number of decoder layers.
  38. encoder_attention_heads (`int`, *optional*, defaults to 16):
  39. Number of attention heads for each attention layer in the Transformer encoder.
  40. decoder_attention_heads (`int`, *optional*, defaults to 16):
  41. Number of attention heads for each attention layer in the Transformer decoder.
  42. decoder_ffn_dim (`int`, *optional*, defaults to 4096):
  43. Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
  44. encoder_ffn_dim (`int`, *optional*, defaults to 4096):
  45. Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
  46. activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
  47. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  48. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  49. dropout (`float`, *optional*, defaults to 0.1):
  50. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  51. attention_dropout (`float`, *optional*, defaults to 0.0):
  52. The dropout ratio for the attention probabilities.
  53. activation_dropout (`float`, *optional*, defaults to 0.0):
  54. The dropout ratio for activations inside the fully connected layer.
  55. classifier_dropout (`float`, *optional*, defaults to 0.0):
  56. The dropout ratio for classifier.
  57. max_position_embeddings (`int`, *optional*, defaults to 1024):
  58. The maximum sequence length that this model might ever be used with. Typically set this to something large
  59. just in case (e.g., 512 or 1024 or 2048).
  60. init_std (`float`, *optional*, defaults to 0.02):
  61. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  62. encoder_layerdrop (`float`, *optional*, defaults to 0.0):
  63. The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
  64. for more details.
  65. decoder_layerdrop (`float`, *optional*, defaults to 0.0):
  66. The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
  67. for more details.
  68. scale_embedding (`bool`, *optional*, defaults to `False`):
  69. Scale embeddings by diving by sqrt(d_model).
  70. use_cache (`bool`, *optional*, defaults to `True`):
  71. Whether or not the model should return the last key/values attentions (not used by all models).
  72. forced_eos_token_id (`int`, *optional*, defaults to 2):
  73. The id of the token to force as the last generated token when `max_length` is reached. Usually set to
  74. `eos_token_id`.
  75. use_prompt (`bool`, *optional*, defaults to `False`):
  76. Whether or not to use prompt.
  77. prompt_length (`int`, *optional*, defaults to 100):
  78. The length of prompt.
  79. prompt_mid_dim (`int`, *optional*, defaults to 800):
  80. Dimensionality of the "intermediate" layer in prompt.
  81. Example:
  82. ```python
  83. >>> from transformers import MvpConfig, MvpModel
  84. >>> # Initializing a MVP RUCAIBox/mvp style configuration
  85. >>> configuration = MvpConfig()
  86. >>> # Initializing a model (with random weights) from the RUCAIBox/mvp style configuration
  87. >>> model = MvpModel(configuration)
  88. >>> # Accessing the model configuration
  89. >>> configuration = model.config
  90. ```"""
  91. model_type = "mvp"
  92. keys_to_ignore_at_inference = ["past_key_values"]
  93. attribute_map = {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}
  94. def __init__(
  95. self,
  96. vocab_size=50267,
  97. max_position_embeddings=1024,
  98. encoder_layers=12,
  99. encoder_ffn_dim=4096,
  100. encoder_attention_heads=16,
  101. decoder_layers=12,
  102. decoder_ffn_dim=4096,
  103. decoder_attention_heads=16,
  104. encoder_layerdrop=0.0,
  105. decoder_layerdrop=0.0,
  106. activation_function="gelu",
  107. d_model=1024,
  108. dropout=0.1,
  109. attention_dropout=0.0,
  110. activation_dropout=0.0,
  111. init_std=0.02,
  112. classifier_dropout=0.0,
  113. scale_embedding=False,
  114. use_cache=True,
  115. pad_token_id=1,
  116. bos_token_id=0,
  117. eos_token_id=2,
  118. is_encoder_decoder=True,
  119. decoder_start_token_id=2,
  120. forced_eos_token_id=2,
  121. use_prompt=False,
  122. prompt_length=100,
  123. prompt_mid_dim=800,
  124. **kwargs,
  125. ):
  126. self.vocab_size = vocab_size
  127. self.max_position_embeddings = max_position_embeddings
  128. self.d_model = d_model
  129. self.encoder_ffn_dim = encoder_ffn_dim
  130. self.encoder_layers = encoder_layers
  131. self.encoder_attention_heads = encoder_attention_heads
  132. self.decoder_ffn_dim = decoder_ffn_dim
  133. self.decoder_layers = decoder_layers
  134. self.decoder_attention_heads = decoder_attention_heads
  135. self.dropout = dropout
  136. self.attention_dropout = attention_dropout
  137. self.activation_dropout = activation_dropout
  138. self.activation_function = activation_function
  139. self.init_std = init_std
  140. self.encoder_layerdrop = encoder_layerdrop
  141. self.decoder_layerdrop = decoder_layerdrop
  142. self.classifier_dropout = classifier_dropout
  143. self.use_cache = use_cache
  144. self.num_hidden_layers = encoder_layers
  145. self.scale_embedding = scale_embedding # scale factor will be sqrt(d_model) if True
  146. self.use_prompt = use_prompt
  147. self.prompt_length = prompt_length
  148. self.prompt_mid_dim = prompt_mid_dim
  149. super().__init__(
  150. pad_token_id=pad_token_id,
  151. bos_token_id=bos_token_id,
  152. eos_token_id=eos_token_id,
  153. is_encoder_decoder=is_encoder_decoder,
  154. decoder_start_token_id=decoder_start_token_id,
  155. forced_eos_token_id=forced_eos_token_id,
  156. **kwargs,
  157. )
  158. if self.forced_bos_token_id is None and kwargs.get("force_bos_token_to_be_generated", False):
  159. self.forced_bos_token_id = self.bos_token_id
  160. warnings.warn(
  161. f"Please make sure the config includes `forced_bos_token_id={self.bos_token_id}` in future versions. "
  162. "The config can simply be saved and uploaded again to be fixed."
  163. )
  164. __all__ = ["MvpConfig"]