configuration_prophetnet.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. # coding=utf-8
  2. # Copyright 2020 The Microsoft Authors and The HuggingFace Inc. team.
  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. """ProphetNet model configuration"""
  16. from typing import Callable, Optional, Union
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class ProphetNetConfig(PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`ProphetNetModel`]. It is used to instantiate a
  23. ProphetNet 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 ProphetNet
  25. [microsoft/prophetnet-large-uncased](https://huggingface.co/microsoft/prophetnet-large-uncased) 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. activation_dropout (`float`, *optional*, defaults to 0.1):
  30. The dropout ratio for activations inside the fully connected layer.
  31. activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
  32. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  33. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  34. vocab_size (`int`, *optional*, defaults to 30522):
  35. Vocabulary size of the ProphetNET model. Defines the number of different tokens that can be represented by
  36. the `inputs_ids` passed when calling [`ProphetNetModel`].
  37. hidden_size (`int`, *optional*, defaults to 1024):
  38. Dimensionality of the layers and the pooler layer.
  39. encoder_ffn_dim (`int`, *optional*, defaults to 4096):
  40. Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
  41. num_encoder_layers (`int`, *optional*, defaults to 12):
  42. Number of encoder layers.
  43. num_encoder_attention_heads (`int`, *optional*, defaults to 16):
  44. Number of attention heads for each attention layer in the Transformer encoder.
  45. decoder_ffn_dim (`int`, *optional*, defaults to 4096):
  46. Dimensionality of the `intermediate` (often named feed-forward) layer in decoder.
  47. num_decoder_layers (`int`, *optional*, defaults to 12):
  48. Number of decoder layers.
  49. num_decoder_attention_heads (`int`, *optional*, defaults to 16):
  50. Number of attention heads for each attention layer in the Transformer decoder.
  51. attention_dropout (`float`, *optional*, defaults to 0.1):
  52. The dropout ratio for the attention probabilities.
  53. dropout (`float`, *optional*, defaults to 0.1):
  54. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  55. max_position_embeddings (`int`, *optional*, defaults to 512):
  56. The maximum sequence length that this model might ever be used with. Typically set this to something large
  57. just in case (e.g., 512 or 1024 or 2048).
  58. init_std (`float`, *optional*, defaults to 0.02):
  59. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  60. add_cross_attention (`bool`, *optional*, defaults to `True`):
  61. Whether cross-attention layers should be added to the model.
  62. is_encoder_decoder (`bool`, *optional*, defaults to `True`):
  63. Whether this is an encoder/decoder model.
  64. pad_token_id (`int`, *optional*, defaults to 1)
  65. Padding token id.
  66. bos_token_id (`int`, *optional*, defaults to 0)
  67. Beginning of stream token id.
  68. eos_token_id (`int`, *optional*, defaults to 2)
  69. End of stream token id.
  70. ngram (`int`, *optional*, defaults to 2)
  71. Number of future tokens to predict. Set to 1 to be same as traditional Language model to predict next first
  72. token.
  73. num_buckets (`int`, *optional*, defaults to 32)
  74. The number of buckets to use for each attention layer. This is for relative position calculation. See the
  75. [T5 paper](see https://arxiv.org/abs/1910.10683) for more details.
  76. relative_max_distance (`int`, *optional*, defaults to 128)
  77. Relative distances greater than this number will be put into the last same bucket. This is for relative
  78. position calculation. See the [T5 paper](see https://arxiv.org/abs/1910.10683) for more details.
  79. disable_ngram_loss (`bool`, *optional*, defaults to `False`):
  80. Whether be trained predicting only the next first token.
  81. eps (`float`, *optional*, defaults to 0.0):
  82. Controls the `epsilon` parameter value for label smoothing in the loss calculation. If set to 0, no label
  83. smoothing is performed.
  84. use_cache (`bool`, *optional*, defaults to `True`):
  85. Whether or not the model should return the last key/values attentions (not used by all models).
  86. """
  87. model_type = "prophetnet"
  88. keys_to_ignore_at_inference = ["past_key_values"]
  89. attribute_map = {
  90. "num_attention_heads": "num_encoder_attention_heads",
  91. }
  92. def __init__(
  93. self,
  94. activation_dropout: Optional[float] = 0.1,
  95. activation_function: Optional[Union[str, Callable]] = "gelu",
  96. vocab_size: Optional[int] = 30522,
  97. hidden_size: Optional[int] = 1024,
  98. encoder_ffn_dim: Optional[int] = 4096,
  99. num_encoder_layers: Optional[int] = 12,
  100. num_encoder_attention_heads: Optional[int] = 16,
  101. decoder_ffn_dim: Optional[int] = 4096,
  102. num_decoder_layers: Optional[int] = 12,
  103. num_decoder_attention_heads: Optional[int] = 16,
  104. attention_dropout: Optional[float] = 0.1,
  105. dropout: Optional[float] = 0.1,
  106. max_position_embeddings: Optional[int] = 512,
  107. init_std: Optional[float] = 0.02,
  108. is_encoder_decoder: Optional[bool] = True,
  109. add_cross_attention: Optional[bool] = True,
  110. decoder_start_token_id: Optional[int] = 0,
  111. ngram: Optional[int] = 2,
  112. num_buckets: Optional[int] = 32,
  113. relative_max_distance: Optional[int] = 128,
  114. disable_ngram_loss: Optional[bool] = False,
  115. eps: Optional[float] = 0.0,
  116. use_cache: Optional[bool] = True,
  117. pad_token_id: Optional[int] = 0,
  118. bos_token_id: Optional[int] = 1,
  119. eos_token_id: Optional[int] = 2,
  120. **kwargs,
  121. ):
  122. self.vocab_size = vocab_size
  123. self.hidden_size = hidden_size
  124. self.encoder_ffn_dim = encoder_ffn_dim
  125. self.num_encoder_layers = num_encoder_layers
  126. self.num_encoder_attention_heads = num_encoder_attention_heads
  127. self.decoder_ffn_dim = decoder_ffn_dim
  128. self.num_decoder_layers = num_decoder_layers
  129. self.num_decoder_attention_heads = num_decoder_attention_heads
  130. self.max_position_embeddings = max_position_embeddings
  131. self.init_std = init_std # Normal(0, this parameter)
  132. self.activation_function = activation_function
  133. # parameters for prophetnet
  134. self.ngram = ngram
  135. self.num_buckets = num_buckets
  136. self.relative_max_distance = relative_max_distance
  137. self.disable_ngram_loss = disable_ngram_loss
  138. self.eps = eps
  139. # 3 Types of Dropout
  140. self.attention_dropout = attention_dropout
  141. self.activation_dropout = activation_dropout
  142. self.dropout = dropout
  143. self.use_cache = use_cache
  144. super().__init__(
  145. pad_token_id=pad_token_id,
  146. bos_token_id=bos_token_id,
  147. eos_token_id=eos_token_id,
  148. is_encoder_decoder=is_encoder_decoder,
  149. add_cross_attention=add_cross_attention,
  150. decoder_start_token_id=decoder_start_token_id,
  151. **kwargs,
  152. )
  153. @property
  154. def num_hidden_layers(self) -> int:
  155. return self.num_encoder_layers + self.num_decoder_layers
  156. @num_hidden_layers.setter
  157. def num_hidden_layers(self, value):
  158. raise NotImplementedError(
  159. "This model does not support the setting of `num_hidden_layers`. Please set `num_encoder_layers` and"
  160. " `num_decoder_layers`."
  161. )