configuration_markuplm.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # coding=utf-8
  2. # Copyright 2021, The Microsoft Research Asia MarkupLM Team authors
  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. """MarkupLM model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class MarkupLMConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`MarkupLMModel`]. It is used to instantiate a
  22. MarkupLM model according to the specified arguments, defining the model architecture. Instantiating a configuration
  23. with the defaults will yield a similar configuration to that of the MarkupLM
  24. [microsoft/markuplm-base](https://huggingface.co/microsoft/markuplm-base) architecture.
  25. Configuration objects inherit from [`BertConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`BertConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 30522):
  29. Vocabulary size of the MarkupLM model. Defines the different tokens that can be represented by the
  30. *inputs_ids* passed to the forward method of [`MarkupLMModel`].
  31. hidden_size (`int`, *optional*, defaults to 768):
  32. Dimensionality of the encoder layers and the pooler layer.
  33. num_hidden_layers (`int`, *optional*, defaults to 12):
  34. Number of hidden layers in the Transformer encoder.
  35. num_attention_heads (`int`, *optional*, defaults to 12):
  36. Number of attention heads for each attention layer in the Transformer encoder.
  37. intermediate_size (`int`, *optional*, defaults to 3072):
  38. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  39. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  40. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  41. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  42. hidden_dropout_prob (`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 (`float`, *optional*, defaults to 0.1):
  45. The dropout ratio for the attention probabilities.
  46. max_position_embeddings (`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 (`int`, *optional*, defaults to 2):
  50. The vocabulary size of the `token_type_ids` passed into [`MarkupLMModel`].
  51. initializer_range (`float`, *optional*, defaults to 0.02):
  52. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  53. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  54. The epsilon used by the layer normalization layers.
  55. max_tree_id_unit_embeddings (`int`, *optional*, defaults to 1024):
  56. The maximum value that the tree id unit embedding might ever use. Typically set this to something large
  57. just in case (e.g., 1024).
  58. max_xpath_tag_unit_embeddings (`int`, *optional*, defaults to 256):
  59. The maximum value that the xpath tag unit embedding might ever use. Typically set this to something large
  60. just in case (e.g., 256).
  61. max_xpath_subs_unit_embeddings (`int`, *optional*, defaults to 1024):
  62. The maximum value that the xpath subscript unit embedding might ever use. Typically set this to something
  63. large just in case (e.g., 1024).
  64. tag_pad_id (`int`, *optional*, defaults to 216):
  65. The id of the padding token in the xpath tags.
  66. subs_pad_id (`int`, *optional*, defaults to 1001):
  67. The id of the padding token in the xpath subscripts.
  68. xpath_tag_unit_hidden_size (`int`, *optional*, defaults to 32):
  69. The hidden size of each tree id unit. One complete tree index will have
  70. (50*xpath_tag_unit_hidden_size)-dim.
  71. max_depth (`int`, *optional*, defaults to 50):
  72. The maximum depth in xpath.
  73. Examples:
  74. ```python
  75. >>> from transformers import MarkupLMModel, MarkupLMConfig
  76. >>> # Initializing a MarkupLM microsoft/markuplm-base style configuration
  77. >>> configuration = MarkupLMConfig()
  78. >>> # Initializing a model from the microsoft/markuplm-base style configuration
  79. >>> model = MarkupLMModel(configuration)
  80. >>> # Accessing the model configuration
  81. >>> configuration = model.config
  82. ```"""
  83. model_type = "markuplm"
  84. def __init__(
  85. self,
  86. vocab_size=30522,
  87. hidden_size=768,
  88. num_hidden_layers=12,
  89. num_attention_heads=12,
  90. intermediate_size=3072,
  91. hidden_act="gelu",
  92. hidden_dropout_prob=0.1,
  93. attention_probs_dropout_prob=0.1,
  94. max_position_embeddings=512,
  95. type_vocab_size=2,
  96. initializer_range=0.02,
  97. layer_norm_eps=1e-12,
  98. pad_token_id=0,
  99. bos_token_id=0,
  100. eos_token_id=2,
  101. max_xpath_tag_unit_embeddings=256,
  102. max_xpath_subs_unit_embeddings=1024,
  103. tag_pad_id=216,
  104. subs_pad_id=1001,
  105. xpath_unit_hidden_size=32,
  106. max_depth=50,
  107. use_cache=True,
  108. classifier_dropout=None,
  109. **kwargs,
  110. ):
  111. super().__init__(
  112. pad_token_id=pad_token_id,
  113. bos_token_id=bos_token_id,
  114. eos_token_id=eos_token_id,
  115. **kwargs,
  116. )
  117. self.vocab_size = vocab_size
  118. self.hidden_size = hidden_size
  119. self.num_hidden_layers = num_hidden_layers
  120. self.num_attention_heads = num_attention_heads
  121. self.hidden_act = hidden_act
  122. self.intermediate_size = intermediate_size
  123. self.hidden_dropout_prob = hidden_dropout_prob
  124. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  125. self.max_position_embeddings = max_position_embeddings
  126. self.type_vocab_size = type_vocab_size
  127. self.initializer_range = initializer_range
  128. self.layer_norm_eps = layer_norm_eps
  129. self.use_cache = use_cache
  130. self.classifier_dropout = classifier_dropout
  131. # additional properties
  132. self.max_depth = max_depth
  133. self.max_xpath_tag_unit_embeddings = max_xpath_tag_unit_embeddings
  134. self.max_xpath_subs_unit_embeddings = max_xpath_subs_unit_embeddings
  135. self.tag_pad_id = tag_pad_id
  136. self.subs_pad_id = subs_pad_id
  137. self.xpath_unit_hidden_size = xpath_unit_hidden_size
  138. __all__ = ["MarkupLMConfig"]