configuration_dpr.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # coding=utf-8
  2. # Copyright 2010, DPR authors, The Hugging Face 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. """DPR model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class DPRConfig(PretrainedConfig):
  20. r"""
  21. [`DPRConfig`] is the configuration class to store the configuration of a *DPRModel*.
  22. This is the configuration class to store the configuration of a [`DPRContextEncoder`], [`DPRQuestionEncoder`], or a
  23. [`DPRReader`]. It is used to instantiate the components of the DPR model according to the specified arguments,
  24. defining the model component architectures. Instantiating a configuration with the defaults will yield a similar
  25. configuration to that of the DPRContextEncoder
  26. [facebook/dpr-ctx_encoder-single-nq-base](https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base)
  27. architecture.
  28. This class is a subclass of [`BertConfig`]. Please check the superclass for the documentation of all kwargs.
  29. Args:
  30. vocab_size (`int`, *optional*, defaults to 30522):
  31. Vocabulary size of the DPR model. Defines the different tokens that can be represented by the *inputs_ids*
  32. passed to the forward method of [`BertModel`].
  33. hidden_size (`int`, *optional*, defaults to 768):
  34. Dimensionality of the encoder layers and the pooler layer.
  35. num_hidden_layers (`int`, *optional*, defaults to 12):
  36. Number of hidden layers in the Transformer encoder.
  37. num_attention_heads (`int`, *optional*, defaults to 12):
  38. Number of attention heads for each attention layer in the Transformer encoder.
  39. intermediate_size (`int`, *optional*, defaults to 3072):
  40. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  41. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  42. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  43. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  44. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  45. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  46. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  47. The dropout ratio for the attention probabilities.
  48. max_position_embeddings (`int`, *optional*, defaults to 512):
  49. The maximum sequence length that this model might ever be used with. Typically set this to something large
  50. just in case (e.g., 512 or 1024 or 2048).
  51. type_vocab_size (`int`, *optional*, defaults to 2):
  52. The vocabulary size of the *token_type_ids* passed into [`BertModel`].
  53. initializer_range (`float`, *optional*, defaults to 0.02):
  54. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  55. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  56. The epsilon used by the layer normalization layers.
  57. pad_token_id (`int`, *optional*, defaults to 0):
  58. Padding token id.
  59. position_embedding_type (`str`, *optional*, defaults to `"absolute"`):
  60. Type of position embedding. Choose one of `"absolute"`, `"relative_key"`, `"relative_key_query"`. For
  61. positional embeddings use `"absolute"`. For more information on `"relative_key"`, please refer to
  62. [Self-Attention with Relative Position Representations (Shaw et al.)](https://arxiv.org/abs/1803.02155).
  63. For more information on `"relative_key_query"`, please refer to *Method 4* in [Improve Transformer Models
  64. with Better Relative Position Embeddings (Huang et al.)](https://arxiv.org/abs/2009.13658).
  65. projection_dim (`int`, *optional*, defaults to 0):
  66. Dimension of the projection for the context and question encoders. If it is set to zero (default), then no
  67. projection is done.
  68. Example:
  69. ```python
  70. >>> from transformers import DPRConfig, DPRContextEncoder
  71. >>> # Initializing a DPR facebook/dpr-ctx_encoder-single-nq-base style configuration
  72. >>> configuration = DPRConfig()
  73. >>> # Initializing a model (with random weights) from the facebook/dpr-ctx_encoder-single-nq-base style configuration
  74. >>> model = DPRContextEncoder(configuration)
  75. >>> # Accessing the model configuration
  76. >>> configuration = model.config
  77. ```"""
  78. model_type = "dpr"
  79. def __init__(
  80. self,
  81. vocab_size=30522,
  82. hidden_size=768,
  83. num_hidden_layers=12,
  84. num_attention_heads=12,
  85. intermediate_size=3072,
  86. hidden_act="gelu",
  87. hidden_dropout_prob=0.1,
  88. attention_probs_dropout_prob=0.1,
  89. max_position_embeddings=512,
  90. type_vocab_size=2,
  91. initializer_range=0.02,
  92. layer_norm_eps=1e-12,
  93. pad_token_id=0,
  94. position_embedding_type="absolute",
  95. projection_dim: int = 0,
  96. **kwargs,
  97. ):
  98. super().__init__(pad_token_id=pad_token_id, **kwargs)
  99. self.vocab_size = vocab_size
  100. self.hidden_size = hidden_size
  101. self.num_hidden_layers = num_hidden_layers
  102. self.num_attention_heads = num_attention_heads
  103. self.hidden_act = hidden_act
  104. self.intermediate_size = intermediate_size
  105. self.hidden_dropout_prob = hidden_dropout_prob
  106. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  107. self.max_position_embeddings = max_position_embeddings
  108. self.type_vocab_size = type_vocab_size
  109. self.initializer_range = initializer_range
  110. self.layer_norm_eps = layer_norm_eps
  111. self.projection_dim = projection_dim
  112. self.position_embedding_type = position_embedding_type