llama
mindnlp.transformers.models.llama.modeling_llama
¶
modeling llama
mindnlp.transformers.models.llama.modeling_llama.LlamaAttention
¶
Bases: Module
Multi-headed attention from 'Attention Is All You Need' paper
Source code in mindnlp\transformers\models\llama\modeling_llama.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 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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaDecoderLayer
¶
Bases: Module
Source code in mindnlp\transformers\models\llama\modeling_llama.py
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaDecoderLayer.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False, cache_position=None, position_embeddings=None, **kwargs)
¶
| PARAMETER | DESCRIPTION |
|---|---|
hidden_states
|
input to the layer of shape
TYPE:
|
attention_mask
|
attention mask of size
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:
|
cache_position
|
Indices depicting the position of the input sequence tokens in the sequence
TYPE:
|
position_embeddings
|
Tuple containing the cosine and sine positional embeddings of shape
TYPE:
|
kwargs
|
Arbitrary kwargs to be ignored, used for FSDP and other methods that injects code into the model
TYPE:
|
Source code in mindnlp\transformers\models\llama\modeling_llama.py
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaDynamicNTKScalingRotaryEmbedding
¶
Bases: LlamaRotaryEmbedding
LlamaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla
Source code in mindnlp\transformers\models\llama\modeling_llama.py
232 233 234 235 236 237 238 239 240 241 242 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForCausalLM
¶
Bases: LlamaPreTrainedModel
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 951 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForCausalLM.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, cache_position=None, num_logits_to_keep=0)
¶
| PARAMETER | DESCRIPTION |
|---|---|
labels
|
Labels for computing the masked language modeling loss. Indices should either be in
TYPE:
|
Example:
>>> from transformers import AutoTokenizer, LlamaForCausalLM
>>> model = LlamaForCausalLM.from_pretrained("meta-llama/Llama-2-7b-hf")
>>> tokenizer = AutoTokenizer.from_pretrained("meta-llama/Llama-2-7b-hf")
>>> prompt = "Hey, are you conscious? Can you talk to me?"
>>> inputs = tokenizer(prompt, return_tensors="ms")
>>> # 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]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForQuestionAnswering
¶
Bases: LlamaPreTrainedModel
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForQuestionAnswering.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, start_positions=None, end_positions=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
start_positions (mindspore.Tensor of shape (batch_size,), optional):
Labels for position (index) of the start of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
end_positions (mindspore.Tensor of shape (batch_size,), optional):
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (sequence_length). Position outside of the sequence
are not taken into account for computing the loss.
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForSequenceClassification
¶
Bases: LlamaPreTrainedModel
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForSequenceClassification.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)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [0, ...,
config.num_labels - 1]. If config.num_labels == 1 a regression loss is computed (Mean-Square loss), If
config.num_labels > 1 a classification loss is computed (Cross-Entropy).
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForTokenClassification
¶
Bases: LlamaPreTrainedModel
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaForTokenClassification.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)
¶
labels (mindspore.Tensor of shape (batch_size,), optional):
Labels for computing the sequence classification/regression loss. Indices should be in [0, ...,
config.num_labels - 1]. If config.num_labels == 1 a regression loss is computed (Mean-Square loss), If
config.num_labels > 1 a classification loss is computed (Cross-Entropy).
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaLinearScalingRotaryEmbedding
¶
Bases: LlamaRotaryEmbedding
LlamaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev
Source code in mindnlp\transformers\models\llama\modeling_llama.py
220 221 222 223 224 225 226 227 228 229 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaModel
¶
Bases: LlamaPreTrainedModel
Transformer decoder consisting of config.num_hidden_layers layers. Each layer is a [LlamaDecoderLayer]
| PARAMETER | DESCRIPTION |
|---|---|
config
|
LlamaConfig
TYPE:
|
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaRMSNorm
¶
Bases: Module
Source code in mindnlp\transformers\models\llama\modeling_llama.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaRMSNorm.__init__(hidden_size, eps=1e-06)
¶
LlamaRMSNorm is equivalent to T5LayerNorm
Source code in mindnlp\transformers\models\llama\modeling_llama.py
113 114 115 116 117 118 119 | |
mindnlp.transformers.models.llama.modeling_llama.LlamaRotaryEmbedding
¶
Bases: Module
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.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:
|
Source code in mindnlp\transformers\models\llama\modeling_llama.py
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 | |
mindnlp.transformers.models.llama.modeling_llama.repeat_kv(hidden_states, n_rep)
¶
This is the equivalent of ops.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\llama\modeling_llama.py
314 315 316 317 318 319 320 321 322 323 324 | |
mindnlp.transformers.models.llama.modeling_llama.rotate_half(x)
¶
Rotates half the hidden dims of the input.
Source code in mindnlp\transformers\models\llama\modeling_llama.py
245 246 247 248 249 250 | |
mindnlp.transformers.models.llama.configuration_llama
¶
LLaMA model configuration
mindnlp.transformers.models.llama.configuration_llama.LlamaConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [LlamaModel]. It is used to instantiate an LLaMA
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 LLaMA-7B.
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 LLaMA 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:
|
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. Llama 1 supports up to 2048 tokens, Llama 2 up to 4096, CodeLlama up to 16384.
TYPE:
|
initializer_range
|
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
rms_norm_eps
|
The epsilon used by the rms normalization layers.
TYPE:
|
use_cache
|
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if
TYPE:
|
pad_token_id
|
Padding token id.
TYPE:
|
bos_token_id
|
Beginning of stream token id.
TYPE:
|
eos_token_id
|
End of stream token id.
TYPE:
|
pretraining_tp
|
Experimental feature. Tensor parallelism rank used during pretraining. Please refer to this document to understand more about it. This value is necessary to ensure exact reproducibility of the pretraining results. Please refer to this issue.
TYPE:
|
tie_word_embeddings
|
Whether to tie weight embeddings
TYPE:
|
rope_theta
|
The base period of the RoPE embeddings.
TYPE:
|
rope_scaling
|
Dictionary containing the scaling configuration for the RoPE embeddings. NOTE: if you apply new rope type
and you expect the model to work on longer
TYPE:
|
attention_bias
|
Whether to use a bias in the query, key, value and output projection layers during self-attention.
TYPE:
|
attention_dropout
|
The dropout ratio for the attention probabilities.
TYPE:
|
mlp_bias
|
Whether to use a bias in up_proj, down_proj and gate_proj layers in the MLP layers.
TYPE:
|
>>> from transformers import LlamaModel, LlamaConfig
>>> # Initializing a LLaMA llama-7b style configuration
>>> configuration = LlamaConfig()
>>> # Initializing a model from the llama-7b style configuration
>>> model = LlamaModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp\transformers\models\llama\configuration_llama.py
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 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 | |
mindnlp.transformers.models.llama.tokenization_llama
¶
Tokenization classes for LLaMA.
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer
¶
Bases: PreTrainedTokenizer
Construct a Llama tokenizer. Based on byte-level Byte-Pair-Encoding. The default padding token is unset as there is no padding token in the original model.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
Path to the vocabulary file.
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 that was used during pretraining. Can be used a sequence classifier token.
TYPE:
|
eos_token
|
The end of sequence token.
TYPE:
|
pad_token
|
A special token used to make arrays of tokens the same size for batching purpose. Will then be ignored by attention mechanisms or loss computation.
TYPE:
|
sp_model_kwargs
|
Will be passed to the
TYPE:
|
add_bos_token
|
Whether or not to add an
TYPE:
|
add_eos_token
|
Whether or not to add an
TYPE:
|
clean_up_tokenization_spaces
|
Whether or not to cleanup spaces after decoding, cleanup consists in removing potential artifacts like extra spaces.
TYPE:
|
use_default_system_prompt
|
Whether or not the default system prompt for Llama should be used.
TYPE:
|
spaces_between_special_tokens
|
Whether or not to add spaces between special tokens.
TYPE:
|
legacy
|
Whether or not the
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. Again, this should be set with
TYPE:
|
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
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 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 | |
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.vocab_size
property
¶
Returns vocab size
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 | |
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence | second sequence |
if token_ids_1 is None, only returns the first portion of the mask (0s).
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of ids.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model method.
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
already_has_special_tokens
|
Whether or not the token list is already formatted with special tokens for the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.get_vocab()
¶
Returns vocab as a dict
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
228 229 230 231 232 | |
mindnlp.transformers.models.llama.tokenization_llama.LlamaTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary and special tokens file to a directory.
| PARAMETER | DESCRIPTION |
|---|---|
save_directory
|
The directory in which to save the vocabulary.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
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.llama.tokenization_llama.LlamaTokenizer.tokenize(text, **kwargs)
¶
Converts a string to a list of tokens. If self.legacy is set to False, a prefix token is added unless the
first token is special.
Source code in mindnlp\transformers\models\llama\tokenization_llama.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | |
mindnlp.transformers.models.llama.tokenization_llama_fast
¶
tokenization llama fast
mindnlp.transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a Llama tokenizer. Based on byte-level Byte-Pair-Encoding.
This uses notably ByteFallback and no normalization.
>>> from transformers import LlamaTokenizerFast
>>> tokenizer = LlamaTokenizerFast.from_pretrained("hf-internal-testing/llama-tokenizer")
>>> tokenizer.encode("Hello this is a test")
[1, 15043, 445, 338, 263, 1243]
If you want to change the bos_token or the eos_token, make sure to specify them when initializing the model, or
call tokenizer.update_post_processor() to make sure that the post-processing is correctly done (otherwise the
values of the first token and final token of an encoded sequence will not be correct). For more details, checkout
[post-processors] (https://huggingface.co/docs/tokenizers/api/post-processors) documentation.
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
|
SentencePiece file (generally has a .model extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
tokenizer_file
|
tokenizers file (generally has a .json extension) that contains everything needed to load the tokenizer.
TYPE:
|
clean_up_tokenization_spaces
|
Whether or not to cleanup spaces after decoding, cleanup consists in removing potential artifacts like extra spaces.
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 that was used during pretraining. Can be used a sequence classifier token.
TYPE:
|
eos_token
|
The end of sequence token.
TYPE:
|
add_bos_token
|
Whether or not to add an
TYPE:
|
add_eos_token
|
Whether or not to add an
TYPE:
|
use_default_system_prompt
|
Whether or not the default system prompt for Llama should be used
TYPE:
|
legacy
|
Whether or not the
TYPE:
|
add_prefix_space
|
Whether or not the tokenizer should automatically add a prefix space
TYPE:
|
Source code in mindnlp\transformers\models\llama\tokenization_llama_fast.py
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 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 | |
mindnlp.transformers.models.llama.tokenization_llama_fast.LlamaTokenizerFast.update_post_processor()
¶
Updates the underlying post processor with the current bos_token and eos_token.
Source code in mindnlp\transformers\models\llama\tokenization_llama_fast.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama
¶
Tokenization classes for Code LLaMA.
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer
¶
Bases: PreTrainedTokenizer
Construct a CodeLlama tokenizer. Based on byte-level Byte-Pair-Encoding. The default padding token is unset as there is no padding token in the original model.
The default configuration match that of codellama/CodeLlama-7b-Instruct-hf which supports prompt infilling.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
Path to the vocabulary file.
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 that was used during pretraining. Can be used a sequence classifier token.
TYPE:
|
eos_token
|
The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence.
The token used is the
TYPE:
|
prefix_token
|
Prefix token used for infilling.
TYPE:
|
middle_token
|
Middle token used for infilling.
TYPE:
|
suffix_token
|
Suffix token used for infilling.
TYPE:
|
eot_token
|
End of text token used for infilling.
TYPE:
|
fill_token
|
The token used to split the input between the prefix and suffix.
TYPE:
|
suffix_first
|
Whether the input prompt and suffix should be formatted with the suffix first.
TYPE:
|
sp_model_kwargs
|
Will be passed to the
TYPE:
|
add_bos_token
|
Whether to add a beginning of sequence token at the start of sequences.
TYPE:
|
add_eos_token
|
Whether to add an end of sequence token at the end of sequences.
TYPE:
|
clean_up_tokenization_spaces
|
Whether or not to clean up the tokenization spaces.
TYPE:
|
additional_special_tokens
|
Additional special tokens used by the tokenizer.
TYPE:
|
use_default_system_prompt
|
Whether or not the default system prompt for Llama should be used.
TYPE:
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.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 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 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 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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.default_chat_template
property
¶
LLaMA uses [INST] and [/INST] to indicate user messages, and <
The output should look something like:
<bos>[INST] B_SYS SystemPrompt E_SYS Prompt [/INST] Answer <eos><bos>[INST] Prompt [/INST] Answer <eos>
<bos>[INST] Prompt [/INST]
The reference for this chat template is this code snippet in the original repository.
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.eot_id
property
¶
This method 'eot_id' is a property in the 'CodeLlamaTokenizer' class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the 'CodeLlamaTokenizer' class.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the '_eot_token' attribute is None, the method returns None. |
int
|
If the '_eot_token' attribute is not None, the method returns the integer value obtained by converting the token to its corresponding ID using the 'convert_tokens_to_ids' method. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.eot_token
property
¶
This method 'eot_token' in the class 'CodeLlamaTokenizer' retrieves the end-of-text token.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns the end-of-text token value stored in the instance. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.middle_id
property
¶
Get the middle ID of the CodeLlamaTokenizer instance.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the middle token is None. |
This method returns the middle ID of the CodeLlamaTokenizer instance. If the middle token is None, it returns None. The middle ID is obtained by converting the middle token to its corresponding ID using the 'convert_tokens_to_ids' method.
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.middle_token
property
¶
This method 'middle_token' is a property method defined in the class 'CodeLlamaTokenizer' that retrieves the middle token stored in the instance.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class. This parameter refers to the current instance of the class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns the middle token stored in the instance. If no middle token is set, it returns None. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.prefix_id
property
¶
Method to retrieve the ID associated with the prefix token in the CodeLlamaTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the prefix token is None, the method returns None. Otherwise, it returns the ID associated with the prefix token. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.prefix_token
property
¶
Returns the prefix token used for tokenizing code in the CodeLlamaTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
This method retrieves the prefix token that is used for tokenizing code in the CodeLlamaTokenizer class. The prefix token serves as a marker or indicator to identify the start of a code block or expression. It is used during the tokenization process to correctly identify and separate different parts of the code.
Note that the prefix token is an internal attribute of the CodeLlamaTokenizer class, and it is not meant to be modified directly. To change the prefix token, use the appropriate setter method or modify the underlying implementation of the class if necessary.
Example
>>> tokenizer = CodeLlamaTokenizer()
>>> prefix = tokenizer.prefix_token
>>> print(prefix)
>>> # Output: '>>'
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.suffix_id
property
¶
Returns the ID of the suffix token.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the suffix token is None. |
This method retrieves the ID corresponding to the suffix token. If the suffix token is None, the method returns None. The suffix token is obtained by converting the suffix token to its corresponding ID using the convert_tokens_to_ids method.
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.suffix_token
property
¶
Method to retrieve the suffix token associated with the CodeLlamaTokenizer instance.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of CodeLlamaTokenizer. This parameter refers to the instance of the CodeLlamaTokenizer class on which the method is being called.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns the suffix token corresponding to the CodeLlamaTokenizer instance. The suffix token is a property value associated with the instance. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.unk_token_length
property
¶
Returns the length of the unknown token in the CodeLlamaTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The length of the unknown token. If the unknown token is not found, it returns 0. |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.vocab_size
property
¶
Returns vocab size
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.__getstate__()
¶
Description
This method is used to retrieve the state of the CodeLlamaTokenizer object for serialization purposes. It returns a dictionary representing the current state of the object.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value. Instead, it modifies the state dictionary and returns None. |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.__init__(vocab_file, unk_token='<unk>', bos_token='<s>', eos_token='</s>', prefix_token='▁<PRE>', middle_token='▁<MID>', suffix_token='▁<SUF>', eot_token='▁<EOT>', fill_token='<FILL_ME>', suffix_first=False, sp_model_kwargs=None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, additional_special_tokens=None, use_default_system_prompt=False, **kwargs)
¶
This method initializes an instance of the CodeLlamaTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
The path to the vocabulary file.
TYPE:
|
unk_token
|
The unknown token, default is '
TYPE:
|
bos_token
|
The beginning of sequence token, default is '
TYPE:
|
eos_token
|
The end of sequence token, default is ''.
TYPE:
|
prefix_token
|
The prefix token, default is '▁ '.
TYPE:
|
middle_token
|
The middle token, default is '▁
TYPE:
|
suffix_token
|
The suffix token, default is '▁
TYPE:
|
eot_token
|
The end of text token, default is '▁
TYPE:
|
fill_token
|
The fill token, default is '
TYPE:
|
suffix_first
|
Indicates whether suffix comes before prefix.
TYPE:
|
sp_model_kwargs
|
Additional arguments for the sentencepiece model.
TYPE:
|
add_bos_token
|
Whether to add the bos token, default is True.
TYPE:
|
add_eos_token
|
Whether to add the eos token, default is False.
TYPE:
|
clean_up_tokenization_spaces
|
Whether to clean up tokenization spaces, default is False.
TYPE:
|
additional_special_tokens
|
Additional special tokens to include.
TYPE:
|
use_default_system_prompt
|
Whether to use the default system prompt.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
MissingBackendError
|
If the required backend 'protobuf' is not available. |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.__setstate__(d)
¶
This method 'setstate' is defined within the 'CodeLlamaTokenizer' class to set the internal state of the object based on the provided dictionary 'd'. It reforwards the object's state including the SentencePiece model by loading it from a serialized proto.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
TYPE:
|
d
|
A dictionary containing the state information to be set. It should include the necessary attributes for the object's state reforwardion.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value explicitly. It operates by modifying the internal state of the object. |
| RAISES | DESCRIPTION |
|---|---|
None
|
However, potential exceptions that could be raised during the execution may include but are not limited to:
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Method to build inputs with special tokens in the CodeLlamaTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
Reference to the current instance of the class.
|
token_ids_0
|
List of token IDs for the first input sequence.
TYPE:
|
token_ids_1
|
List of token IDs for the second input sequence. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list representing the input sequences with special tokens added based on the configuration settings. |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence | second sequence |
if token_ids_1 is None, only returns the first portion of the mask (0s).
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of ids.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model method.
| PARAMETER | DESCRIPTION |
|---|---|
token_ids_0
|
List of IDs.
TYPE:
|
token_ids_1
|
Optional second list of IDs for sequence pairs.
TYPE:
|
already_has_special_tokens
|
Whether or not the token list is already formatted with special tokens for the model.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.get_spm_processor()
¶
This method initializes and returns a SentencePieceProcessor object for tokenizing text using the SentencePiece library.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
spm.SentencePieceProcessor: A tokenizer object of type spm.SentencePieceProcessor. |
| RAISES | DESCRIPTION |
|---|---|
None
|
However, potential exceptions that may occur during the method execution include:
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.get_vocab()
¶
Returns vocab as a dict
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
442 443 444 445 446 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary and special tokens file to a directory.
| PARAMETER | DESCRIPTION |
|---|---|
save_directory
|
The directory in which to save the vocabulary.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama.CodeLlamaTokenizer.tokenize(prefix, suffix=None, suffix_first=False, **kwargs)
¶
Tokenizes the given prefix and suffix to generate a list of integers representing tokens.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizer class.
TYPE:
|
prefix
|
The prefix string to tokenize.
TYPE:
|
suffix
|
The suffix string to tokenize. Defaults to None.
TYPE:
|
suffix_first
|
Flag indicating whether to place the suffix before the prefix. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
List[int]
|
List[int]: A list of integers representing the tokens generated from the prefix and suffix. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input includes a prefix and a suffix used for the infilling task, or if the model does not support infilling. |
Note
- The
prefixandsuffixparameters are used to split the input on thefill_tokentoken to create a suffix and prefix. - If only a prefix is provided, the method tokenizes the prefix and returns the resulting tokens.
- If a prefix and suffix are provided, the method tokenizes both and returns the tokens in the specified order.
- The
suffix_firstparameter takes precedence over the class attributesuffix_firstif both are provided. - The method removes special tokens from the beginning of the tokens list if they match the specified conditions.
- The method replaces occurrences of the
SPIECE_UNDERLINEtoken in the prefix with a space.
Source code in mindnlp\transformers\models\llama\tokenization_code_llama.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast
¶
Fast Tokenization classes for Code LLaMA.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a Llama tokenizer. Based on byte-level Byte-Pair-Encoding. This uses notably ByteFallback and no normalization.
Example
>>> from transformers import CodeLlamaTokenizerFast
...
>>> tokenizer = CodeLlamaTokenizerFast.from_pretrained("hf-internal-testing/llama-tokenizer")
>>> tokenizer.encode("Hello this is a test")
[1, 15043, 445, 338, 263, 1243]
If you want to change the bos_token or the eos_token, make sure to specify them when initializing the model, or
call tokenizer.update_post_processor() to make sure that the post-processing is correctly done (otherwise the
values of the first token and final token of an encoded sequence will not be correct). For more details, checkout
[post-processors] (https://hf-mirror.com/docs/tokenizers/api/post-processors) documentation.
This tokenizer inherits from [PreTrainedTokenizerFast] which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods. The default configuration match that of
codellama/CodeLlama-7b-Instruct-hf
which supports prompt infilling.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file
|
SentencePiece file (generally has a .model extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
tokenizer_file
|
tokenizers file (generally has a .json extension) that contains everything needed to load the tokenizer.
TYPE:
|
clean_up_tokenization_spaces
|
Wether to cleanup spaces after decoding, cleanup consists in removing potential artifacts like extra spaces.
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 that was used during pretraining. Can be used a sequence classifier token.
TYPE:
|
eos_token
|
The end of sequence token.
TYPE:
|
prefix_token
|
Prefix token used for infilling.
TYPE:
|
middle_token
|
Middle token used for infilling.
TYPE:
|
suffix_token
|
Suffix token used for infilling.
TYPE:
|
eot_token
|
End of text token used for infilling.
TYPE:
|
fill_token
|
The token used to split the input between the prefix and suffix.
TYPE:
|
additional_special_tokens
|
Additional special tokens used by the tokenizer.
TYPE:
|
add_bos_token
|
Whether to add a beginning of sequence token at the start of sequences.
TYPE:
|
add_eos_token
|
Whether to add an end of sequence token at the end of sequences.
TYPE:
|
use_default_system_prompt
|
Whether or not the default system prompt for Llama should be used.
TYPE:
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 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 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 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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.add_bos_token
property
writable
¶
Method to add a beginning of sentence (BOS) token to the tokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class. It is used to access the tokenizer object.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.add_eos_token
property
writable
¶
Adds an end-of-sequence (EOS) token to the tokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizerFast class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.can_save_slow_tokenizer: bool
property
¶
Checks if the slow tokenizer can be saved.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if the slow tokenizer can be saved, False otherwise.
TYPE:
|
This method checks if the slow tokenizer can be saved by verifying if the vocab_file attribute exists. If the vocab_file attribute is not None and it corresponds to an existing file, the method returns True. Otherwise, it returns False.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.default_chat_template
property
¶
LLaMA uses [INST] and [/INST] to indicate user messages, and <
The output should look something like:
The reference for this chat template is this code snippet in the original repository.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.eot_id
property
¶
Returns the ID representation of the end-of-text (EOT) token in the CodeLlamaTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the EOT token is not set. |
int
|
The ID representation of the EOT token. |
This method retrieves the ID representation of the EOT token. If the EOT token is not set (None), it returns None. Otherwise, it uses the 'convert_tokens_to_ids' method to convert the EOT token to its corresponding ID representation and returns it.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.eot_token
property
¶
eot_token method in the CodeLlamaTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizerFast class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
The value of the _eot_token attribute. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.middle_id
property
¶
Returns the middle token ID of the CodeLlamaTokenizerFast instance.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the middle token is not set or is set to None. |
int
|
The ID of the middle token. |
This method retrieves the ID of the middle token in the CodeLlamaTokenizerFast instance. If the middle token is not set or is set to None, None is returned. Otherwise, the method calls the 'convert_tokens_to_ids' function to convert the middle token into its corresponding ID and returns the ID value.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.middle_token
property
¶
This method 'middle_token' is a property method in the class 'CodeLlamaTokenizerFast' that returns the middle token.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns the middle token or None if there is no middle token. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.prefix_id
property
¶
Returns the prefix token converted to its corresponding ID.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the prefix token is None. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.prefix_token
property
¶
Returns the prefix token for the CodeLlamaTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the CodeLlamaTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.suffix_id
property
¶
This method is defined in the CodeLlamaTokenizerFast class and is named suffix_id.
It takes one parameter, self, which refers to the instance of the class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
If the |
Description
This method retrieves the suffix ID associated with the _suffix_token attribute.
If the _suffix_token is None, indicating the absence of a suffix token, the method returns None.
Otherwise, it calls the convert_tokens_to_ids method to convert the _suffix_token to its
corresponding ID and returns the result.
Note
- The
_suffix_tokenattribute should be set before calling this method to ensure accurate results. - The return value is of type
None.
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.suffix_token
property
¶
This method, 'suffix_token', is a property method defined in the 'CodeLlamaTokenizerFast' class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the 'CodeLlamaTokenizerFast' class. It is used to access the attributes and methods of the class within this method.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, clean_up_tokenization_spaces=False, unk_token='<unk>', bos_token='<s>', eos_token='</s>', prefix_token='▁<PRE>', middle_token='▁<MID>', suffix_token='▁<SUF>', eot_token='▁<EOT>', fill_token='<FILL_ME>', additional_special_tokens=None, add_bos_token=True, add_eos_token=False, use_default_system_prompt=False, **kwargs)
¶
Initializes an instance of the CodeLlamaTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the class.
|
vocab_file
|
Path to the vocabulary file. Defaults to None.
TYPE:
|
tokenizer_file
|
Path to the tokenizer file. Defaults to None.
TYPE:
|
clean_up_tokenization_spaces
|
Whether to clean up tokenization spaces. Defaults to False.
TYPE:
|
unk_token
|
Unknown token. Defaults to '
TYPE:
|
bos_token
|
Beginning of sentence token. Defaults to '
TYPE:
|
eos_token
|
End of sentence token. Defaults to ''.
TYPE:
|
prefix_token
|
Prefix token. Defaults to '▁ '.
TYPE:
|
middle_token
|
Middle token. Defaults to '▁
TYPE:
|
suffix_token
|
Suffix token. Defaults to '▁
TYPE:
|
eot_token
|
End of text token. Defaults to '▁
TYPE:
|
fill_token
|
Fill token. Defaults to '
TYPE:
|
additional_special_tokens
|
Additional special tokens. Defaults to None.
TYPE:
|
add_bos_token
|
Whether to add the beginning of sentence token. Defaults to True.
TYPE:
|
add_eos_token
|
Whether to add the end of sentence token. Defaults to False.
TYPE:
|
use_default_system_prompt
|
Whether to use the default system prompt. Defaults to False.
TYPE:
|
**kwargs
|
Additional keyword arguments.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.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. The special tokens depend on calling set_lang.
An NLLB sequence has the following format, where X represents the sequence:
input_ids(for encoder)X [eos, src_lang_code]decoder_input_ids: (for decoder)X [eos, tgt_lang_code]
BOS is never used. Pairs of sequences are not the expected use case, but they will be handled without a separator.
| 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 |
|---|---|
List[int]
|
|
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.encode_plus(text, text_pair=None, suffix_first=False, add_special_tokens=True, **kwargs)
¶
Encodes the given text and text pair into tokens using the CodeLlamaTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class.
TYPE:
|
text
|
The input text to be encoded.
TYPE:
|
text_pair
|
The optional second input text to be encoded. Defaults to None.
TYPE:
|
suffix_first
|
Specifies whether the suffix should be placed first. Defaults to False.
TYPE:
|
add_special_tokens
|
Specifies whether to add special tokens. Defaults to True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tokens
|
The encoded tokens. This is an instance of a class defined in the CodeLlamaTokenizerFast class. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the input includes a |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary for a fast tokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the CodeLlamaTokenizerFast class.
TYPE:
|
save_directory
|
The directory path where the vocabulary will be saved.
TYPE:
|
filename_prefix
|
A prefix to be added to the filename. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the fast tokenizer does not have the necessary information to save the vocabulary for a slow tokenizer. |
FileNotFoundError
|
If the save_directory does not exist. |
IsADirectoryError
|
If the save_directory is not a directory. |
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.set_infilling_processor(reset, suffix_first=False, add_special_tokens=True)
¶
Updates the normalizer to make sure the prompt format for infilling is respected. The infilling format is the
following:
-
if suffix_first
" <PRE> <SUF>{suf} <MID> {pre}"
-
else:
" <PRE> {pre} <SUF>{suf} <MID>"
If reset is set to True, the normalizer and post_processor are reset to their "normal" behaviour, which
is to add a prefix space for the normalizer, and add a bos_token to the input text for the post_processor.
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
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 | |
mindnlp.transformers.models.llama.tokenization_code_llama_fast.CodeLlamaTokenizerFast.update_post_processor()
¶
Updates the underlying post processor with the current bos_token and eos_token.
Source code in mindnlp\transformers\models\llama\tokenization_code_llama_fast.py
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 | |