configuration_gpt2.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. # Copyright 2018 The OpenAI Team Authors and HuggingFace Inc. team.
  2. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """OpenAI GPT-2 configuration"""
  16. from huggingface_hub.dataclasses import strict
  17. from ...configuration_utils import PreTrainedConfig
  18. from ...utils import auto_docstring
  19. @auto_docstring(checkpoint="openai-community/gpt2")
  20. @strict
  21. class GPT2Config(PreTrainedConfig):
  22. r"""
  23. summary_type (`string`, *optional*, defaults to `"cls_index"`):
  24. Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`].
  25. Has to be one of the following options:
  26. - `"last"`: Take the last token hidden state (like XLNet).
  27. - `"first"`: Take the first token hidden state (like BERT).
  28. - `"mean"`: Take the mean of all tokens hidden states.
  29. - `"cls_index"`: Supply a Tensor of classification token position (like GPT/GPT-2).
  30. - `"attn"`: Not implemented now, use multi-head attention.
  31. summary_use_proj (`bool`, *optional*, defaults to `True`):
  32. Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`].
  33. Whether or not to add a projection after the vector extraction.
  34. summary_activation (`str`, *optional*):
  35. Argument used when doing sequence summary. Used in for the multiple choice head in
  36. [`GPT2DoubleHeadsModel`].
  37. Pass `"tanh"` for a tanh activation to the output, any other value will result in no activation.
  38. summary_proj_to_labels (`bool`, *optional*, defaults to `True`):
  39. Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`].
  40. Whether the projection outputs should have `config.num_labels` or `config.hidden_size` classes.
  41. summary_first_dropout (`float`, *optional*, defaults to 0.1):
  42. Argument used when doing sequence summary, used in the models [`GPT2DoubleHeadsModel`].
  43. The dropout ratio to be used after the projection and activation.
  44. scale_attn_by_inverse_layer_idx (`bool`, *optional*, defaults to `False`):
  45. Whether to additionally scale attention weights by `1 / layer_idx + 1`.
  46. reorder_and_upcast_attn (`bool`, *optional*, defaults to `False`):
  47. Whether to scale keys (K) prior to computing attention (dot-product) and upcast attention
  48. dot-product/softmax to float() when training with mixed precision.
  49. Example:
  50. ```python
  51. >>> from transformers import GPT2Config, GPT2Model
  52. >>> # Initializing a GPT2 configuration
  53. >>> configuration = GPT2Config()
  54. >>> # Initializing a model (with random weights) from the configuration
  55. >>> model = GPT2Model(configuration)
  56. >>> # Accessing the model configuration
  57. >>> configuration = model.config
  58. ```"""
  59. model_type = "gpt2"
  60. keys_to_ignore_at_inference = ["past_key_values"]
  61. attribute_map = {
  62. "hidden_size": "n_embd",
  63. "max_position_embeddings": "n_positions",
  64. "num_attention_heads": "n_head",
  65. "num_hidden_layers": "n_layer",
  66. }
  67. vocab_size: int = 50257
  68. n_positions: int = 1024
  69. n_embd: int = 768
  70. n_layer: int = 12
  71. n_head: int = 12
  72. n_inner: int | None = None
  73. activation_function: str = "gelu_new"
  74. resid_pdrop: float | int = 0.1
  75. embd_pdrop: float | int = 0.1
  76. attn_pdrop: float | int = 0.1
  77. layer_norm_epsilon: float = 1e-5
  78. initializer_range: float = 0.02
  79. summary_type: str = "cls_index"
  80. summary_use_proj: bool = True
  81. summary_activation: str | None = None
  82. summary_proj_to_labels: bool = True
  83. summary_first_dropout: float | int = 0.1
  84. scale_attn_weights: bool = True
  85. use_cache: bool = True
  86. bos_token_id: int | None = 50256
  87. eos_token_id: int | list[int] | None = 50256
  88. pad_token_id: int | None = None
  89. scale_attn_by_inverse_layer_idx: bool = False
  90. reorder_and_upcast_attn: bool = False
  91. add_cross_attention: bool = False
  92. tie_word_embeddings: bool = True
  93. __all__ = ["GPT2Config"]