configuration_levit.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # Copyright 2022 Meta Platforms, Inc. and The HuggingFace Inc. team. All rights reserved.
  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. """LeViT 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="facebook/levit-128S")
  19. @strict
  20. class LevitConfig(PreTrainedConfig):
  21. r"""
  22. stride (`int`, *optional*, defaults to 2):
  23. The stride size for the initial convolution layers of patch embedding.
  24. padding (`int`, *optional*, defaults to 1):
  25. The padding size for the initial convolution layers of patch embedding.
  26. key_dim (`list[int]`, *optional*, defaults to `[16, 16, 16]`):
  27. The size of key in each of the encoder blocks.
  28. attention_ratio (`list[int]`, *optional*, defaults to `[2, 2, 2]`):
  29. Ratio of the size of the output dimension compared to input dimension of attention layers.
  30. Example:
  31. ```python
  32. >>> from transformers import LevitConfig, LevitModel
  33. >>> # Initializing a LeViT levit-128S style configuration
  34. >>> configuration = LevitConfig()
  35. >>> # Initializing a model (with random weights) from the levit-128S style configuration
  36. >>> model = LevitModel(configuration)
  37. >>> # Accessing the model configuration
  38. >>> configuration = model.config
  39. ```"""
  40. model_type = "levit"
  41. image_size: int | list[int] | tuple[int, int] = 224
  42. num_channels: int = 3
  43. kernel_size: int = 3
  44. stride: int = 2
  45. padding: int = 1
  46. patch_size: int | list[int] | tuple[int, int] = 16
  47. hidden_sizes: list[int] | tuple[int, ...] = (128, 256, 384)
  48. num_attention_heads: list[int] | tuple[int, ...] = (4, 8, 12)
  49. depths: list[int] | tuple[int, ...] = (4, 4, 4)
  50. key_dim: list[int] | tuple[int, ...] = (16, 16, 16)
  51. drop_path_rate: int = 0
  52. mlp_ratio: list[int] | tuple[int, ...] = (2, 2, 2)
  53. attention_ratio: list[int] | tuple[int, ...] = (2, 2, 2)
  54. initializer_range: float = 0.02
  55. def __post_init__(self, **kwargs):
  56. self.down_ops = [
  57. ["Subsample", self.key_dim[0], self.hidden_sizes[0] // self.key_dim[0], 4, 2, 2],
  58. ["Subsample", self.key_dim[0], self.hidden_sizes[1] // self.key_dim[0], 4, 2, 2],
  59. ]
  60. super().__post_init__(**kwargs)
  61. __all__ = ["LevitConfig"]