configuration_squeezebert.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # coding=utf-8
  2. # Copyright 2020 The SqueezeBert authors and The HuggingFace Inc. team.
  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. """SqueezeBERT 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 SqueezeBertConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`SqueezeBertModel`]. It is used to instantiate a
  25. SqueezeBERT model according to the specified arguments, defining the model architecture. Instantiating a
  26. configuration with the defaults will yield a similar configuration to that of the SqueezeBERT
  27. [squeezebert/squeezebert-uncased](https://huggingface.co/squeezebert/squeezebert-uncased) 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 SqueezeBERT model. Defines the number of different tokens that can be represented by
  33. the `inputs_ids` passed when calling [`SqueezeBertModel`].
  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 [`BertModel`] or [`TFBertModel`].
  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. pad_token_id (`int`, *optional*, defaults to 0):
  58. The ID of the token in the word embedding to use as padding.
  59. embedding_size (`int`, *optional*, defaults to 768):
  60. The dimension of the word embedding vectors.
  61. q_groups (`int`, *optional*, defaults to 4):
  62. The number of groups in Q layer.
  63. k_groups (`int`, *optional*, defaults to 4):
  64. The number of groups in K layer.
  65. v_groups (`int`, *optional*, defaults to 4):
  66. The number of groups in V layer.
  67. post_attention_groups (`int`, *optional*, defaults to 1):
  68. The number of groups in the first feed forward network layer.
  69. intermediate_groups (`int`, *optional*, defaults to 4):
  70. The number of groups in the second feed forward network layer.
  71. output_groups (`int`, *optional*, defaults to 4):
  72. The number of groups in the third feed forward network layer.
  73. Examples:
  74. ```python
  75. >>> from transformers import SqueezeBertConfig, SqueezeBertModel
  76. >>> # Initializing a SqueezeBERT configuration
  77. >>> configuration = SqueezeBertConfig()
  78. >>> # Initializing a model (with random weights) from the configuration above
  79. >>> model = SqueezeBertModel(configuration)
  80. >>> # Accessing the model configuration
  81. >>> configuration = model.config
  82. ```
  83. """
  84. model_type = "squeezebert"
  85. def __init__(
  86. self,
  87. vocab_size=30522,
  88. hidden_size=768,
  89. num_hidden_layers=12,
  90. num_attention_heads=12,
  91. intermediate_size=3072,
  92. hidden_act="gelu",
  93. hidden_dropout_prob=0.1,
  94. attention_probs_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. pad_token_id=0,
  100. embedding_size=768,
  101. q_groups=4,
  102. k_groups=4,
  103. v_groups=4,
  104. post_attention_groups=1,
  105. intermediate_groups=4,
  106. output_groups=4,
  107. **kwargs,
  108. ):
  109. super().__init__(pad_token_id=pad_token_id, **kwargs)
  110. self.vocab_size = vocab_size
  111. self.hidden_size = hidden_size
  112. self.num_hidden_layers = num_hidden_layers
  113. self.num_attention_heads = num_attention_heads
  114. self.hidden_act = hidden_act
  115. self.intermediate_size = intermediate_size
  116. self.hidden_dropout_prob = hidden_dropout_prob
  117. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  118. self.max_position_embeddings = max_position_embeddings
  119. self.type_vocab_size = type_vocab_size
  120. self.initializer_range = initializer_range
  121. self.layer_norm_eps = layer_norm_eps
  122. self.embedding_size = embedding_size
  123. self.q_groups = q_groups
  124. self.k_groups = k_groups
  125. self.v_groups = v_groups
  126. self.post_attention_groups = post_attention_groups
  127. self.intermediate_groups = intermediate_groups
  128. self.output_groups = output_groups
  129. # # Copied from transformers.models.bert.configuration_bert.BertOnxxConfig with Bert->SqueezeBert
  130. class SqueezeBertOnnxConfig(OnnxConfig):
  131. @property
  132. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  133. if self.task == "multiple-choice":
  134. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  135. else:
  136. dynamic_axis = {0: "batch", 1: "sequence"}
  137. return OrderedDict(
  138. [
  139. ("input_ids", dynamic_axis),
  140. ("attention_mask", dynamic_axis),
  141. ("token_type_ids", dynamic_axis),
  142. ]
  143. )