ernie_m
mindnlp.transformers.models.ernie_m.configuration_ernie_m
¶
ErnieM model configuration
mindnlp.transformers.models.ernie_m.configuration_ernie_m.ErnieMConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [ErnieMModel
]. It is used to instantiate a
Ernie-M model according to the specified arguments, defining the model architecture. Instantiating a configuration
with the defaults will yield a similar configuration to that of the Ernie-M
susnato/ernie-m-base_pytorch architecture.
Configuration objects inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
Vocabulary size of
TYPE:
|
hidden_size |
Dimensionality of the embedding layer, encoder layers and pooler layer.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
intermediate_size |
Dimensionality of the feed-forward (ff) layer in the encoder. Input tensors to feed-forward layers are firstly projected from hidden_size to intermediate_size, and then projected back to hidden_size. Typically intermediate_size is larger than hidden_size.
TYPE:
|
hidden_act |
The non-linear activation function in the feed-forward layer.
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings and encoder.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability used in
TYPE:
|
act_dropout |
This dropout probability is used in
TYPE:
|
max_position_embeddings |
The maximum value of the dimensionality of position encoding, which dictates the maximum supported length of an input sequence.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
classifier_dropout |
The dropout ratio for the classification head.
TYPE:
|
initializer_range |
The standard deviation of the normal initializer for initializing all weight matrices.
TYPE:
|
pad_token_id(`int`, |
The index of padding token in the token vocabulary.
TYPE:
|
A normal_initializer initializes weight matrices as normal distributions. See
ErnieMPretrainedModel._init_weights()
for how weights are initialized in ErnieMModel
.
Source code in mindnlp/transformers/models/ernie_m/configuration_ernie_m.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
mindnlp.transformers.models.ernie_m.configuration_ernie_m.ErnieMConfig.__init__(vocab_size=250002, hidden_size=768, num_hidden_layers=12, num_attention_heads=12, intermediate_size=3072, hidden_act='gelu', hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=514, initializer_range=0.02, pad_token_id=1, layer_norm_eps=1e-05, classifier_dropout=None, is_decoder=False, act_dropout=0.0, **kwargs)
¶
This method initializes an instance of the ErnieMConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Default is 250002.
TYPE:
|
hidden_size |
The size of the hidden layers. Default is 768.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Default is 12.
TYPE:
|
num_attention_heads |
The number of attention heads. Default is 12.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the transformer. Default is 3072.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Default is 'gelu'.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers. Default is 0.1.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for the attention probabilities. Default is 0.1.
TYPE:
|
max_position_embeddings |
The maximum position for the embeddings. Default is 514.
TYPE:
|
initializer_range |
The range for the weight initializers. Default is 0.02.
TYPE:
|
pad_token_id |
The ID for padding tokens. Default is 1.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Default is 1e-05.
TYPE:
|
classifier_dropout |
The dropout rate for the classifier layer. Default is None.
TYPE:
|
is_decoder |
Whether the model is a decoder. Default is False.
TYPE:
|
act_dropout |
The dropout rate for the activation function. Default is 0.0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/configuration_ernie_m.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m
¶
MindSpore ErnieM model.
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMAttention
¶
Bases: Module
ErnieMAttention is a class that represents an attention mechanism used in the ERNIE-M model.
It contains methods for initializing the attention mechanism, pruning attention heads, and forwarding attention outputs.
This class inherits from nn.Module and utilizes an ErnieMSelfAttention module for self-attention calculations.
The attention mechanism includes projection layers for query, key, and value, as well as an output projection layer.
The prune_heads
method allows for pruning specific attention heads based on provided indices.
The forward
method processes input hidden states through the self-attention mechanism and output projection
layer to generate attention outputs.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMAttention.__init__(config, position_embedding_type=None)
¶
Initialize the ErnieMAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object containing configuration parameters.
|
position_embedding_type |
Type of position embedding to be used, default is None.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMAttention.forward(hidden_states, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_value=None, output_attentions=False)
¶
This method forwards the ErnieMAttention module.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMAttention class.
|
hidden_states |
The input hidden states tensor.
TYPE:
|
attention_mask |
Optional tensor containing attention mask values.
TYPE:
|
head_mask |
Optional tensor containing head mask values.
TYPE:
|
encoder_hidden_states |
Optional tensor containing encoder hidden states.
TYPE:
|
encoder_attention_mask |
Optional tensor containing encoder attention mask values.
TYPE:
|
past_key_value |
Optional tuple containing past key and value tensors.
TYPE:
|
output_attentions |
Optional boolean indicating whether to output attentions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor]
|
Tuple[mindspore.Tensor]: A tuple containing the attention output tensor. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMAttention.prune_heads(heads)
¶
This method 'prune_heads' belongs to the class 'ErnieMAttention' and is responsible for pruning specific attention heads in the model based on the provided list of heads.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the 'ErnieMAttention' class. It is used to access attributes and methods within the class.
|
heads |
A list containing the indices of the attention heads that need to be pruned. Each element in the list should be an integer representing the index of the head to be pruned.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value but modifies the attention heads in the model in-place. |
RAISES | DESCRIPTION |
---|---|
None
|
However, it is assumed that the functions called within this method, such as 'find_pruneable_heads_and_indices' and 'prune_linear_layer', may raise exceptions related to input validation or processing errors. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEmbeddings
¶
Bases: Module
Construct the embeddings from word and position embeddings.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEmbeddings.__init__(config)
¶
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMEmbeddings class.
TYPE:
|
config |
An object containing configuration parameters for the ErnieMEmbeddings instance, including the hidden size, vocabulary size, maximum position embeddings, padding token ID, layer normalization epsilon, and hidden dropout probability.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of the expected type. |
ValueError
|
If the config parameter does not contain required attributes or if the padding token ID is not valid. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEmbeddings.forward(input_ids=None, position_ids=None, inputs_embeds=None, past_key_values_length=0)
¶
This method 'forward' in the class 'ErnieMEmbeddings' forwards the embeddings for the input tokens.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
input_ids |
The input token IDs. Default is None. If None, 'inputs_embeds' is used to generate the embeddings.
TYPE:
|
position_ids |
The position IDs for the input tokens. Default is None. If None, position IDs are calculated based on the input shape.
TYPE:
|
inputs_embeds |
The input embeddings. Default is None. If None, input embeddings are generated using 'word_embeddings' based on 'input_ids'.
TYPE:
|
past_key_values_length |
The length of past key values. Default is 0. It is used to adjust the 'position_ids' if past key values are present.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The forwarded embeddings for the input tokens. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input shape is invalid or if 'position_ids' cannot be calculated. |
TypeError
|
If the input types are not as expected. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoder
¶
Bases: Module
ErnieMEncoder represents a multi-layer Transformer-based encoder model for processing sequences of input data.
The ErnieMEncoder class inherits from nn.Module and implements a multi-layer Transformer-based encoder, with the ability to return hidden states and attention weights if specified. The class provides methods for initializing the model and processing input data through its layers.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
A configuration object containing the model's hyperparameters.
|
layers |
A list of ErnieMEncoderLayer instances representing the individual layers of the encoder model.
|
METHOD | DESCRIPTION |
---|---|
forward |
Processes input embeddings through the encoder layers, optionally returning hidden states and |
Please note that the actual code implementation is not included in this docstring.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoder.__init__(config)
¶
Initializes an instance of the ErnieMEncoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMEncoder class.
TYPE:
|
config |
The configuration object containing settings for the ErnieMEncoder. This parameter is required for configuring the ErnieMEncoder instance. It should be an object that provides necessary configuration details. It is expected to have attributes such as num_hidden_layers to specify the number of hidden layers.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoder.forward(input_embeds, attention_mask=None, head_mask=None, past_key_values=None, output_attentions=False, output_hidden_states=False, return_dict=True)
¶
Constructs the ErnieMEncoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
input_embeds |
The input embeddings. Shape (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
The attention mask. Shape (batch_size, sequence_length).
TYPE:
|
head_mask |
The head mask. Shape (num_layers, num_heads).
TYPE:
|
past_key_values |
The past key values. Shape (num_layers, 2, batch_size, num_heads, sequence_length // num_heads, hidden_size // num_heads).
TYPE:
|
output_attentions |
Whether to output attention weights. Default is False.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default is False.
TYPE:
|
return_dict |
Whether to return a BaseModelOutputWithPastAndCrossAttentions. Default is True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], BaseModelOutputWithPastAndCrossAttentions]
|
Union[Tuple[mindspore.Tensor], BaseModelOutputWithPastAndCrossAttentions]: The encoded last hidden state, optional hidden states, and optional attention weights. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoderLayer
¶
Bases: Module
The ErnieMEncoderLayer class represents a single layer of the ErnieM (Enhanced Representation through kNowledge Integration) encoder, which is designed for natural language processing tasks. This class inherits from the nn.Module class and implements the functionality for processing input hidden states using multi-head self-attention mechanism and feedforward neural network layers with layer normalization and dropout.
ATTRIBUTE | DESCRIPTION |
---|---|
self_attn |
Instance of ErnieMAttention for multi-head self-attention mechanism.
|
linear1 |
Instance of nn.Linear for the first feedforward neural network layer.
|
dropout |
Instance of nn.Dropout for applying dropout within the feedforward network.
|
linear2 |
Instance of nn.Linear for the second feedforward neural network layer.
|
norm1 |
Instance of nn.LayerNorm for the first layer normalization.
|
norm2 |
Instance of nn.LayerNorm for the second layer normalization.
|
dropout1 |
Instance of nn.Dropout for applying dropout after the first feedforward network layer.
|
dropout2 |
Instance of nn.Dropout for applying dropout after the second feedforward network layer.
|
activation |
Activation function for the feedforward network.
|
METHOD | DESCRIPTION |
---|---|
forward |
Applies the multi-head self-attention mechanism and feedforward network layers to the input hidden states, optionally producing attention weights. Args:
Returns:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoderLayer.__init__(config)
¶
Initialize an instance of the ErnieMEncoderLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMEncoderLayer class.
TYPE:
|
config |
An object containing configuration parameters for the encoder layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMEncoderLayer.forward(hidden_states, attention_mask=None, head_mask=None, past_key_value=None, output_attentions=True)
¶
Constructs an ErnieMEncoderLayer.
This method applies the ErnieMEncoderLayer transformation to the input hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the ErnieMEncoderLayer class.
|
hidden_states |
The input hidden states. This should be a tensor.
TYPE:
|
attention_mask |
The attention mask tensor. Defaults to None.
TYPE:
|
head_mask |
The head mask tensor. Defaults to None.
TYPE:
|
past_key_value |
The past key value tensor. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attention weights. Defaults to True.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForInformationExtraction
¶
Bases: ErnieMPreTrainedModel
ErnieMForInformationExtraction is a class that represents an ErnieM model for information extraction tasks. It inherits from ErnieMPreTrainedModel and includes methods for initializing the model and forwarding the forward pass.
ATTRIBUTE | DESCRIPTION |
---|---|
ernie_m |
The ErnieM model used for information extraction.
TYPE:
|
linear_start |
Linear layer for predicting the start position in the input sequence.
TYPE:
|
linear_end |
Linear layer for predicting the end position in the input sequence.
TYPE:
|
sigmoid |
Sigmoid activation function for probability calculation.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the ErnieMForInformationExtraction class with the provided configuration. |
forward |
Constructs the forward pass of the model for information extraction tasks. |
PARAMETER | DESCRIPTION |
---|---|
input_ids |
Input tensor containing token ids.
TYPE:
|
attention_mask |
Tensor specifying which tokens should be attended to.
TYPE:
|
position_ids |
Tensor specifying the position ids of tokens.
TYPE:
|
head_mask |
Tensor for masking specific heads in the self-attention layers.
TYPE:
|
inputs_embeds |
Tensor for providing custom embeddings instead of token ids.
TYPE:
|
start_positions |
Labels for start positions in the input sequence.
TYPE:
|
end_positions |
Labels for end positions in the input sequence.
TYPE:
|
output_attentions |
Flag to output attention weights.
TYPE:
|
output_hidden_states |
Flag to output hidden states.
TYPE:
|
return_dict |
Flag to return outputs as a dictionary.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[mindspore.Tensor], QuestionAnsweringModelOutput]: Tuple of output tensors or a QuestionAnsweringModelOutput object. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If start_positions or end_positions are not of the expected shape. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForInformationExtraction.__init__(config)
¶
Initializes a new instance of the ErnieMForInformationExtraction class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An instance of the ErnieMConfig class containing the configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForInformationExtraction.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=True)
¶
PARAMETER | DESCRIPTION |
---|---|
start_positions |
Labels for position (index) for computing the start_positions loss. Position outside of the sequence are not taken into account for computing the loss.
TYPE:
|
end_positions |
Labels for position (index) for computing the end_positions loss. Position outside of the sequence are not taken into account for computing the loss.
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForMultipleChoice
¶
Bases: ErnieMPreTrainedModel
ErnieMForMultipleChoice is a class that represents a multiple choice question answering model based on the ERNIE-M architecture. It inherits from ErnieMPreTrainedModel and implements methods for forwarding the model and computing the multiple choice classification loss.
ATTRIBUTE | DESCRIPTION |
---|---|
ernie_m |
The ERNIE-M model used for processing inputs.
TYPE:
|
dropout |
Dropout layer used in the classifier.
TYPE:
|
classifier |
Dense layer for classification.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the ErnieMForMultipleChoice model with the given configuration. |
forward |
Constructs the model for multiple choice question answering and computes the classification loss. |
The forward method takes various input tensors and parameters, processes them through the ERNIE-M model, applies dropout, and computes the classification logits. If labels are provided, it calculates the cross-entropy loss. The method returns the loss and model outputs based on the return_dict parameter.
This class is designed to be used for multiple choice question answering tasks with ERNIE-M models.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForMultipleChoice.__init__(config)
¶
Initializes an instance of the ErnieMForMultipleChoice class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the ErnieMConfig class containing the model configuration.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForMultipleChoice.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=True)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the multiple choice classification loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForQuestionAnswering
¶
Bases: ErnieMPreTrainedModel
ErnieMForQuestionAnswering is a class that represents a fine-tuned ErnieM model for question answering tasks. It is a subclass of ErnieMPreTrainedModel.
This class extends the functionality of the base ErnieM model by adding a question answering head on top of it. It takes as input the configuration of the model and initializes the necessary components. The class provides a method called 'forward' which performs the forward pass of the model for question answering.
The 'forward' method takes several input tensors such as 'input_ids', 'attention_mask', 'position_ids', 'head_mask', and 'inputs_embeds'. It also supports optional inputs like 'start_positions', 'end_positions', 'output_attentions', 'output_hidden_states', and 'return_dict'. The method returns the question answering model output, which includes the start and end logits, hidden states, attentions, and an optional total loss.
The 'forward' method internally calls the 'ernie_m' method of the base ErnieM model to obtain the sequence output. It then passes the sequence output through a dense layer 'qa_outputs' to get the logits for the start and end positions. The logits are then processed to obtain the final start and end logits. If 'start_positions' and 'end_positions' are provided, the method calculates the cross-entropy loss for the predicted logits and the provided positions. The total loss is computed as the average of the start and end losses.
The 'forward' method returns the model output in a structured manner based on the 'return_dict' parameter.
- If 'return_dict' is False, the method returns a tuple containing the total loss, start logits, end logits, and any additional hidden states or attentions.
- If 'return_dict' is True, the method returns an instance of the 'QuestionAnsweringModelOutput' class, which encapsulates the output elements as attributes.
Note
- If 'start_positions' and 'end_positions' are not provided, the total loss will be None.
- The start and end positions are clamped to the length of the sequence and positions outside the sequence are ignored for computing the loss.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForQuestionAnswering.__init__(config)
¶
Initializes a new instance of the ErnieMForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An instance of the ErnieMConfig class containing the model configuration.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForQuestionAnswering.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=True)
¶
PARAMETER | DESCRIPTION |
---|---|
start_positions |
Labels for position (index) of the start of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (
TYPE:
|
end_positions |
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForSequenceClassification
¶
Bases: ErnieMPreTrainedModel
ErnieMForSequenceClassification is a class that represents a fine-tuned ErnieM model for sequence classification tasks. It inherits from ErnieMPreTrainedModel and implements methods for initializing the model and forwarding predictions.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
Number of labels for sequence classification.
|
config |
Configuration object for the model.
|
ernie_m |
ErnieMModel instance for processing input sequences.
|
dropout |
Dropout layer for regularization.
|
classifier |
Dense layer for classification predictions.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the ErnieMForSequenceClassification instance with the provided configuration. |
forward |
Constructs the model for making predictions on input sequences and computes the loss based on predicted labels. Args:
Returns:
Raises:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForSequenceClassification.__init__(config)
¶
Initializes an instance of the ErnieMForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing settings for the model initialization. It must have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the config object is missing the num_labels attribute. |
TypeError
|
If the config object does not have the expected attributes or if their types are incorrect. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForSequenceClassification.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, past_key_values=None, use_cache=None, output_hidden_states=None, output_attentions=None, return_dict=True, labels=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForTokenClassification
¶
Bases: ErnieMPreTrainedModel
This class represents a fine-tuned ErnieM model for token classification tasks. It inherits from the ErnieMPreTrainedModel class.
The ErnieMForTokenClassification class implements the necessary methods and attributes for token classification tasks. It takes a configuration object as input during initialization and sets up the model architecture accordingly. The model consists of an ErnieMModel instance, a dropout layer, and a classifier layer.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the ErnieMForTokenClassification instance with the given configuration. It sets the number of labels, creates an ErnieMModel object, initializes the dropout layer, and creates the classifier layer. |
forward |
Constructs the forward pass of the model. It takes various input tensors and returns the token classification output. Optionally, it can also compute the token classification loss if labels are provided. |
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of possible labels for the token classification task.
|
Example
>>> config = ErnieMConfig()
>>> model = ErnieMForTokenClassification(config)
>>> input_ids = ...
>>> attention_mask = ...
>>> output = model.forward(input_ids=input_ids, attention_mask=attention_mask)
Note
It is important to provide the input tensors in the correct shape and format to ensure proper model functioning.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForTokenClassification.__init__(config)
¶
Initializes an instance of the ErnieMForTokenClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMForTokenClassification class.
|
config |
An instance of the configuration class containing the model configuration settings.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMForTokenClassification.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, past_key_values=None, output_hidden_states=None, output_attentions=None, return_dict=True, labels=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the token classification loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMModel
¶
Bases: ErnieMPreTrainedModel
This class represents an ERNIE-M (Enhanced Representation through kNowledge Integration) model for multi-purpose pre-training and fine-tuning on downstream tasks. It incorporates ERNIE-M embeddings, encoder, and optional pooling layer. The class provides methods for initializing, getting and setting input embeddings, pruning model heads, and forwarding the model with various input and output options. The class inherits from ErnieMPreTrainedModel and extends its functionality to support specific ERNIE-M model architecture and operations.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMModel.__init__(config, add_pooling_layer=True)
¶
Initializes the ErnieMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing model settings.
TYPE:
|
add_pooling_layer |
A flag indicating whether to add a pooling layer to the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMModel.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, past_key_values=None, use_cache=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
Constructs the ERNIE-M model.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
input_ids |
The input tensor of token indices. Default is None.
TYPE:
|
position_ids |
The tensor indicating the position of tokens. Default is None.
TYPE:
|
attention_mask |
The tensor indicating which elements in the input do not need to be attended to. Default is None.
TYPE:
|
head_mask |
The tensor indicating the heads in the multi-head attention layer to be masked. Default is None.
TYPE:
|
inputs_embeds |
The input embeddings. Default is None.
TYPE:
|
past_key_values |
The previous key values. Default is None.
TYPE:
|
use_cache |
Whether to use the cache. Default is None.
TYPE:
|
output_hidden_states |
Whether to output the hidden states. Default is None.
TYPE:
|
output_attentions |
Whether to output the attentions. Default is None.
TYPE:
|
return_dict |
Whether to return a dictionary. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], BaseModelOutputWithPoolingAndCrossAttentions]
|
Union[Tuple[mindspore.Tensor], BaseModelOutputWithPoolingAndCrossAttentions]:
Depending on the value of |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMModel.get_input_embeddings()
¶
This method returns the input embeddings from the ErnieMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
ErnieMModel object. The instance of the ErnieMModel class.
|
RETURNS | DESCRIPTION |
---|---|
word_embeddings
|
The method returns the input embeddings from the ErnieMModel. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMModel.set_input_embeddings(value)
¶
Set the input embeddings for the ErnieMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMModel class.
TYPE:
|
value |
The input embeddings value to be set. It should be a tensor representing the input embeddings.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMPooler
¶
Bases: Module
This class represents the MPooler module of the ERNIE model, which is responsible for pooling the hidden states to obtain a single representation of the input sequence.
Inherits from
nn.Module
ATTRIBUTE | DESCRIPTION |
---|---|
dense |
A fully connected layer that projects the input hidden states to a new hidden size.
TYPE:
|
activation |
The activation function applied to the projected hidden states.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the ERNIE MPooler module. |
forward |
Constructs the MPooler module by pooling the hidden states. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMPooler.__init__(config)
¶
Initializes a new instance of the ErnieMPooler class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the configuration class used to configure the ErnieMPooler. It provides various settings and parameters for the ErnieMPooler's behavior. This parameter is required.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMPooler.forward(hidden_states)
¶
Constructs the pooled output tensor for the ERNIE model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the ErnieMPooler class.
TYPE:
|
hidden_states |
A tensor containing the hidden states from the ERNIE model. It should have the shape (batch_size, sequence_length, hidden_size) where:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: A tensor representing the pooled output of the ERNIE model. The pooled output is obtained by applying dense and activation layers to the first token tensor extracted from the hidden states tensor. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMPreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMSelfAttention
¶
Bases: Module
A module that implements the self-attention mechanism used in ERNIE model.
This module contains the ErnieMSelfAttention
class, which represents the self-attention mechanism used in the
ERNIE model. It is a subclass of nn.Module
and is responsible for calculating the attention scores and producing
the context layer.
ATTRIBUTE | DESCRIPTION |
---|---|
num_attention_heads |
The number of attention heads in the self-attention mechanism.
TYPE:
|
attention_head_size |
The size of each attention head.
TYPE:
|
all_head_size |
The total size of all attention heads combined.
TYPE:
|
q_proj |
The projection layer for the query tensor.
TYPE:
|
k_proj |
The projection layer for the key tensor.
TYPE:
|
v_proj |
The projection layer for the value tensor.
TYPE:
|
dropout |
The dropout layer applied to the attention probabilities.
TYPE:
|
position_embedding_type |
The type of position embedding used in the attention mechanism.
TYPE:
|
distance_embedding |
The embedding layer for computing relative positions in the attention scores.
TYPE:
|
is_decoder |
Whether the self-attention mechanism is used in a decoder module.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
transpose_for_scores |
Reshapes the input tensor for calculating attention scores. |
forward |
Constructs the self-attention mechanism by calculating attention scores and producing the context layer. |
Example
>>> config = ErnieConfig(hidden_size=768, num_attention_heads=12, attention_probs_dropout_prob=0.1)
>>> self_attention = ErnieMSelfAttention(config)
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMSelfAttention.__init__(config, position_embedding_type=None)
¶
Initializes the ErnieMSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An object containing configuration parameters for the self-attention mechanism.
TYPE:
|
position_embedding_type |
The type of position embedding to use. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the hidden size is not a multiple of the number of attention heads. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMSelfAttention.forward(hidden_states, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_value=None, output_attentions=False)
¶
This method forwards the self-attention mechanism for the ErnieMSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_states |
The input tensor representing the hidden states.
TYPE:
|
attention_mask |
Optional tensor for masking attention scores. Defaults to None.
TYPE:
|
head_mask |
Optional tensor for masking attention heads. Defaults to None.
TYPE:
|
encoder_hidden_states |
Optional tensor representing hidden states from an encoder. Defaults to None.
TYPE:
|
encoder_attention_mask |
Optional tensor for masking encoder attention scores. Defaults to None.
TYPE:
|
past_key_value |
Optional tuple of past key and value tensors. Defaults to None.
TYPE:
|
output_attentions |
Flag indicating whether to output attentions. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor]
|
Tuple[mindspore.Tensor]: A tuple containing the context layer tensor and optionally the attention probabilities tensor. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input tensor shapes are incompatible for matrix multiplication. |
ValueError
|
If the position_embedding_type specified is not supported. |
RuntimeError
|
If there is an issue with applying softmax or dropout operations. |
RuntimeError
|
If there is an issue with reshaping the context layer tensor. |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.ErnieMSelfAttention.transpose_for_scores(x)
¶
Transposes the input tensor for calculating attention scores in the ErnieMSelfAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the ErnieMSelfAttention class.
TYPE:
|
x |
The input tensor to be transposed. It should have a shape of (batch_size, sequence_length, hidden_size).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The transposed tensor with shape (batch_size, num_attention_heads, sequence_length, attention_head_size). |
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.UIEM
¶
Bases: ErnieMForInformationExtraction
UIEM model
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 |
|
mindnlp.transformers.models.ernie_m.modeling_ernie_m.UIEM.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=True)
¶
PARAMETER | DESCRIPTION |
---|---|
start_positions |
Labels for position (index) for computing the start_positions loss. Position outside of the sequence are not taken into account for computing the loss.
TYPE:
|
end_positions |
Labels for position (index) for computing the end_positions loss. Position outside of the sequence are not taken into account for computing the loss.
TYPE:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_ernie_m.py
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m
¶
MindSpore ErnieM model.
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMAttention
¶
Bases: Module
This class represents an attention module for MSErnieM model, which includes self-attention mechanism and projection layers. It inherits from nn.Module and provides methods to initialize the attention module, prune attention heads, and perform attention computation. The attention module consists of self-attention mechanism with configurable position embedding type and projection layers for output transformation. The 'prune_heads' method allows pruning specific attention heads based on provided indices. The 'forward' method computes the attention output given input hidden states, optional masks, and other optional inputs.
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMAttention.__init__(config, position_embedding_type=None)
¶
Initializes an instance of the MSErnieMAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object that contains the configuration settings for the attention layer.
TYPE:
|
position_embedding_type |
The type of position embedding to use. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMAttention.forward(hidden_states, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_value=None, output_attentions=False)
¶
Constructs the MSErnieMAttention module.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MSErnieMAttention class.
TYPE:
|
hidden_states |
The input hidden states of the model. Shape: (batch_size, seq_length, hidden_size).
TYPE:
|
attention_mask |
The attention mask tensor, indicating which tokens should be attended to and which should not. Shape: (batch_size, seq_length). Defaults to None.
TYPE:
|
head_mask |
The head mask tensor, indicating which heads should be masked out. Shape: (num_heads, seq_length, seq_length). Defaults to None.
TYPE:
|
encoder_hidden_states |
The hidden states of the encoder. Shape: (batch_size, seq_length, hidden_size). Defaults to None.
TYPE:
|
encoder_attention_mask |
The attention mask tensor for the encoder, indicating which tokens should be attended to and which should not. Shape: (batch_size, seq_length). Defaults to None.
TYPE:
|
past_key_value |
The tuple of past key and value tensors for keeping the previous attention weights. Shape: ((batch_size, num_heads, seq_length, hidden_size), (batch_size, num_heads, seq_length, hidden_size)). Defaults to None.
TYPE:
|
output_attentions |
Whether to output attention weights. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor]
|
Tuple[mindspore.Tensor]: A tuple containing the attention output tensor and other optional outputs. |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMAttention.prune_heads(heads)
¶
This method 'prune_heads' in the class 'MSErnieMAttention' prunes heads from the attention mechanism.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
heads |
A list of integers representing the indices of heads to be pruned from the attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return anything explicitly, as it operates by mutating the internal state of the class. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of the 'heads' list is equal to 0. |
TypeError
|
If the 'heads' parameter is not a list of integers. |
IndexError
|
If the indices in 'heads' exceed the available attention heads in the mechanism. |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEmbeddings
¶
Bases: Module
Construct the embeddings from word and position embeddings.
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEmbeddings.__init__(config)
¶
Initializes an instance of the MSErnieMEmbeddings class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
A configuration object containing various parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEmbeddings.forward(input_ids=None, position_ids=None, inputs_embeds=None, past_key_values_length=0)
¶
Constructs the embeddings for MSErnieM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The MSErnieMEmbeddings instance.
TYPE:
|
input_ids |
The input tensor containing the indices of input tokens. Default is None.
TYPE:
|
position_ids |
The input tensor containing the indices of position tokens. Default is None.
TYPE:
|
inputs_embeds |
The input tensor containing the embeddings of input tokens. Default is None.
TYPE:
|
past_key_values_length |
The length of past key values. Default is 0.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The forwarded embeddings tensor. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input_ids and inputs_embeds are both None. |
ValueError
|
If the input_shape is invalid for position_ids calculation. |
ValueError
|
If past_key_values_length is negative. |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEncoder
¶
Bases: Module
This class represents an MSErnieMEncoder, which is a multi-layer transformer-based encoder model for natural language processing tasks.
The MSErnieMEncoder inherits from the nn.Module class and is designed to process input embeddings and generate hidden states, attentions, and last hidden state output.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object that contains the model's hyperparameters and settings.
TYPE:
|
layers |
A list of MSErnieMEncoderLayer instances that make up the layers of the encoder.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new MSErnieMEncoder instance with the given configuration. |
forward |
Constructs the MSErnieMEncoder model by processing the input embeddings and generating the desired outputs. Args:
Returns:
|
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEncoder.__init__(config)
¶
Initializes the MSErnieMEncoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An object containing the configuration parameters for the MSErnieMEncoder. The config object should have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/ernie_m/modeling_graph_ernie_m.py
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
|
mindnlp.transformers.models.ernie_m.modeling_graph_ernie_m.MSErnieMEncoder.forward(input_embeds, attention_mask=None, head_mask=None, past_key_values=None, output_attentions=False, output_hidden_states=False)
¶
This method forwards the MSErnieMEncoder by processing the input embeddings and applying attention masks and head masks if provided.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MSErnieMEncoder class.
|
input_embeds |
The input embeddings to be processed by the encoder.
TYPE:
|
attention_mask |
An optional tensor representing the attention mask. If provided, it restricts the attention of the encoder.
TYPE:
|
head_mask |
An optional tensor representing the head mask. If provided, it restricts the attention heads of the encoder.
TYPE:
|
past_key_values |
An optional tuple of past key values, if provided, it allows the encoder to reuse previously computed key value states.
TYPE:
|
output_attentions |
An optional boolean indicating whether to output attentions. Default is False.
TYPE:
|