configuration_bros.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. # coding=utf-8
  2. # Copyright 2023-present NAVER Corp, The Microsoft Research Asia LayoutLM Team Authors 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. """Bros model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class BrosConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`BrosModel`] or a [`TFBrosModel`]. It is used to
  22. instantiate a Bros 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 Bros
  24. [jinho8345/bros-base-uncased](https://huggingface.co/jinho8345/bros-base-uncased) 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. Args:
  28. vocab_size (`int`, *optional*, defaults to 30522):
  29. Vocabulary size of the Bros model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`BrosModel`] or [`TFBrosModel`].
  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" (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"` 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 when calling [`BrosModel`] or [`TFBrosModel`].
  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. pad_token_id (`int`, *optional*, defaults to 0):
  56. The index of the padding token in the token vocabulary.
  57. dim_bbox (`int`, *optional*, defaults to 8):
  58. The dimension of the bounding box coordinates. (x0, y1, x1, y0, x1, y1, x0, y1)
  59. bbox_scale (`float`, *optional*, defaults to 100.0):
  60. The scale factor of the bounding box coordinates.
  61. n_relations (`int`, *optional*, defaults to 1):
  62. The number of relations for SpadeEE(entity extraction), SpadeEL(entity linking) head.
  63. classifier_dropout_prob (`float`, *optional*, defaults to 0.1):
  64. The dropout ratio for the classifier head.
  65. Examples:
  66. ```python
  67. >>> from transformers import BrosConfig, BrosModel
  68. >>> # Initializing a BROS jinho8345/bros-base-uncased style configuration
  69. >>> configuration = BrosConfig()
  70. >>> # Initializing a model from the jinho8345/bros-base-uncased style configuration
  71. >>> model = BrosModel(configuration)
  72. >>> # Accessing the model configuration
  73. >>> configuration = model.config
  74. ```"""
  75. model_type = "bros"
  76. def __init__(
  77. self,
  78. vocab_size=30522,
  79. hidden_size=768,
  80. num_hidden_layers=12,
  81. num_attention_heads=12,
  82. intermediate_size=3072,
  83. hidden_act="gelu",
  84. hidden_dropout_prob=0.1,
  85. attention_probs_dropout_prob=0.1,
  86. max_position_embeddings=512,
  87. type_vocab_size=2,
  88. initializer_range=0.02,
  89. layer_norm_eps=1e-12,
  90. pad_token_id=0,
  91. dim_bbox=8,
  92. bbox_scale=100.0,
  93. n_relations=1,
  94. classifier_dropout_prob=0.1,
  95. **kwargs,
  96. ):
  97. super().__init__(
  98. vocab_size=vocab_size,
  99. hidden_size=hidden_size,
  100. num_hidden_layers=num_hidden_layers,
  101. num_attention_heads=num_attention_heads,
  102. intermediate_size=intermediate_size,
  103. hidden_act=hidden_act,
  104. hidden_dropout_prob=hidden_dropout_prob,
  105. attention_probs_dropout_prob=attention_probs_dropout_prob,
  106. max_position_embeddings=max_position_embeddings,
  107. type_vocab_size=type_vocab_size,
  108. initializer_range=initializer_range,
  109. layer_norm_eps=layer_norm_eps,
  110. pad_token_id=pad_token_id,
  111. **kwargs,
  112. )
  113. self.dim_bbox = dim_bbox
  114. self.bbox_scale = bbox_scale
  115. self.n_relations = n_relations
  116. self.dim_bbox_sinusoid_emb_2d = self.hidden_size // 4
  117. self.dim_bbox_sinusoid_emb_1d = self.dim_bbox_sinusoid_emb_2d // self.dim_bbox
  118. self.dim_bbox_projection = self.hidden_size // self.num_attention_heads
  119. self.classifier_dropout_prob = classifier_dropout_prob
  120. __all__ = ["BrosConfig"]