configuration_data2vec_text.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # coding=utf-8
  2. # Copyright 2022 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. """Data2VecText configuration"""
  16. from collections import OrderedDict
  17. from typing 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 Data2VecTextConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`Data2VecTextModel`] and [`Data2VecTextModel`]. It
  25. is used to instantiate a Data2VecText model according to the specified arguments, defining the model architecture.
  26. Instantiating a configuration with the defaults will yield a similar configuration to that of the Data2VecText
  27. [facebook/data2vec-text-base](https://huggingface.co/facebook/data2vec-text-base) 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 30522):
  32. Vocabulary size of the DATA2VEC model. Defines the number of different tokens that can be represented by
  33. the `inputs_ids` passed when calling [`Data2VecModel`].
  34. hidden_size (`int`, *optional*, defaults to 768):
  35. Dimensionality of the encoder layers and the pooler layer.
  36. num_hidden_layers (`int`, *optional*, defaults to 12):
  37. Number of hidden layers in the Transformer encoder.
  38. num_attention_heads (`int`, *optional*, defaults to 12):
  39. Number of attention heads for each attention layer in the Transformer encoder.
  40. intermediate_size (`int`, *optional*, defaults to 3072):
  41. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  42. hidden_act (`str` or `Callable`, *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. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  46. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  47. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  48. The dropout ratio for the attention probabilities.
  49. max_position_embeddings (`int`, *optional*, defaults to 512):
  50. The maximum sequence length that this model might ever be used with. Typically set this to something large
  51. just in case (e.g., 512 or 1024 or 2048).
  52. type_vocab_size (`int`, *optional*, defaults to 2):
  53. The vocabulary size of the `token_type_ids` passed when calling [`Data2VecModel`].
  54. initializer_range (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  56. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  57. The epsilon used by the layer normalization layers.
  58. position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
  59. Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
  60. positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
  61. [Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155).
  62. For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
  63. with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658).
  64. is_decoder (`bool`, *optional*, defaults to `False`):
  65. Whether the model is used as a decoder or not. If `False`, the model is used as an encoder.
  66. use_cache (`bool`, *optional*, defaults to `True`):
  67. Whether or not the model should return the last key/values attentions (not used by all models). Only
  68. relevant if `config.is_decoder=True`.
  69. classifier_dropout (`float`, *optional*):
  70. The dropout ratio for the classification head.
  71. Examples:
  72. ```python
  73. >>> from transformers import Data2VecTextConfig, Data2VecTextModel
  74. >>> # Initializing a Data2VecText facebook/data2vec-text-base style configuration
  75. >>> configuration = Data2VecTextConfig()
  76. >>> # Initializing a model (with random weights) from the facebook/data2vec-text-base style configuration
  77. >>> model = Data2VecTextModel(configuration)
  78. >>> # Accessing the model configuration
  79. >>> configuration = model.config
  80. ```"""
  81. model_type = "data2vec-text"
  82. def __init__(
  83. self,
  84. vocab_size=30522,
  85. hidden_size=768,
  86. num_hidden_layers=12,
  87. num_attention_heads=12,
  88. intermediate_size=3072,
  89. hidden_act="gelu",
  90. hidden_dropout_prob=0.1,
  91. attention_probs_dropout_prob=0.1,
  92. max_position_embeddings=512,
  93. type_vocab_size=2,
  94. initializer_range=0.02,
  95. layer_norm_eps=1e-12,
  96. pad_token_id=1,
  97. bos_token_id=0,
  98. eos_token_id=2,
  99. position_embedding_type="absolute",
  100. use_cache=True,
  101. classifier_dropout=None,
  102. **kwargs,
  103. ):
  104. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  105. self.vocab_size = vocab_size
  106. self.hidden_size = hidden_size
  107. self.num_hidden_layers = num_hidden_layers
  108. self.num_attention_heads = num_attention_heads
  109. self.hidden_act = hidden_act
  110. self.intermediate_size = intermediate_size
  111. self.hidden_dropout_prob = hidden_dropout_prob
  112. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  113. self.max_position_embeddings = max_position_embeddings
  114. self.type_vocab_size = type_vocab_size
  115. self.initializer_range = initializer_range
  116. self.layer_norm_eps = layer_norm_eps
  117. self.position_embedding_type = position_embedding_type
  118. self.use_cache = use_cache
  119. self.classifier_dropout = classifier_dropout
  120. class Data2VecTextOnnxConfig(OnnxConfig):
  121. @property
  122. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  123. if self.task == "multiple-choice":
  124. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  125. else:
  126. dynamic_axis = {0: "batch", 1: "sequence"}
  127. return OrderedDict(
  128. [
  129. ("input_ids", dynamic_axis),
  130. ("attention_mask", dynamic_axis),
  131. ]
  132. )