configuration_lxmert.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. # Copyright 2018, Hao Tan, Mohit Bansal
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """LXMERT model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="unc-nlp/lxmert-base-uncased")
  19. @strict
  20. class LxmertConfig(PreTrainedConfig):
  21. r"""
  22. num_qa_labels (`int`, *optional*, defaults to 9500):
  23. This represents the total number of different question answering (QA) labels there are. If using more than
  24. one dataset with QA, the user will need to account for the total number of labels that all of the datasets
  25. have in total.
  26. num_object_labels (`int`, *optional*, defaults to 1600):
  27. This represents the total number of semantically unique objects that lxmert will be able to classify a
  28. pooled-object feature as belonging too.
  29. num_attr_labels (`int`, *optional*, defaults to 400):
  30. This represents the total number of semantically unique attributes that lxmert will be able to classify a
  31. pooled-object feature as possessing.
  32. l_layers (`int`, *optional*, defaults to 9):
  33. Number of hidden layers in the Transformer language encoder.
  34. x_layers (`int`, *optional*, defaults to 5):
  35. Number of hidden layers in the Transformer cross modality encoder.
  36. r_layers (`int`, *optional*, defaults to 5):
  37. Number of hidden layers in the Transformer visual encoder.
  38. visual_feat_dim (`int`, *optional*, defaults to 2048):
  39. This represents the last dimension of the pooled-object features used as input for the model, representing
  40. the size of each object feature itself.
  41. visual_pos_dim (`int`, *optional*, defaults to 4):
  42. This represents the number of spatial features that are mixed into the visual features. The default is set
  43. to 4 because most commonly this will represent the location of a bounding box. i.e., (x, y, width, height)
  44. visual_loss_normalizer (`float`, *optional*, defaults to 6.67):
  45. This represents the scaling factor in which each visual loss is multiplied by if during pretraining, one
  46. decided to train with multiple vision-based loss objectives.
  47. task_matched (`bool`, *optional*, defaults to `True`):
  48. This task is used for sentence-image matching. If the sentence correctly describes the image the label will
  49. be 1. If the sentence does not correctly describe the image, the label will be 0.
  50. task_mask_lm (`bool`, *optional*, defaults to `True`):
  51. Whether or not to add masked language modeling (as used in pretraining models such as BERT) to the loss
  52. objective.
  53. task_obj_predict (`bool`, *optional*, defaults to `True`):
  54. Whether or not to add object prediction, attribute prediction and feature regression to the loss objective.
  55. task_qa (`bool`, *optional*, defaults to `True`):
  56. Whether or not to add the question-answering loss to the objective
  57. visual_obj_loss (`bool`, *optional*, defaults to `True`):
  58. Whether or not to calculate the object-prediction loss objective
  59. visual_attr_loss (`bool`, *optional*, defaults to `True`):
  60. Whether or not to calculate the attribute-prediction loss objective
  61. visual_feat_loss (`bool`, *optional*, defaults to `True`):
  62. Whether or not to calculate the feature-regression loss objective
  63. """
  64. model_type = "lxmert"
  65. attribute_map = {}
  66. vocab_size: int = 30522
  67. hidden_size: int = 768
  68. num_attention_heads: int = 12
  69. num_qa_labels: int = 9500
  70. num_object_labels: int = 1600
  71. num_attr_labels: int = 400
  72. intermediate_size: int = 3072
  73. hidden_act: str = "gelu"
  74. hidden_dropout_prob: float | int = 0.1
  75. attention_probs_dropout_prob: float | int = 0.1
  76. max_position_embeddings: int = 512
  77. type_vocab_size: int = 2
  78. initializer_range: float = 0.02
  79. l_layers: int = 9
  80. x_layers: int = 5
  81. r_layers: int = 5
  82. visual_feat_dim: int = 2048
  83. visual_pos_dim: int = 4
  84. visual_loss_normalizer: float = 6.67
  85. task_matched: bool = True
  86. task_mask_lm: bool = True
  87. task_obj_predict: bool = True
  88. task_qa: bool = True
  89. visual_obj_loss: bool = True
  90. visual_attr_loss: bool = True
  91. visual_feat_loss: bool = True
  92. pad_token_id: int | None = None
  93. bos_token_id: int | None = None
  94. eos_token_id: int | list[int] | None = None
  95. tie_word_embeddings: bool = True
  96. def __post_init__(self, **kwargs):
  97. self.num_hidden_layers = {"vision": self.r_layers, "cross_encoder": self.x_layers, "language": self.l_layers}
  98. super().__post_init__(**kwargs)
  99. __all__ = ["LxmertConfig"]