configuration_xglm.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # coding=utf-8
  2. # Copyright 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. """XGLM model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class XGLMConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`XGLMModel`]. It is used to instantiate an XGLM
  22. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the XGLM
  24. [facebook/xglm-564M](https://huggingface.co/facebook/xglm-564M) 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 256008):
  29. Vocabulary size of the XGLM model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`XGLMModel`] or [`FlaxXGLMModel`].
  31. max_position_embeddings (`int`, *optional*, defaults to 2048):
  32. The maximum sequence length that this model might ever be used with. Typically set this to something large
  33. just in case (e.g., 512 or 1024 or 2048).
  34. d_model (`int`, *optional*, defaults to 1024):
  35. Dimension of the layers and the pooler layer.
  36. ffn_dim (`int`, *optional*, defaults to 4096):
  37. Dimension of the "intermediate" (often named feed-forward) layer in decoder.
  38. num_layers (`int`, *optional*, defaults to 24):
  39. Number of hidden layers Transformer decoder.
  40. attention_heads (`int`, *optional*, defaults to 16):
  41. Number of attention heads for each attention layer in the Transformer decoder.
  42. activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
  43. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  44. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  45. dropout (`float`, *optional*, defaults to 0.1):
  46. The dropout probability for all fully connected layers in the embeddings, dencoder, and pooler.
  47. attention_dropout (`float`, *optional*, defaults to 0.1):
  48. The dropout ratio for the attention probabilities.
  49. activation_dropout (`float`, *optional*, defaults to 0.0):
  50. The dropout ratio for activations inside the fully connected layer.
  51. layerdrop (`float`, *optional*, defaults to 0.0):
  52. The LayerDrop probability for the encoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
  53. for more details.
  54. init_std (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  56. scale_embedding (`bool`, *optional*, defaults to `True`):
  57. Scale embeddings by diving by sqrt(d_model).
  58. use_cache (`bool`, *optional*, defaults to `True`):
  59. Whether or not the model should return the last key/values attentions (not used by all models).
  60. Example:
  61. ```python
  62. >>> from transformers import XGLMModel, XGLMConfig
  63. >>> # Initializing a XGLM facebook/xglm-564M style configuration
  64. >>> configuration = XGLMConfig()
  65. >>> # Initializing a model from the facebook/xglm-564M style configuration
  66. >>> model = XGLMModel(configuration)
  67. >>> # Accessing the model configuration
  68. >>> configuration = model.config
  69. ```"""
  70. model_type = "xglm"
  71. keys_to_ignore_at_inference = ["past_key_values"]
  72. attribute_map = {
  73. "num_attention_heads": "attention_heads",
  74. "hidden_size": "d_model",
  75. "num_hidden_layers": "num_layers",
  76. }
  77. def __init__(
  78. self,
  79. vocab_size=256008,
  80. max_position_embeddings=2048,
  81. d_model=1024,
  82. ffn_dim=4096,
  83. num_layers=24,
  84. attention_heads=16,
  85. activation_function="gelu",
  86. dropout=0.1,
  87. attention_dropout=0.1,
  88. activation_dropout=0.0,
  89. layerdrop=0.0,
  90. init_std=0.02,
  91. scale_embedding=True,
  92. use_cache=True,
  93. decoder_start_token_id=2,
  94. pad_token_id=1,
  95. bos_token_id=0,
  96. eos_token_id=2,
  97. **kwargs,
  98. ):
  99. self.vocab_size = vocab_size
  100. self.max_position_embeddings = max_position_embeddings
  101. self.d_model = d_model
  102. self.ffn_dim = ffn_dim
  103. self.num_layers = num_layers
  104. self.attention_heads = attention_heads
  105. self.activation_function = activation_function
  106. self.dropout = dropout
  107. self.attention_dropout = attention_dropout
  108. self.activation_dropout = activation_dropout
  109. self.layerdrop = layerdrop
  110. self.init_std = init_std
  111. self.scale_embedding = scale_embedding # scale factor will be sqrt(d_model) if True
  112. self.use_cache = use_cache
  113. super().__init__(
  114. pad_token_id=pad_token_id,
  115. bos_token_id=bos_token_id,
  116. eos_token_id=eos_token_id,
  117. decoder_start_token_id=decoder_start_token_id,
  118. **kwargs,
  119. )
  120. __all__ = ["XGLMConfig"]