configuration_rembert.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # coding=utf-8
  2. # Copyright The HuggingFace Team 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. """RemBERT model configuration"""
  16. from collections import OrderedDict
  17. from collections.abc import Mapping
  18. from ...configuration_utils import PretrainedConfig
  19. from ...onnx import OnnxConfig
  20. from ...utils import logging
  21. logger = logging.get_logger(__name__)
  22. class RemBertConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`RemBertModel`]. It is used to instantiate an
  25. RemBERT model according to the specified arguments, defining the model architecture. Instantiating a configuration
  26. with the defaults will yield a similar configuration to that of the RemBERT
  27. [google/rembert](https://huggingface.co/google/rembert) architecture.
  28. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  29. documentation from [`PretrainedConfig`] for more information.
  30. Args:
  31. vocab_size (`int`, *optional*, defaults to 250300):
  32. Vocabulary size of the RemBERT model. Defines the number of different tokens that can be represented by the
  33. `inputs_ids` passed when calling [`RemBertModel`] or [`TFRemBertModel`]. Vocabulary size of the model.
  34. Defines the different tokens that can be represented by the *inputs_ids* passed to the forward method of
  35. [`RemBertModel`].
  36. hidden_size (`int`, *optional*, defaults to 1152):
  37. Dimensionality of the encoder layers and the pooler layer.
  38. num_hidden_layers (`int`, *optional*, defaults to 32):
  39. Number of hidden layers in the Transformer encoder.
  40. num_attention_heads (`int`, *optional*, defaults to 18):
  41. Number of attention heads for each attention layer in the Transformer encoder.
  42. input_embedding_size (`int`, *optional*, defaults to 256):
  43. Dimensionality of the input embeddings.
  44. output_embedding_size (`int`, *optional*, defaults to 1664):
  45. Dimensionality of the output embeddings.
  46. intermediate_size (`int`, *optional*, defaults to 4608):
  47. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  48. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  49. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  50. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  51. hidden_dropout_prob (`float`, *optional*, defaults to 0):
  52. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  53. attention_probs_dropout_prob (`float`, *optional*, defaults to 0):
  54. The dropout ratio for the attention probabilities.
  55. classifier_dropout_prob (`float`, *optional*, defaults to 0.1):
  56. The dropout ratio for the classifier layer when fine-tuning.
  57. max_position_embeddings (`int`, *optional*, defaults to 512):
  58. The maximum sequence length that this model might ever be used with. Typically set this to something large
  59. just in case (e.g., 512 or 1024 or 2048).
  60. type_vocab_size (`int`, *optional*, defaults to 2):
  61. The vocabulary size of the `token_type_ids` passed when calling [`RemBertModel`] or [`TFRemBertModel`].
  62. initializer_range (`float`, *optional*, defaults to 0.02):
  63. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  64. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  65. The epsilon used by the layer normalization layers.
  66. is_decoder (`bool`, *optional*, defaults to `False`):
  67. Whether the model is used as a decoder or not. If `False`, the model is used as an encoder.
  68. use_cache (`bool`, *optional*, defaults to `True`):
  69. Whether or not the model should return the last key/values attentions (not used by all models). Only
  70. relevant if `config.is_decoder=True`.
  71. Example:
  72. ```python
  73. >>> from transformers import RemBertModel, RemBertConfig
  74. >>> # Initializing a RemBERT rembert style configuration
  75. >>> configuration = RemBertConfig()
  76. >>> # Initializing a model from the rembert style configuration
  77. >>> model = RemBertModel(configuration)
  78. >>> # Accessing the model configuration
  79. >>> configuration = model.config
  80. ```"""
  81. model_type = "rembert"
  82. def __init__(
  83. self,
  84. vocab_size=250300,
  85. hidden_size=1152,
  86. num_hidden_layers=32,
  87. num_attention_heads=18,
  88. input_embedding_size=256,
  89. output_embedding_size=1664,
  90. intermediate_size=4608,
  91. hidden_act="gelu",
  92. hidden_dropout_prob=0.0,
  93. attention_probs_dropout_prob=0.0,
  94. classifier_dropout_prob=0.1,
  95. max_position_embeddings=512,
  96. type_vocab_size=2,
  97. initializer_range=0.02,
  98. layer_norm_eps=1e-12,
  99. use_cache=True,
  100. pad_token_id=0,
  101. bos_token_id=312,
  102. eos_token_id=313,
  103. **kwargs,
  104. ):
  105. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  106. self.vocab_size = vocab_size
  107. self.input_embedding_size = input_embedding_size
  108. self.output_embedding_size = output_embedding_size
  109. self.max_position_embeddings = max_position_embeddings
  110. self.hidden_size = hidden_size
  111. self.num_hidden_layers = num_hidden_layers
  112. self.num_attention_heads = num_attention_heads
  113. self.intermediate_size = intermediate_size
  114. self.hidden_act = hidden_act
  115. self.hidden_dropout_prob = hidden_dropout_prob
  116. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  117. self.classifier_dropout_prob = classifier_dropout_prob
  118. self.initializer_range = initializer_range
  119. self.type_vocab_size = type_vocab_size
  120. self.layer_norm_eps = layer_norm_eps
  121. self.use_cache = use_cache
  122. self.tie_word_embeddings = False
  123. class RemBertOnnxConfig(OnnxConfig):
  124. @property
  125. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  126. if self.task == "multiple-choice":
  127. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  128. else:
  129. dynamic_axis = {0: "batch", 1: "sequence"}
  130. return OrderedDict(
  131. [
  132. ("input_ids", dynamic_axis),
  133. ("attention_mask", dynamic_axis),
  134. ("token_type_ids", dynamic_axis),
  135. ]
  136. )
  137. @property
  138. def atol_for_validation(self) -> float:
  139. return 1e-4
  140. __all__ = ["RemBertConfig", "RemBertOnnxConfig"]