configuration_vjepa2.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # coding=utf-8
  2. # Copyright 2025 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. """VJEPA 2 model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. class VJEPA2Config(PretrainedConfig):
  18. r"""
  19. This is the configuration class to store the configuration of a [`VJEPA2Model`]. It is used to instantiate an
  20. VJEPA2 model according to the specified arguments, defining the model architecture. Instantiating a configuration
  21. with the defaults will yield a similar configuration to that of the VJEPA2
  22. [facebook/vjepa2-vitl-fpc64-256](https://huggingface.co/facebook/vjepa2-vitl-fpc64-256) architecture.
  23. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  24. documentation from [`PretrainedConfig`] for more information.
  25. Args:
  26. patch_size (`int`, *optional*, defaults to 16):
  27. The size (resolution) of each patch.
  28. crop_size (`int`, *optional*, defaults to 256):
  29. Input resolution of the model
  30. frames_per_clip (`int`, *optional*, defaults to 64):
  31. The number of frames the model has been pretrained with. Does not impact inference.
  32. tubelet_size (`int`, *optional*, defaults to 2):
  33. The number of temporal frames used for a single rastor, check paper for more information.
  34. hidden_size (`int`, *optional*, defaults to 1024):
  35. Dimensionality of the encoder layers
  36. in_chans (`int`, *optional*, defaults to 3):
  37. The number of input channels
  38. num_attention_heads (`int`, *optional*, defaults to 16):
  39. Number of attention heads for each attention layer in the Encoder
  40. num_hidden_layers (`int`, *optional*, defaults to 24):
  41. The number of hidden layers
  42. drop_path_rate (`float`, *optional*, defaults to 0.0):
  43. Stochastic depth rate per sample (when applied in the main path of residual layers).
  44. mlp_ratio (`float`, *optional*, defaults to 4.0):
  45. Ratio of the hidden size of the MLPs used in Encoder relative to the `hidden_size`.
  46. layer_norm_eps (`float`, *optional*, defaults to 1e-06):
  47. The epsilon used by the layer normalization layers.
  48. qkv_bias (`bool`, *optional*, defaults to `True`):
  49. Whether to add a bias to the queries, keys and values.
  50. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.0):
  51. The dropout probability for attentions.
  52. The dropout probability for all fully connected layers.
  53. hidden_act (`str`, *optional*, defaults to `"gelu"`):
  54. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  55. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  56. initializer_range (`float`, *optional*, defaults to 0.02):
  57. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  58. attention_dropout (`float`, *optional*, defaults to 0.0):
  59. The dropout probability for attentions.
  60. num_pooler_layers (`int`, *optional*, defaults to 3):
  61. The number of self-attention layers in the pooler.
  62. pred_hidden_size (`int`, *optional*, defaults to 384):
  63. Dimensionality of the predictor layers
  64. pred_num_attention_heads (`int`, *optional*, defaults to 12):
  65. Number of attention heads for each attention layer in the Predictor
  66. pred_num_hidden_layers (`int`, *optional*, defaults to 12):
  67. Number of hidden layers in the Predictor
  68. pred_num_mask_tokens (`int`, *optional*, defaults to 10):
  69. Define the number of mask tokens to use in the Predictor
  70. pred_zero_init_mask_tokens (`bool`, *optional*, defaults to `True`):
  71. Initialize the mask tokens in the predictor with 0.
  72. pred_mlp_ratio (`float`, *optional*, defaults to 4.0):
  73. Ratio of the hidden size of the MLPs used in Predictor relative to the `pred_hidden_size`.
  74. Example:
  75. ```python
  76. >>> from transformers import VJEPA2Config, VJEPA2Model
  77. >>> # Initializing a VJEPA2 vjepa2-vitl-fpc64-256 style configuration
  78. >>> configuration = VJEPA2Config()
  79. >>> # Initializing a model (with random weights) from the vjepa2-vitl-fpc64-256 style configuration
  80. >>> model = VJEPA2Model(configuration)
  81. >>> # Accessing the model configuration
  82. >>> configuration = model.config
  83. ```"""
  84. model_type = "vjepa2"
  85. def __init__(
  86. self,
  87. patch_size=16,
  88. crop_size=256,
  89. frames_per_clip=64,
  90. tubelet_size=2,
  91. hidden_size=1024,
  92. in_chans=3,
  93. num_attention_heads=16,
  94. num_hidden_layers=24,
  95. drop_path_rate=0.0,
  96. mlp_ratio=4.0,
  97. layer_norm_eps=1e-6,
  98. qkv_bias=True,
  99. attention_probs_dropout_prob=0.0,
  100. hidden_act="gelu",
  101. initializer_range=0.02,
  102. attention_dropout=0.0,
  103. num_pooler_layers=3,
  104. # predictor params
  105. pred_hidden_size=384,
  106. pred_num_attention_heads=12,
  107. pred_num_hidden_layers=12,
  108. pred_num_mask_tokens=10,
  109. pred_zero_init_mask_tokens=True,
  110. pred_mlp_ratio=4.0,
  111. **kwargs,
  112. ):
  113. super().__init__(**kwargs)
  114. self.crop_size = crop_size
  115. self.frames_per_clip = frames_per_clip
  116. self.patch_size = patch_size
  117. self.tubelet_size = tubelet_size
  118. self.hidden_size = hidden_size
  119. self.in_chans = in_chans
  120. self.num_attention_heads = num_attention_heads
  121. self.num_hidden_layers = num_hidden_layers
  122. self.drop_path_rate = drop_path_rate
  123. self.mlp_ratio = mlp_ratio
  124. self.layer_norm_eps = layer_norm_eps
  125. self.qkv_bias = qkv_bias
  126. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  127. self.hidden_act = hidden_act
  128. self.initializer_range = initializer_range
  129. self.image_size = crop_size
  130. self.attention_dropout = attention_dropout
  131. self.num_pooler_layers = num_pooler_layers
  132. # predictor params
  133. self.pred_hidden_size = pred_hidden_size
  134. self.pred_num_attention_heads = pred_num_attention_heads
  135. self.pred_num_hidden_layers = pred_num_hidden_layers
  136. self.pred_num_mask_tokens = pred_num_mask_tokens
  137. self.pred_zero_init_mask_tokens = pred_zero_init_mask_tokens
  138. self.pred_mlp_ratio = pred_mlp_ratio
  139. __all__ = ["VJEPA2Config"]