configuration_vilt.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. # coding=utf-8
  2. # Copyright 2022 The HuggingFace Inc. team. All rights reserved.
  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. """VilT model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class ViltConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`ViLTModel`]. It is used to instantiate an ViLT
  22. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the ViLT
  24. [dandelin/vilt-b32-mlm](https://huggingface.co/dandelin/vilt-b32-mlm) 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 text part of the model. Defines the number of different tokens that can be
  30. represented by the `inputs_ids` passed when calling [`ViltModel`].
  31. type_vocab_size (`int`, *optional*, defaults to 2):
  32. The vocabulary size of the `token_type_ids` passed when calling [`ViltModel`]. This is used when encoding
  33. text.
  34. modality_type_vocab_size (`int`, *optional*, defaults to 2):
  35. The vocabulary size of the modalities passed when calling [`ViltModel`]. This is used after concatenating the
  36. embeddings of the text and image modalities.
  37. max_position_embeddings (`int`, *optional*, defaults to 40):
  38. The maximum sequence length that this model might ever be used with.
  39. hidden_size (`int`, *optional*, defaults to 768):
  40. Dimensionality of the encoder layers and the pooler layer.
  41. num_hidden_layers (`int`, *optional*, defaults to 12):
  42. Number of hidden layers in the Transformer encoder.
  43. num_attention_heads (`int`, *optional*, defaults to 12):
  44. Number of attention heads for each attention layer in the Transformer encoder.
  45. intermediate_size (`int`, *optional*, defaults to 3072):
  46. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  47. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  48. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  49. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  50. hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
  51. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  52. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
  53. The dropout ratio for the attention probabilities.
  54. initializer_range (`float`, *optional*, defaults to 0.02):
  55. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  56. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  57. The epsilon used by the layer normalization layers.
  58. image_size (`int`, *optional*, defaults to 384):
  59. The size (resolution) of each image.
  60. patch_size (`int`, *optional*, defaults to 32):
  61. The size (resolution) of each patch.
  62. num_channels (`int`, *optional*, defaults to 3):
  63. The number of input channels.
  64. qkv_bias (`bool`, *optional*, defaults to `True`):
  65. Whether to add a bias to the queries, keys and values.
  66. max_image_length (`int`, *optional*, defaults to -1):
  67. The maximum number of patches to take as input for the Transformer encoder. If set to a positive integer,
  68. the encoder will sample `max_image_length` patches at maximum. If set to -1, will not be taken into
  69. account.
  70. num_images (`int`, *optional*, defaults to -1):
  71. The number of images to use for natural language visual reasoning. If set to a positive integer, will be
  72. used by [`ViltForImagesAndTextClassification`] for defining the classifier head.
  73. Example:
  74. ```python
  75. >>> from transformers import ViLTModel, ViLTConfig
  76. >>> # Initializing a ViLT dandelin/vilt-b32-mlm style configuration
  77. >>> configuration = ViLTConfig()
  78. >>> # Initializing a model from the dandelin/vilt-b32-mlm style configuration
  79. >>> model = ViLTModel(configuration)
  80. >>> # Accessing the model configuration
  81. >>> configuration = model.config
  82. ```"""
  83. model_type = "vilt"
  84. def __init__(
  85. self,
  86. vocab_size=30522,
  87. type_vocab_size=2,
  88. modality_type_vocab_size=2,
  89. max_position_embeddings=40,
  90. hidden_size=768,
  91. num_hidden_layers=12,
  92. num_attention_heads=12,
  93. intermediate_size=3072,
  94. hidden_act="gelu",
  95. hidden_dropout_prob=0.0,
  96. attention_probs_dropout_prob=0.0,
  97. initializer_range=0.02,
  98. layer_norm_eps=1e-12,
  99. image_size=384,
  100. patch_size=32,
  101. num_channels=3,
  102. qkv_bias=True,
  103. max_image_length=-1,
  104. tie_word_embeddings=False,
  105. num_images=-1,
  106. **kwargs,
  107. ):
  108. super().__init__(tie_word_embeddings=tie_word_embeddings, **kwargs)
  109. self.vocab_size = vocab_size
  110. self.type_vocab_size = type_vocab_size
  111. self.modality_type_vocab_size = modality_type_vocab_size
  112. self.max_position_embeddings = max_position_embeddings
  113. self.hidden_size = hidden_size
  114. self.num_hidden_layers = num_hidden_layers
  115. self.num_attention_heads = num_attention_heads
  116. self.intermediate_size = intermediate_size
  117. self.hidden_act = hidden_act
  118. self.hidden_dropout_prob = hidden_dropout_prob
  119. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  120. self.initializer_range = initializer_range
  121. self.layer_norm_eps = layer_norm_eps
  122. self.image_size = image_size
  123. self.patch_size = patch_size
  124. self.num_channels = num_channels
  125. self.qkv_bias = qkv_bias
  126. self.max_image_length = max_image_length
  127. self.num_images = num_images
  128. __all__ = ["ViltConfig"]