configuration_albert.py 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # coding=utf-8
  2. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """ALBERT model configuration"""
  17. from collections import OrderedDict
  18. from typing import Mapping
  19. from ...configuration_utils import PretrainedConfig
  20. from ...onnx import OnnxConfig
  21. class AlbertConfig(PretrainedConfig):
  22. r"""
  23. This is the configuration class to store the configuration of a [`AlbertModel`] or a [`TFAlbertModel`]. It is used
  24. to instantiate an ALBERT model according to the specified arguments, defining the model architecture. Instantiating
  25. a configuration with the defaults will yield a similar configuration to that of the ALBERT
  26. [albert/albert-xxlarge-v2](https://huggingface.co/albert/albert-xxlarge-v2) architecture.
  27. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  28. documentation from [`PretrainedConfig`] for more information.
  29. Args:
  30. vocab_size (`int`, *optional*, defaults to 30000):
  31. Vocabulary size of the ALBERT model. Defines the number of different tokens that can be represented by the
  32. `inputs_ids` passed when calling [`AlbertModel`] or [`TFAlbertModel`].
  33. embedding_size (`int`, *optional*, defaults to 128):
  34. Dimensionality of vocabulary embeddings.
  35. hidden_size (`int`, *optional*, defaults to 4096):
  36. Dimensionality of the encoder layers and the pooler layer.
  37. num_hidden_layers (`int`, *optional*, defaults to 12):
  38. Number of hidden layers in the Transformer encoder.
  39. num_hidden_groups (`int`, *optional*, defaults to 1):
  40. Number of groups for the hidden layers, parameters in the same group are shared.
  41. num_attention_heads (`int`, *optional*, defaults to 64):
  42. Number of attention heads for each attention layer in the Transformer encoder.
  43. intermediate_size (`int`, *optional*, defaults to 16384):
  44. The dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  45. inner_group_num (`int`, *optional*, defaults to 1):
  46. The number of inner repetition of attention and ffn.
  47. hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu_new"`):
  48. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  49. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  50. hidden_dropout_prob (`float`, *optional*, defaults to 0):
  51. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  52. attention_probs_dropout_prob (`float`, *optional*, defaults to 0):
  53. The dropout ratio for the attention probabilities.
  54. max_position_embeddings (`int`, *optional*, defaults to 512):
  55. The maximum sequence length that this model might ever be used with. Typically set this to something large
  56. (e.g., 512 or 1024 or 2048).
  57. type_vocab_size (`int`, *optional*, defaults to 2):
  58. The vocabulary size of the `token_type_ids` passed when calling [`AlbertModel`] or [`TFAlbertModel`].
  59. initializer_range (`float`, *optional*, defaults to 0.02):
  60. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  61. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  62. The epsilon used by the layer normalization layers.
  63. classifier_dropout_prob (`float`, *optional*, defaults to 0.1):
  64. The dropout ratio for attached classifiers.
  65. position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
  66. Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
  67. positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
  68. [Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155).
  69. For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
  70. with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658).
  71. pad_token_id (`int`, *optional*, defaults to 0):
  72. Padding token id.
  73. bos_token_id (`int`, *optional*, defaults to 2):
  74. Beginning of stream token id.
  75. eos_token_id (`int`, *optional*, defaults to 3):
  76. End of stream token id.
  77. Examples:
  78. ```python
  79. >>> from transformers import AlbertConfig, AlbertModel
  80. >>> # Initializing an ALBERT-xxlarge style configuration
  81. >>> albert_xxlarge_configuration = AlbertConfig()
  82. >>> # Initializing an ALBERT-base style configuration
  83. >>> albert_base_configuration = AlbertConfig(
  84. ... hidden_size=768,
  85. ... num_attention_heads=12,
  86. ... intermediate_size=3072,
  87. ... )
  88. >>> # Initializing a model (with random weights) from the ALBERT-base style configuration
  89. >>> model = AlbertModel(albert_xxlarge_configuration)
  90. >>> # Accessing the model configuration
  91. >>> configuration = model.config
  92. ```"""
  93. model_type = "albert"
  94. def __init__(
  95. self,
  96. vocab_size=30000,
  97. embedding_size=128,
  98. hidden_size=4096,
  99. num_hidden_layers=12,
  100. num_hidden_groups=1,
  101. num_attention_heads=64,
  102. intermediate_size=16384,
  103. inner_group_num=1,
  104. hidden_act="gelu_new",
  105. hidden_dropout_prob=0,
  106. attention_probs_dropout_prob=0,
  107. max_position_embeddings=512,
  108. type_vocab_size=2,
  109. initializer_range=0.02,
  110. layer_norm_eps=1e-12,
  111. classifier_dropout_prob=0.1,
  112. position_embedding_type="absolute",
  113. pad_token_id=0,
  114. bos_token_id=2,
  115. eos_token_id=3,
  116. **kwargs,
  117. ):
  118. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  119. self.vocab_size = vocab_size
  120. self.embedding_size = embedding_size
  121. self.hidden_size = hidden_size
  122. self.num_hidden_layers = num_hidden_layers
  123. self.num_hidden_groups = num_hidden_groups
  124. self.num_attention_heads = num_attention_heads
  125. self.inner_group_num = inner_group_num
  126. self.hidden_act = hidden_act
  127. self.intermediate_size = intermediate_size
  128. self.hidden_dropout_prob = hidden_dropout_prob
  129. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  130. self.max_position_embeddings = max_position_embeddings
  131. self.type_vocab_size = type_vocab_size
  132. self.initializer_range = initializer_range
  133. self.layer_norm_eps = layer_norm_eps
  134. self.classifier_dropout_prob = classifier_dropout_prob
  135. self.position_embedding_type = position_embedding_type
  136. # Copied from transformers.models.bert.configuration_bert.BertOnnxConfig with Roberta->Albert
  137. class AlbertOnnxConfig(OnnxConfig):
  138. @property
  139. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  140. if self.task == "multiple-choice":
  141. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  142. else:
  143. dynamic_axis = {0: "batch", 1: "sequence"}
  144. return OrderedDict(
  145. [
  146. ("input_ids", dynamic_axis),
  147. ("attention_mask", dynamic_axis),
  148. ("token_type_ids", dynamic_axis),
  149. ]
  150. )
  151. __all__ = ["AlbertConfig", "AlbertOnnxConfig"]