r"""The base backbone of the PanGuAlpha model
| 294 | |
| 295 | |
| 296 | class PanguAlpha_Model(Cell): |
| 297 | r"""The base backbone of the PanGuAlpha model""" |
| 298 | |
| 299 | def __init__(self, config): |
| 300 | super(PanguAlpha_Model, self).__init__() |
| 301 | self.is_pipeline = config.parallel_config.pipeline_stage > 1 |
| 302 | self.embedding = EmbeddingLayer(config) |
| 303 | self.config = config |
| 304 | self.layernorm = _LayerNorm((config.hidden_size,)).to_float( |
| 305 | mstype.float32 |
| 306 | ) |
| 307 | if config.parallel_config.pipeline_stage > 1: |
| 308 | self.layernorm.set_comm_fusion(2) |
| 309 | else: |
| 310 | self.layernorm.set_comm_fusion( |
| 311 | config.parallel_config.gradient_aggregation_group |
| 312 | ) |
| 313 | self.layernorm.shard(((config.parallel_config.data_parallel, 1),)) |
| 314 | self.layernorm.pipeline_stage = ( |
| 315 | config.parallel_config.pipeline_stage - 1 |
| 316 | ) |
| 317 | # Configure the shard configure of the Embedding layer |
| 318 | self.embedding.pipeline_stage = 0 |
| 319 | self.num_layers = config.num_layers |
| 320 | if config.use_moe: |
| 321 | moe_config = MoEConfig( |
| 322 | expert_num=config.parallel_config.data_parallel |
| 323 | * config.per_dp_dim_expert_num |
| 324 | ) |
| 325 | else: |
| 326 | moe_config = MoEConfig(expert_num=1) |
| 327 | # The shard setting of Transformer is set within the class StackedTransformer |
| 328 | self.blocks = TransformerEncoder(num_layers=config.num_layers - 1, |
| 329 | batch_size=config.batch_size, |
| 330 | hidden_size=config.hidden_size, |
| 331 | ffn_hidden_size=config.ffn_hidden_size, |
| 332 | num_heads=config.num_heads, |
| 333 | seq_length=config.seq_length, |
| 334 | attention_dropout_rate=config.dropout_rate, |
| 335 | hidden_dropout_rate=config.dropout_rate, |
| 336 | lambda_func=set_parallel_configure_for_layer, |
| 337 | hidden_act="fast_gelu", |
| 338 | param_init_type=config.param_init_type, |
| 339 | use_past=config.use_past, |
| 340 | parallel_config=config.parallel_config, |
| 341 | moe_config=moe_config, |
| 342 | softmax_compute_type=config.softmax_compute_type).blocks |
| 343 | for block in self.blocks: |
| 344 | block.attention.dense1.bias.parallel_optimizer = False |
| 345 | block.attention.dense2.bias.parallel_optimizer = False |
| 346 | block.attention.dense3.bias.parallel_optimizer = False |
| 347 | block.output.mapping.bias.parallel_optimizer = False |
| 348 | copied_parallel_config = copy.deepcopy(config.parallel_config) |
| 349 | copied_parallel_config.vocab_emb_dp = True |
| 350 | self.top_query_embedding = VocabEmbedding(vocab_size=config.seq_length, |
| 351 | embedding_size=config.hidden_size, |
| 352 | param_init=initializer("normal", |
| 353 | [config.seq_length, config.hidden_size], |