configuration_deit.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. # coding=utf-8
  2. # Copyright 2021 Facebook AI Research (FAIR) and 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. """DeiT model configuration"""
  16. from collections import OrderedDict
  17. from typing import Mapping
  18. from packaging import version
  19. from ...configuration_utils import PretrainedConfig
  20. from ...onnx import OnnxConfig
  21. from ...utils import logging
  22. logger = logging.get_logger(__name__)
  23. class DeiTConfig(PretrainedConfig):
  24. r"""
  25. This is the configuration class to store the configuration of a [`DeiTModel`]. It is used to instantiate an DeiT
  26. model according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  27. defaults will yield a similar configuration to that of the DeiT
  28. [facebook/deit-base-distilled-patch16-224](https://huggingface.co/facebook/deit-base-distilled-patch16-224)
  29. architecture.
  30. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  31. documentation from [`PretrainedConfig`] for more information.
  32. Args:
  33. hidden_size (`int`, *optional*, defaults to 768):
  34. Dimensionality of the encoder layers and the pooler layer.
  35. num_hidden_layers (`int`, *optional*, defaults to 12):
  36. Number of hidden layers in the Transformer encoder.
  37. num_attention_heads (`int`, *optional*, defaults to 12):
  38. Number of attention heads for each attention layer in the Transformer encoder.
  39. intermediate_size (`int`, *optional*, defaults to 3072):
  40. Dimensionality of the "intermediate" (i.e., feed-forward) layer in the Transformer encoder.
  41. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  42. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  43. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  44. hidden_dropout_prob (`float`, *optional*, defaults to 0.0):
  45. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  46. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
  47. The dropout ratio for the attention probabilities.
  48. initializer_range (`float`, *optional*, defaults to 0.02):
  49. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  50. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  51. The epsilon used by the layer normalization layers.
  52. image_size (`int`, *optional*, defaults to 224):
  53. The size (resolution) of each image.
  54. patch_size (`int`, *optional*, defaults to 16):
  55. The size (resolution) of each patch.
  56. num_channels (`int`, *optional*, defaults to 3):
  57. The number of input channels.
  58. qkv_bias (`bool`, *optional*, defaults to `True`):
  59. Whether to add a bias to the queries, keys and values.
  60. encoder_stride (`int`, *optional*, defaults to 16):
  61. Factor to increase the spatial resolution by in the decoder head for masked image modeling.
  62. Example:
  63. ```python
  64. >>> from transformers import DeiTConfig, DeiTModel
  65. >>> # Initializing a DeiT deit-base-distilled-patch16-224 style configuration
  66. >>> configuration = DeiTConfig()
  67. >>> # Initializing a model (with random weights) from the deit-base-distilled-patch16-224 style configuration
  68. >>> model = DeiTModel(configuration)
  69. >>> # Accessing the model configuration
  70. >>> configuration = model.config
  71. ```"""
  72. model_type = "deit"
  73. def __init__(
  74. self,
  75. hidden_size=768,
  76. num_hidden_layers=12,
  77. num_attention_heads=12,
  78. intermediate_size=3072,
  79. hidden_act="gelu",
  80. hidden_dropout_prob=0.0,
  81. attention_probs_dropout_prob=0.0,
  82. initializer_range=0.02,
  83. layer_norm_eps=1e-12,
  84. image_size=224,
  85. patch_size=16,
  86. num_channels=3,
  87. qkv_bias=True,
  88. encoder_stride=16,
  89. **kwargs,
  90. ):
  91. super().__init__(**kwargs)
  92. self.hidden_size = hidden_size
  93. self.num_hidden_layers = num_hidden_layers
  94. self.num_attention_heads = num_attention_heads
  95. self.intermediate_size = intermediate_size
  96. self.hidden_act = hidden_act
  97. self.hidden_dropout_prob = hidden_dropout_prob
  98. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  99. self.initializer_range = initializer_range
  100. self.layer_norm_eps = layer_norm_eps
  101. self.image_size = image_size
  102. self.patch_size = patch_size
  103. self.num_channels = num_channels
  104. self.qkv_bias = qkv_bias
  105. self.encoder_stride = encoder_stride
  106. class DeiTOnnxConfig(OnnxConfig):
  107. torch_onnx_minimum_version = version.parse("1.11")
  108. @property
  109. def inputs(self) -> Mapping[str, Mapping[int, str]]:
  110. return OrderedDict(
  111. [
  112. ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
  113. ]
  114. )
  115. @property
  116. def atol_for_validation(self) -> float:
  117. return 1e-4