configuration_longt5.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. # Copyright 2022, The LongT5 Authors and 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. """LongT5 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="google/long-t5-local-base")
  19. @strict
  20. class LongT5Config(PreTrainedConfig):
  21. r"""
  22. d_ff (`int`, *optional*, defaults to 2048):
  23. Size of the intermediate feed forward layer in each `LongT5Block`.
  24. local_radius (`int`, *optional*, defaults to 127):
  25. Number of tokens to the left/right for each token to locally self-attend in a local attention mechanism.
  26. global_block_size (`int`, *optional*, defaults to 16):
  27. Length of blocks an input sequence is divided into for a global token representation. Used only for
  28. `encoder_attention_type = "transient-global"`.
  29. relative_attention_num_buckets (`int`, *optional*, defaults to 32):
  30. The number of buckets to use for each attention layer.
  31. relative_attention_max_distance (`int`, *optional*, defaults to 128):
  32. The maximum distance of the longer sequences for the bucket separation.
  33. feed_forward_proj (`string`, *optional*, defaults to `"relu"`):
  34. Type of feed forward layer to be used. Should be one of `"relu"` or `"gated-gelu"`. LongT5v1.1 uses the
  35. `"gated-gelu"` feed forward projection. Original LongT5 implementation uses `"gated-gelu"`.
  36. encoder_attention_type (`string`, *optional*, defaults to `"local"`):
  37. Type of encoder attention to be used. Should be one of `"local"` or `"transient-global"`, which are
  38. supported by LongT5 implementation.
  39. """
  40. model_type = "longt5"
  41. keys_to_ignore_at_inference = ["past_key_values"]
  42. attribute_map = {
  43. "hidden_size": "d_model",
  44. "num_attention_heads": "num_heads",
  45. "num_hidden_layers": "num_layers",
  46. "head_dim": "d_kv",
  47. }
  48. vocab_size: int = 32128
  49. d_model: int = 512
  50. d_kv: int = 64
  51. d_ff: int = 2048
  52. num_layers: int = 6
  53. num_decoder_layers: int | None = None
  54. num_heads: int = 8
  55. local_radius: int = 127
  56. global_block_size: int = 16
  57. relative_attention_num_buckets: int = 32
  58. relative_attention_max_distance: int = 128
  59. dropout_rate: float | int = 0.1
  60. layer_norm_epsilon: float = 1e-6
  61. initializer_factor: float = 1.0
  62. feed_forward_proj: str = "relu"
  63. is_encoder_decoder: bool = True
  64. encoder_attention_type: str = "local"
  65. use_cache: bool = True
  66. pad_token_id: int | None = 0
  67. eos_token_id: int | list[int] | None = 1
  68. bos_token_id: int | None = None
  69. is_decoder: bool = False
  70. tie_word_embeddings: bool = True
  71. def __post_init__(self, **kwargs):
  72. self.num_decoder_layers = self.num_decoder_layers if self.num_decoder_layers is not None else self.num_layers
  73. act_info = self.feed_forward_proj.split("-")
  74. self.dense_act_fn = act_info[-1]
  75. self.is_gated_act = act_info[0] == "gated"
  76. if self.feed_forward_proj == "gated-gelu":
  77. self.dense_act_fn = "gelu_new"
  78. super().__post_init__(**kwargs)
  79. def validate_architecture(self):
  80. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  81. act_info = self.feed_forward_proj.split("-")
  82. if len(act_info) > 1 and act_info[0] != "gated" or len(act_info) > 2:
  83. raise ValueError(
  84. f"`feed_forward_proj`: {self.feed_forward_proj} is not a valid activation function of the dense layer. "
  85. "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
  86. "'gated-gelu' or 'relu'"
  87. )
  88. __all__ = ["LongT5Config"]