gpt_pangu
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu
¶
MindSpore PanguAlpha GPT2 Model
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguAttention
¶
Bases: Module
Represents the GPTPanguAttention class, which inherits from nn.Module. This class contains methods for attention mechanism used in GPT (Generative Pre-trained Transformer) models.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPTPanguAttention instance with the given configuration. |
_attn |
Computes the attention mechanism using the query, key, and value tensors, with optional attention and head masks. |
_split_heads |
Splits the hidden_size dimension of the given tensor into attn_head_size and num_heads. |
_merge_heads |
Merges attn_head_size dimension and num_attn_heads dimension into hidden_size. |
forward |
Constructs the attention mechanism using the provided hidden_states and optional past layers, masks, custom query, cache usage, and attention output flag. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 247 248 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguAttention.__init__(config)
¶
Initializes the GPTPanguAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class itself.
TYPE:
|
config |
An object containing configuration parameters for the attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the embed_dim is not divisible by num_heads, an exception is raised with a detailed error message. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguAttention.forward(hidden_states, layer_past=None, attention_mask=None, head_mask=None, custom_query=None, use_cache=False, output_attentions=False)
¶
Constructs the attention mechanism used in the GPTPangu model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPTPanguAttention class.
TYPE:
|
hidden_states |
The input tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
layer_past |
A tuple containing the past key and value tensors. Defaults to None.
TYPE:
|
attention_mask |
The attention mask tensor of shape (batch_size, sequence_length). Defaults to None.
TYPE:
|
head_mask |
The head mask tensor of shape (num_heads, sequence_length, sequence_length). Defaults to None.
TYPE:
|
custom_query |
The custom query tensor of shape (batch_size, sequence_length, hidden_size). Defaults to None.
TYPE:
|
use_cache |
Whether to use the past key and value tensors. Defaults to False.
TYPE:
|
output_attentions |
Whether to output the attention weights. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the attention output tensor and the present key-value tuple:
|
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguBlock
¶
Bases: Module
This class represents a block of the GPTPangu model, containing layers for attention and feed-forward processing.
PARAMETER | DESCRIPTION |
---|---|
config |
An object containing configuration settings for the GPTPanguBlock.
|
ATTRIBUTE | DESCRIPTION |
---|---|
ln_1 |
Layer normalization module for the first layer.
|
attn |
GPTPanguAttention module for attention processing.
|
ln_2 |
Layer normalization module for the second layer.
|
mlp |
GPTPanguMLP module for feed-forward processing.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPTPanguBlock with the given configuration settings. |
forward |
Constructs the block by processing the input hidden_states through attention and feed-forward layers. |
RETURNS | DESCRIPTION |
---|---|
outputs
|
A tuple containing the final hidden states after processing. |
Inherits from
nn.Module
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguBlock.__init__(config)
¶
Initialize a GPTPanguBlock instance with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguBlock class.
TYPE:
|
config |
The configuration object containing parameters for the block.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguBlock.forward(hidden_states, layer_past=None, attention_mask=None, head_mask=None, custom_query=None, use_cache=False, output_attentions=False)
¶
Constructs the GPTPanguBlock.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_states |
The input hidden states of shape
TYPE:
|
layer_past |
The cached past hidden states of shape
TYPE:
|
attention_mask |
The attention mask of shape
TYPE:
|
head_mask |
The head mask of shape
TYPE:
|
custom_query |
The custom query tensor of shape
TYPE:
|
use_cache |
Whether to use the cache for the hidden states. Default is
TYPE:
|
output_attentions |
Whether to output attentions probabilities. Default is
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[torch.Tensor]: A tuple containing the following:
|
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM
¶
Bases: GPTPanguPreTrainedModel
The GPTPanguForCausalLM class represents a Pangu model for causal language modeling. It inherits from the GPTPanguPreTrainedModel class.
This class includes methods for initializing the model, getting and setting output embeddings, preparing inputs for generation, and generating outputs based on input data. Additionally, it provides a method for re-ordering the past key values cache when using beam search or beam sampling.
The init method initializes the model with a given configuration and sets up the transformer and lm_head layers. The get_output_embeddings and set_output_embeddings methods deal with accessing and modifying the output embeddings for the model. The prepare_inputs_for_generation method prepares input data for generation, considering past key values, attention mask, position ids, and token type ids. The forward method forwards outputs based on input data, including handling labels for language modeling and computing loss.
The _reorder_cache method is a static method used to re-order the past_key_values cache when beam search or beam sample methods are called, ensuring correct alignment with the beam index at each generation step.
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 787 788 789 790 791 792 793 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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM.__init__(config)
¶
Initializes an instance of the GPTPanguForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A configuration object containing settings for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for language modeling. Note that the labels are shifted inside the model, i.e. you can set
DEFAULT:
|
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM.get_output_embeddings()
¶
This method returns the output embeddings of the GPTPanguForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
lm_head
|
This method returns the output embeddings of the model. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
797 798 799 800 801 802 803 804 805 806 807 808 809 810 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM.prepare_inputs_for_generation(input_ids, past=None, **kwargs)
¶
Prepare inputs for generation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguForCausalLM class.
TYPE:
|
input_ids |
The input tensor of token indices representing the sequence.
TYPE:
|
past |
The past key values used for fast decoding.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for generation with the following keys:
|
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguForCausalLM.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the GPTPanguForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguForCausalLM class.
TYPE:
|
new_embeddings |
The new embeddings to set as the output embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguMLP
¶
Bases: Module
GPTPanguMLP represents a multi-layer perceptron (MLP) used in the GPT-Pangu model for processing intermediate hidden states.
This class inherits from nn.Module and contains methods for initializing the MLP layers and processing hidden states through a feedforward neural network.
ATTRIBUTE | DESCRIPTION |
---|---|
c_fc |
Fully connected layer to transform input hidden states.
TYPE:
|
c_proj |
Fully connected layer to project intermediate hidden states back to original embed dimension.
TYPE:
|
act |
Activation function applied to hidden states.
TYPE:
|
dropout |
Dropout layer to add regularization to the model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPTPanguMLP with specified intermediate size and configuration parameters. |
forward |
Processes the input 'hidden_states' through the MLP layers and returns the processed hidden states. |
Example
>>> intermediate_size = 512
>>> config = Configuration(hidden_size=768, activation_function='gelu', resid_pdrop=0.1)
>>> mlp = GPTPanguMLP(intermediate_size, config)
>>> output = mlp.forward(hidden_states)
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguMLP.__init__(intermediate_size, config)
¶
Initializes the GPTPanguMLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
intermediate_size |
The size of the intermediate layer.
TYPE:
|
config |
The configuration object containing hidden_size, activation_function, and resid_pdrop attributes.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguMLP.forward(hidden_states)
¶
This method forwards the hidden states by applying a series of transformations.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguMLP class.
TYPE:
|
hidden_states |
The input hidden states to be processed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value explicitly, as the processed hidden states are modified in place. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguModel
¶
Bases: GPTPanguPreTrainedModel
GPTPanguModel
This class represents a GPT-Pangu model, which is a variant of the GPT (Generative Pre-trained Transformer) model. It is designed for pre-training and fine-tuning on large-scale Chinese text data. The GPTPanguModel class inherits from the GPTPanguPreTrainedModel class.
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimensionality of the embedding layer.
TYPE:
|
wte |
The word/token embedding layer.
TYPE:
|
wpe |
The position embedding layer.
TYPE:
|
wqe |
The query embedding layer.
TYPE:
|
drop |
The dropout layer.
TYPE:
|
h |
The list of GPTPanguBlock layers.
TYPE:
|
ln_f |
The layer normalization layer.
TYPE:
|
gradient_checkpointing |
Whether to use gradient checkpointing.
TYPE:
|
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguModel.__init__(config)
¶
Initializes a new instance of the GPTPanguModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguModel class.
|
config |
A configuration object that contains the settings for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguModel.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs the GPTPanguModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
TYPE:
|
input_ids |
The input tensor of shape (batch_size, sequence_length). It represents the input token IDs. Defaults to None.
TYPE:
|
past_key_values |
The tuple of past key values. Each element in the tuple is a tensor of shape (batch_size, num_heads, sequence_length, hidden_size//num_heads). Defaults to None.
TYPE:
|
attention_mask |
The attention mask tensor of shape (batch_size, sequence_length). It indicates which tokens should be attended to and which ones should not. Defaults to None.
TYPE:
|
token_type_ids |
The token type IDs tensor of shape (batch_size, sequence_length). It represents the token type embeddings. Defaults to None.
TYPE:
|
position_ids |
The position IDs tensor of shape (batch_size, sequence_length). It represents the position embeddings. Defaults to None.
TYPE:
|
head_mask |
The head mask tensor of shape (num_layers, num_heads). It specifies which heads should be masked for each layer. Defaults to None.
TYPE:
|
inputs_embeds |
The input embeddings tensor of shape (batch_size, sequence_length, hidden_size). It represents the input embeddings directly instead of using input_ids. Defaults to None.
TYPE:
|
use_cache |
Whether to use cache for faster decoding. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attention weights. Defaults to None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Defaults to None.
TYPE:
|
return_dict |
Whether to use a dictionary as the return type. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both input_ids and inputs_embeds are provided simultaneously. |
ValueError
|
If neither input_ids nor inputs_embeds are provided. |
ValueError
|
If batch_size is not defined or is less than or equal to 0. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguModel.get_input_embeddings()
¶
Method to retrieve input embeddings from the GPTPanguModel.
PARAMETER | DESCRIPTION |
---|---|
self |
GPTPanguModel instance. The object instance of the GPTPanguModel class.
|
RETURNS | DESCRIPTION |
---|---|
The input embeddings for further processing in the model. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
550 551 552 553 554 555 556 557 558 559 560 561 562 563 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguModel.set_input_embeddings(new_embeddings)
¶
Set the input embeddings for the GPTPanguModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPTPanguModel class.
TYPE:
|
new_embeddings |
The new input embeddings to be set for the model. It should be a tensor or array representing the embeddings.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method updates the input embeddings of the model in-place. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the new_embeddings parameter is not of the correct type. |
ValueError
|
If the new_embeddings parameter is empty or invalid. |
Source code in mindnlp/transformers/models/gpt_pangu/modeling_gptpangu.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 |
|
mindnlp.transformers.models.gpt_pangu.modeling_gptpangu.GPTPanguPreTrainedModel
¶
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/gpt_pangu/modeling_gptpangu.py
455 456 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 |
|
mindnlp.transformers.models.gpt_pangu.configuration_gptpangu
¶
PanGu_Alpha Models config
mindnlp.transformers.models.gpt_pangu.configuration_gptpangu.GPTPanguConfig
¶
Bases: PretrainedConfig
GPTPanguConfig
Source code in mindnlp/transformers/models/gpt_pangu/configuration_gptpangu.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 |
|
mindnlp.transformers.models.gpt_pangu.configuration_gptpangu.GPTPanguConfig.__init__(vocab_size=40000, max_position_embeddings=1024, hidden_size=2560, intermediate_size=None, num_layers=32, num_heads=32, activation_function='gelu', resid_pdrop=0.1, embd_pdrop=0.1, attn_pdrop=0.1, layer_norm_epsilon=1e-05, scale_attn_weights=True, initializer_range=0.02, summary_type='cls_index', summary_use_proj=True, summary_activation=None, summary_proj_to_labels=True, summary_first_dropout=0.1, use_cache=True, bos_token_id=9, eos_token_id=9, **kwargs)
¶
This method initializes an instance of the GPTPanguConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Defaults to 40000.
TYPE:
|
max_position_embeddings |
The maximum position index. Defaults to 1024.
TYPE:
|
hidden_size |
The hidden size of the model. Defaults to 2560.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the transformer encoder. Defaults to None.
TYPE:
|
num_layers |
The number of layers in the transformer encoder. Defaults to 32.
TYPE:
|
num_heads |
The number of attention heads in the transformer encoder. Defaults to 32.
TYPE:
|
activation_function |
The activation function used in the transformer layers. Defaults to 'gelu'.
TYPE:
|
resid_pdrop |
The dropout probability for the residual connections. Defaults to 0.1.
TYPE:
|
embd_pdrop |
The dropout probability for the embedding layer. Defaults to 0.1.
TYPE:
|
attn_pdrop |
The dropout probability for the attention layers. Defaults to 0.1.
TYPE:
|
layer_norm_epsilon |
The epsilon value for layer normalization. Defaults to 1e-05.
TYPE:
|
scale_attn_weights |
Whether to scale the attention weights. Defaults to True.
TYPE:
|
initializer_range |
The range of the initializer. Defaults to 0.02.
TYPE:
|
summary_type |
The type of summary produced by the model. Defaults to 'cls_index'.
TYPE:
|
summary_use_proj |
Whether to use projection in the summary. Defaults to True.
TYPE:
|
summary_activation |
The activation function used in the summary. Defaults to None.
TYPE:
|
summary_proj_to_labels |
Whether to project to labels in the summary. Defaults to True.
TYPE:
|
summary_first_dropout |
The dropout probability for the first summary layer. Defaults to 0.1.
TYPE:
|
use_cache |
Whether to use cache in the model. Defaults to True.
TYPE:
|
bos_token_id |
The beginning of sequence token id. Defaults to 9.
TYPE:
|
eos_token_id |
The end of sequence token id. Defaults to 9.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/configuration_gptpangu.py
21 22 23 24 25 26 27 28 29 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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu
¶
PanGu_Alpha Tokenizer.
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer
¶
Bases: PreTrainedTokenizer
This class represents a tokenizer for the GPTPangu model, which is used for tokenizing Chinese text. It inherits from the PreTrainedTokenizer class.
ATTRIBUTE | DESCRIPTION |
---|---|
sp |
An instance of the SentencePieceProcessor class used for tokenization.
TYPE:
|
translator |
A translation dictionary to replace spaces and newlines with special tokens.
TYPE:
|
Properties
vocab_size (int): Returns the size of the vocabulary used by the tokenizer.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPTPanguTokenizer object. |
get_vocab |
Returns the vocabulary as a dictionary. |
build_inputs_with_special_tokens |
Builds model inputs by adding special tokens to a sequence or a pair of sequences for sequence classification tasks. |
tokenize |
Tokenizes a string. |
convert_tokens_to_ids |
Converts a list of tokens to their corresponding IDs. |
convert_ids_to_tokens |
Converts a list of IDs to their corresponding tokens. |
decode |
Decodes a list of IDs into text. |
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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 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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.vocab_size
property
¶
Returns vocab size
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.__init__(model_file, **kwargs)
¶
Initializes a new instance of the GPTPanguTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPTPanguTokenizer class.
|
model_file |
The path to the model file used by the tokenizer. The model file should be in the format expected by the sentencepiece.SentencePieceProcessor. The tokenizer will load the model file during initialization.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence or a pair of sequence for sequence classification tasks by concatenating and adding special tokens. A BERT sequence has the following format:
- single sequence:
[CLS] X [SEP]
- pair of sequences:
[CLS] A [SEP] B [SEP]
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs to which the special tokens will be added.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
|
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.convert_ids_to_tokens(ids)
¶
Converts a list of token IDs to their corresponding tokens using the GPTPanguTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPTPanguTokenizer class.
TYPE:
|
ids |
A list of token IDs to be converted to tokens. Each ID represents a unique token.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Note
The GPTPanguTokenizer must be initialized with a pretrained model before using this method.
Example
>>> tokenizer = GPTPanguTokenizer()
>>> token_ids = [0, 1, 2]
>>> tokenizer.convert_ids_to_tokens(token_ids)
['<s>', 'Hello', '</s>']
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.convert_tokens_to_ids(tokens)
¶
Converts a list of tokens into their corresponding token IDs using the GPTPanguTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPTPanguTokenizer class.
TYPE:
|
tokens |
The tokens to be converted into token IDs. If a string is provided, it will be treated as a single token.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
list or None: A list of token IDs corresponding to the input tokens. Returns None if the input tokens are None. |
Note
- If the input tokens are None, the method returns None.
- If the input tokens are a string, the method calls the _convert_token_to_id_with_added_voc() method to convert it into a token ID.
- If the input tokens contain special tokens, the method identifies their indices and splits the tokens into segments. Each segment is then encoded using the sp.encode() method and appended to the list of token IDs.
- The method concatenates all the encoded segments and returns the final list of token IDs.
Example
>>> tokenizer = GPTPanguTokenizer()
>>> tokens = ['Hello', 'world', '!']
>>> ids = tokenizer.convert_tokens_to_ids(tokens)
>>> # ids = [123, 456, 789]
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.decode(ids, **kwargs)
¶
Decode the given token IDs into text using the GPTPanguTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPTPanguTokenizer class.
TYPE:
|
ids |
The token IDs to decode into text. If passed as a mindspore.Tensor or np.ndarray, it will be converted to a list of integers. This parameter is required.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The decoded text corresponding to the provided token IDs. Whitespace characters ' ' will be replaced with spaces, '▂' will be replaced with spaces, and '▃' will be replaced with newline characters. |
RAISES | DESCRIPTION |
---|---|
None
|
This method does not raise any exceptions. |
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
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 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.get_vocab()
¶
Returns vocab as a dict
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
110 111 112 113 114 |
|
mindnlp.transformers.models.gpt_pangu.tokenization_gptpangu.GPTPanguTokenizer.tokenize(text, **kwargs)
¶
Tokenize a string.
Source code in mindnlp/transformers/models/gpt_pangu/tokenization_gptpangu.py
147 148 149 150 |
|