configuration_marian.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2021 The Marian Team Authors and 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. """Marian 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="Helsinki-NLP/opus-mt-en-de")
  19. @strict
  20. class MarianConfig(PreTrainedConfig):
  21. r"""
  22. decoder_vocab_size (`int`, *optional*):
  23. Vocab size of the decoder layer's embedding.
  24. share_encoder_decoder_embeddings (`bool`, *optional*, defaults to `True`):
  25. Whether to tie and share embeddings of encoder and decoder
  26. Examples:
  27. ```python
  28. >>> from transformers import MarianModel, MarianConfig
  29. >>> # Initializing a Marian Helsinki-NLP/opus-mt-en-de style configuration
  30. >>> configuration = MarianConfig()
  31. >>> # Initializing a model from the Helsinki-NLP/opus-mt-en-de style configuration
  32. >>> model = MarianModel(configuration)
  33. >>> # Accessing the model configuration
  34. >>> configuration = model.config
  35. ```"""
  36. model_type = "marian"
  37. keys_to_ignore_at_inference = ["past_key_values"]
  38. attribute_map = {
  39. "num_attention_heads": "encoder_attention_heads",
  40. "hidden_size": "d_model",
  41. "num_hidden_layers": "encoder_layers",
  42. }
  43. vocab_size: int = 58101
  44. decoder_vocab_size: int | None = None
  45. max_position_embeddings: int = 1024
  46. encoder_layers: int = 12
  47. encoder_ffn_dim: int = 4096
  48. encoder_attention_heads: int = 16
  49. decoder_layers: int = 12
  50. decoder_ffn_dim: int = 4096
  51. decoder_attention_heads: int = 16
  52. encoder_layerdrop: float | int = 0.0
  53. decoder_layerdrop: float | int = 0.0
  54. use_cache: bool = True
  55. is_encoder_decoder: bool = True
  56. activation_function: str = "gelu"
  57. d_model: int = 1024
  58. dropout: float | int = 0.1
  59. attention_dropout: float | int = 0.0
  60. activation_dropout: float | int = 0.0
  61. init_std: float = 0.02
  62. decoder_start_token_id: int = 58100
  63. scale_embedding: bool = False
  64. pad_token_id: int | None = 58100
  65. eos_token_id: int | list[int] | None = 0
  66. bos_token_id: int | None = None
  67. forced_eos_token_id: int | list[int] | None = 0
  68. share_encoder_decoder_embeddings: bool = True
  69. is_decoder: bool = False
  70. tie_word_embeddings: bool = True
  71. def __post_init__(self, **kwargs):
  72. self.decoder_vocab_size = self.decoder_vocab_size or self.vocab_size
  73. super().__post_init__(**kwargs)
  74. __all__ = ["MarianConfig"]