configuration_canine.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # coding=utf-8
  2. # Copyright Google AI 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. """CANINE model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class CanineConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`CanineModel`]. It is used to instantiate an
  22. CANINE model according to the specified arguments, defining the model architecture. Instantiating a configuration
  23. with the defaults will yield a similar configuration to that of the CANINE
  24. [google/canine-s](https://huggingface.co/google/canine-s) 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. hidden_size (`int`, *optional*, defaults to 768):
  29. Dimension of the encoder layers and the pooler layer.
  30. num_hidden_layers (`int`, *optional*, defaults to 12):
  31. Number of hidden layers in the deep Transformer encoder.
  32. num_attention_heads (`int`, *optional*, defaults to 12):
  33. Number of attention heads for each attention layer in the Transformer encoders.
  34. intermediate_size (`int`, *optional*, defaults to 3072):
  35. Dimension of the "intermediate" (i.e., feed-forward) layer in the Transformer encoders.
  36. hidden_act (`str` or `function`, *optional*, defaults to `"gelu"`):
  37. The non-linear activation function (function or string) in the encoder and pooler. If string, `"gelu"`,
  38. `"relu"`, `"selu"` and `"gelu_new"` are supported.
  39. hidden_dropout_prob (`float`, *optional*, defaults to 0.1):
  40. The dropout probability for all fully connected layers in the embeddings, encoders, and pooler.
  41. attention_probs_dropout_prob (`float`, *optional*, defaults to 0.1):
  42. The dropout ratio for the attention probabilities.
  43. max_position_embeddings (`int`, *optional*, defaults to 16384):
  44. The maximum sequence length that this model might ever be used with.
  45. type_vocab_size (`int`, *optional*, defaults to 16):
  46. The vocabulary size of the `token_type_ids` passed when calling [`CanineModel`].
  47. initializer_range (`float`, *optional*, defaults to 0.02):
  48. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  49. layer_norm_eps (`float`, *optional*, defaults to 1e-12):
  50. The epsilon used by the layer normalization layers.
  51. pad_token_id (`int`, *optional*, defaults to 0):
  52. Padding token id.
  53. bos_token_id (`int`, *optional*, defaults to 57344):
  54. Beginning of stream token id.
  55. eos_token_id (`int`, *optional*, defaults to 57345):
  56. End of stream token id.
  57. downsampling_rate (`int`, *optional*, defaults to 4):
  58. The rate at which to downsample the original character sequence length before applying the deep Transformer
  59. encoder.
  60. upsampling_kernel_size (`int`, *optional*, defaults to 4):
  61. The kernel size (i.e. the number of characters in each window) of the convolutional projection layer when
  62. projecting back from `hidden_size`*2 to `hidden_size`.
  63. num_hash_functions (`int`, *optional*, defaults to 8):
  64. The number of hash functions to use. Each hash function has its own embedding matrix.
  65. num_hash_buckets (`int`, *optional*, defaults to 16384):
  66. The number of hash buckets to use.
  67. local_transformer_stride (`int`, *optional*, defaults to 128):
  68. The stride of the local attention of the first shallow Transformer encoder. Defaults to 128 for good
  69. TPU/XLA memory alignment.
  70. Example:
  71. ```python
  72. >>> from transformers import CanineConfig, CanineModel
  73. >>> # Initializing a CANINE google/canine-s style configuration
  74. >>> configuration = CanineConfig()
  75. >>> # Initializing a model (with random weights) from the google/canine-s style configuration
  76. >>> model = CanineModel(configuration)
  77. >>> # Accessing the model configuration
  78. >>> configuration = model.config
  79. ```"""
  80. model_type = "canine"
  81. def __init__(
  82. self,
  83. hidden_size=768,
  84. num_hidden_layers=12,
  85. num_attention_heads=12,
  86. intermediate_size=3072,
  87. hidden_act="gelu",
  88. hidden_dropout_prob=0.1,
  89. attention_probs_dropout_prob=0.1,
  90. max_position_embeddings=16384,
  91. type_vocab_size=16,
  92. initializer_range=0.02,
  93. layer_norm_eps=1e-12,
  94. pad_token_id=0,
  95. bos_token_id=0xE000,
  96. eos_token_id=0xE001,
  97. downsampling_rate=4,
  98. upsampling_kernel_size=4,
  99. num_hash_functions=8,
  100. num_hash_buckets=16384,
  101. local_transformer_stride=128, # Good TPU/XLA memory alignment.
  102. **kwargs,
  103. ):
  104. super().__init__(pad_token_id=pad_token_id, bos_token_id=bos_token_id, eos_token_id=eos_token_id, **kwargs)
  105. self.max_position_embeddings = max_position_embeddings
  106. self.hidden_size = hidden_size
  107. self.num_hidden_layers = num_hidden_layers
  108. self.num_attention_heads = num_attention_heads
  109. self.intermediate_size = intermediate_size
  110. self.hidden_act = hidden_act
  111. self.hidden_dropout_prob = hidden_dropout_prob
  112. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  113. self.initializer_range = initializer_range
  114. self.type_vocab_size = type_vocab_size
  115. self.layer_norm_eps = layer_norm_eps
  116. # Character config:
  117. self.downsampling_rate = downsampling_rate
  118. self.upsampling_kernel_size = upsampling_kernel_size
  119. self.num_hash_functions = num_hash_functions
  120. self.num_hash_buckets = num_hash_buckets
  121. self.local_transformer_stride = local_transformer_stride
  122. __all__ = ["CanineConfig"]