configuration_distilbert.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # coding=utf-8
  2. # Copyright 2019-present, the HuggingFace Inc. team, The Google AI Language Team and Facebook, Inc.
  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. """DistilBERT 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 DistilBertConfig(PretrainedConfig):
  23. r"""
  24. This is the configuration class to store the configuration of a [`DistilBertModel`] or a [`TFDistilBertModel`]. It
  25. is used to instantiate a DistilBERT 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 DistilBERT
  27. [distilbert-base-uncased](https://huggingface.co/distilbert-base-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 DistilBERT model. Defines the number of different tokens that can be represented by
  33. the `inputs_ids` passed when calling [`DistilBertModel`] or [`TFDistilBertModel`].
  34. max_position_embeddings (`int`, *optional*, defaults to 512):
  35. The maximum sequence length that this model might ever be used with. Typically set this to something large
  36. just in case (e.g., 512 or 1024 or 2048).
  37. sinusoidal_pos_embds (`boolean`, *optional*, defaults to `False`):
  38. Whether to use sinusoidal positional embeddings.
  39. n_layers (`int`, *optional*, defaults to 6):
  40. Number of hidden layers in the Transformer encoder.
  41. n_heads (`int`, *optional*, defaults to 12):
  42. Number of attention heads for each attention layer in the Transformer encoder.
  43. dim (`int`, *optional*, defaults to 768):
  44. Dimensionality of the encoder layers and the pooler layer.
  45. hidden_dim (`int`, *optional*, defaults to 3072):
  46. The size of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  47. dropout (`float`, *optional*, defaults to 0.1):
  48. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  49. attention_dropout (`float`, *optional*, defaults to 0.1):
  50. The dropout ratio for the attention probabilities.
  51. activation (`str` or `Callable`, *optional*, defaults to `"gelu"`):
  52. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  53. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  54. initializer_range (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  56. qa_dropout (`float`, *optional*, defaults to 0.1):
  57. The dropout probabilities used in the question answering model [`DistilBertForQuestionAnswering`].
  58. seq_classif_dropout (`float`, *optional*, defaults to 0.2):
  59. The dropout probabilities used in the sequence classification and the multiple choice model
  60. [`DistilBertForSequenceClassification`].
  61. Examples:
  62. ```python
  63. >>> from transformers import DistilBertConfig, DistilBertModel
  64. >>> # Initializing a DistilBERT configuration
  65. >>> configuration = DistilBertConfig()
  66. >>> # Initializing a model (with random weights) from the configuration
  67. >>> model = DistilBertModel(configuration)
  68. >>> # Accessing the model configuration
  69. >>> configuration = model.config
  70. ```"""
  71. model_type = "distilbert"
  72. attribute_map = {
  73. "hidden_size": "dim",
  74. "num_attention_heads": "n_heads",
  75. "num_hidden_layers": "n_layers",
  76. }
  77. def __init__(
  78. self,
  79. vocab_size=30522,
  80. max_position_embeddings=512,
  81. sinusoidal_pos_embds=False,
  82. n_layers=6,
  83. n_heads=12,
  84. dim=768,
  85. hidden_dim=4 * 768,
  86. dropout=0.1,
  87. attention_dropout=0.1,
  88. activation="gelu",
  89. initializer_range=0.02,
  90. qa_dropout=0.1,
  91. seq_classif_dropout=0.2,
  92. pad_token_id=0,
  93. **kwargs,
  94. ):
  95. self.vocab_size = vocab_size
  96. self.max_position_embeddings = max_position_embeddings
  97. self.sinusoidal_pos_embds = sinusoidal_pos_embds
  98. self.n_layers = n_layers
  99. self.n_heads = n_heads
  100. self.dim = dim
  101. self.hidden_dim = hidden_dim
  102. self.dropout = dropout
  103. self.attention_dropout = attention_dropout
  104. self.activation = activation
  105. self.initializer_range = initializer_range
  106. self.qa_dropout = qa_dropout
  107. self.seq_classif_dropout = seq_classif_dropout
  108. super().__init__(**kwargs, pad_token_id=pad_token_id)
  109. class DistilBertOnnxConfig(OnnxConfig):
  110. @property
  111. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  112. if self.task == "multiple-choice":
  113. dynamic_axis = {0: "batch", 1: "choice", 2: "sequence"}
  114. else:
  115. dynamic_axis = {0: "batch", 1: "sequence"}
  116. return OrderedDict(
  117. [
  118. ("input_ids", dynamic_axis),
  119. ("attention_mask", dynamic_axis),
  120. ]
  121. )
  122. __all__ = ["DistilBertConfig", "DistilBertOnnxConfig"]