configuration_convbert.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. # coding=utf-8
  2. # Copyright The HuggingFace 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. """ConvBERT model 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 ConvBertConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`ConvBertModel`]. It is used to instantiate an
  25. ConvBERT 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 ConvBERT
  27. [YituTech/conv-bert-base](https://huggingface.co/YituTech/conv-bert-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 ConvBERT model. Defines the number of different tokens that can be represented by
  33. the `inputs_ids` passed when calling [`ConvBertModel`] or [`TFConvBertModel`].
  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" (i.e., feed-forward) layer in the Transformer encoder.
  42. hidden_act (`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"`, `"selu"` 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 [`ConvBertModel`] or [`TFConvBertModel`].
  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. head_ratio (`int`, *optional*, defaults to 2):
  59. Ratio gamma to reduce the number of attention heads.
  60. num_groups (`int`, *optional*, defaults to 1):
  61. The number of groups for grouped linear layers for ConvBert model
  62. conv_kernel_size (`int`, *optional*, defaults to 9):
  63. The size of the convolutional kernel.
  64. classifier_dropout (`float`, *optional*):
  65. The dropout ratio for the classification head.
  66. Example:
  67. ```python
  68. >>> from transformers import ConvBertConfig, ConvBertModel
  69. >>> # Initializing a ConvBERT convbert-base-uncased style configuration
  70. >>> configuration = ConvBertConfig()
  71. >>> # Initializing a model (with random weights) from the convbert-base-uncased style configuration
  72. >>> model = ConvBertModel(configuration)
  73. >>> # Accessing the model configuration
  74. >>> configuration = model.config
  75. ```"""
  76. model_type = "convbert"
  77. def __init__(
  78. self,
  79. vocab_size=30522,
  80. hidden_size=768,
  81. num_hidden_layers=12,
  82. num_attention_heads=12,
  83. intermediate_size=3072,
  84. hidden_act="gelu",
  85. hidden_dropout_prob=0.1,
  86. attention_probs_dropout_prob=0.1,
  87. max_position_embeddings=512,
  88. type_vocab_size=2,
  89. initializer_range=0.02,
  90. layer_norm_eps=1e-12,
  91. pad_token_id=1,
  92. bos_token_id=0,
  93. eos_token_id=2,
  94. embedding_size=768,
  95. head_ratio=2,
  96. conv_kernel_size=9,
  97. num_groups=1,
  98. classifier_dropout=None,
  99. **kwargs,
  100. ):
  101. super().__init__(
  102. pad_token_id=pad_token_id,
  103. bos_token_id=bos_token_id,
  104. eos_token_id=eos_token_id,
  105. **kwargs,
  106. )
  107. self.vocab_size = vocab_size
  108. self.hidden_size = hidden_size
  109. self.num_hidden_layers = num_hidden_layers
  110. self.num_attention_heads = num_attention_heads
  111. self.intermediate_size = intermediate_size
  112. self.hidden_act = hidden_act
  113. self.hidden_dropout_prob = hidden_dropout_prob
  114. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  115. self.max_position_embeddings = max_position_embeddings
  116. self.type_vocab_size = type_vocab_size
  117. self.initializer_range = initializer_range
  118. self.layer_norm_eps = layer_norm_eps
  119. self.embedding_size = embedding_size
  120. self.head_ratio = head_ratio
  121. self.conv_kernel_size = conv_kernel_size
  122. self.num_groups = num_groups
  123. self.classifier_dropout = classifier_dropout
  124. # Copied from transformers.models.bert.configuration_bert.BertOnnxConfig
  125. class ConvBertOnnxConfig(OnnxConfig):
  126. @property
  127. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  128. if self.task == "multiple-choice":
  129. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  130. else:
  131. dynamic_axis = {0: "batch", 1: "sequence"}
  132. return OrderedDict(
  133. [
  134. ("input_ids", dynamic_axis),
  135. ("attention_mask", dynamic_axis),
  136. ("token_type_ids", dynamic_axis),
  137. ]
  138. )