configuration_bit.py 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. """BiT 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="google/bit-50")
  20. @strict
  21. class BitConfig(BackboneConfigMixin, PreTrainedConfig):
  22. r"""
  23. layer_type (`str`, *optional*, defaults to `"preactivation"`):
  24. The layer to use, it can be either `"preactivation"` or `"bottleneck"`.
  25. global_padding (`str`, *optional*):
  26. Padding strategy to use for the convolutional layers. Can be either `"valid"`, `"same"`, or `None`.
  27. num_groups (`int`, *optional*, defaults to 32):
  28. Number of groups used for the `BitGroupNormActivation` layers.
  29. embedding_dynamic_padding (`bool`, *optional*, defaults to `False`):
  30. Whether or not to make use of dynamic padding for the embedding layer.
  31. width_factor (`int`, *optional*, defaults to 1):
  32. The width factor for the model.
  33. Example:
  34. ```python
  35. >>> from transformers import BitConfig, BitModel
  36. >>> # Initializing a BiT bit-50 style configuration
  37. >>> configuration = BitConfig()
  38. >>> # Initializing a model (with random weights) from the bit-50 style configuration
  39. >>> model = BitModel(configuration)
  40. >>> # Accessing the model configuration
  41. >>> configuration = model.config
  42. ```
  43. """
  44. model_type = "bit"
  45. layer_types = ["preactivation", "bottleneck"]
  46. supported_padding = [None, "SAME", "VALID"]
  47. num_channels: int = 3
  48. embedding_size: int = 64
  49. hidden_sizes: list[int] | tuple[int, ...] = (256, 512, 1024, 2048)
  50. depths: list[int] | tuple[int, ...] = (3, 4, 6, 3)
  51. layer_type: str = "preactivation"
  52. hidden_act: str = "relu"
  53. global_padding: str | None = None
  54. num_groups: int = 32
  55. drop_path_rate: float | int = 0.0
  56. embedding_dynamic_padding: bool = False
  57. output_stride: int = 32
  58. width_factor: int = 1
  59. _out_features: list[str] | None = None
  60. _out_indices: list[int] | None = None
  61. def __post_init__(self, **kwargs):
  62. self.hidden_sizes = list(self.hidden_sizes)
  63. self.depths = list(self.depths)
  64. if self.global_padding is not None:
  65. self.global_padding = self.global_padding.upper()
  66. self.stage_names = ["stem"] + [f"stage{idx}" for idx in range(1, len(self.depths) + 1)]
  67. self.set_output_features_output_indices(
  68. out_indices=kwargs.pop("out_indices", None), out_features=kwargs.pop("out_features", None)
  69. )
  70. super().__post_init__(**kwargs)
  71. def validate_architecture(self):
  72. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  73. if self.layer_type not in self.layer_types:
  74. raise ValueError(f"layer_type={self.layer_type} is not one of {','.join(self.layer_types)}")
  75. if self.global_padding not in self.supported_padding:
  76. raise ValueError(f"Padding strategy {self.global_padding} not supported")
  77. __all__ = ["BitConfig"]