codegen
mindnlp.transformers.models.codegen.modeling_codegen.CODEGEN_PRETRAINED_MODEL_ARCHIVE_LIST = ['Salesforce/codegen-350M-nl', 'Salesforce/codegen-350M-multi', 'Salesforce/codegen-350M-mono', 'Salesforce/codegen-2B-nl', 'Salesforce/codegen-2B-multi', 'Salesforce/codegen-2B-mono', 'Salesforce/codegen-6B-nl', 'Salesforce/codegen-6B-multi', 'Salesforce/codegen-6B-mono', 'Salesforce/codegen-16B-nl', 'Salesforce/codegen-16B-multi', 'Salesforce/codegen-16B-mono']
module-attribute
¶
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM
¶
Bases: CodeGenPreTrainedModel
This class represents a code generation model for causal language modeling (LM) using a transformer-based architecture. It inherits from the CodeGenPreTrainedModel class.
ATTRIBUTE | DESCRIPTION |
---|---|
transformer |
The transformer model for code generation.
TYPE:
|
lm_head |
The dense layer for predicting the next token in the language modeling task.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the CodeGenForCausalLM instance. |
get_output_embeddings |
Returns the output embeddings of the LM head. |
set_output_embeddings |
Sets the output embeddings of the LM head. |
prepare_inputs_for_generation |
Prepares the input tensors for generation. |
forward |
Constructs the output of the model for a given set of inputs. |
_reorder_cache |
Reorders the cache of past key values for beam search or beam sampling. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 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 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM.__init__(config)
¶
Initializes an instance of the CodeGenForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenForCausalLM class.
|
config |
An object containing configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM.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
TYPE:
|
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM.get_output_embeddings()
¶
This method 'get_output_embeddings' in the class 'CodeGenForCausalLM' retrieves the output embeddings from the model's head.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class 'CodeGenForCausalLM' itself.
|
RETURNS | DESCRIPTION |
---|---|
lm_head
|
This method returns the output embeddings represented by 'self.lm_head', which is of type 'None'. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
932 933 934 935 936 937 938 939 940 941 942 943 944 945 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM.prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs)
¶
Prepare inputs for generation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenForCausalLM class.
TYPE:
|
input_ids |
The input tensor containing token IDs for the model.
TYPE:
|
past_key_values |
Tuple of past key values used for fast decoding. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for generation with the following keys:
|
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
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 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenForCausalLM.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the CodeGenForCausalLM.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenForCausalLM class.
TYPE:
|
new_embeddings |
The new embeddings to be set as the output embeddings. It should be of the same type as the current embeddings.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Note
This method replaces the current output embeddings of the CodeGenForCausalLM instance with the provided new embeddings. The new embeddings should have the same type as the current embeddings to ensure compatibility with the other methods of the class.
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenModel
¶
Bases: CodeGenPreTrainedModel
The CodeGenModel
class is a Python class that represents a code generation model.
It is a subclass of CodeGenPreTrainedModel
and provides functionality for forwarding, setting and getting input
embeddings, and generating code outputs.
ATTRIBUTE | DESCRIPTION |
---|---|
`embed_dim` |
An integer representing the embedding dimension.
|
`vocab_size` |
An integer representing the size of the vocabulary.
|
`wte` |
An embedding layer that maps input tokens to their corresponding embeddings.
|
`drop` |
A dropout layer for regularization.
|
`h` |
A list of
|
`ln_f` |
A layer normalization layer.
|
`rotary_dim` |
An integer representing the dimension of the rotary encoding.
|
`gradient_checkpointing` |
A boolean indicating whether gradient checkpointing is enabled.
|
Note
Please refer to the CodeGenPreTrainedModel
class for additional inherited attributes and methods.
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
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 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 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenModel.__init__(config)
¶
Initializes an instance of the CodeGenModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object of the configuration class containing the following attributes:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
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 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenModel.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 CodeGenModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenModel class.
TYPE:
|
input_ids |
The input IDs of the model. Default is None.
TYPE:
|
past_key_values |
The past key values for the model. Default is None.
TYPE:
|
attention_mask |
The attention mask for the model. Default is None.
TYPE:
|
token_type_ids |
The token type IDs for the model. Default is None.
TYPE:
|
position_ids |
The position IDs for the model. Default is None.
TYPE:
|
head_mask |
The head mask for the model. Default is None.
TYPE:
|
inputs_embeds |
The embedded inputs for the model. Default is None.
TYPE:
|
use_cache |
Whether to use cache. Default is None.
TYPE:
|
output_attentions |
Whether to output attentions. Default is None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default is None.
TYPE:
|
return_dict |
Whether to return the result as a dictionary. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPast]
|
Union[Tuple, BaseModelOutputWithPast]: The forwarded model output. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both input_ids and inputs_embeds are specified. |
ValueError
|
If neither input_ids nor inputs_embeds are specified. |
ValueError
|
If batch_size is less than or equal to zero. |
Warning
|
If use_cache is True and config.gradient_checkpointing is True. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
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 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 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenModel.get_input_embeddings()
¶
This method is part of the CodeGenModel class and is named get_input_embeddings. It takes 1 parameter, self, which refers to the instance of the class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenModel.set_input_embeddings(new_embeddings)
¶
Sets the input embeddings for the CodeGenModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenModel class.
TYPE:
|
new_embeddings |
The new embeddings to be set. This argument could be of any type.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/modeling_codegen.py
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 |
|
mindnlp.transformers.models.codegen.modeling_codegen.CodeGenPreTrainedModel
¶
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/codegen/modeling_codegen.py
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 |
|
mindnlp.transformers.models.codegen.configuration_codegen.CodeGenConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [CodeGenModel
]. It is used to instantiate a
CodeGen 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 CodeGen
Salesforce/codegen-2B-mono architecture. Configuration objects
inherit from [PretrainedConfig
] and can be used to control the model outputs. Read the documentation from
[PretrainedConfig
] for more information.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
Vocabulary size of the CodeGen model. Defines the number of different tokens that can be represented by the
TYPE:
|
n_positions |
The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
TYPE:
|
n_ctx |
This attribute is used in
TYPE:
|
n_embd |
Dimensionality of the embeddings and hidden states.
TYPE:
|
n_layer |
Number of hidden layers in the Transformer encoder.
TYPE:
|
n_head |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
rotary_dim |
Number of dimensions in the embedding that Rotary Position Embedding is applied to.
TYPE:
|
n_inner |
Dimensionality of the inner feed-forward layers.
TYPE:
|
activation_function |
Activation function, to be selected in the list
TYPE:
|
resid_pdrop |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
embd_pdrop |
The dropout ratio for the embeddings.
TYPE:
|
attn_pdrop |
The dropout ratio for the attention.
TYPE:
|
layer_norm_epsilon |
The epsilon to use in the layer normalization layers.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models).
TYPE:
|
bos_token_id |
Beginning of stream token id.
TYPE:
|
eos_token_id |
End of stream token id.
TYPE:
|
tie_word_embeddings |
Whether the model's input and output word embeddings should be tied. Note that this is only relevant if the model has a output word embedding layer.
TYPE:
|
Example
>>> from transformers import CodeGenConfig, CodeGenModel
...
>>> # Initializing a CodeGen 6B configuration
>>> configuration = CodeGenConfig()
...
>>> # Initializing a model (with random weights) from the configuration
>>> model = CodeGenModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/codegen/configuration_codegen.py
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 |
|
mindnlp.transformers.models.codegen.configuration_codegen.CodeGenConfig.__init__(vocab_size=50400, n_positions=2048, n_ctx=2048, n_embd=4096, n_layer=28, n_head=16, rotary_dim=64, n_inner=None, activation_function='gelu_new', resid_pdrop=0.0, embd_pdrop=0.0, attn_pdrop=0.0, layer_norm_epsilon=1e-05, initializer_range=0.02, use_cache=True, bos_token_id=50256, eos_token_id=50256, tie_word_embeddings=False, **kwargs)
¶
Initializes an instance of the CodeGenConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Defaults to 50400.
TYPE:
|
n_positions |
The number of positions. Defaults to 2048.
TYPE:
|
n_ctx |
The context size. Defaults to 2048.
TYPE:
|
n_embd |
The embedding size. Defaults to 4096.
TYPE:
|
n_layer |
The number of layers. Defaults to 28.
TYPE:
|
n_head |
The number of attention heads. Defaults to 16.
TYPE:
|
rotary_dim |
The dimension for rotary positional embeddings. Defaults to 64.
TYPE:
|
n_inner |
The inner size of the feed-forward networks. Defaults to None.
TYPE:
|
activation_function |
The activation function to use. Defaults to 'gelu_new'.
TYPE:
|
resid_pdrop |
The dropout rate for residual connections. Defaults to 0.0.
TYPE:
|
embd_pdrop |
The dropout rate for embeddings. Defaults to 0.0.
TYPE:
|
attn_pdrop |
The dropout rate for attention probabilities. Defaults to 0.0.
TYPE:
|
layer_norm_epsilon |
The epsilon value for layer normalization. Defaults to 1e-05.
TYPE:
|
initializer_range |
The range for weight initialization. Defaults to 0.02.
TYPE:
|
use_cache |
Whether to use caching. Defaults to True.
TYPE:
|
bos_token_id |
The ID of the beginning-of-sentence token. Defaults to 50256.
TYPE:
|
eos_token_id |
The ID of the end-of-sentence token. Defaults to 50256.
TYPE:
|
tie_word_embeddings |
Whether to tie word embeddings. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/configuration_codegen.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer
¶
Bases: PreTrainedTokenizer
Construct a CodeGen tokenizer. Based on byte-level Byte-Pair-Encoding.
This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will be encoded differently whether it is at the beginning of the sentence (without space) or not:
Example
>>> from transformers import CodeGenTokenizer
...
>>> tokenizer = CodeGenTokenizer.from_pretrained("Salesforce/codegen-350M-mono")
>>> tokenizer("Hello world")["input_ids"]
[15496, 995]
>>> tokenizer(" Hello world")["input_ids"]
[18435, 995]
You can get around that behavior by passing add_prefix_space=True
when instantiating this tokenizer or when you
call it on some text, but since the model was not pretrained this way, it might yield a decrease in performance.
When used with is_split_into_words=True
, this tokenizer will add a space before each word (even the first one).
This tokenizer inherits from [PreTrainedTokenizer
] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
Path to the vocabulary file.
TYPE:
|
merges_file |
Path to the merges file.
TYPE:
|
errors |
Paradigm to follow when decoding bytes to UTF-8. See bytes.decode for more information.
TYPE:
|
unk_token |
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
bos_token |
The beginning of sequence token.
TYPE:
|
eos_token |
The end of sequence token.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
add_prefix_space |
Whether or not to add an initial space to the input. This allows to treat the leading word just as any other word. (CodeGen tokenizer detect beginning of words by the preceding space).
TYPE:
|
add_bos_token |
Whether to add a beginning of sequence token at the start of sequences.
TYPE:
|
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.vocab_size
property
¶
This method returns the size of the vocabulary in the CodeGenTokenizer instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizer class. It is used to access the encoder attribute to calculate the vocabulary size.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The size of the vocabulary in the CodeGenTokenizer instance. |
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.__init__(vocab_file, merges_file, errors='replace', unk_token='<|endoftext|>', bos_token='<|endoftext|>', eos_token='<|endoftext|>', pad_token=None, add_prefix_space=False, add_bos_token=False, **kwargs)
¶
Initializes an instance of the CodeGenTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizer class.
TYPE:
|
vocab_file |
The path to the vocabulary file.
TYPE:
|
merges_file |
The path to the merges file.
TYPE:
|
errors |
Specifies the error handling scheme. Defaults to 'replace'.
TYPE:
|
unk_token |
The special token used to represent unknown tokens. Defaults to 'endoftext'.
TYPE:
|
bos_token |
The special token used to represent the beginning of a sentence. Defaults to 'endoftext'.
TYPE:
|
eos_token |
The special token used to represent the end of a sentence. Defaults to 'endoftext'.
TYPE:
|
pad_token |
The special token used to represent padding. Defaults to None.
TYPE:
|
add_prefix_space |
Specifies whether a prefix space should be added. Defaults to False.
TYPE:
|
add_bos_token |
Specifies whether the bos_token should be added. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
FileNotFoundError
|
If the vocab_file or merges_file could not be found. |
UnicodeDecodeError
|
If there is an error decoding the vocab_file or merges_file. |
TypeError
|
If any of the special token parameters (unk_token, bos_token, eos_token, pad_token) are not strings. |
TypeError
|
If add_bos_token or add_prefix_space are not boolean values. |
ValueError
|
If the merges_file is not formatted correctly. |
Note
This method initializes the CodeGenTokenizer class by loading the vocabulary and merges files, setting the error handling scheme, and initializing various attributes.
Example
>>> tokenizer = CodeGenTokenizer('vocab.txt', 'merges.txt', errors='ignore', unk_token='<unk>', bos_token='<s>')
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.bpe(token)
¶
This method implements the Byte Pair Encoding (BPE) algorithm to tokenize a given token.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CodeGenTokenizer class.
TYPE:
|
token |
The token to be tokenized using the BPE algorithm.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The token after applying the BPE algorithm. |
This method first checks if the token is already present in the cache and returns the cached value if found. It then converts the token into a tuple of characters and generates a list of all possible pairs of characters in the token. If no pairs are found, the method returns the original token as there is no need for further processing.
The method then enters a loop where it iteratively selects the pair with the lowest rank according to the BPE ranks. If the selected pair is not present in the BPE ranks, the loop is terminated.
The method then finds the first occurrence of the selected pair in the token and replaces it with the concatenation of the pair. This process is repeated until no more occurrences of the selected pair are found in the token.
Finally, the token is converted back to a string and stored in the cache for future use before being returned as the result of the method.
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizer class.
TYPE:
|
token_ids_0 |
A list of token IDs for the first input sequence.
TYPE:
|
token_ids_1 |
A list of token IDs for the second input sequence. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
370 371 372 373 374 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.decode(token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=None, truncate_before_pattern=None, **kwargs)
¶
Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special
tokens and clean up tokenization spaces.
Similar to doing self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))
.
PARAMETER | DESCRIPTION |
---|---|
token_ids |
List of tokenized input ids. Can be obtained using the
TYPE:
|
skip_special_tokens |
Whether or not to remove special tokens in the decoding.
TYPE:
|
clean_up_tokenization_spaces |
Whether or not to clean up the tokenization spaces. If
TYPE:
|
truncate_before_pattern |
A list of regular expression strings that will be used to truncate the returned string. This can be
used to remove extra pieces of code (e.g. truncate if observing a comment symbol "#" at the beginning
of a new line). An example pattern could be
TYPE:
|
kwargs |
Will be passed to the underlying model specific decode method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
|
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
441 442 443 444 445 446 447 448 449 450 451 452 453 454 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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.get_vocab()
¶
Returns a dictionary containing the vocabulary of the CodeGenTokenizer instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The CodeGenTokenizer instance.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the vocabulary of the CodeGenTokenizer. The keys are tokens from the encoder and added_tokens_encoder, and the values are their corresponding indices. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.prepare_for_tokenization(text, is_split_into_words=False, **kwargs)
¶
Prepare the text for tokenization by adding prefix space if required.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizer class.
TYPE:
|
text |
The input text to be prepared for tokenization.
TYPE:
|
is_split_into_words |
A flag indicating if the text is already split into words. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The text prepared for tokenization with potential prefix space added. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If 'add_prefix_space' is not found in kwargs. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the generated vocabulary files to the specified directory.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizer class.
TYPE:
|
save_directory |
The directory path where the vocabulary files will be saved.
TYPE:
|
filename_prefix |
An optional prefix to be added to the filenames. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the paths to the saved vocabulary and merge files. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided save_directory is not a valid directory path. |
IOError
|
If there are issues with writing the vocabulary or merge files to the specified directory. |
RuntimeError
|
If the BPE merge indices are not consecutive, indicating potential corruption in the tokenizer. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen.CodeGenTokenizer.truncate(completion, truncate_before_pattern)
¶
This method 'truncate' is defined in the 'CodeGenTokenizer' class and is used to truncate a given completion based on specified patterns.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
completion |
The completion string to be truncated.
TYPE:
|
truncate_before_pattern |
A list of patterns to truncate the completion string before.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen_fast.CodeGenTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" CodeGen tokenizer (backed by HuggingFace's tokenizers library). Based on byte-level Byte-Pair-Encoding.
This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will be encoded differently whether it is at the beginning of the sentence (without space) or not:
Example
>>> from transformers import CodeGenTokenizerFast
...
>>> tokenizer = CodeGenTokenizerFast.from_pretrained("Salesforce/codegen-350M-mono")
>>> tokenizer("Hello world")["input_ids"]
[15496, 995]
>>> tokenizer(" Hello world")["input_ids"]
[18435, 995]
You can get around that behavior by passing add_prefix_space=True
when instantiating this tokenizer, but since
the model was not pretrained this way, it might yield a decrease in performance.
When used with is_split_into_words=True
, this tokenizer needs to be instantiated with add_prefix_space=True
.
This tokenizer inherits from [PreTrainedTokenizerFast
] which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
Path to the vocabulary file.
TYPE:
|
merges_file |
Path to the merges file.
TYPE:
|
tokenizer_file |
Path to tokenizers file (generally has a .json extension) that contains everything needed to load the tokenizer.
TYPE:
|
unk_token |
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
bos_token |
The beginning of sequence token.
TYPE:
|
eos_token |
The end of sequence token.
TYPE:
|
add_prefix_space |
Whether or not to add an initial space to the input. This allows to treat the leading word just as any other word. (CodeGen tokenizer detect beginning of words by the preceding space).
TYPE:
|
Source code in mindnlp/transformers/models/codegen/tokenization_codegen_fast.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 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 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|
mindnlp.transformers.models.codegen.tokenization_codegen_fast.CodeGenTokenizerFast.__init__(vocab_file=None, merges_file=None, tokenizer_file=None, unk_token='<|endoftext|>', bos_token='<|endoftext|>', eos_token='<|endoftext|>', add_prefix_space=False, **kwargs)
¶
Initializes an instance of the CodeGenTokenizerFast class.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
The path to the vocabulary file. Default is None.
TYPE:
|
merges_file |
The path to the merges file. Default is None.
TYPE:
|
tokenizer_file |
The path to the tokenizer file. Default is None.
TYPE:
|
unk_token |
The unknown token to be used. Default is 'endoftext'.
TYPE:
|
bos_token |
The beginning of sequence token. Default is 'endoftext'.
TYPE:
|
eos_token |
The end of sequence token. Default is 'endoftext'.
TYPE:
|
add_prefix_space |
Whether to add prefix space. Default is False.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If attempting to add a BOS token using the fast tokenizer. Suggests using the slow tokenizer instead. |
JSONDecodeError
|
If the pre_tokenizer state cannot be decoded from JSON. |
AttributeError
|
If the pre_tokenizer class cannot be found. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen_fast.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen_fast.CodeGenTokenizerFast.decode(token_ids, skip_special_tokens=False, clean_up_tokenization_spaces=None, truncate_before_pattern=None, **kwargs)
¶
Converts a sequence of ids in a string, using the tokenizer and vocabulary with options to remove special tokens
and clean up tokenization spaces.
Similar to doing self.convert_tokens_to_string(self.convert_ids_to_tokens(token_ids))
.
PARAMETER | DESCRIPTION |
---|---|
token_ids |
List of tokenized input ids. Can be obtained using the
TYPE:
|
skip_special_tokens |
Whether or not to remove special tokens in the decoding.
TYPE:
|
clean_up_tokenization_spaces |
Whether or not to clean up the tokenization spaces. If
TYPE:
|
truncate_before_pattern |
A list of regular expression strings that will be used to truncate the returned string. This can be used to remove extra pieces of code (e.g. truncate if observing a comment symbol "#" at the beginning of a new line).
TYPE:
|
kwargs |
Will be passed to the underlying model specific decode method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
|
Source code in mindnlp/transformers/models/codegen/tokenization_codegen_fast.py
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 |
|
mindnlp.transformers.models.codegen.tokenization_codegen_fast.CodeGenTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary files generated by the tokenizer model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the CodeGenTokenizerFast class.
TYPE:
|
save_directory |
The directory path where the vocabulary files will be saved.
TYPE:
|
filename_prefix |
An optional prefix to be added to the filename of the saved vocabulary files. Defaults to None if not provided.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the filenames of the saved vocabulary files. |
RAISES | DESCRIPTION |
---|---|
SpecificException
|
Describes when a specific exception might be raised during the save operation. |
AnotherException
|
Describes when another type of exception might be raised during the save operation. |
AnyOtherException
|
Describes any other exception that the function may raise. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen_fast.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
mindnlp.transformers.models.codegen.tokenization_codegen_fast.CodeGenTokenizerFast.truncate(completion, truncate_before_pattern)
¶
Truncate the completion string before a given pattern using a list of truncate_before_patterns.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the CodeGenTokenizerFast class.
TYPE:
|
completion |
The completion string to be truncated.
TYPE:
|
truncate_before_pattern |
A list of patterns to truncate the completion string before. Each pattern is compiled using the re.compile() method with the re.MULTILINE flag.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/codegen/tokenization_codegen_fast.py
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 |
|