configuration_seggpt.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # Copyright 2024 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. """SegGpt 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="BAAI/seggpt-vit-large")
  19. @strict
  20. class SegGptConfig(PreTrainedConfig):
  21. r"""
  22. mlp_dim (`int`, *optional*):
  23. The dimensionality of the MLP layer in the Transformer encoder. If unset, defaults to
  24. `hidden_size` * 4.
  25. pretrain_image_size (`int`, *optional*, defaults to 224):
  26. The pretrained size of the absolute position embeddings.
  27. use_relative_position_embeddings (`bool`, *optional*, defaults to `True`):
  28. Whether to use relative position embeddings in the attention layers.
  29. merge_index (`int`, *optional*, defaults to 2):
  30. The index of the encoder layer to merge the embeddings.
  31. intermediate_hidden_state_indices (`list[int]`, *optional*, defaults to `[5, 11, 17, 23]`):
  32. The indices of the encoder layers which we store as features for the decoder.
  33. beta (`float`, *optional*, defaults to 0.01):
  34. Regularization factor for SegGptLoss (smooth-l1 loss).
  35. Example:
  36. ```python
  37. >>> from transformers import SegGptConfig, SegGptModel
  38. >>> # Initializing a SegGPT seggpt-vit-large style configuration
  39. >>> configuration = SegGptConfig()
  40. >>> # Initializing a model (with random weights) from the seggpt-vit-large style configuration
  41. >>> model = SegGptModel(configuration)
  42. >>> # Accessing the model configuration
  43. >>> configuration = model.config
  44. ```"""
  45. model_type = "seggpt"
  46. hidden_size: int = 1024
  47. num_hidden_layers: int = 24
  48. num_attention_heads: int = 16
  49. hidden_act: str = "gelu"
  50. hidden_dropout_prob: float | int = 0.0
  51. initializer_range: float = 0.02
  52. layer_norm_eps: float = 1e-6
  53. image_size: int | list[int] | tuple[int, ...] = (896, 448)
  54. patch_size: int | list[int] | tuple[int, int] = 16
  55. num_channels: int = 3
  56. qkv_bias: bool = True
  57. mlp_dim: int | None = None
  58. drop_path_rate: float | int = 0.1
  59. pretrain_image_size: int | list[int] | tuple[int, int] = 224
  60. decoder_hidden_size: int = 64
  61. use_relative_position_embeddings: bool = True
  62. merge_index: int = 2
  63. intermediate_hidden_state_indices: list[int] | tuple[int, ...] = (5, 11, 17, 23)
  64. beta: float = 0.01
  65. def __post_init__(self, **kwargs):
  66. self.mlp_dim = int(self.hidden_size * 4) if self.mlp_dim is None else self.mlp_dim
  67. super().__post_init__(**kwargs)
  68. def validate_architecture(self):
  69. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  70. if self.merge_index > min(self.intermediate_hidden_state_indices):
  71. raise ValueError(
  72. f"Merge index must be less than the minimum encoder output index, but got {self.merge_index=} and {self.intermediate_hidden_state_indices=}"
  73. )
  74. __all__ = ["SegGptConfig"]