phi3
mindnlp.transformers.models.phi3.configuration_phi3
¶
Phi-3 model configuration
mindnlp.transformers.models.phi3.configuration_phi3.Phi3Config
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [Phi3Model
]. It is used to instantiate a Phi-3
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
microsoft/Phi-3-mini-4k-instruct.
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 the Phi-3 model. Defines the number of different tokens that can be represented by the
TYPE:
|
hidden_size |
Dimension of the hidden representations.
TYPE:
|
intermediate_size |
Dimension of the MLP representations.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer decoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer decoder.
TYPE:
|
num_key_value_heads |
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
TYPE:
|
resid_pdrop |
Dropout probability for mlp outputs.
TYPE:
|
embd_pdrop |
The dropout ratio for the embeddings.
TYPE:
|
attention_dropout |
The dropout ratio after computing the attention scores.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the decoder.
TYPE:
|
max_position_embeddings |
The maximum sequence length that this model might ever be used with.
TYPE:
|
original_max_position_embeddings |
The maximum sequence length that this model was trained with. This is used to determine the size of the original RoPE embeddings when using long scaling.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
rms_norm_eps |
The epsilon value used for the RMSNorm.
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if
TYPE:
|
tie_word_embeddings |
Whether to tie weight embeddings
TYPE:
|
rope_theta |
The base period of the RoPE embeddings.
TYPE:
|
rope_scaling |
The scaling strategy for the RoPE embeddings. If
TYPE:
|
bos_token_id |
The id of the "beginning-of-sequence" token.
TYPE:
|
eos_token_id |
The id of the "end-of-sequence" token.
TYPE:
|
pad_token_id |
The id of the padding token.
TYPE:
|
sliding_window |
Sliding window attention window size. If
TYPE:
|
Example
>>> from transformers import Phi3Model, Phi3Config
...
>>> # Initializing a Phi-3 style configuration
>>> configuration = Phi3Config.from_pretrained("microsoft/Phi-3-mini-4k-instruct")
...
>>> # Initializing a model from the configuration
>>> model = Phi3Model(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/phi3/configuration_phi3.py
30 31 32 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 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 |
|
mindnlp.transformers.models.phi3.configuration_phi3.Phi3Config.__init__(vocab_size=32064, hidden_size=3072, intermediate_size=8192, num_hidden_layers=32, num_attention_heads=32, num_key_value_heads=None, resid_pdrop=0.0, embd_pdrop=0.0, attention_dropout=0.0, hidden_act='silu', max_position_embeddings=4096, original_max_position_embeddings=4096, initializer_range=0.02, rms_norm_eps=1e-05, use_cache=True, tie_word_embeddings=False, rope_theta=10000.0, rope_scaling=None, bos_token_id=1, eos_token_id=32000, pad_token_id=32000, sliding_window=None, **kwargs)
¶
This method initializes an instance of the Phi3Config class with the provided parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Default is 32064.
TYPE:
|
hidden_size |
The size of the hidden layers in the model. Default is 3072.
TYPE:
|
intermediate_size |
The size of the intermediate layers in the model. Default is 8192.
TYPE:
|
num_hidden_layers |
The number of hidden layers in the model. Default is 32.
TYPE:
|
num_attention_heads |
The number of attention heads. Default is 32.
TYPE:
|
num_key_value_heads |
The number of key and value heads. If None, it defaults to num_attention_heads.
TYPE:
|
resid_pdrop |
The dropout probability for residual connections. Default is 0.0.
TYPE:
|
embd_pdrop |
The dropout probability for the embeddings. Default is 0.0.
TYPE:
|
attention_dropout |
The dropout probability for attention layers. Default is 0.0.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Default is 'silu'.
TYPE:
|
max_position_embeddings |
The maximum position embeddings. Default is 4096.
TYPE:
|
original_max_position_embeddings |
The original maximum position embeddings. Default is 4096.
TYPE:
|
initializer_range |
The range for parameter initializations. Default is 0.02.
TYPE:
|
rms_norm_eps |
The epsilon value for RMS normalization. Default is 1e-05.
TYPE:
|
use_cache |
Indicates whether caching is used. Default is True.
TYPE:
|
tie_word_embeddings |
Indicates whether word embeddings are tied. Default is False.
TYPE:
|
rope_theta |
The theta value for ROPE. Default is 10000.0.
TYPE:
|
rope_scaling |
The scaling factor for ROPE.
TYPE:
|
bos_token_id |
The beginning of sequence token id. Default is 1.
TYPE:
|
eos_token_id |
The end of sequence token id. Default is 32000.
TYPE:
|
pad_token_id |
The padding token id. Default is 32000.
TYPE:
|
sliding_window |
Not specified.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If rope_scaling is provided without rope_theta or vice versa. |
TypeError
|
If any of the input parameters have an unexpected type. |
Source code in mindnlp/transformers/models/phi3/configuration_phi3.py
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 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3
¶
MindSpore Phi-3 model.
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Attention
¶
Bases: Module
Multi-headed attention from 'Attention Is All You Need' paper
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 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 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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Attention.__init__(config, layer_idx=None)
¶
Initializes an instance of the Phi3Attention
class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the
|
config |
An instance of the
TYPE:
|
layer_idx |
The index of the layer. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Notes
- Instantiating
Phi3Attention
without passing alayer_idx
is not recommended and may lead to errors during the forward call if caching is used. It is advised to provide alayer_idx
when creating this class. -
The
Phi3Attention
class expectshidden_size
to be divisible bynum_heads
. -
The following attributes are initialized within the
__init__
method:self.config
: An instance of thePhi3Config
class containing the configuration settings.self.layer_idx
: The index of the layer.self.attention_dropout
: The dropout rate for attention.self.hidden_size
: The hidden size of the layer.self.num_heads
: The number of attention heads.self.head_dim
: The dimension of each attention head.self.num_key_value_heads
: The number of key-value attention heads.self.num_key_value_groups
: The number of groups formed by key-value attention heads.self.max_position_embeddings
: The maximum number of position embeddings.self.original_max_position_embeddings
: The original maximum number of position embeddings.self.rope_theta
: The theta value for relative position encoding.self.rope_scaling
: The scaling factor for relative position encoding.self.is_causal
: A boolean indicating if the attention is causal.self.o_proj
: A fully connected layer for projecting the output.self.qkv_proj
: A fully connected layer for projecting the queries, keys, and values.self._init_rope()
: A private method for initializing the relative position encoding.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Attention.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False)
¶
This method forwards the Phi3Attention mechanism.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3Attention class.
|
hidden_states |
The input hidden states tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
An optional mask tensor of shape (batch_size, 1, sequence_length, sequence_length) to mask some positions in the input.
TYPE:
|
position_ids |
An optional tensor of shape (batch_size, sequence_length) containing the position indices.
TYPE:
|
past_key_value |
An optional cache storing the past key and value states for efficient auto-regressive decoding.
TYPE:
|
output_attentions |
A flag indicating whether to output the attention weights.
TYPE:
|
use_cache |
A flag indicating whether to use the cache for storing key and value states.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, Optional[Tensor], Optional[Tuple[Tensor]]]
|
Tuple[mindspore.Tensor, Optional[mindspore.Tensor], Optional[Tuple[mindspore.Tensor]]]: A tuple containing the attention output tensor of shape (batch_size, sequence_length, hidden_size), optionally the attention weights tensor, and optionally the updated past key and value states. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raised if the cache structure has changed, if the attention weights or mask tensors have incorrect shapes, or if the output tensors have unexpected shapes. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 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 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3DecoderLayer
¶
Bases: Module
Phi3DecoderLayer represents a single layer of the Phi3 decoder. This layer includes self-attention, residual connections, layer normalization, and a multi-layer perceptron (MLP) sublayer.
This class inherits from the nn.Module class and is designed to be used as a building block for forwarding Phi3 decoder models.
The init method initializes the Phi3DecoderLayer with the provided configuration and layer index. It sets up the self-attention mechanism, MLP, input layer normalization, and dropout layers.
The forward method processes the input hidden states through the layer. It applies input layer normalization, self-attention, residual connections, post-attention layer normalization, and the MLP sublayer. The method also handles optional arguments such as attention_mask, position_ids, past_key_value, output_attentions, and use_cache, and returns the resulting hidden states along with optional outputs based on the provided arguments.
Note
The forward method also issues a warning if the 'padding_mask' argument is used, as it is deprecated and will be removed in a future version in favor of 'attention_mask'.
PARAMETER | DESCRIPTION |
---|---|
hidden_states |
Input to the layer of shape
TYPE:
|
attention_mask |
Attention mask of size
TYPE:
|
position_ids |
Indices of positions of each input sequence tokens in the position embeddings. Selected in the range
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
use_cache |
If set to
TYPE:
|
past_key_value |
Cached past key and value projection states
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[mindspore.Tensor, Optional[Tuple[mindspore.Tensor, mindspore.Tensor]]]: The resulting hidden states, and optionally, the self-attention weights and present key-value states if requested. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
794 795 796 797 798 799 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3DecoderLayer.__init__(config, layer_idx)
¶
Initializes a new instance of the Phi3DecoderLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the Phi3DecoderLayer class.
TYPE:
|
config |
The configuration object containing parameters for the decoder layer.
TYPE:
|
layer_idx |
The index of the decoder layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Description
This method initializes the Phi3DecoderLayer object with the provided configuration and layer index. It sets up the self-attention mechanism, multi-layer perceptron, input layer normalization, and other components required for the decoder layer.
-
config: The Phi3Config object that contains the configuration parameters for the decoder layer. This includes parameters such as hidden size, dropout rate, and RMS normalization epsilon.
-
layer_idx: An integer representing the index of the decoder layer. This index is used to identify the layer and is required for initializing the self-attention mechanism.
The method does not return any value.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3DecoderLayer.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False, **kwargs)
¶
Constructs a Phi3DecoderLayer object.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
hidden_states |
Input to the layer of shape
TYPE:
|
attention_mask |
Attention mask of size
TYPE:
|
position_ids |
Indices of positions of each input sequence tokens in the
position embeddings. Selected in the range
TYPE:
|
past_key_value |
Cached past key and value projection states. (default: None)
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers.
See
TYPE:
|
use_cache |
If set to True,
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, Optional[Tuple[Tensor, Tensor]]]
|
Tuple[mindspore.Tensor, Optional[Tuple[mindspore.Tensor, mindspore.Tensor]]]: A tuple containing the
hidden states of shape |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM
¶
Bases: Phi3PreTrainedModel
A class representing the Phi3 model for causal language modeling.
This class extends the Phi3PreTrainedModel class and provides methods for initializing the model, setting and getting input and output embeddings, setting and getting the decoder, forwarding the model, and preparing inputs for generation.
ATTRIBUTE | DESCRIPTION |
---|---|
model |
The Phi3 model.
TYPE:
|
vocab_size |
The size of the vocabulary.
TYPE:
|
lm_head |
The language model head.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the Phi3ForCausalLM instance. |
get_input_embeddings |
Returns the input embeddings. |
set_input_embeddings |
Sets the input embeddings. |
get_output_embeddings |
Returns the output embeddings. |
set_output_embeddings |
Sets the output embeddings. |
set_decoder |
Sets the decoder. |
get_decoder |
Returns the decoder. |
forward |
Constructs the model and returns the output. |
prepare_inputs_for_generation |
Prepares inputs for generation. |
_reorder_cache |
Reorders the cache based on the beam index. |
Example
>>> from transformers import AutoTokenizer, Phi3ForCausalLM
...
>>> model = Phi3ForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")
>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
...
>>> prompt = "This is an example script ."
>>> inputs = tokenizer(prompt, return_tensors="pt")
...
>>> # Generate
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'This is an example script .\n Certainly! Below is a sample script that demonstrates a simple task, such as calculating the sum'
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 1349 1350 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 1480 1481 1482 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 1613 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.__init__(config)
¶
Initializes a new instance of the Phi3ForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
A configuration object of type Config, containing the necessary parameters for model initialization.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the masked language modeling loss. Indices should either be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, CausalLMOutputWithPast]
|
Union[Tuple, CausalLMOutputWithPast] |
Example
>>> from transformers import AutoTokenizer, Phi3ForCausalLM
...
>>> model = Phi3ForCausalLM.from_pretrained("microsoft/phi-3-mini-4k-instruct")
>>> tokenizer = AutoTokenizer.from_pretrained("microsoft/phi-3-mini-4k-instruct")
...
>>> prompt = "This is an example script ."
>>> inputs = tokenizer(prompt, return_tensors="pt")
...
>>> # Generate
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
'This is an example script .\n Certainly! Below is a sample script that demonstrates a simple task, such as calculating the sum'
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1480 1481 1482 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.get_decoder()
¶
This method returns the decoder model used in the Phi3ForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3ForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
model
|
The decoder model associated with the Phi3ForCausalLM instance. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.get_input_embeddings()
¶
Method to retrieve the input embeddings from the Phi3ForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the Phi3ForCausalLM class. This parameter represents the current instance of the Phi3ForCausalLM class. It is used to access the model's embed_tokens attribute.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it directly returns the embed_tokens attribute of the model. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.get_output_embeddings()
¶
Returns the output embeddings of the Phi3ForCausalLM model.
This method takes no additional parameters.
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs)
¶
This method prepares inputs for generation in the Phi3ForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of Phi3ForCausalLM.
TYPE:
|
input_ids |
The input tensor containing token indices for the input sequence.
TYPE:
|
past_key_values |
A cache of past key values or the tuple of past key and value tensors. Defaults to None.
TYPE:
|
attention_mask |
An optional tensor containing attention mask values for the input sequence.
TYPE:
|
inputs_embeds |
An optional tensor containing the embedded inputs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
model_inputs
|
A dictionary containing the model inputs with the following keys:
TYPE:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the dimensions of the input tensors are incompatible. |
TypeError
|
If the input types are invalid or incompatible. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 1613 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.set_decoder(decoder)
¶
Sets the decoder for the Phi3ForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of Phi3ForCausalLM class.
TYPE:
|
decoder |
The decoder object to be set for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.set_input_embeddings(value)
¶
Method to set input embeddings for the Phi3ForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3ForCausalLM class.
TYPE:
|
value |
The input embeddings to be set for the model. Should be compatible with the model's embed_tokens attribute.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForCausalLM.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings of the Phi3ForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3ForCausalLM class.
TYPE:
|
new_embeddings |
The new embeddings to be set for the model's output. It can be of any type.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForSequenceClassification
¶
Bases: Phi3PreTrainedModel
This class represents a Phi3 model for sequence classification. It is a subclass of the Phi3PreTrainedModel class.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for sequence classification.
TYPE:
|
model |
The Phi3 model for sequence classification.
TYPE:
|
score |
The dense layer for scoring the hidden states.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new instance of the Phi3ForSequenceClassification class. |
get_input_embeddings |
Retrieves the input embeddings from the Phi3 model. |
set_input_embeddings |
Sets the input embeddings for the Phi3 model. |
forward |
Constructs the Phi3 model for sequence classification. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForSequenceClassification.__init__(config)
¶
Initializes a new instance of the Phi3ForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the Phi3ForSequenceClassification class. |
config |
An object containing configuration settings for the model. It should have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForSequenceClassification.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForSequenceClassification.get_input_embeddings()
¶
Retrieves the input embeddings from the Phi3ForSequenceClassification model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the Phi3ForSequenceClassification class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Description
This method is used to extract the input embeddings from the Phi3ForSequenceClassification model. The input embeddings represent the learned representations of the input tokens in the model.
This method takes one parameter 'self', which refers to the current instance of the Phi3ForSequenceClassification class. It is required to access the model and its embedded tokens.
Example
>>> model = Phi3ForSequenceClassification()
>>> input_embeddings = model.get_input_embeddings()
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForSequenceClassification.set_input_embeddings(value)
¶
Set the input embeddings for the Phi3ForSequenceClassification model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of Phi3ForSequenceClassification. |
value |
The input embeddings to be set for the model. Should be a tensor of shape (vocab_size, embedding_dim).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForTokenClassification
¶
Bases: Phi3PreTrainedModel
Phi3ForTokenClassification is a class that represents a token classification model for Phi3, inheriting from Phi3PreTrainedModel.
The class includes an init method for initializing the model with a Phi3Config object, setting up the necessary components such as the model architecture, dropout layers, and classifier for token classification.
It also contains a forward method for performing the token classification task, taking input tensors, past key values, attention masks, and other optional arguments. It computes the classification loss using cross-entropy and returns the loss along with logits and hidden states if specified in the return_dict.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for token classification.
|
model |
The Phi3Model instance for processing inputs.
|
dropout |
Dropout layer for regularization.
|
classifier |
Dense layer for classification.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Constructor method to initialize the Phi3ForTokenClassification instance. |
forward |
Method for performing token classification task using the model. |
Note
Ensure to set the appropriate labels for computing the loss, and handle the return_dict parameter for controlling the output format.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForTokenClassification.__init__(config)
¶
Initializes an instance of Phi3ForTokenClassification with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3ForTokenClassification class.
|
config |
An instance of Phi3Config containing the configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type Phi3Config. |
AttributeError
|
If the config object does not have the required attributes. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3ForTokenClassification.forward(input_ids=None, past_key_values=None, attention_mask=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None, **deprecated_arguments)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3MLP
¶
Bases: Module
This class represents a multi-layer perceptron (MLP) module with a Phi3 activation function. It inherits from the nn.Module class.
The Phi3MLP module is used for processing hidden states in a neural network. It consists of an up projection layer, a gate activation function, and a down projection layer.
ATTRIBUTE | DESCRIPTION |
---|---|
config |
An object containing configuration settings for the module.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a Phi3MLP instance. Args:
|
forward |
Constructs the Phi3MLP module. Args:
Returns:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
457 458 459 460 461 462 463 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3MLP.__init__(config)
¶
Initializes an instance of the Phi3MLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3MLP class.
|
config |
An object containing configuration settings for the Phi3MLP model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3MLP.forward(hidden_states)
¶
This method forwards and processes the hidden states using the Phi3MLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3MLP class.
TYPE:
|
hidden_states |
The input tensor containing the hidden states to be processed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The processed tensor representing the output of the method. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Model
¶
Bases: Phi3PreTrainedModel
Transformer decoder consisting of config.num_hidden_layers layers. Each layer is a [Phi3DecoderLayer
]
PARAMETER | DESCRIPTION |
---|---|
config |
Phi3Config
TYPE:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 1121 1122 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 1236 1237 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Model.__init__(config)
¶
Initializes a new instance of the Phi3Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
The configuration object for Phi3Model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Description
This method initializes a new instance of the Phi3Model class. It takes in a configuration object, 'config', which is of type Phi3Config. The 'config' parameter contains various settings and hyperparameters for the model.
The method performs the following steps:
- Calls the init method of the parent class (super().init(config)) to initialize the parent class with the provided configuration.
- Sets the 'padding_idx' attribute to the 'pad_token_id' value from the 'config' object. This value represents the padding token index in the vocabulary.
- Sets the 'vocab_size' attribute to the 'vocab_size' value from the 'config' object. This value represents the size of the vocabulary.
- Initializes the 'embed_tokens' attribute as an instance of the nn.Embedding class. It takes the 'vocab_size', 'hidden_size', and 'padding_idx' values from the 'config' object as parameters. This embedding layer is responsible for converting input tokens to their corresponding embeddings.
- Initializes the 'embed_dropout' attribute as an instance of the nn.Dropout class. It takes the 'embd_pdrop' value from the 'config' object as a parameter. This dropout layer is applied to the embeddings.
- Initializes the 'layers' attribute as an instance of the nn.ModuleList class. It contains Phi3DecoderLayer instances, one for each layer index from 0 to 'num_hidden_layers' - 1 (inclusive). Each Phi3DecoderLayer is initialized with the 'config' object and the corresponding layer index.
- Sets the '_attn_implementation' attribute to the '_attn_implementation' value from the 'config' object. This value represents the implementation type of the attention mechanism.
- Initializes the 'norm' attribute as an instance of the Phi3RMSNorm class. It takes the 'hidden_size' and 'eps' values from the 'config' object as parameters. This layer applies root mean square normalization to the hidden states.
- Sets the 'gradient_checkpointing' attribute to False. This attribute determines whether gradient checkpointing is enabled during training.
- Calls the 'post_init' method, which can be overridden by subclasses to perform additional initialization steps.
Note
This method is called automatically when creating a new instance of the Phi3Model class.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Model.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs the Phi3Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
input_ids |
The input tensor of shape (batch_size, seq_length). Defaults to None.
TYPE:
|
attention_mask |
The attention mask tensor of shape (batch_size, seq_length). Defaults to None.
TYPE:
|
position_ids |
The position ids tensor of shape (batch_size, seq_length). Defaults to None.
TYPE:
|
past_key_values |
List of past key value tensors. Defaults to None.
TYPE:
|
inputs_embeds |
The input embeddings tensor of shape (batch_size, seq_length). Defaults to None.
TYPE:
|
use_cache |
Whether to use cache. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attentions. Defaults to None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Defaults to None.
TYPE:
|
return_dict |
Whether to return a dictionary. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPast]
|
Union[Tuple, BaseModelOutputWithPast]: The output of the Phi3Model. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both input_ids and inputs_embeds are specified. |
ValueError
|
If neither input_ids nor inputs_embeds are specified. |
ValueError
|
If attempting to perform batched generation with padding_side='right' in flash_attention_2 implementation. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 1236 1237 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Model.get_input_embeddings()
¶
This method retrieves the input embeddings for the Phi3Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3Model class.
|
RETURNS | DESCRIPTION |
---|---|
embed_tokens
|
The method returns the input embeddings stored in the 'embed_tokens' attribute of the Phi3Model instance. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3Model.set_input_embeddings(value)
¶
Sets the input embeddings for the Phi3Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3Model class.
|
value |
A tensor representing the input embeddings. It should have a shape of (batch_size, sequence_length, embedding_dim).
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3PreTrainedModel
¶
Bases: PreTrainedModel
This class represents a Phi3PreTrainedModel, which is a subclass of PreTrainedModel.
Phi3PreTrainedModel inherits the following methods from PreTrainedModel:
-
forward(input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None): This method performs the forward pass for the Phi3PreTrainedModel. It takes input_ids as input and returns the model's output.
-
save_pretrained(save_directory): This method saves the model's weights and configuration to the specified directory.
-
from_pretrained(pretrained_model_name_or_path, *model_args, **kwargs): This method loads the pretrained model from the specified path or model name. Additional arguments can be passed to customize the loading process.
-
config_class: This attribute holds the configuration class of the model.
-
base_model_prefix: This attribute holds the prefix used by the model's modules.
The Phi3PreTrainedModel class introduces the following methods:
- _init_weights: This method initializes the weights for the given module. If the module is of type nn.Linear, the weight is initialized using the Normal distribution with a standard deviation of self.config.initializer_range. If the module has a bias, it is initialized with zeros. If the module is of type nn.Embedding, the weight is randomly initialized using the Normal distribution with a standard deviation of self.config.initializer_range. If the module has a padding index, the weight at the padding index is set to zero.
Note
This class does not provide an implementation for the forward method. The implementation should be provided by subclasses that inherit from Phi3PreTrainedModel.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
967 968 969 970 971 972 973 974 975 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RMSNorm
¶
Bases: Module
Phi3RMSNorm is a custom normalization layer that performs the Phi3 RMS normalization, equivalent to T5LayerNorm.
This class inherits from the nn.Module class in the MindSpore framework.
ATTRIBUTE | DESCRIPTION |
---|---|
weight |
The weight parameter for the normalization layer.
TYPE:
|
variance_epsilon |
A small value added to the variance to avoid division by zero.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a new instance of the Phi3RMSNorm class. |
forward |
Applies Phi3 RMS normalization to the input hidden_states. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RMSNorm.__init__(hidden_size, eps=1e-06)
¶
Phi3RMSNorm is equivalent to T5LayerNorm
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
75 76 77 78 79 80 81 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RMSNorm.forward(hidden_states)
¶
This method forwards Phi3RMSNorm by performing normalization on the hidden_states.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the Phi3RMSNorm class.
|
hidden_states |
A tensor containing the hidden states to be normalized. It should be of type 'Tensor' and compatible with the operations performed in the method.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RotaryEmbedding
¶
Bases: Module
This class represents the Phi3RotaryEmbedding, a rotary positional embedding layer used in neural network models. It is a subclass of nn.Module.
The Phi3RotaryEmbedding class provides methods for forwarding rotary embeddings based on input tensors and position IDs. It utilizes cosine and sine functions to generate embeddings with rotational properties.
ATTRIBUTE | DESCRIPTION |
---|---|
dim |
The dimension of the embeddings.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings.
TYPE:
|
base |
The base value for calculating inverse frequencies.
TYPE:
|
inv_freq |
The inverse frequencies calculated based on the dimension and base values.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Constructs rotary embeddings based on the input tensor and position IDs. Args:
Returns:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RotaryEmbedding.__init__(dim, max_position_embeddings=2048, base=10000)
¶
Initializes a new instance of the Phi3RotaryEmbedding class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
dim |
The dimension of the embedding.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings allowed (default is 2048).
TYPE:
|
base |
The base value for calculations (default is 10000).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3RotaryEmbedding.forward(x, position_ids, seq_len=None)
¶
This method forwards the rotary embedding for the Phi3RotaryEmbedding class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3RotaryEmbedding class.
TYPE:
|
x |
The input tensor for which the rotary embedding is being forwarded.
TYPE:
|
position_ids |
The tensor containing the position IDs.
TYPE:
|
seq_len |
The length of the sequence. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, Tensor]: Returns a tuple containing the cosine and sine values of the forwarded rotary embedding. Both tensors have the same shape as the input tensor x. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of the position_ids tensor does not match the sequence length. |
TypeError
|
If the input parameters are not of the expected types. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3SuScaledRotaryEmbedding
¶
Bases: Phi3RotaryEmbedding
The Phi3SuScaledRotaryEmbedding
class represents a specialized implementation of the Phi3RotaryEmbedding
class,
which provides functionalities for forwarding a scaled rotary embedding for a given input tensor.
ATTRIBUTE | DESCRIPTION |
---|---|
`dim` |
The dimensionality of the input tensor.
TYPE:
|
`config` |
The configuration object containing various parameters.
TYPE:
|
`short_factor` |
The scaling factor for short sequences.
TYPE:
|
`long_factor` |
The scaling factor for long sequences.
TYPE:
|
`original_max_position_embeddings` |
The maximum number of positions in the original input tensor.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
`__init__` |
Initializes the Args:
|
`forward` |
Constructs the scaled rotary embedding. Args:
Returns:
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3SuScaledRotaryEmbedding.__init__(dim, config)
¶
Initializes an instance of the Phi3SuScaledRotaryEmbedding class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class. |
dim |
The dimension of the embedding.
TYPE:
|
config |
The configuration object containing various settings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3SuScaledRotaryEmbedding.forward(x, position_ids, seq_len=None)
¶
Constructs the scaled rotary embedding for the Phi3SuScaledRotaryEmbedding.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
x |
The input tensor for which the scaled rotary embedding is forwarded.
TYPE:
|
position_ids |
The position indices for the input tensor.
TYPE:
|
seq_len |
The length of the sequence. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor, Tensor]: A tuple of tensors containing the cosine and sine of the scaled rotary embedding. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the sequence length exceeds the original maximum position embeddings. |
TypeError
|
If the input tensors are not of the expected data type. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3YarnScaledRotaryEmbedding
¶
Bases: Phi3RotaryEmbedding
This class represents the Phi3YarnScaledRotaryEmbedding, a subclass of Phi3RotaryEmbedding. It provides methods for forwarding scaled rotary embeddings for Phi3Yarn models.
ATTRIBUTE | DESCRIPTION |
---|---|
dim |
The dimension of the embeddings.
TYPE:
|
config |
The configuration object containing various parameters.
TYPE:
|
short_factor |
The scaling factor for short sequences.
TYPE:
|
long_factor |
The scaling factor for long sequences.
TYPE:
|
original_max_position_embeddings |
The original maximum position embeddings.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a Phi3YarnScaledRotaryEmbedding instance. |
forward |
Constructs the scaled rotary embeddings. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
330 331 332 333 334 335 336 337 338 339 340 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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3YarnScaledRotaryEmbedding.__init__(dim, config)
¶
Initializes a Phi3YarnScaledRotaryEmbedding object with the specified dimension and configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
dim |
The dimension of the embedding space.
TYPE:
|
config |
An object containing configuration parameters including max_position_embeddings, rope_theta, rope_scaling, and original_max_position_embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the 'short_factor' or 'long_factor' keys are missing in the 'rope_scaling' dictionary within the config object. |
TypeError
|
If the 'max_position_embeddings' or 'original_max_position_embeddings' attributes are not present in the config object. |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
|
mindnlp.transformers.models.phi3.modeling_phi3.Phi3YarnScaledRotaryEmbedding.forward(x, position_ids, seq_len=None)
¶
Constructs the Phi3YarnScaledRotaryEmbedding.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the Phi3YarnScaledRotaryEmbedding class.
|
x |
A tensor representing the input data.
|
position_ids |
A tensor containing the position IDs for each element in the input tensor.
|
seq_len |
An optional integer representing the length of the input sequence. If not provided, it is calculated as the maximum value in the position_ids tensor plus one.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.apply_rotary_pos_emb(q, k, cos, sin, position_ids=None, unsqueeze_dim=1)
¶
Applies Rotary Position Embedding to the query and key tensors.
PARAMETER | DESCRIPTION |
---|---|
q |
The query tensor.
TYPE:
|
k |
The key tensor.
TYPE:
|
cos |
The cosine part of the rotary embedding.
TYPE:
|
sin |
The sine part of the rotary embedding.
TYPE:
|
position_ids |
Deprecated and unused.
TYPE:
|
unsqueeze_dim |
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
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 |
|
mindnlp.transformers.models.phi3.modeling_phi3.repeat_kv(hidden_states, n_rep)
¶
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
538 539 540 541 542 543 544 545 546 547 |
|
mindnlp.transformers.models.phi3.modeling_phi3.rotate_half(x)
¶
Rotates half the hidden dims of the input.
Source code in mindnlp/transformers/models/phi3/modeling_phi3.py
419 420 421 422 423 424 |
|