configuration_ctrl.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # coding=utf-8
  2. # Copyright 2018 Salesforce and HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  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. """Salesforce CTRL configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class CTRLConfig(PretrainedConfig):
  20. """
  21. This is the configuration class to store the configuration of a [`CTRLModel`] or a [`TFCTRLModel`]. It is used to
  22. instantiate a CTRL model according to the specified arguments, defining the model architecture. Instantiating a
  23. configuration with the defaults will yield a similar configuration to that of the
  24. [Salesforce/ctrl](https://huggingface.co/Salesforce/ctrl) architecture from SalesForce.
  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 246534):
  29. Vocabulary size of the CTRL model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`CTRLModel`] or [`TFCTRLModel`].
  31. n_positions (`int`, *optional*, defaults to 256):
  32. The maximum sequence length that this model might ever be used with. Typically set this to something large
  33. just in case (e.g., 512 or 1024 or 2048).
  34. n_embd (`int`, *optional*, defaults to 1280):
  35. Dimensionality of the embeddings and hidden states.
  36. dff (`int`, *optional*, defaults to 8192):
  37. Dimensionality of the inner dimension of the feed forward networks (FFN).
  38. n_layer (`int`, *optional*, defaults to 48):
  39. Number of hidden layers in the Transformer encoder.
  40. n_head (`int`, *optional*, defaults to 16):
  41. Number of attention heads for each attention layer in the Transformer encoder.
  42. resid_pdrop (`float`, *optional*, defaults to 0.1):
  43. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  44. embd_pdrop (`int`, *optional*, defaults to 0.1):
  45. The dropout ratio for the embeddings.
  46. layer_norm_epsilon (`float`, *optional*, defaults to 1e-06):
  47. The epsilon to use in the layer normalization layers
  48. initializer_range (`float`, *optional*, defaults to 0.02):
  49. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  50. use_cache (`bool`, *optional*, defaults to `True`):
  51. Whether or not the model should return the last key/values attentions (not used by all models).
  52. Examples:
  53. ```python
  54. >>> from transformers import CTRLConfig, CTRLModel
  55. >>> # Initializing a CTRL configuration
  56. >>> configuration = CTRLConfig()
  57. >>> # Initializing a model (with random weights) from the configuration
  58. >>> model = CTRLModel(configuration)
  59. >>> # Accessing the model configuration
  60. >>> configuration = model.config
  61. ```"""
  62. model_type = "ctrl"
  63. keys_to_ignore_at_inference = ["past_key_values"]
  64. attribute_map = {
  65. "max_position_embeddings": "n_positions",
  66. "hidden_size": "n_embd",
  67. "num_attention_heads": "n_head",
  68. "num_hidden_layers": "n_layer",
  69. }
  70. def __init__(
  71. self,
  72. vocab_size=246534,
  73. n_positions=256,
  74. n_embd=1280,
  75. dff=8192,
  76. n_layer=48,
  77. n_head=16,
  78. resid_pdrop=0.1,
  79. embd_pdrop=0.1,
  80. layer_norm_epsilon=1e-6,
  81. initializer_range=0.02,
  82. use_cache=True,
  83. **kwargs,
  84. ):
  85. self.vocab_size = vocab_size
  86. self.n_positions = n_positions
  87. self.n_embd = n_embd
  88. self.n_layer = n_layer
  89. self.n_head = n_head
  90. self.dff = dff
  91. self.resid_pdrop = resid_pdrop
  92. self.embd_pdrop = embd_pdrop
  93. self.layer_norm_epsilon = layer_norm_epsilon
  94. self.initializer_range = initializer_range
  95. self.use_cache = use_cache
  96. super().__init__(**kwargs)