configuration_trocr.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. # coding=utf-8
  2. # Copyright 2021 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. """TrOCR model configuration"""
  16. from ...configuration_utils import PretrainedConfig
  17. from ...utils import logging
  18. logger = logging.get_logger(__name__)
  19. class TrOCRConfig(PretrainedConfig):
  20. r"""
  21. This is the configuration class to store the configuration of a [`TrOCRForCausalLM`]. It is used to instantiate an
  22. TrOCR 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 TrOCR
  24. [microsoft/trocr-base-handwritten](https://huggingface.co/microsoft/trocr-base-handwritten) 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 50265):
  29. Vocabulary size of the TrOCR model. Defines the number of different tokens that can be represented by the
  30. `inputs_ids` passed when calling [`TrOCRForCausalLM`].
  31. d_model (`int`, *optional*, defaults to 1024):
  32. Dimensionality of the layers and the pooler layer.
  33. decoder_layers (`int`, *optional*, defaults to 12):
  34. Number of decoder layers.
  35. decoder_attention_heads (`int`, *optional*, defaults to 16):
  36. Number of attention heads for each attention layer in the Transformer decoder.
  37. decoder_ffn_dim (`int`, *optional*, defaults to 4096):
  38. Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
  39. activation_function (`str` or `function`, *optional*, defaults to `"gelu"`):
  40. The non-linear activation function (function or string) in the pooler. If string, `"gelu"`, `"relu"`,
  41. `"silu"` and `"gelu_new"` are supported.
  42. max_position_embeddings (`int`, *optional*, defaults to 512):
  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. dropout (`float`, *optional*, defaults to 0.1):
  46. The dropout probability for all fully connected layers in the embeddings, and pooler.
  47. attention_dropout (`float`, *optional*, defaults to 0.0):
  48. The dropout ratio for the attention probabilities.
  49. activation_dropout (`float`, *optional*, defaults to 0.0):
  50. The dropout ratio for activations inside the fully connected layer.
  51. init_std (`float`, *optional*, defaults to 0.02):
  52. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  53. decoder_layerdrop (`float`, *optional*, defaults to 0.0):
  54. The LayerDrop probability for the decoder. See the [LayerDrop paper](see https://huggingface.co/papers/1909.11556)
  55. for more details.
  56. use_cache (`bool`, *optional*, defaults to `True`):
  57. Whether or not the model should return the last key/values attentions (not used by all models).
  58. scale_embedding (`bool`, *optional*, defaults to `False`):
  59. Whether or not to scale the word embeddings by sqrt(d_model).
  60. use_learned_position_embeddings (`bool`, *optional*, defaults to `True`):
  61. Whether or not to use learned position embeddings. If not, sinusoidal position embeddings will be used.
  62. layernorm_embedding (`bool`, *optional*, defaults to `True`):
  63. Whether or not to use a layernorm after the word + position embeddings.
  64. Example:
  65. ```python
  66. >>> from transformers import TrOCRConfig, TrOCRForCausalLM
  67. >>> # Initializing a TrOCR-base style configuration
  68. >>> configuration = TrOCRConfig()
  69. >>> # Initializing a model (with random weights) from the TrOCR-base style configuration
  70. >>> model = TrOCRForCausalLM(configuration)
  71. >>> # Accessing the model configuration
  72. >>> configuration = model.config
  73. ```"""
  74. model_type = "trocr"
  75. keys_to_ignore_at_inference = ["past_key_values"]
  76. attribute_map = {
  77. "num_attention_heads": "decoder_attention_heads",
  78. "hidden_size": "d_model",
  79. "num_hidden_layers": "decoder_layers",
  80. }
  81. def __init__(
  82. self,
  83. vocab_size=50265,
  84. d_model=1024,
  85. decoder_layers=12,
  86. decoder_attention_heads=16,
  87. decoder_ffn_dim=4096,
  88. activation_function="gelu",
  89. max_position_embeddings=512,
  90. dropout=0.1,
  91. attention_dropout=0.0,
  92. activation_dropout=0.0,
  93. decoder_start_token_id=2,
  94. init_std=0.02,
  95. decoder_layerdrop=0.0,
  96. use_cache=True,
  97. scale_embedding=False,
  98. use_learned_position_embeddings=True,
  99. layernorm_embedding=True,
  100. pad_token_id=1,
  101. bos_token_id=0,
  102. eos_token_id=2,
  103. **kwargs,
  104. ):
  105. self.vocab_size = vocab_size
  106. self.d_model = d_model
  107. self.decoder_layers = decoder_layers
  108. self.decoder_attention_heads = decoder_attention_heads
  109. self.decoder_ffn_dim = decoder_ffn_dim
  110. self.activation_function = activation_function
  111. self.max_position_embeddings = max_position_embeddings
  112. self.dropout = dropout
  113. self.attention_dropout = attention_dropout
  114. self.activation_dropout = activation_dropout
  115. self.init_std = init_std
  116. self.decoder_layerdrop = decoder_layerdrop
  117. self.use_cache = use_cache
  118. self.scale_embedding = scale_embedding
  119. self.use_learned_position_embeddings = use_learned_position_embeddings
  120. self.layernorm_embedding = layernorm_embedding
  121. super().__init__(
  122. pad_token_id=pad_token_id,
  123. bos_token_id=bos_token_id,
  124. eos_token_id=eos_token_id,
  125. decoder_start_token_id=decoder_start_token_id,
  126. **kwargs,
  127. )
  128. __all__ = ["TrOCRConfig"]