configuration_openai.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. # coding=utf-8
  2. # Copyright 2018 The OpenAI Team Authors and HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """OpenAI GPT configuration"""
  17. from ...configuration_utils import PretrainedConfig
  18. from ...utils import logging
  19. logger = logging.get_logger(__name__)
  20. class OpenAIGPTConfig(PretrainedConfig):
  21. """
  22. This is the configuration class to store the configuration of a [`OpenAIGPTModel`] or a [`TFOpenAIGPTModel`]. It is
  23. used to instantiate a GPT model according to the specified arguments, defining the model architecture.
  24. Instantiating a configuration with the defaults will yield a similar configuration to that of the GPT
  25. [openai-community/openai-gpt](https://huggingface.co/openai-community/openai-gpt) architecture from OpenAI.
  26. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  27. documentation from [`PretrainedConfig`] for more information.
  28. Args:
  29. vocab_size (`int`, *optional*, defaults to 40478):
  30. Vocabulary size of the GPT-2 model. Defines the number of different tokens that can be represented by the
  31. `inputs_ids` passed when calling [`OpenAIGPTModel`] or [`TFOpenAIGPTModel`].
  32. n_positions (`int`, *optional*, defaults to 512):
  33. The maximum sequence length that this model might ever be used with. Typically set this to something large
  34. just in case (e.g., 512 or 1024 or 2048).
  35. n_embd (`int`, *optional*, defaults to 768):
  36. Dimensionality of the embeddings and hidden states.
  37. n_layer (`int`, *optional*, defaults to 12):
  38. Number of hidden layers in the Transformer encoder.
  39. n_head (`int`, *optional*, defaults to 12):
  40. Number of attention heads for each attention layer in the Transformer encoder.
  41. afn (`str` or `Callable`, *optional*, defaults to `"gelu"`):
  42. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  43. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  44. resid_pdrop (`float`, *optional*, defaults to 0.1):
  45. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  46. embd_pdrop (`int`, *optional*, defaults to 0.1):
  47. The dropout ratio for the embeddings.
  48. attn_pdrop (`float`, *optional*, defaults to 0.1):
  49. The dropout ratio for the attention.
  50. layer_norm_epsilon (`float`, *optional*, defaults to 1e-05):
  51. The epsilon to use in the layer normalization layers
  52. initializer_range (`float`, *optional*, defaults to 0.02):
  53. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  54. summary_type (`str`, *optional*, defaults to `"cls_index"`):
  55. Argument used when doing sequence summary, used in the models [`OpenAIGPTDoubleHeadsModel`] and
  56. [`OpenAIGPTDoubleHeadsModel`].
  57. Has to be one of the following options:
  58. - `"last"`: Take the last token hidden state (like XLNet).
  59. - `"first"`: Take the first token hidden state (like BERT).
  60. - `"mean"`: Take the mean of all tokens hidden states.
  61. - `"cls_index"`: Supply a Tensor of classification token position (like GPT/GPT-2).
  62. - `"attn"`: Not implemented now, use multi-head attention.
  63. summary_use_proj (`bool`, *optional*, defaults to `True`):
  64. Argument used when doing sequence summary, used in the models [`OpenAIGPTDoubleHeadsModel`] and
  65. [`OpenAIGPTDoubleHeadsModel`].
  66. Whether or not to add a projection after the vector extraction.
  67. summary_activation (`str`, *optional*):
  68. Argument used when doing sequence summary, used in the models [`OpenAIGPTDoubleHeadsModel`] and
  69. [`OpenAIGPTDoubleHeadsModel`].
  70. Pass `"tanh"` for a tanh activation to the output, any other value will result in no activation.
  71. summary_proj_to_labels (`bool`, *optional*, defaults to `True`):
  72. Argument used when doing sequence summary, used in the models [`OpenAIGPTDoubleHeadsModel`] and
  73. [`OpenAIGPTDoubleHeadsModel`].
  74. Whether the projection outputs should have `config.num_labels` or `config.hidden_size` classes.
  75. summary_first_dropout (`float`, *optional*, defaults to 0.1):
  76. Argument used when doing sequence summary, used in the models [`OpenAIGPTDoubleHeadsModel`] and
  77. [`OpenAIGPTDoubleHeadsModel`].
  78. The dropout ratio to be used after the projection and activation.
  79. Examples:
  80. ```python
  81. >>> from transformers import OpenAIGPTConfig, OpenAIGPTModel
  82. >>> # Initializing a GPT configuration
  83. >>> configuration = OpenAIGPTConfig()
  84. >>> # Initializing a model (with random weights) from the configuration
  85. >>> model = OpenAIGPTModel(configuration)
  86. >>> # Accessing the model configuration
  87. >>> configuration = model.config
  88. ```"""
  89. model_type = "openai-gpt"
  90. attribute_map = {
  91. "max_position_embeddings": "n_positions",
  92. "hidden_size": "n_embd",
  93. "num_attention_heads": "n_head",
  94. "num_hidden_layers": "n_layer",
  95. }
  96. def __init__(
  97. self,
  98. vocab_size=40478,
  99. n_positions=512,
  100. n_embd=768,
  101. n_layer=12,
  102. n_head=12,
  103. afn="gelu",
  104. resid_pdrop=0.1,
  105. embd_pdrop=0.1,
  106. attn_pdrop=0.1,
  107. layer_norm_epsilon=1e-5,
  108. initializer_range=0.02,
  109. summary_type="cls_index",
  110. summary_use_proj=True,
  111. summary_activation=None,
  112. summary_proj_to_labels=True,
  113. summary_first_dropout=0.1,
  114. **kwargs,
  115. ):
  116. self.vocab_size = vocab_size
  117. self.n_positions = n_positions
  118. self.n_embd = n_embd
  119. self.n_layer = n_layer
  120. self.n_head = n_head
  121. self.afn = afn
  122. self.resid_pdrop = resid_pdrop
  123. self.embd_pdrop = embd_pdrop
  124. self.attn_pdrop = attn_pdrop
  125. self.layer_norm_epsilon = layer_norm_epsilon
  126. self.initializer_range = initializer_range
  127. self.summary_type = summary_type
  128. self.summary_use_proj = summary_use_proj
  129. self.summary_activation = summary_activation
  130. self.summary_first_dropout = summary_first_dropout
  131. self.summary_proj_to_labels = summary_proj_to_labels
  132. super().__init__(**kwargs)
  133. __all__ = ["OpenAIGPTConfig"]