configuration.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2021-2022 The Alibaba DAMO NLP Team Authors.
  2. # Copyright 2020, Microsoft 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. """ DeBERTa-v2 model configuration, mainly copied from :class:`~transformers.DeBERTaV2Config"""
  16. from transformers import PretrainedConfig
  17. from modelscope.utils import logger as logging
  18. logger = logging.get_logger()
  19. class DebertaV2Config(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`DebertaV2Model`]. It is used to instantiate a
  22. DeBERTa-v2 model according to the specified arguments, defining the model architecture. Instantiating a
  23. configuration with the defaults will yield a similar configuration to that of the DeBERTa
  24. [microsoft/deberta-v2-xlarge](https://huggingface.co/microsoft/deberta-v2-xlarge) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Arguments:
  28. vocab_size (`int`, *optional*, defaults to 128100):
  29. Vocabulary size of the DeBERTa-v2 model. Defines the number of different tokens that can be represented by
  30. the `inputs_ids` passed when calling [`DebertaV2Model`].
  31. hidden_size (`int`, *optional*, defaults to 1536):
  32. Dimensionality of the encoder layers and the pooler layer.
  33. num_hidden_layers (`int`, *optional*, defaults to 24):
  34. Number of hidden layers in the Transformer encoder.
  35. num_attention_heads (`int`, *optional*, defaults to 24):
  36. Number of attention heads for each attention layer in the Transformer encoder.
  37. intermediate_size (`int`, *optional*, defaults to 6144):
  38. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  39. hidden_act (`str` or `Callable`, *optional*, defaults to `"gelu"`):
  40. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  41. `"relu"`, `"silu"`, `"gelu"`, `"tanh"`, `"gelu_fast"`, `"mish"`, `"linear"`, `"sigmoid"` and `"gelu_new"`
  42. are supported.
  43. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  44. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  45. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  46. The dropout ratio for the attention probabilities.
  47. max_position_embeddings (`int`, *optional*, defaults to 512):
  48. The maximum sequence length that this model might ever be used with. Typically set this to something large
  49. just in case (e.g., 512 or 1024 or 2048).
  50. type_vocab_size (`int`, *optional*, defaults to 0):
  51. The vocabulary size of the `token_type_ids` passed when calling [`DebertaModel`] or [`TFDebertaModel`].
  52. initializer_range (`float`, *optional*, defaults to 0.02):
  53. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  54. layer_norm_eps (`float`, *optional*, defaults to 1e-7):
  55. The epsilon used by the layer normalization layers.
  56. relative_attention (`bool`, *optional*, defaults to `True`):
  57. Whether use relative position encoding.
  58. max_relative_positions (`int`, *optional*, defaults to -1):
  59. The range of relative positions `[-max_position_embeddings, max_position_embeddings]`. Use the same value
  60. as `max_position_embeddings`.
  61. pad_token_id (`int`, *optional*, defaults to 0):
  62. The value used to pad input_ids.
  63. position_biased_input (`bool`, *optional*, defaults to `False`):
  64. Whether add absolute position embedding to content embedding.
  65. pos_att_type (`List[str]`, *optional*):
  66. The type of relative position attention, it can be a combination of `["p2c", "c2p"]`, e.g. `["p2c"]`,
  67. `["p2c", "c2p"]`, `["p2c", "c2p"]`.
  68. layer_norm_eps (`float`, optional, defaults to 1e-12):
  69. The epsilon used by the layer normalization layers.
  70. """
  71. model_type = 'deberta_v2'
  72. def __init__(self,
  73. vocab_size=128100,
  74. hidden_size=1536,
  75. num_hidden_layers=24,
  76. num_attention_heads=24,
  77. intermediate_size=6144,
  78. hidden_act='gelu',
  79. hidden_dropout_prob=0.1,
  80. attention_probs_dropout_prob=0.1,
  81. max_position_embeddings=512,
  82. type_vocab_size=0,
  83. initializer_range=0.02,
  84. layer_norm_eps=1e-7,
  85. relative_attention=False,
  86. max_relative_positions=-1,
  87. pad_token_id=0,
  88. position_biased_input=True,
  89. pos_att_type=None,
  90. pooler_dropout=0,
  91. pooler_hidden_act='gelu',
  92. **kwargs):
  93. super().__init__(**kwargs)
  94. self.hidden_size = hidden_size
  95. self.num_hidden_layers = num_hidden_layers
  96. self.num_attention_heads = num_attention_heads
  97. self.intermediate_size = intermediate_size
  98. self.hidden_act = hidden_act
  99. self.hidden_dropout_prob = hidden_dropout_prob
  100. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  101. self.max_position_embeddings = max_position_embeddings
  102. self.type_vocab_size = type_vocab_size
  103. self.initializer_range = initializer_range
  104. self.relative_attention = relative_attention
  105. self.max_relative_positions = max_relative_positions
  106. self.pad_token_id = pad_token_id
  107. self.position_biased_input = position_biased_input
  108. # Backwards compatibility
  109. if type(pos_att_type) == str:
  110. pos_att_type = [x.strip() for x in pos_att_type.lower().split('|')]
  111. self.pos_att_type = pos_att_type
  112. self.vocab_size = vocab_size
  113. self.layer_norm_eps = layer_norm_eps
  114. self.pooler_hidden_size = kwargs.get('pooler_hidden_size', hidden_size)
  115. self.pooler_dropout = pooler_dropout
  116. self.pooler_hidden_act = pooler_hidden_act