configuration.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2021-2022 The Alibaba DAMO NLP Team Authors.
  2. # Copyright 2018 The Google AI Language Team Authors and The HuggingFace Inc. team.
  3. # Copyright (c) 2018, NVIDIA CORPORATION. All rights reserved.
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """ PALM model configuration """
  17. from transformers.configuration_utils import PretrainedConfig
  18. from modelscope.utils import logger as logging
  19. logger = logging.get_logger()
  20. class PalmConfig(PretrainedConfig):
  21. r"""
  22. Configuration objects inherit from :class:`~transformers.PretrainedConfig` and can be used to control the model
  23. outputs. Read the documentation from :class:`~transformers.PretrainedConfig` for more information.
  24. Args:
  25. vocab_size (:obj:`int`, `optional`, defaults to 30522):
  26. Vocabulary size of the BERT model. Defines the number of different tokens that can be represented by the
  27. :obj:`inputs_ids` passed when calling :class:`~transformers.BertModel` or
  28. :class:`~transformers.TFBertModel`.
  29. hidden_size (:obj:`int`, `optional`, defaults to 768):
  30. Dimensionality of the encoder layers and the pooler layer.
  31. num_hidden_layers (:obj:`int`, `optional`, defaults to 12):
  32. Number of hidden layers in the Transformer encoder.
  33. num_attention_heads (:obj:`int`, `optional`, defaults to 12):
  34. Number of attention heads for each attention layer in the Transformer encoder.
  35. intermediate_size (:obj:`int`, `optional`, defaults to 3072):
  36. Dimensionality of the "intermediate" (often named feed-forward) layer in the Transformer encoder.
  37. hidden_act (:obj:`str` or :obj:`Callable`, `optional`, defaults to :obj:`"gelu"`):
  38. The non-linear activation function (function or string) in the encoder and pooler. If string,
  39. :obj:`"gelu"`, :obj:`"relu"`, :obj:`"silu"` and :obj:`"gelu_new"` are supported.
  40. hidden_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
  41. The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
  42. attention_probs_dropout_prob (:obj:`float`, `optional`, defaults to 0.1):
  43. The dropout ratio for the attention probabilities.
  44. max_position_embeddings (:obj:`int`, `optional`, defaults to 512):
  45. The maximum sequence length that this model might ever be used with. Typically set this to something large
  46. just in case (e.g., 512 or 1024 or 2048).
  47. type_vocab_size (:obj:`int`, `optional`, defaults to 2):
  48. The vocabulary size of the :obj:`token_type_ids` passed when calling :class:`~transformers.BertModel` or
  49. :class:`~transformers.TFBertModel`.
  50. initializer_range (:obj:`float`, `optional`, defaults to 0.02):
  51. The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
  52. layernorm_epsilon (:obj:`float`, `optional`, defaults to 1e-12):
  53. The epsilon used by the layer normalization layers.
  54. dec_hidden_layers (:obj:`int`, `optional`, defaults to 12):
  55. Number of hidden layers in the Transformer decoder.
  56. attn_separate (:obj:`bool`, `optional`, defaults to false):
  57. Whether or not to separate the q, k, v of attention.
  58. Examples:
  59. >>> from modelscope.models.nlp.palm_v2 import PalmForConditionalGeneration, PalmConfig
  60. >>> configuration = PalmConfig()
  61. >>> # Initializing a model from the configuration
  62. >>> model = PalmForConditionalGeneration(configuration)
  63. >>> # Accessing the model configuration
  64. >>> configuration = model.config
  65. """
  66. model_type = 'palm'
  67. def __init__(self,
  68. encoder='roberta',
  69. encoder_pth='roberta-base',
  70. max_pos=512,
  71. share_emb=False,
  72. dec_layers=12,
  73. dec_hidden_size=768,
  74. dec_heads=8,
  75. dec_ff_size=3072,
  76. dec_dropout=0.2,
  77. use_bert_emb=True,
  78. label_smoothing=0.1,
  79. alpha=0.95,
  80. beam_size=5,
  81. min_length=40,
  82. max_length=130,
  83. sample_topk=False,
  84. block_trigram=False,
  85. **kwargs):
  86. super().__init__(**kwargs)
  87. self.encoder = encoder
  88. self.encoder_pth = encoder_pth
  89. self.max_pos = max_pos
  90. self.share_emb = share_emb
  91. self.dec_layers = dec_layers
  92. self.dec_hidden_size = dec_hidden_size
  93. self.dec_heads = dec_heads
  94. self.dec_ff_size = dec_ff_size
  95. self.dec_dropout = dec_dropout
  96. self.use_bert_emb = use_bert_emb
  97. self.label_smoothing = label_smoothing
  98. # Translator
  99. self.alpha = alpha
  100. self.beam_size = beam_size
  101. self.min_length = min_length
  102. self.max_length = max_length
  103. self.sample_topk = sample_topk
  104. self.block_trigram = block_trigram