configuration_xlm.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # Copyright 2019-present, Facebook, Inc and the HuggingFace Inc. team.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """XLM configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="FacebookAI/xlm-mlm-en-2048")
  19. @strict
  20. class XLMConfig(PreTrainedConfig):
  21. r"""
  22. gelu_activation (`bool`, *optional*, defaults to `True`):
  23. Whether or not to use *gelu* for the activations instead of *relu*.
  24. sinusoidal_embeddings (`bool`, *optional*, defaults to `False`):
  25. Whether or not to use sinusoidal positional embeddings instead of absolute positional embeddings.
  26. causal (`bool`, *optional*, defaults to `False`):
  27. Whether or not the model should behave in a causal manner. Causal models use a triangular attention mask in
  28. order to only attend to the left-side context instead if a bidirectional context.
  29. asm (`bool`, *optional*, defaults to `False`):
  30. Whether or not to use an adaptive log softmax projection layer instead of a linear layer for the prediction
  31. layer.
  32. n_langs (`int`, *optional*, defaults to 1):
  33. The number of languages the model handles. Set to 1 for monolingual models.
  34. use_lang_emb (`bool`, *optional*, defaults to `True`):
  35. Whether to use language embeddings. Some models use additional language embeddings, see [the multilingual
  36. models page](http://huggingface.co/transformers/multilingual.html#xlm-language-embeddings) for information
  37. on how to use them.
  38. embed_init_std (`float`, *optional*, defaults to 2048^-0.5):
  39. The standard deviation of the truncated_normal_initializer for initializing the embedding matrices.
  40. unk_index (`int`, *optional*, defaults to 3):
  41. The index of the unknown token in the vocabulary.
  42. mask_index (`int`, *optional*, defaults to 5):
  43. The index of the masking token in the vocabulary.
  44. is_encoder (`bool`, *optional*, defaults to `True`):
  45. Whether or not the initialized model should be a transformer encoder or decoder as seen in Vaswani et al.
  46. summary_type (`string`, *optional*, defaults to "first"):
  47. Argument used when doing sequence summary. Used in the sequence classification and multiple choice models.
  48. Has to be one of the following options:
  49. - `"last"`: Take the last token hidden state (like XLNet).
  50. - `"first"`: Take the first token hidden state (like BERT).
  51. - `"mean"`: Take the mean of all tokens hidden states.
  52. - `"cls_index"`: Supply a Tensor of classification token position (like GPT/GPT-2).
  53. - `"attn"`: Not implemented now, use multi-head attention.
  54. summary_use_proj (`bool`, *optional*, defaults to `True`):
  55. Argument used when doing sequence summary. Used in the sequence classification and multiple choice models.
  56. Whether or not to add a projection after the vector extraction.
  57. summary_activation (`str`, *optional*):
  58. Argument used when doing sequence summary. Used in the sequence classification and multiple choice models.
  59. Pass `"tanh"` for a tanh activation to the output, any other value will result in no activation.
  60. summary_proj_to_labels (`bool`, *optional*, defaults to `True`):
  61. Used in the sequence classification and multiple choice models.
  62. Whether the projection outputs should have `config.num_labels` or `config.hidden_size` classes.
  63. summary_first_dropout (`float`, *optional*, defaults to 0.1):
  64. Used in the sequence classification and multiple choice models.
  65. The dropout ratio to be used after the projection and activation.
  66. start_n_top (`int`, *optional*, defaults to 5):
  67. Used in the SQuAD evaluation script.
  68. end_n_top (`int`, *optional*, defaults to 5):
  69. Used in the SQuAD evaluation script.
  70. mask_token_id (`int`, *optional*, defaults to 0):
  71. Model agnostic parameter to identify masked tokens when generating text in an MLM context.
  72. lang_id (`int`, *optional*, defaults to 1):
  73. The ID of the language used by the model. This parameter is used when generating text in a given language.
  74. Examples:
  75. ```python
  76. >>> from transformers import XLMConfig, XLMModel
  77. >>> # Initializing a XLM configuration
  78. >>> configuration = XLMConfig()
  79. >>> # Initializing a model (with random weights) from the configuration
  80. >>> model = XLMModel(configuration)
  81. >>> # Accessing the model configuration
  82. >>> configuration = model.config
  83. ```"""
  84. model_type = "xlm"
  85. attribute_map = {
  86. "hidden_size": "emb_dim",
  87. "num_attention_heads": "n_heads",
  88. "num_hidden_layers": "n_layers",
  89. "n_words": "vocab_size", # For backward compatibility
  90. "bos_index": "bos_token_id",
  91. "eos_index": "eos_token_id",
  92. "pad_index": "pad_token_id",
  93. }
  94. vocab_size: int = 30145
  95. emb_dim: int = 2048
  96. n_layers: int = 12
  97. n_heads: int = 16
  98. dropout: float | int = 0.1
  99. attention_dropout: float | int = 0.1
  100. gelu_activation: bool = True
  101. sinusoidal_embeddings: bool = False
  102. causal: bool = False
  103. asm: bool = False
  104. n_langs: int = 1
  105. use_lang_emb: bool = True
  106. max_position_embeddings: int = 512
  107. embed_init_std: float = 2048**-0.5
  108. layer_norm_eps: float = 1e-12
  109. init_std: float = 0.02
  110. unk_index: int = 3
  111. mask_index: int = 5
  112. is_encoder: bool = True
  113. summary_type: str = "first"
  114. summary_use_proj: bool = True
  115. summary_activation: str | None = None
  116. summary_proj_to_labels: bool = True
  117. summary_first_dropout: float | int = 0.1
  118. start_n_top: int = 5
  119. end_n_top: int = 5
  120. mask_token_id: int | None = 0
  121. lang_id: int = 0
  122. pad_token_id: int | None = 2
  123. bos_token_id: int | None = 0
  124. eos_token_id: int | list[int] | None = 1
  125. tie_word_embeddings: bool = True
  126. __all__ = ["XLMConfig"]