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