configuration_t5.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. # Copyright 2020, The T5 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. """T5 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-t5/t5-small")
  19. @strict
  20. class T5Config(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. feed_forward_proj (`string`, *optional*, defaults to `"relu"`):
  27. Type of feed forward layer to be used. Should be one of `"relu"` or `"gated-gelu"`. T5v1.1 uses the
  28. `"gated-gelu"` feed forward projection. Original T5 uses `"relu"`.
  29. """
  30. model_type = "t5"
  31. keys_to_ignore_at_inference = ["past_key_values"]
  32. attribute_map = {
  33. "hidden_size": "d_model",
  34. "num_attention_heads": "num_heads",
  35. "num_hidden_layers": "num_layers",
  36. "head_dim": "d_kv",
  37. }
  38. vocab_size: int = 32128
  39. d_model: int = 512
  40. d_kv: int = 64
  41. d_ff: int = 2048
  42. num_layers: int = 6
  43. num_decoder_layers: int | None = None
  44. num_heads: int = 8
  45. relative_attention_num_buckets: int = 32
  46. relative_attention_max_distance: int = 128
  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. classifier_dropout: float | int = 0.0
  56. is_decoder: bool = False
  57. def __post_init__(self, **kwargs):
  58. self.num_decoder_layers = (
  59. self.num_decoder_layers if self.num_decoder_layers is not None else self.num_layers
  60. ) # default = symmetry
  61. act_info = self.feed_forward_proj.split("-")
  62. self.dense_act_fn = act_info[-1]
  63. self.is_gated_act = act_info[0] == "gated"
  64. # for backwards compatibility
  65. if self.feed_forward_proj == "gated-gelu":
  66. self.dense_act_fn = "gelu_new"
  67. # Super weird feature of T5 because we support T5 and T51.1 from the same
  68. # model code. Original T5 always scaled outputs, but the 1.1v does not.
  69. # The model code was relying on saved configs where `tie_word_embeddings` is
  70. # set to `False` in 1.1v and using it as indicator of whether to scale or not
  71. # But in fact we tie weights always and force it to be `True`
  72. self.scale_decoder_outputs = kwargs.pop("tie_word_embeddings", None) is not False
  73. self.tie_word_embeddings = True
  74. super().__post_init__(**kwargs)
  75. def validate_architecture(self):
  76. """Part of `@strict`-powered validation. Validates the architecture of the config."""
  77. act_info = self.feed_forward_proj.split("-")
  78. if len(act_info) > 1 and act_info[0] != "gated" or len(act_info) > 2:
  79. raise ValueError(
  80. f"`feed_forward_proj`: {self.feed_forward_proj} is not a valid activation function of the dense layer. "
  81. "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
  82. "'gated-gelu' or 'relu'"
  83. )
  84. __all__ = ["T5Config"]