configuration_led.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # coding=utf-8
  2. # Copyright 2021 Iz Beltagy, Matthew E. Peters, Arman Cohan 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. """LED model configuration"""
  16. from typing import List, Union
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class LEDConfig(PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration of a [`LEDModel`]. It is used to instantiate an LED
  23. model 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 LED
  25. [allenai/led-base-16384](https://huggingface.co/allenai/led-base-16384) 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 50265):
  30. Vocabulary size of the LED model. Defines the number of different tokens that can be represented by the
  31. `inputs_ids` passed when calling [`LEDModel`] or [`TFLEDModel`].
  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_encoder_position_embeddings (`int`, *optional*, defaults to 16384):
  58. The maximum sequence length that the encoder might ever be used with.
  59. max_decoder_position_embeddings (`int`, *optional*, defaults to 16384):
  60. The maximum sequence length that the decoder might ever be used with.
  61. init_std (`float`, *optional*, defaults to 0.02):
  62. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  63. encoder_layerdrop (`float`, *optional*, defaults to 0.0):
  64. The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
  65. for more details.
  66. decoder_layerdrop (`float`, *optional*, defaults to 0.0):
  67. The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556)
  68. for more details.
  69. use_cache (`bool`, *optional*, defaults to `True`):
  70. Whether or not the model should return the last key/values attentions (not used by all models)
  71. Example:
  72. ```python
  73. >>> from transformers import LEDModel, LEDConfig
  74. >>> # Initializing a LED allenai/led-base-16384 style configuration
  75. >>> configuration = LEDConfig()
  76. >>> # Initializing a model from the allenai/led-base-16384 style configuration
  77. >>> model = LEDModel(configuration)
  78. >>> # Accessing the model configuration
  79. >>> configuration = model.config
  80. ```"""
  81. model_type = "led"
  82. attribute_map = {
  83. "num_attention_heads": "encoder_attention_heads",
  84. "hidden_size": "d_model",
  85. "attention_probs_dropout_prob": "attention_dropout",
  86. "initializer_range": "init_std",
  87. }
  88. def __init__(
  89. self,
  90. vocab_size=50265,
  91. max_encoder_position_embeddings=16384,
  92. max_decoder_position_embeddings=1024,
  93. encoder_layers=12,
  94. encoder_ffn_dim=4096,
  95. encoder_attention_heads=16,
  96. decoder_layers=12,
  97. decoder_ffn_dim=4096,
  98. decoder_attention_heads=16,
  99. encoder_layerdrop=0.0,
  100. decoder_layerdrop=0.0,
  101. use_cache=True,
  102. is_encoder_decoder=True,
  103. activation_function="gelu",
  104. d_model=1024,
  105. dropout=0.1,
  106. attention_dropout=0.0,
  107. activation_dropout=0.0,
  108. init_std=0.02,
  109. decoder_start_token_id=2,
  110. classifier_dropout=0.0,
  111. pad_token_id=1,
  112. bos_token_id=0,
  113. eos_token_id=2,
  114. attention_window: Union[List[int], int] = 512,
  115. **kwargs,
  116. ):
  117. self.vocab_size = vocab_size
  118. self.max_encoder_position_embeddings = max_encoder_position_embeddings
  119. self.max_decoder_position_embeddings = max_decoder_position_embeddings
  120. self.d_model = d_model
  121. self.encoder_ffn_dim = encoder_ffn_dim
  122. self.encoder_layers = encoder_layers
  123. self.encoder_attention_heads = encoder_attention_heads
  124. self.decoder_ffn_dim = decoder_ffn_dim
  125. self.decoder_layers = decoder_layers
  126. self.decoder_attention_heads = decoder_attention_heads
  127. self.dropout = dropout
  128. self.attention_dropout = attention_dropout
  129. self.activation_dropout = activation_dropout
  130. self.activation_function = activation_function
  131. self.init_std = init_std
  132. self.encoder_layerdrop = encoder_layerdrop
  133. self.decoder_layerdrop = decoder_layerdrop
  134. self.classifier_dropout = classifier_dropout
  135. self.use_cache = use_cache
  136. self.num_hidden_layers = encoder_layers
  137. self.attention_window = attention_window
  138. super().__init__(
  139. pad_token_id=pad_token_id,
  140. bos_token_id=bos_token_id,
  141. eos_token_id=eos_token_id,
  142. is_encoder_decoder=is_encoder_decoder,
  143. decoder_start_token_id=decoder_start_token_id,
  144. **kwargs,
  145. )