configuration_trocr.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # Copyright 2021 The HuggingFace Inc. 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. """TrOCR 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="microsoft/trocr-base-handwritten")
  19. @strict
  20. class TrOCRConfig(PreTrainedConfig):
  21. r"""
  22. use_learned_position_embeddings (`bool`, *optional*, defaults to `True`):
  23. Whether or not to use learned position embeddings. If not, sinusoidal position embeddings will be used.
  24. layernorm_embedding (`bool`, *optional*, defaults to `True`):
  25. Whether or not to use a layernorm after the word + position embeddings.
  26. Example:
  27. ```python
  28. >>> from transformers import TrOCRConfig, TrOCRForCausalLM
  29. >>> # Initializing a TrOCR-base style configuration
  30. >>> configuration = TrOCRConfig()
  31. >>> # Initializing a model (with random weights) from the TrOCR-base style configuration
  32. >>> model = TrOCRForCausalLM(configuration)
  33. >>> # Accessing the model configuration
  34. >>> configuration = model.config
  35. ```"""
  36. model_type = "trocr"
  37. keys_to_ignore_at_inference = ["past_key_values"]
  38. attribute_map = {
  39. "num_attention_heads": "decoder_attention_heads",
  40. "hidden_size": "d_model",
  41. "num_hidden_layers": "decoder_layers",
  42. }
  43. vocab_size: int = 50265
  44. d_model: int = 1024
  45. decoder_layers: int = 12
  46. decoder_attention_heads: int = 16
  47. decoder_ffn_dim: int = 4096
  48. activation_function: str = "gelu"
  49. max_position_embeddings: int = 512
  50. dropout: float | int = 0.1
  51. attention_dropout: float | int = 0.0
  52. activation_dropout: float | int = 0.0
  53. decoder_start_token_id: int = 2
  54. init_std: float = 0.02
  55. decoder_layerdrop: float | int = 0.0
  56. use_cache: bool = True
  57. scale_embedding: bool = False
  58. use_learned_position_embeddings: bool = True
  59. layernorm_embedding: bool = True
  60. pad_token_id: int | None = 1
  61. bos_token_id: int | None = 0
  62. eos_token_id: int | list[int] | None = 2
  63. cross_attention_hidden_size: int | None = None
  64. is_decoder: bool = False
  65. tie_word_embeddings: bool = True
  66. __all__ = ["TrOCRConfig"]