configuration_dinat.py 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # Copyright 2022 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. """Dilated Neighborhood Attention Transformer model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...backbone_utils import BackboneConfigMixin
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="shi-labs/dinat-mini-in1k-224")
  20. @strict
  21. class DinatConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. dilations (`list[list[int]]`, *optional*, defaults to `[[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]`):
  24. Dilation value of each NA layer in the Transformer encoder.
  25. Example:
  26. ```python
  27. >>> from transformers import DinatConfig, DinatModel
  28. >>> # Initializing a Dinat shi-labs/dinat-mini-in1k-224 style configuration
  29. >>> configuration = DinatConfig()
  30. >>> # Initializing a model (with random weights) from the shi-labs/dinat-mini-in1k-224 style configuration
  31. >>> model = DinatModel(configuration)
  32. >>> # Accessing the model configuration
  33. >>> configuration = model.config
  34. ```"""
  35. model_type = "dinat"
  36. attribute_map = {
  37. "num_attention_heads": "num_heads",
  38. "num_hidden_layers": "num_layers",
  39. }
  40. patch_size: int | list[int] | tuple[int, int] = 4
  41. num_channels: int = 3
  42. embed_dim: int = 64
  43. depths: list[int] | tuple[int, ...] = (3, 4, 6, 5)
  44. num_heads: list[int] | tuple[int, ...] = (2, 4, 8, 16)
  45. kernel_size: int = 7
  46. dilations: list | tuple | None = None
  47. mlp_ratio: float = 3.0
  48. qkv_bias: bool = True
  49. hidden_dropout_prob: float | int = 0.0
  50. attention_probs_dropout_prob: float | int = 0.0
  51. drop_path_rate: float | int = 0.1
  52. hidden_act: str = "gelu"
  53. initializer_range: float = 0.02
  54. layer_norm_eps: float = 1e-5
  55. layer_scale_init_value: float = 0.0
  56. _out_features: list[str] | None = None
  57. _out_indices: list[int] | None = None
  58. def __post_init__(self, **kwargs):
  59. self.num_layers = len(self.depths)
  60. self.dilations = self.dilations or [[1, 8, 1], [1, 4, 1, 4], [1, 2, 1, 2, 1, 2], [1, 1, 1, 1, 1]]
  61. # we set the hidden_size attribute in order to make Dinat work with VisionEncoderDecoderModel
  62. # this indicates the channel dimension after the last stage of the model
  63. self.hidden_size = int(self.embed_dim * 2 ** (len(self.depths) - 1))
  64. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  65. self.set_output_features_output_indices(
  66. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  67. )
  68. super().__post_init__(**kwargs)
  69. __all__ = ["DinatConfig"]