gemma
mindnlp.transformers.models.gemma.configuration_gemma
¶
Gemma model configuration
mindnlp.transformers.models.gemma.configuration_gemma.GemmaConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [GemmaModel]. It is used to instantiate an Gemma
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 Gemma-7B.
e.g. google/gemma-7b
Configuration objects inherit from [PretrainedConfig] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig] for more information.
Args:
vocab_size (int, optional, defaults to 256000):
Vocabulary size of the Gemma model. Defines the number of different tokens that can be represented by the
inputs_ids passed when calling [GemmaModel]
hidden_size (int, optional, defaults to 3072):
Dimension of the hidden representations.
intermediate_size (int, optional, defaults to 24576):
Dimension of the MLP representations.
num_hidden_layers (int, optional, defaults to 28):
Number of hidden layers in the Transformer decoder.
num_attention_heads (int, optional, defaults to 16):
Number of attention heads for each attention layer in the Transformer decoder.
num_key_value_heads (int, optional, defaults to 16):
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
num_key_value_heads=num_attention_heads, the model will use Multi Head Attention (MHA), if
num_key_value_heads=1 the model will use Multi Query Attention (MQA) otherwise GQA is used. When
converting a multi-head checkpoint to a GQA checkpoint, each group key and value head should be constructed
by meanpooling all the original heads within that group. For more details checkout this
paper. If it is not specified, will default to
num_attention_heads.
head_dim (int, optional, defaults to 256):
The attention head dimension.
hidden_act (str or function, optional, defaults to "gelu_pytorch_tanh"):
The legacy activation function. It is overwritten by the hidden_activation.
hidden_activation (str or function, optional):
The non-linear activation function (function or string) in the decoder. Will default to "gelu_pytorch_tanh"
if not specified. "gelu_pytorch_tanh" uses an approximation of the "gelu" activation function.
max_position_embeddings (int, optional, defaults to 8192):
The maximum sequence length that this model might ever be used with.
initializer_range (float, optional, defaults to 0.02):
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
rms_norm_eps (float, optional, defaults to 1e-06):
The epsilon used by the rms normalization layers.
use_cache (bool, optional, defaults to True):
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if config.is_decoder=True.
pad_token_id (int, optional, defaults to 0):
Padding token id.
eos_token_id (int, optional, defaults to 1):
End of stream token id.
bos_token_id (int, optional, defaults to 2):
Beginning of stream token id.
tie_word_embeddings (bool, optional, defaults to True):
Whether to tie weight embeddings
rope_theta (float, optional, defaults to 10000.0):
The base period of the RoPE embeddings.
attention_bias (bool, defaults to False, optional, defaults to False):
Whether to use a bias in the query, key, value and output projection layers during self-attention.
attention_dropout (float, optional, defaults to 0.0):
The dropout ratio for the attention probabilities.
>>> from transformers import GemmaModel, GemmaConfig
>>> # Initializing a Gemma gemma-7b style configuration
>>> configuration = GemmaConfig()
>>> # Initializing a model from the gemma-7b style configuration
>>> model = GemmaModel(configuration)
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp\transformers\models\gemma\configuration_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma
¶
MindSpore Gemma
mindnlp.transformers.models.gemma.modeling_gemma.GemmaAttention
¶
Bases: Module
Multi-headed attention from 'Attention Is All You Need' paper
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaDecoderLayer
¶
Bases: Module
Source code in mindnlp\transformers\models\gemma\modeling_gemma.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 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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaDecoderLayer.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, use_cache=False, cache_position=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:
|
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\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaDynamicNTKScalingRotaryEmbedding
¶
Bases: GemmaRotaryEmbedding
GemmaRotaryEmbedding extended with Dynamic NTK scaling. Credits to the Reddit users /u/bloc97 and /u/emozilla
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForCausalLM
¶
Bases: GemmaPreTrainedModel
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForCausalLM.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)
¶
| PARAMETER | DESCRIPTION |
|---|---|
labels
|
Labels for computing the masked language modeling loss. Indices should either be in
TYPE:
|
Example:
>>> from transformers import AutoTokenizer, GemmaForCausalLM
>>> model = GemmaForCausalLM.from_pretrained("google/gemma-7b")
>>> tokenizer = AutoTokenizer.from_pretrained("google/gemma-7b")
>>> prompt = "What is your favorite condiment?"
>>> 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]
"What is your favorite condiment?"
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForSequenceClassification
¶
Bases: GemmaPreTrainedModel
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForSequenceClassification.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\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForTokenClassification
¶
Bases: GemmaPreTrainedModel
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaForTokenClassification.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\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaLinearScalingRotaryEmbedding
¶
Bases: GemmaRotaryEmbedding
GemmaRotaryEmbedding extended with linear scaling. Credits to the Reddit user /u/kaiokendev
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
164 165 166 167 168 169 170 171 | |
mindnlp.transformers.models.gemma.modeling_gemma.GemmaModel
¶
Bases: GemmaPreTrainedModel
Transformer decoder consisting of config.num_hidden_layers layers. Each layer is a [GemmaDecoderLayer]
| PARAMETER | DESCRIPTION |
|---|---|
config
|
GemmaConfig
TYPE:
|
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.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\gemma\modeling_gemma.py
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 | |
mindnlp.transformers.models.gemma.modeling_gemma.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\gemma\modeling_gemma.py
227 228 229 230 231 232 233 234 235 236 | |
mindnlp.transformers.models.gemma.modeling_gemma.rotate_half(x)
¶
Rotates half the hidden dims of the input.
Source code in mindnlp\transformers\models\gemma\modeling_gemma.py
193 194 195 196 197 | |
mindnlp.transformers.models.gemma.tokenization_gemma
¶
Tokenization classes for Gemma.
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer
¶
Bases: PreTrainedTokenizer
Construct a Gemma 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 Gemma should be used.
TYPE:
|
spaces_between_special_tokens
|
Whether or not to add spaces between special tokens.
TYPE:
|
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.vocab_size
property
¶
Returns vocab size
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.__getstate__()
¶
Get the state of the GemmaTokenizer object for serialization.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The current instance of the GemmaTokenizer class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.__init__(vocab_file, unk_token='<unk>', bos_token='<bos>', eos_token='<eos>', pad_token='<pad>', sp_model_kwargs=None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, use_default_system_prompt=False, spaces_between_special_tokens=False, **kwargs)
¶
This method initializes an instance of GemmaTokenizer.
| 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:
|
pad_token
|
The padding token. Default is '
TYPE:
|
sp_model_kwargs
|
Optional keyword arguments for SentencePiece model configuration. Default is None.
TYPE:
|
add_bos_token
|
Whether to add the beginning of sequence token. Default is True.
TYPE:
|
add_eos_token
|
Whether to add the end of sequence token. Default is False.
TYPE:
|
clean_up_tokenization_spaces
|
Whether to clean up tokenization spaces. Default is False.
TYPE:
|
use_default_system_prompt
|
Whether to use the default system prompt. Default is False.
TYPE:
|
spaces_between_special_tokens
|
Whether to add spaces between special tokens. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the provided vocab_file is invalid or does not exist. |
OSError
|
If an I/O or OS error occurs while loading the vocab_file. |
TypeError
|
If the provided sp_model_kwargs is not a dictionary. |
RuntimeError
|
If an error occurs during the initialization process. |
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.__setstate__(d)
¶
This method 'setstate' in the class 'GemmaTokenizer' is used to set the internal state of the tokenizer object based on the provided dictionary 'd'.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the GemmaTokenizer class on which this method is called. It represents the tokenizer object itself.
TYPE:
|
d
|
A dictionary containing the state information to be set on the tokenizer object. This dictionary should include the necessary information for reforwarding the object's state.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value explicitly. It updates the state of the GemmaTokenizer object in-place. |
| RAISES | DESCRIPTION |
|---|---|
None
|
However, potential exceptions could be raised during the execution of the code within the method, such as:
|
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
build_inputs_with_special_tokens method in GemmaTokenizer class.
This method takes three parameters:
| PARAMETER | DESCRIPTION |
|---|---|
self
|
GemmaTokenizer object.
|
token_ids_0
|
list of integers. The token IDs for the first sequence.
|
token_ids_1
|
(optional) list of integers. The token IDs for the second sequence.
DEFAULT:
|
| RETURNS | DESCRIPTION |
|---|---|
|
list of integers: The concatenated token IDs with special tokens added at the beginning and end of each sequence. |
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
292 293 294 295 296 297 298 299 300 301 302 303 304 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.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\gemma\tokenization_gemma.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.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\gemma\tokenization_gemma.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.get_vocab()
¶
Returns vocab as a dict
Source code in mindnlp\transformers\models\gemma\tokenization_gemma.py
209 210 211 212 213 | |
mindnlp.transformers.models.gemma.tokenization_gemma.GemmaTokenizer.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\gemma\tokenization_gemma.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma_fast
¶
Gemma Tokenizer
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a Gemma tokenizer fast. Based on byte-level Byte-Pair-Encoding.
This uses notably ByteFallback and no prefix space. Normalization is applied to replace " " with "▁"
Example
>>> from transformers import GemmaTokenizerFast
...
>>> tokenizer = GemmaTokenizerFast.from_pretrained("hf-internal-testing/dummy-gemma")
>>> tokenizer.encode("Hello this is a test")
[2, 4521, 736, 603, 476, 2121]
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.
| 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:
|
pad_token
|
The padding token
TYPE:
|
add_bos_token
|
Whether or not to add an
TYPE:
|
add_eos_token
|
Whether or not to add an
TYPE:
|
Source code in mindnlp\transformers\models\gemma\tokenization_gemma_fast.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 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 | |
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.add_bos_token
property
writable
¶
This method adds the beginning of sentence (BOS) token to the tokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of GemmaTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.add_eos_token
property
writable
¶
Adds an end-of-sentence (EOS) token to the GemmaTokenizerFast object.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the GemmaTokenizerFast class.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
This method adds an EOS token to the GemmaTokenizerFast object. The EOS token is used to mark the end of a sentence or text sequence. It is commonly used in natural language processing tasks such as language modeling and text generation. By adding an EOS token, the GemmaTokenizerFast object can handle text sequences more effectively, allowing for better analysis and processing.
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.can_save_slow_tokenizer: bool
property
¶
Checks if the slow tokenizer can be saved.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the GemmaTokenizerFast class.
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
A boolean value indicating whether the slow tokenizer can be saved. Returns True if the vocab_file exists, otherwise False.
TYPE:
|
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, clean_up_tokenization_spaces=False, unk_token='<unk>', bos_token='<bos>', eos_token='<eos>', pad_token='<pad>', add_bos_token=True, add_eos_token=False, **kwargs)
¶
Initialize GemmaTokenizerFast object.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The GemmaTokenizerFast object itself.
TYPE:
|
vocab_file
|
Path to the vocabulary file. Default is None.
TYPE:
|
tokenizer_file
|
Path to the tokenizer file. Default is None.
TYPE:
|
clean_up_tokenization_spaces
|
Whether to clean up tokenization spaces. Default is False.
TYPE:
|
unk_token
|
Unknown token to be used. Default is '
TYPE:
|
bos_token
|
Beginning of sentence token. Default is '
TYPE:
|
eos_token
|
End of sentence token. Default is '
TYPE:
|
pad_token
|
Padding token. Default is '
TYPE:
|
add_bos_token
|
Whether to add the beginning of sentence token. Default is True.
TYPE:
|
add_eos_token
|
Whether to add the end of sentence token. Default is False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp\transformers\models\gemma\tokenization_gemma_fast.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build inputs with special tokens for the GemmaTokenizerFast.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
An instance of the GemmaTokenizerFast class.
TYPE:
|
token_ids_0
|
A list of token IDs representing the first sequence.
TYPE:
|
token_ids_1
|
A list of token IDs representing the second sequence. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
list
|
A list of token IDs representing the input sequences with added special tokens. |
This method takes two sequences of token IDs and adds special tokens, such as beginning of sequence (bos) and end of sequence (eos) tokens. The special tokens are added based on the configuration of the tokenizer.
The token_ids_0 parameter is a list of token IDs representing the first sequence. This parameter is required.
The token_ids_1 parameter is an optional list of token IDs representing the second sequence. If provided, the method concatenates the first and second sequences with the special tokens in between.
The method returns a list of token IDs representing the input sequences with the special tokens added.
Example
>>> tokenizer = GemmaTokenizerFast()
>>> token_ids_0 = [101, 202, 303]
>>> token_ids_1 = [404, 505]
>>> inputs = tokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1)
>>> print(inputs)
Output:
[101, 202, 303, 102, 404, 505, 102]
Source code in mindnlp\transformers\models\gemma\tokenization_gemma_fast.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary of the GemmaTokenizerFast instance to the specified directory with an optional filename prefix.
| PARAMETER | DESCRIPTION |
|---|---|
self
|
The instance of the GemmaTokenizerFast class.
TYPE:
|
save_directory
|
The directory path where the vocabulary will be saved.
TYPE:
|
filename_prefix
|
An optional 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. |
OSError
|
If the save_directory provided is not a valid directory path. |
IOError
|
If an error occurs during the file copying process. |
Source code in mindnlp\transformers\models\gemma\tokenization_gemma_fast.py
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 | |
mindnlp.transformers.models.gemma.tokenization_gemma_fast.GemmaTokenizerFast.update_post_processor()
¶
Updates the underlying post processor with the current bos_token and eos_token.
Source code in mindnlp\transformers\models\gemma\tokenization_gemma_fast.py
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 | |