configuration_mobilebert.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. # Copyright 2020 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """MobileBERT model configuration"""
  15. from huggingface_hub.dataclasses import strict
  16. from ...configuration_utils import PreTrainedConfig
  17. from ...utils import auto_docstring
  18. @auto_docstring(checkpoint="google/mobilebert-uncased")
  19. @strict
  20. class MobileBertConfig(PreTrainedConfig):
  21. r"""
  22. embedding_size (`int`, *optional*, defaults to 128):
  23. The dimension of the word embedding vectors.
  24. trigram_input (`bool`, *optional*, defaults to `True`):
  25. Use a convolution of trigram as input.
  26. use_bottleneck (`bool`, *optional*, defaults to `True`):
  27. Whether to use bottleneck in BERT.
  28. intra_bottleneck_size (`int`, *optional*, defaults to 128):
  29. Size of bottleneck layer output.
  30. use_bottleneck_attention (`bool`, *optional*, defaults to `False`):
  31. Whether to use attention inputs from the bottleneck transformation.
  32. key_query_shared_bottleneck (`bool`, *optional*, defaults to `True`):
  33. Whether to use the same linear transformation for query&key in the bottleneck.
  34. num_feedforward_networks (`int`, *optional*, defaults to 4):
  35. Number of FFNs in a block.
  36. normalization_type (`str`, *optional*, defaults to `"no_norm"`):
  37. The normalization type in MobileBERT.
  38. Examples:
  39. ```python
  40. >>> from transformers import MobileBertConfig, MobileBertModel
  41. >>> # Initializing a MobileBERT configuration
  42. >>> configuration = MobileBertConfig()
  43. >>> # Initializing a model (with random weights) from the configuration above
  44. >>> model = MobileBertModel(configuration)
  45. >>> # Accessing the model configuration
  46. >>> configuration = model.config
  47. ```
  48. """
  49. model_type = "mobilebert"
  50. vocab_size: int = 30522
  51. hidden_size: int = 512
  52. num_hidden_layers: int = 24
  53. num_attention_heads: int = 4
  54. intermediate_size: int = 512
  55. hidden_act: str = "relu"
  56. hidden_dropout_prob: float | int = 0.0
  57. attention_probs_dropout_prob: float | int = 0.1
  58. max_position_embeddings: int = 512
  59. type_vocab_size: int = 2
  60. initializer_range: float = 0.02
  61. layer_norm_eps: float = 1e-12
  62. pad_token_id: int | None = 0
  63. embedding_size: int = 128
  64. trigram_input: bool = True
  65. use_bottleneck: bool = True
  66. intra_bottleneck_size: int = 128
  67. use_bottleneck_attention: bool = False
  68. key_query_shared_bottleneck: bool = True
  69. num_feedforward_networks: int = 4
  70. normalization_type: str = "no_norm"
  71. classifier_activation: bool = True
  72. classifier_dropout: float | int | None = None
  73. tie_word_embeddings: bool = True
  74. def __post_init__(self, **kwargs):
  75. if self.use_bottleneck:
  76. self.true_hidden_size = self.intra_bottleneck_size
  77. else:
  78. self.true_hidden_size = self.hidden_size
  79. super().__post_init__(**kwargs)
  80. __all__ = ["MobileBertConfig"]