configuration_funnel.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. # Copyright 2020, Hugging Face
  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. """Funnel Transformer 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="funnel-transformer/small")
  19. @strict
  20. class FunnelConfig(PreTrainedConfig):
  21. r"""
  22. block_sizes (`list[int]`, *optional*, defaults to `[4, 4, 4]`):
  23. The sizes of the blocks used in the model.
  24. block_repeats (`list[int]`, *optional*):
  25. If passed along, each layer of each block is repeated the number of times indicated.
  26. num_decoder_layers (`int`, *optional*, defaults to 2):
  27. The number of layers in the decoder (when not using the base model).
  28. pooling_type (`str`, *optional*, defaults to `"mean"`):
  29. Possible values are `"mean"` or `"max"`. The way pooling is performed at the beginning of each block.
  30. attention_type (`str`, *optional*, defaults to `"relative_shift"`):
  31. Possible values are `"relative_shift"` or `"factorized"`. The former is faster on CPU/GPU while the latter
  32. is faster on TPU.
  33. separate_cls (`bool`, *optional*, defaults to `True`):
  34. Whether or not to separate the cls token when applying pooling.
  35. truncate_seq (`bool`, *optional*, defaults to `True`):
  36. When using `separate_cls`, whether or not to truncate the last token when pooling, to avoid getting a
  37. sequence length that is not a multiple of 2.
  38. pool_q_only (`bool`, *optional*, defaults to `True`):
  39. Whether or not to apply the pooling only to the query or to query, key and values for the attention layers.
  40. """
  41. model_type = "funnel"
  42. attribute_map = {
  43. "hidden_size": "d_model",
  44. "num_attention_heads": "n_head",
  45. }
  46. vocab_size: int = 30522
  47. block_sizes: list[int] | tuple[int, ...] = (4, 4, 4)
  48. block_repeats: list[int] | None = None
  49. num_decoder_layers: int = 2
  50. d_model: int = 768
  51. n_head: int = 12
  52. d_head: int = 64
  53. d_inner: int = 3072
  54. hidden_act: str = "gelu_new"
  55. hidden_dropout: float | int = 0.1
  56. attention_dropout: float | int = 0.1
  57. activation_dropout: float | int = 0.0
  58. initializer_range: float = 0.1
  59. initializer_std: float | None = None
  60. layer_norm_eps: float = 1e-9
  61. pooling_type: str = "mean"
  62. attention_type: str = "relative_shift"
  63. separate_cls: bool = True
  64. truncate_seq: bool = True
  65. pool_q_only: bool = True
  66. pad_token_id: int | None = None
  67. tie_word_embeddings: bool = True
  68. def __post_init__(self, **kwargs):
  69. self.block_repeats = [1] * len(self.block_sizes) if self.block_repeats is None else self.block_repeats
  70. super().__post_init__(**kwargs)
  71. def validate_architecture(self):
  72. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  73. if len(self.block_sizes) != len(self.block_repeats):
  74. raise ValueError("`block_sizes` and `block_repeats` should have the same length.")
  75. if self.pooling_type not in [
  76. "mean",
  77. "max",
  78. ]:
  79. raise ValueError(f"Got {self.pooling_type} for `pooling_type` but only 'mean' and 'max' are supported.")
  80. if self.attention_type not in [
  81. "relative_shift",
  82. "factorized",
  83. ]:
  84. raise ValueError(
  85. f"Got {self.attention_type} for `attention_type` but only 'relative_shift' and 'factorized' are supported."
  86. )
  87. @property
  88. def num_hidden_layers(self):
  89. return sum(self.block_sizes)
  90. @num_hidden_layers.setter
  91. def num_hidden_layers(self, value):
  92. raise NotImplementedError(
  93. "This model does not support the setting of `num_hidden_layers`. Please set `block_sizes`."
  94. )
  95. @property
  96. def num_blocks(self):
  97. return len(self.block_sizes)
  98. @num_blocks.setter
  99. def num_blocks(self, value):
  100. raise NotImplementedError("This model does not support the setting of `num_blocks`. Please set `block_sizes`.")
  101. __all__ = ["FunnelConfig"]