configuration_perceiver.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright Deepmind 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. """Perceiver 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="deepmind/language-perceiver")
  19. @strict
  20. class PerceiverConfig(PreTrainedConfig):
  21. r"""
  22. num_latents (`int`, *optional*, defaults to 256):
  23. The number of latents.
  24. d_latents (`int`, *optional*, defaults to 1280):
  25. Dimension of the latent embeddings.
  26. num_blocks (`int`, *optional*, defaults to 1):
  27. Number of blocks in the Transformer encoder.
  28. num_self_attends_per_block (`int`, *optional*, defaults to 26):
  29. The number of self-attention layers per block.
  30. num_self_attention_heads (`int`, *optional*, defaults to 8):
  31. Number of attention heads for each self-attention layer in the Transformer encoder.
  32. num_cross_attention_heads (`int`, *optional*, defaults to 8):
  33. Number of attention heads for each cross-attention layer in the Transformer encoder.
  34. qk_channels (`int`, *optional*):
  35. Dimension to project the queries + keys before applying attention in the cross-attention and self-attention
  36. layers of the encoder. Will default to preserving the dimension of the queries if not specified.
  37. v_channels (`int`, *optional*):
  38. Dimension to project the values before applying attention in the cross-attention and self-attention layers
  39. of the encoder. Will default to preserving the dimension of the queries if not specified.
  40. cross_attention_shape_for_attention (`str`, *optional*, defaults to `"kv"`):
  41. Dimension to use when downsampling the queries and keys in the cross-attention layer of the encoder.
  42. self_attention_widening_factor (`int`, *optional*, defaults to 1):
  43. Dimension of the feed-forward layer in the cross-attention layer of the Transformer encoder.
  44. cross_attention_widening_factor (`int`, *optional*, defaults to 1):
  45. Dimension of the feed-forward layer in the self-attention layers of the Transformer encoder.
  46. use_query_residual (`float`, *optional*, defaults to `True`):
  47. Whether to add a query residual in the cross-attention layer of the encoder.
  48. image_size (`int`, *optional*, defaults to 56):
  49. Size of the images after preprocessing, for [`PerceiverForImageClassificationLearned`].
  50. train_size (`list[int]`, *optional*, defaults to `[368, 496]`):
  51. Training size of the images for the optical flow model.
  52. num_frames (`int`, *optional*, defaults to 16):
  53. Number of video frames used for the multimodal autoencoding model.
  54. audio_samples_per_frame (`int`, *optional*, defaults to 1920):
  55. Number of audio samples per frame for the multimodal autoencoding model.
  56. samples_per_patch (`int`, *optional*, defaults to 16):
  57. Number of audio samples per patch when preprocessing the audio for the multimodal autoencoding model.
  58. output_shape (`list[int]`, *optional*, defaults to `[1, 16, 224, 224]`):
  59. Shape of the output (batch_size, num_frames, height, width) for the video decoder queries of the multimodal
  60. autoencoding model. This excludes the channel dimension.
  61. output_num_channels (`int`, *optional*, defaults to 512):
  62. Number of output channels for each modalitiy decoder.
  63. Example:
  64. ```python
  65. >>> from transformers import PerceiverModel, PerceiverConfig
  66. >>> # Initializing a Perceiver deepmind/language-perceiver style configuration
  67. >>> configuration = PerceiverConfig()
  68. >>> # Initializing a model from the deepmind/language-perceiver style configuration
  69. >>> model = PerceiverModel(configuration)
  70. >>> # Accessing the model configuration
  71. >>> configuration = model.config
  72. ```"""
  73. model_type = "perceiver"
  74. num_latents: int = 256
  75. d_latents: int = 1280
  76. d_model: int = 768
  77. num_blocks: int = 1
  78. num_self_attends_per_block: int = 26
  79. num_self_attention_heads: int = 8
  80. num_cross_attention_heads: int = 8
  81. qk_channels: int | None = None
  82. v_channels: int | None = None
  83. cross_attention_shape_for_attention: str = "kv"
  84. self_attention_widening_factor: int = 1
  85. cross_attention_widening_factor: int = 1
  86. hidden_act: str = "gelu"
  87. attention_probs_dropout_prob: float | int = 0.1
  88. initializer_range: float = 0.02
  89. layer_norm_eps: float = 1e-12
  90. use_query_residual: bool = True
  91. vocab_size: int = 262
  92. max_position_embeddings: int = 2048
  93. image_size: int | list[int] | tuple[int, int] = 56
  94. train_size: list[int] | tuple[int, ...] = (368, 496)
  95. num_frames: int = 16
  96. audio_samples_per_frame: int = 1920
  97. samples_per_patch: int = 16
  98. output_shape: list[int] | tuple[int, ...] = (1, 16, 224, 224)
  99. output_num_channels: int = 512
  100. _label_trainable_num_channels: int = 1024
  101. __all__ = ["PerceiverConfig"]