configuration.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. # Copyright 2021-2022 The Alibaba PAI Team Authors.
  2. # Copyright (c) 2019, NVIDIA CORPORATION. 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. import torch
  16. from transformers.configuration_utils import PretrainedConfig
  17. from transformers.utils import logging
  18. logger = logging.get_logger()
  19. class GPTMoEConfig(PretrainedConfig):
  20. model_type = 'gpt-moe'
  21. def __init__(
  22. self,
  23. vocab_size=25600,
  24. hidden_size=768,
  25. ffn_hidden_size=None,
  26. num_hidden_layers=12,
  27. num_attention_heads=12,
  28. intermediate_size=3072,
  29. hidden_act='gelu',
  30. hidden_dropout_prob=0.1,
  31. attention_probs_dropout_prob=0.1,
  32. max_position_embeddings=2048,
  33. type_vocab_size=2,
  34. layernorm_epsilon=1e-12,
  35. bias_gelu_fusion=True,
  36. fp32_residual_connection=False,
  37. sequence_parallel=False,
  38. fp16=False,
  39. bf16=False,
  40. apply_query_key_layer_scaling=True,
  41. attention_softmax_in_fp32=False,
  42. kv_channels=None,
  43. masked_softmax_fusion=True,
  44. attention_dropout=0.1,
  45. bias_dropout_fusion=True,
  46. apply_residual_connection_post_layernorm=False,
  47. hidden_dropout=0.1,
  48. init_method_std=0.02,
  49. # generate
  50. eod_id=7,
  51. tokens_to_generate=100,
  52. top_k=0,
  53. top_p=0.9,
  54. num_experts=[0],
  55. use_tutel=False,
  56. top_k_linear_strategy='standard',
  57. use_expert_residual_network=False,
  58. load_ds_ckpts=False,
  59. model_dir=None,
  60. **kwargs):
  61. super().__init__(layer_norm_eps=layernorm_epsilon, **kwargs)
  62. self.vocab_size = vocab_size
  63. self.hidden_size = hidden_size
  64. self.ffn_hidden_size = 4 * hidden_size \
  65. if ffn_hidden_size is None else ffn_hidden_size
  66. self.num_hidden_layers = num_hidden_layers
  67. self.num_attention_heads = num_attention_heads
  68. self.hidden_act = hidden_act
  69. self.intermediate_size = intermediate_size
  70. self.hidden_dropout_prob = hidden_dropout_prob
  71. self.attention_probs_dropout_prob = attention_probs_dropout_prob
  72. self.max_position_embeddings = max_position_embeddings
  73. self.type_vocab_size = type_vocab_size
  74. self.layernorm_epsilon = layernorm_epsilon
  75. self.bias_gelu_fusion = bias_gelu_fusion
  76. self.fp32_residual_connection = fp32_residual_connection
  77. self.sequence_parallel = sequence_parallel
  78. self.fp16 = fp16
  79. self.bf16 = bf16
  80. assert not (fp16 and bf16)
  81. self.apply_query_key_layer_scaling = apply_query_key_layer_scaling
  82. self.attention_softmax_in_fp32 = attention_softmax_in_fp32
  83. if kv_channels is None:
  84. assert hidden_size % num_attention_heads == 0
  85. self.kv_channels = hidden_size // num_attention_heads
  86. self.masked_softmax_fusion = masked_softmax_fusion
  87. self.attention_dropout = attention_dropout
  88. self.bias_dropout_fusion = bias_dropout_fusion
  89. self.apply_residual_connection_post_layernorm = \
  90. apply_residual_connection_post_layernorm
  91. self.hidden_dropout = hidden_dropout
  92. self.init_method_std = init_method_std
  93. self.eod_id = eod_id
  94. self.tokens_to_generate = tokens_to_generate
  95. self.top_k = top_k
  96. self.top_p = top_p
  97. self.num_experts = num_experts
  98. self.use_tutel = use_tutel
  99. self.top_k_linear_strategy = top_k_linear_strategy
  100. self.use_expert_residual_network = use_expert_residual_network
  101. self.load_ds_ckpts = load_ds_ckpts
  102. self.model_dir = model_dir
  103. if self.num_experts[0] > torch.cuda.device_count():
  104. self.moe_expert_parallel_size = torch.cuda.device_count()
  105. else:
  106. self.moe_expert_parallel_size = self.num_experts[0]
  107. TORCH_MAJOR = int(torch.__version__.split('.')[0])
  108. TORCH_MINOR = int(torch.__version__.split('.')[1])
  109. self.no_persist_layer_norm = \
  110. TORCH_MAJOR < 1 or (TORCH_MAJOR == 1 and TORCH_MINOR < 11)
  111. @property
  112. def params_dtype(self):
  113. if self.fp16:
  114. return torch.half
  115. elif self.bf16:
  116. return torch.bfloat16
  117. else:
  118. return torch.float