configuration.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright 2021-2022 The Alibaba DAMO Team Authors.
  2. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  3. # 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. """ PoNet model configuration, mainly copied from :class:`~transformers.BertConfig` """
  17. from transformers import PretrainedConfig
  18. from modelscope.utils import logger as logging
  19. logger = logging.get_logger()
  20. class PoNetConfig(PretrainedConfig):
  21. r"""
  22. This is the configuration class to store the configuration
  23. of a :class:`~modelscope.models.nlp.ponet.PoNetModel`.
  24. It is used to instantiate a PoNet model according to the specified arguments.
  25. Configuration objects inherit from :class:`~transformers.PretrainedConfig` and can be used to control the model
  26. outputs. Read the documentation from :class:`~transformers.PretrainedConfig` for more information.
  27. Args:
  28. vocab_size (:obj:`int`, `optional`, defaults to 30522):
  29. Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the
  30. :obj:`inputs_ids` passed.
  31. hidden_size (:obj:`int`, `optional`, defaults to 768):
  32. Dimensionality of the encoder layers and the pooler layer.
  33. num_hidden_layers (:obj:`int`, `optional`, defaults to 12):
  34. Number of hidden layers in the Transformer encoder.
  35. num_attention_heads (:obj:`int`, `optional`, defaults to 12):
  36. Number of attention heads for each attention layer in the Transformer encoder.
  37. intermediate_size (:obj:`int`, `optional`, defaults to 3072):
  38. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  39. hidden_act (:obj:`str` or :obj:`Callable`, `optional`, defaults to :obj:`"gelu"`):
  40. The non-linear activation function (function or string) in the encoder and pooler. If string,
  41. :obj:`"gelu"`, :obj:`"relu"`, :obj:`"silu"` and :obj:`"gelu_new"` are supported.
  42. hidden_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
  43. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  44. attention_probs_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
  45. The dropout ratio for the attention probabilities.
  46. max_position_embeddings (:obj:`int`, `optional`, defaults to 512):
  47. The maximum sequence length that this model might ever be used with. Typically set this to something large
  48. just in case (e.g., 512 or 1024 or 2048).
  49. type_vocab_size (:obj:`int`, `optional`, defaults to 2):
  50. The vocabulary size of the :obj:`token_type_ids` passed.
  51. initializer_range (:obj:`float`, `optional`, defaults to 0.02):
  52. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  53. layer_norm_eps (:obj:`float`, `optional`, defaults to 1e-12):
  54. The epsilon used by the layer normalization layers.
  55. position_embedding_type (:obj:`str`, `optional`, defaults to :obj:`"absolute"`):
  56. Type of position embedding. Choose one of :obj:`"absolute"`, :obj:`"relative_key"`,
  57. :obj:`"relative_key_query"`. For positional embeddings use :obj:`"absolute"`. For more information on
  58. :obj:`"relative_key"`, please refer to `Self-Attention with Relative Position Representations (Shaw et al.)
  59. <https://arxiv.org/abs/1803.02155>`__. For more information on :obj:`"relative_key_query"`, please refer to
  60. `Method 4` in `Improve Transformer Models with Better Relative Position Embeddings (Huang et al.)
  61. <https://arxiv.org/abs/2009.13658>`__.
  62. use_cache (:obj:`bool`, `optional`, defaults to :obj:`True`):
  63. Whether or not the model should return the last key/values attentions (not used by all models). Only
  64. relevant if ``config.is_decoder=True``.
  65. classifier_dropout (:obj:`float`, `optional`):
  66. The dropout ratio for the classification head.
  67. clsgsepg (:obj:`bool`, `optional`, defaults to :obj:`True`):
  68. Whether or not use a trick to make sure the segment and local information will not leak.
  69. """
  70. model_type = 'ponet'
  71. def __init__(self,
  72. vocab_size=30522,
  73. hidden_size=768,
  74. num_hidden_layers=12,
  75. num_attention_heads=12,
  76. intermediate_size=3072,
  77. hidden_act='gelu',
  78. hidden_dropout_prob=0.1,
  79. attention_probs_dropout_prob=0.1,
  80. max_position_embeddings=512,
  81. type_vocab_size=2,
  82. initializer_range=0.02,
  83. layer_norm_eps=1e-12,
  84. pad_token_id=0,
  85. position_embedding_type='absolute',
  86. use_cache=True,
  87. classifier_dropout=None,
  88. clsgsepg=True,
  89. **kwargs):
  90. super().__init__(pad_token_id=pad_token_id, **kwargs)
  91. self.vocab_size = vocab_size
  92. self.hidden_size = hidden_size
  93. self.num_hidden_layers = num_hidden_layers
  94. self.num_attention_heads = num_attention_heads
  95. self.hidden_act = hidden_act
  96. self.intermediate_size = intermediate_size
  97. self.hidden_dropout_prob = hidden_dropout_prob
  98. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  99. self.max_position_embeddings = max_position_embeddings
  100. self.type_vocab_size = type_vocab_size
  101. self.initializer_range = initializer_range
  102. self.layer_norm_eps = layer_norm_eps
  103. self.position_embedding_type = position_embedding_type
  104. self.use_cache = use_cache
  105. self.classifier_dropout = classifier_dropout
  106. self.clsgsepg = clsgsepg