configuration_udop.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. # Copyright 2024 HuggingFace Inc.
  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. """UDOP 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="microsoft/udop-large")
  19. @strict
  20. class UdopConfig(PreTrainedConfig):
  21. r"""
  22. relative_attention_num_buckets (`int`, *optional*, defaults to 32):
  23. The number of buckets to use for each attention layer.
  24. relative_attention_max_distance (`int`, *optional*, defaults to 128):
  25. The maximum distance of the longer sequences for the bucket separation.
  26. relative_bias_args (`list[dict]`, *optional*, defaults to `[{'type': '1d'}, {'type': 'horizontal'}, {'type': 'vertical'}]`):
  27. A list of dictionaries containing the arguments for the relative bias layers.
  28. feed_forward_proj (`string`, *optional*, defaults to `"relu"`):
  29. Type of feed forward layer to be used. Should be one of `"relu"` or `"gated-gelu"`. Udopv1.1 uses the
  30. `"gated-gelu"` feed forward projection. Original Udop uses `"relu"`.
  31. max_2d_position_embeddings (`int`, *optional*, defaults to 1024):
  32. The maximum absolute position embeddings for relative position encoding.
  33. """
  34. model_type = "udop"
  35. keys_to_ignore_at_inference = ["past_key_values"]
  36. attribute_map = {"hidden_size": "d_model", "num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"}
  37. vocab_size: int = 33201
  38. d_model: int = 1024
  39. d_kv: int = 64
  40. d_ff: int = 4096
  41. num_layers: int = 24
  42. num_decoder_layers: int | None = None
  43. num_heads: int = 16
  44. relative_attention_num_buckets: int = 32
  45. relative_attention_max_distance: int = 128
  46. relative_bias_args: list[dict] | None = None
  47. dropout_rate: float | int = 0.1
  48. layer_norm_epsilon: float = 1e-6
  49. initializer_factor: float = 1.0
  50. feed_forward_proj: str = "relu"
  51. is_encoder_decoder: bool = True
  52. use_cache: bool = True
  53. pad_token_id: int | None = 0
  54. eos_token_id: int | list[int] | None = 1
  55. max_2d_position_embeddings: int = 1024
  56. image_size: int | list[int] | tuple[int, int] = 224
  57. patch_size: int | list[int] | tuple[int, int] = 16
  58. num_channels: int = 3
  59. is_decoder: bool = False
  60. add_cross_attention: bool = False
  61. tie_word_embeddings: bool = True
  62. def __post_init__(self, **kwargs):
  63. if self.relative_bias_args is None:
  64. self.relative_bias_args = [{"type": "1d"}, {"type": "horizontal"}, {"type": "vertical"}]
  65. self.num_decoder_layers = (
  66. self.num_decoder_layers if self.num_decoder_layers is not None else self.num_layers
  67. ) # default = symmetry
  68. act_info = self.feed_forward_proj.split("-")
  69. self.dense_act_fn = act_info[-1]
  70. self.is_gated_act = act_info[0] == "gated"
  71. kwargs.pop("tie_word_embeddings", None)
  72. self.tie_word_embeddings = True
  73. super().__post_init__(**kwargs)
  74. def validate_architecture(self):
  75. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  76. act_info = self.feed_forward_proj.split("-")
  77. if len(act_info) > 1 and act_info[0] != "gated" or len(act_info) > 2:
  78. raise ValueError(
  79. f"`feed_forward_proj`: {self.feed_forward_proj} is not a valid activation function of the dense layer."
  80. "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
  81. "'gated-gelu' or 'relu'"
  82. )
  83. __all__ = ["UdopConfig"]