configuration_opt.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. # coding=utf-8
  2. # Copyright 2022 The Metaseq Authors and The HuggingFace Inc. team. 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. """OPT model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class OPTConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`OPTModel`]. It is used to instantiate a OPT model
  22. according to the specified arguments, defining the model architecture. Instantiating a configuration with the
  23. defaults will yield a similar configuration to that of the OPT
  24. [facebook/opt-350m](https://huggingface.co/facebook/opt-350m) architecture.
  25. Configuration objects inherit from [`PretrainedConfig`] and can be used to control the model outputs. Read the
  26. documentation from [`PretrainedConfig`] for more information.
  27. Args:
  28. vocab_size (`int`, *optional*, defaults to 50272):
  29. Vocabulary size of the OPT model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`OPTModel`]
  31. hidden_size (`int`, *optional*, defaults to 768):
  32. Dimensionality of the layers and the pooler layer.
  33. num_hidden_layers (`int`, *optional*, defaults to 12):
  34. Number of decoder layers.
  35. ffn_dim (`int`, *optional*, defaults to 3072):
  36. Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
  37. num_attention_heads (`int`, *optional*, defaults to 12):
  38. Number of attention heads for each attention layer in the Transformer decoder.
  39. activation_function (`str` or `function`, *optional*, defaults to `"relu"`):
  40. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  41. `"relu"`, `"silu"` and `"gelu_new"` are supported.
  42. max_position_embeddings (`int`, *optional*, defaults to 2048):
  43. The maximum sequence length that this model might ever be used with. Typically set this to something large
  44. just in case (e.g., 512 or 1024 or 2048).
  45. do_layer_norm_before (`bool`, *optional*, defaults to `True`):
  46. Whether to perform layer normalization before the attention block.
  47. word_embed_proj_dim (`int`, *optional*):
  48. `word_embed_proj_dim` can be set to down-project word embeddings, *e.g.* `opt-350m`. Defaults to
  49. `hidden_size`.
  50. dropout (`float`, *optional*, defaults to 0.1):
  51. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  52. attention_dropout (`float`, *optional*, defaults to 0.0):
  53. The dropout ratio for the attention probabilities.
  54. layerdrop (`float`, *optional*, defaults to 0.0):
  55. The LayerDrop probability. See the [LayerDrop paper](see https://arxiv.org/abs/1909.11556) for more
  56. details.
  57. init_std (`float`, *optional*, defaults to 0.02):
  58. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  59. use_cache (`bool`, *optional*, defaults to `True`):
  60. Whether or not the model should return the last key/values attentions (not used by all models).
  61. enable_bias (`bool`, *optional*, defaults to `True`):
  62. Whether or not if the linear layers in the attention blocks should use the bias term.
  63. layer_norm_elementwise_affine (`bool`, *optional*, defaults to `True`):
  64. Whether or not if the layer norms should have learnable parameters.
  65. Example:
  66. ```python
  67. >>> from transformers import OPTConfig, OPTModel
  68. >>> # Initializing a OPT facebook/opt-large style configuration
  69. >>> configuration = OPTConfig()
  70. >>> # Initializing a model (with random weights) from the facebook/opt-large style configuration
  71. >>> model = OPTModel(configuration)
  72. >>> # Accessing the model configuration
  73. >>> configuration = model.config
  74. ```"""
  75. model_type = "opt"
  76. keys_to_ignore_at_inference = ["past_key_values"]
  77. def __init__(
  78. self,
  79. vocab_size=50272,
  80. hidden_size=768,
  81. num_hidden_layers=12,
  82. ffn_dim=3072,
  83. max_position_embeddings=2048,
  84. do_layer_norm_before=True,
  85. _remove_final_layer_norm=False,
  86. word_embed_proj_dim=None,
  87. dropout=0.1,
  88. attention_dropout=0.0,
  89. num_attention_heads=12,
  90. activation_function="relu",
  91. layerdrop=0.0,
  92. init_std=0.02,
  93. use_cache=True,
  94. pad_token_id=1,
  95. bos_token_id=2,
  96. eos_token_id=2,
  97. enable_bias=True,
  98. layer_norm_elementwise_affine=True,
  99. **kwargs,
  100. ):
  101. super().__init__(
  102. pad_token_id=pad_token_id,
  103. bos_token_id=bos_token_id,
  104. eos_token_id=eos_token_id,
  105. **kwargs,
  106. )
  107. self.vocab_size = vocab_size
  108. self.max_position_embeddings = max_position_embeddings
  109. self.num_attention_heads = num_attention_heads
  110. self.word_embed_proj_dim = word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
  111. self.ffn_dim = ffn_dim
  112. self.hidden_size = hidden_size
  113. self.num_hidden_layers = num_hidden_layers
  114. self.dropout = dropout
  115. self.attention_dropout = attention_dropout
  116. self.activation_function = activation_function
  117. self.init_std = init_std
  118. self.layerdrop = layerdrop
  119. self.use_cache = use_cache
  120. self.do_layer_norm_before = do_layer_norm_before
  121. # We keep these variables at `True` for backward compatibility.
  122. self.enable_bias = enable_bias
  123. self.layer_norm_elementwise_affine = layer_norm_elementwise_affine
  124. # Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
  125. # with checkpoints that have been fine-tuned before transformers v4.20.1
  126. # see https://github.com/facebookresearch/metaseq/pull/164
  127. self._remove_final_layer_norm = _remove_final_layer_norm