configuration_pegasus.py 7.3 KB

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