reformer
mindnlp.transformers.models.reformer.configuration_reformer
¶
Reformer model configuration
mindnlp.transformers.models.reformer.configuration_reformer.ReformerConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [ReformerModel]. It is used to instantiate a
Reformer 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 ReFormer
google/reformer-crime-and-punishment architecture.
Configuration objects inherit from [PretrainedConfig] and can be used to control the model outputs. Read the
documentation from [PretrainedConfig] for more information.
| PARAMETER | DESCRIPTION |
|---|---|
attention_head_size |
Dimensionality of the projected key, query and value vectors
TYPE:
|
attn_layers |
List of attention layer types in ascending order. It can be chosen between a LSHSelfAttention layer
( For more information on LSHSelfAttention layer, see LSH Self Attention. For more information on LocalSelfAttention layer, see Local Self Attention.
TYPE:
|
axial_pos_embds |
Whether or not to use axial position embeddings. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
axial_norm_std |
The standard deviation of the normal_initializer for initializing the weight matrices of the axial positional encodings.
TYPE:
|
axial_pos_shape |
The position dims of the axial position encodings. During training, the product of the position dims has to be equal to the sequence length. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
axial_pos_embds_dim |
The embedding dims of the axial position encodings. The sum of the embedding dims has to be equal to the hidden size. For more information on how axial position embeddings work, see Axial Position Encodings.
TYPE:
|
chunk_size_lm_head |
The chunk size of the final language model feed forward head layer. A chunk size of 0 means that the feed forward layer is not chunked. A chunk size of n means that the feed forward layer processes n < sequence_length embeddings at a time. For more information on feed forward chunking, see How does Feed Forward Chunking work?.
TYPE:
|
eos_token_id |
The token id for the end-of-sentence token.
TYPE:
|
feed_forward_size |
Dimensionality of the feed_forward layer in the residual attention block.
TYPE:
|
hash_seed |
Seed that can be used to make local sensitive hashing in
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the feed forward layer in the residual attention
block. If string,
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
hidden_size |
Dimensionality of the output hidden states of the residual attention blocks.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
is_decoder |
Whether or not to use a causal mask in addition to the
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
local_chunk_length |
Length of chunk which attends to itself in
TYPE:
|
local_num_chunks_before |
Number of previous neighbouring chunks to attend to in
TYPE:
|
local_num_chunks_after |
Number of following neighbouring chunks to attend to in
TYPE:
|
local_attention_probs_dropout_prob |
The dropout ratio for the attention probabilities in
TYPE:
|
lsh_attn_chunk_length |
Length of chunk which attends to itself in
TYPE:
|
lsh_num_chunks_before |
Number of previous neighbouring chunks to attend to in
TYPE:
|
lsh_num_chunks_after |
Number of following neighbouring chunks to attend to in
TYPE:
|
lsh_attention_probs_dropout_prob |
The dropout ratio for the attention probabilities in
TYPE:
|
max_position_embeddings |
The maximum sequence length that this model might ever be used with. Typically set this to something large just in case (e.g., 512 or 1024 or 2048).
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
num_buckets |
Number of buckets, the key query vectors can be "hashed into" using the locality sensitive hashing scheme.
Each query key vector is hashed into a hash in
TYPE:
|
num_hashes |
Number of hashing rounds (e.g., number of random rotations) in Local Sensitive Hashing scheme. The higher
TYPE:
|
pad_token_id |
The token id for the padding token.
TYPE:
|
vocab_size |
\
Vocabulary size of the Reformer model. Defines the number of different tokens that can be represented by
the
TYPE:
|
tie_word_embeddings |
Whether to tie input and output embeddings.
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models).
TYPE:
|
classifier_dropout |
The dropout ratio for the classification head.
TYPE:
|
Example
>>> from transformers import ReformerConfig, ReformerModel
...
>>> # Initializing a Reformer configuration
>>> configuration = ReformerConfig()
...
>>> # Initializing a Reformer model (with random weights)
>>> model = ReformerModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/reformer/configuration_reformer.py
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 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 | |
mindnlp.transformers.models.reformer.configuration_reformer.ReformerConfig.__init__(attention_head_size=64, attn_layers=['local', 'lsh', 'local', 'lsh', 'local', 'lsh'], axial_norm_std=1.0, axial_pos_embds=True, axial_pos_shape=[64, 64], axial_pos_embds_dim=[64, 192], chunk_size_lm_head=0, eos_token_id=2, feed_forward_size=512, hash_seed=None, hidden_act='relu', hidden_dropout_prob=0.05, hidden_size=256, initializer_range=0.02, is_decoder=False, layer_norm_eps=1e-12, local_num_chunks_before=1, local_num_chunks_after=0, local_attention_probs_dropout_prob=0.05, local_attn_chunk_length=64, lsh_attn_chunk_length=64, lsh_attention_probs_dropout_prob=0.0, lsh_num_chunks_before=1, lsh_num_chunks_after=0, max_position_embeddings=4096, num_attention_heads=12, num_buckets=None, num_hashes=1, pad_token_id=0, vocab_size=320, tie_word_embeddings=False, use_cache=True, classifier_dropout=None, **kwargs)
¶
Initializes a new instance of the ReformerConfig class.
| PARAMETER | DESCRIPTION |
|---|---|
attention_head_size |
The size of each attention head.
TYPE:
|
attn_layers |
The list of attention layer types to be used.
TYPE:
|
axial_norm_std |
Standard deviation for axial positional embeddings normalization.
TYPE:
|
axial_pos_embds |
Whether to use axial positional embeddings.
TYPE:
|
axial_pos_shape |
The shape of axial positional embeddings.
TYPE:
|
axial_pos_embds_dim |
The dimensions of axial positional embeddings.
TYPE:
|
chunk_size_lm_head |
Size of chunk for the language model head.
TYPE:
|
eos_token_id |
The token ID for the end-of-sequence token.
TYPE:
|
feed_forward_size |
The size of the feed-forward network.
TYPE:
|
hash_seed |
The seed for hashing functions.
TYPE:
|
hidden_act |
The activation function for hidden layers.
TYPE:
|
hidden_dropout_prob |
The dropout probability for hidden layers.
TYPE:
|
hidden_size |
The size of the hidden layers.
TYPE:
|
initializer_range |
The range for weight initialization.
TYPE:
|
is_decoder |
Whether the model is used as a decoder.
TYPE:
|
layer_norm_eps |
Epsilon value for layer normalization.
TYPE:
|
local_num_chunks_before |
Number of local attention chunks before.
TYPE:
|
local_num_chunks_after |
Number of local attention chunks after.
TYPE:
|
local_attention_probs_dropout_prob |
Dropout probability for local attention.
TYPE:
|
local_attn_chunk_length |
Length of chunks for local attention.
TYPE:
|
lsh_attn_chunk_length |
Length of chunks for LSH attention.
TYPE:
|
lsh_attention_probs_dropout_prob |
Dropout probability for LSH attention.
TYPE:
|
lsh_num_chunks_before |
Number of LSH attention chunks before.
TYPE:
|
lsh_num_chunks_after |
Number of LSH attention chunks after.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings.
TYPE:
|
num_attention_heads |
The number of attention heads.
TYPE:
|
num_buckets |
The number of buckets for hashing.
TYPE:
|
num_hashes |
The number of hashes for LSH attention.
TYPE:
|
pad_token_id |
The token ID for padding.
TYPE:
|
vocab_size |
The size of the vocabulary.
TYPE:
|
tie_word_embeddings |
Whether to tie word embeddings.
TYPE:
|
use_cache |
Whether to cache intermediate values.
TYPE:
|
classifier_dropout |
Dropout probability for classifier layers.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/configuration_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer
¶
MindSpore REFORMER model.
mindnlp.transformers.models.reformer.modeling_reformer.AxialPositionEmbeddings
¶
Bases: Module
Constructs axial position embeddings. Useful for very long input sequences to save memory and time.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.AxialPositionEmbeddings.__init__(config)
¶
This method initializes an instance of the AxialPositionEmbeddings class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the AxialPositionEmbeddings class.
|
config |
An object containing configuration parameters for the axial position embeddings.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the sum of axial_pos_embds_dim does not match the hidden_size specified in the configuration. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.AxialPositionEmbeddings.forward(position_ids)
¶
This method forwards position encodings based on the given position IDs and axial position weights.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the AxialPositionEmbeddings class.
|
position_ids |
A 2D tensor representing the position IDs of the input sequences. It has a shape of (batch_size, sequence_length).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
Raised if the following conditions are met:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.ChunkReformerFeedForward
¶
Bases: Module
This class represents a feed-forward module for chunked reformer attention output in a neural network.
The ChunkReformerFeedForward class inherits from the nn.Module class and is designed to process attention output in a chunked manner. It applies layer normalization, dense transformation, and output transformation to the input hidden states.
| ATTRIBUTE | DESCRIPTION |
|---|---|
chunk_size_feed_forward |
The size of each chunk for processing the attention output.
TYPE:
|
seq_len_dim |
The dimension representing the sequence length in the attention output.
TYPE:
|
layer_norm |
The layer normalization module applied to the hidden states.
TYPE:
|
dense |
The dense transformation module applied to the layer-normalized hidden states.
TYPE:
|
output |
The final output transformation module applied to the transformed hidden states. |
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes a new instance of the ChunkReformerFeedForward class. Args:
|
forward |
Constructs the output of the ChunkReformerFeedForward module. Args:
Returns:
|
forward_chunk |
Constructs a chunk of the ChunkReformerFeedForward module. Args:
Returns:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 | |
mindnlp.transformers.models.reformer.modeling_reformer.ChunkReformerFeedForward.__init__(config)
¶
Initializes a ChunkReformerFeedForward instance.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The ChunkReformerFeedForward instance itself.
TYPE:
|
config |
A configuration object containing the necessary parameters for initialization.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the provided config is not of the expected type. |
ValueError
|
If any required parameter is missing in the config. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 | |
mindnlp.transformers.models.reformer.modeling_reformer.ChunkReformerFeedForward.forward(attention_output)
¶
Constructs the feed-forward chunk reformer for the given attention output.
| PARAMETER | DESCRIPTION |
|---|---|
self |
Instance of the ChunkReformerFeedForward class.
TYPE:
|
attention_output |
The attention output tensor to be processed by the chunk reformer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the input parameters are not of the expected types. |
ValueError
|
If the chunk size for feed-forward is invalid. |
RuntimeError
|
If there is an issue with applying chunking during the forward pass. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 | |
mindnlp.transformers.models.reformer.modeling_reformer.ChunkReformerFeedForward.forward_chunk(hidden_states)
¶
Constructs a chunk of reformer feed forward layer.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ChunkReformerFeedForward class.
TYPE:
|
hidden_states |
The input tensor containing the hidden states.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
AttributeError
|
If the 'layer_norm', 'dense', or 'output' attributes are not found in the instance. |
ValueError
|
If the 'hidden_states' parameter is not a valid tensor. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 | |
mindnlp.transformers.models.reformer.modeling_reformer.EfficientAttentionMixin
¶
A few utilities for nn.Modules in Reformer, to be used as a mixin.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.LSHSelfAttention
¶
Bases: Module, EfficientAttentionMixin
This class represents a self-attention mechanism using Locality Sensitive Hashing (LSH) for efficient attention computation. It inherits from nn.Module, EfficientAttentionMixin.
The class implements LSH self-attention mechanism for neural networks. It includes methods for initializing the LSH attention layer, forwarding the attention mechanism, computing attention masks, hashing vectors, and other related operations.
| ATTRIBUTE | DESCRIPTION |
|---|---|
config |
Configuration parameters for the LSH self-attention layer.
|
chunk_length |
Length of each attention chunk.
|
num_hashes |
Number of hashes used in the LSH mechanism.
|
num_buckets |
Number of buckets used for hashing.
|
num_chunks_before |
Number of chunks to consider before the current chunk.
|
num_chunks_after |
Number of chunks to consider after the current chunk.
|
hash_seed |
Seed for random hash rotations.
|
is_decoder |
Flag indicating if the layer is used in a decoder.
|
max_position_embeddings |
Maximum position embeddings allowed.
|
dropout |
Dropout probability for attention weights.
|
num_attention_heads |
Number of attention heads.
|
attention_head_size |
Size of each attention head.
|
all_head_size |
Total size of all attention heads.
|
hidden_size |
Size of the hidden layer.
|
query_key |
Dense layer for query and key vectors.
|
value |
Dense layer for value vectors.
|
self_mask_value_float16 |
Float16 value for masking in self-attention.
|
self_mask_value_float32 |
Float32 value for masking in self-attention.
|
mask_value_float16 |
Float16 value for general masking.
|
mask_value_float32 |
Float32 value for general masking.
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Constructs the LSH self-attention mechanism based on input hidden states and optional parameters. |
_query_per_attn_head |
Computes query vectors per attention head. |
_value_per_attn_head |
Computes value vectors per attention head. |
_hash_vectors |
Hashes input vectors into buckets for attention computation. |
_get_sorted_bucket_idx_and_undo_sorted_bucket_idx |
Computes sorted bucket indices for efficient attention calculation. |
_set_num_buckets |
Sets the number of buckets based on the input sequence length. |
_attend |
Computes attention scores and outputs based on query, key, and value vectors. |
_compute_attn_mask |
Computes attention mask based on query and key indices. |
_get_relevant_hid_states_and_buckets |
Retrieves relevant hidden states and buckets for efficient attention calculation. |
_expand_to_indices_in_relevant_chunk |
Expands indices for relevant chunks in hidden states. |
_len_and_dim_norm |
Normalizes vectors based on length and attention head size. |
_len_norm |
Length normalization for input vectors. |
_gather_by_expansion |
Expands indices and vectors for all hashes and gathers relevant elements. |
Note
This class is designed for implementing efficient self-attention mechanisms using Locality Sensitive Hashing.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 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 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 | |
mindnlp.transformers.models.reformer.modeling_reformer.LSHSelfAttention.__init__(config)
¶
Initializes the LSHSelfAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the LSHSelfAttention class.
TYPE:
|
config |
An object containing the configuration parameters.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.LSHSelfAttention.forward(hidden_states, attention_mask=None, head_mask=None, num_hashes=None, buckets=None, past_buckets_states=None, use_cache=False, output_attentions=False, **kwargs)
¶
Constructs the LSH self-attention mechanism.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The object instance.
|
hidden_states |
The input hidden states with shape (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
Masking tensor for attention scores with shape (batch_size, sequence_length). Defaults to None.
TYPE:
|
head_mask |
Masking tensor for individual attention heads with shape (num_attention_heads). Defaults to None.
TYPE:
|
num_hashes |
Number of hash functions for LSH. Defaults to None.
TYPE:
|
buckets |
Hash buckets for attention mechanism with shape (batch_size, num_attention_heads, num_hashes, sequence_length). Defaults to None.
TYPE:
|
past_buckets_states |
Tuple containing past hash buckets and states. Defaults to None.
TYPE:
|
use_cache |
Flag to enable caching for attention mechanism. Defaults to False.
TYPE:
|
output_attentions |
Flag to output attention probabilities. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the input sequence length is not equal to 1 when |
AssertionError
|
If the last dimension of query_key_vectors or value_vectors does not match the attention head size. |
AssertionError
|
If the last dimension of the buckets tensor does not match the expected value based on the number of hashes and sequence length. |
AssertionError
|
If the shape of out_vectors does not match the expected shape. |
AssertionError
|
If the configuration parameters are not set correctly when chunk_length is None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 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 | |
mindnlp.transformers.models.reformer.modeling_reformer.LocalSelfAttention
¶
Bases: Module, EfficientAttentionMixin
The LocalSelfAttention class is a subclass of nn.Module and EfficientAttentionMixin that represents
a local self-attention mechanism. This mechanism is commonly used in transformer-based models for
processing sequential data.
| ATTRIBUTE | DESCRIPTION |
|---|---|
`num_attention_heads` |
The number of attention heads.
TYPE:
|
`chunk_length` |
The length of each attention chunk.
TYPE:
|
`num_chunks_before` |
The number of chunks before the current position.
TYPE:
|
`num_chunks_after` |
The number of chunks after the current position.
TYPE:
|
`is_decoder` |
Indicates whether the attention is used in a decoder architecture.
TYPE:
|
`pad_token_id` |
The token ID used for padding.
TYPE:
|
`attention_head_size` |
The size of each attention head.
TYPE:
|
`all_head_size` |
The total size of all attention heads.
TYPE:
|
`hidden_size` |
The hidden size of the input.
TYPE:
|
`query` |
The dense layer used for computing query vectors.
TYPE:
|
`key` |
The dense layer used for computing key vectors.
TYPE:
|
`value` |
The dense layer used for computing value vectors.
TYPE:
|
`dropout` |
The dropout rate for attention probabilities.
TYPE:
|
`mask_value_float16` |
The mask value for float16 data type.
TYPE:
|
`mask_value_float32` |
The mask value for float32 data type.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
`forward` |
Computes the local self-attention mechanism for the given hidden states. |
`_compute_attn_mask` |
Computes the attention mask based on query and key indices. |
`_retrieve_relevant_hidden_states` |
Retrieves the relevant hidden states from previous states. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 | |
mindnlp.transformers.models.reformer.modeling_reformer.LocalSelfAttention.__init__(config)
¶
Initializes the LocalSelfAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the LocalSelfAttention class.
|
config |
An object containing configuration parameters for the attention mechanism. This parameter is expected to have the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the configuration parameters provided are invalid or missing. |
TypeError
|
If any of the configuration attribute types are incorrect. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 | |
mindnlp.transformers.models.reformer.modeling_reformer.LocalSelfAttention.forward(hidden_states, attention_mask=None, head_mask=None, past_buckets_states=None, use_cache=False, output_attentions=False, **kwargs)
¶
Constructs the local self-attention mechanism.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the LocalSelfAttention class.
TYPE:
|
hidden_states |
The input hidden states with shape (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
Mask to prevent attention to certain positions. Shape should be (batch_size, 1, sequence_length, sequence_length). Defaults to None.
TYPE:
|
head_mask |
Mask to prevent attention to certain heads. Shape should be (num_attention_heads,) or (num_layers, num_attention_heads). Defaults to None.
TYPE:
|
past_buckets_states |
Tuple containing the previous key and value hidden states. Defaults to None.
TYPE:
|
use_cache |
Flag indicating whether to use cached key and value states. Defaults to False.
TYPE:
|
output_attentions |
Flag indicating whether to output attention probabilities. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
LocalSelfAttentionOutput
|
An object containing the output hidden states and attention probabilities. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
Raised if the conditions specified in the method are not met. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 | |
mindnlp.transformers.models.reformer.modeling_reformer.PositionEmbeddings
¶
Bases: Module
Constructs conventional position embeddings of shape [max_pos_embeddings, hidden_size].
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.PositionEmbeddings.__init__(config)
¶
Initializes an instance of the PositionEmbeddings class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the PositionEmbeddings class.
|
config |
An instance of the configuration class containing the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | |
mindnlp.transformers.models.reformer.modeling_reformer.PositionEmbeddings.forward(position_ids)
¶
Constructs position embeddings based on given position IDs.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the PositionEmbeddings class.
TYPE:
|
position_ids |
A tensor containing the position IDs for which embeddings need to be forwarded. It should be a 1D tensor of integers representing the positions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
position_embeddings
|
The method returns the forwarded position embeddings. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the position_ids tensor is not provided or is not a valid 1D tensor. |
TypeError
|
If the dropout rate is not a float or the training flag is not a boolean. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerAttention
¶
Bases: Module
This class represents a ReformerAttention module, which is used in the Reformer model for attention mechanisms. It inherits from the nn.Module class.
| ATTRIBUTE | DESCRIPTION |
|---|---|
layer_id |
The ID of the attention layer.
TYPE:
|
attn_layers |
The types of attention layers used in the Reformer model.
TYPE:
|
layer_norm |
Layer normalization module applied to the input hidden states.
TYPE:
|
self_attention |
The self-attention module used based on the attention layer type.
TYPE:
|
output |
The module responsible for the final output of the self-attention mechanism.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Applies the ReformerAttention module to the input hidden_states and returns the attention output. |
Note
- Only 'lsh' and 'local' attention layer types are supported. The attention layer types can be selected from ['lsh', 'local'] only.
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the input attention layer types are not 'lsh' or 'local'. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerAttention.__init__(config, layer_id=0)
¶
Initialize the ReformerAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerAttention class.
TYPE:
|
config |
An object containing configuration settings for the attention layer.
TYPE:
|
layer_id |
The ID of the layer within the attention module. Defaults to 0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
NotImplementedError
|
If the specified attention layer types are not 'lsh' or 'local'. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerAttention.forward(hidden_states, attention_mask=None, head_mask=None, num_hashes=None, past_buckets_states=None, use_cache=False, orig_sequence_length=None, output_attentions=False, buckets=None)
¶
This method forwards the attention output in the ReformerAttention class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
Reference to the class instance.
|
hidden_states |
Input tensor containing the hidden states.
TYPE:
|
attention_mask |
Mask tensor for controlling attention computation.
TYPE:
|
head_mask |
Mask tensor for controlling attention within each head.
TYPE:
|
num_hashes |
Number of hashes to use in hashing attention.
TYPE:
|
past_buckets_states |
Tensor containing past bucket states.
TYPE:
|
use_cache |
Flag indicating whether to use cache for attention computation.
TYPE:
|
orig_sequence_length |
Original length of the sequence.
TYPE:
|
output_attentions |
Flag indicating whether to output attention weights.
TYPE:
|
buckets |
Tensor containing bucket information.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the orig_sequence_length is less than 1. |
AttributeError
|
If the self_attention_outputs object does not have the 'buckets' attribute. |
IndexError
|
If the layer_id in past_buckets_states is out of bounds. |
RuntimeError
|
If there is an issue with concatenating tensors using ops.cat. |
TypeError
|
If the input types are not as expected. |
Exception
|
For any other unforeseen errors during the method execution. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerClassificationHead
¶
Bases: Module
Head for sentence-level classification tasks.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerClassificationHead.__init__(config)
¶
Initializes the ReformerClassificationHead class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerClassificationHead class. |
config |
The configuration object containing the settings for the Reformer model. It should have attributes such as hidden_size (int), classifier_dropout (float, optional), hidden_dropout_prob (float), and num_labels (int). The config object is required and should not be None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the config parameter is None or if any of the required attributes in the config object are missing. |
TypeError
|
If the config parameter is not of the expected type or if any attribute in the config object has an unexpected type. |
RuntimeError
|
If there is an issue with the initialization of the Dense and Dropout layers. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerClassificationHead.forward(hidden_states, **kwargs)
¶
Constructs the classification head for the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerClassificationHead class. |
hidden_states |
The hidden states of the input sequence. It should have shape (batch_size, sequence_length, hidden_size).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value. |
| RAISES | DESCRIPTION |
|---|---|
None
|
No exceptions are raised by this method. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEmbeddings
¶
Bases: Module
Construct the embeddings from word, position and token_type embeddings.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEmbeddings.__init__(config)
¶
Initializes the ReformerEmbeddings class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerEmbeddings class.
TYPE:
|
config |
An object containing configuration parameters for the embeddings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
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 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEmbeddings.forward(input_ids=None, position_ids=None, inputs_embeds=None, start_idx_pos_encodings=0)
¶
Constructs the embeddings for the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerEmbeddings class.
TYPE:
|
input_ids |
The input tensor representing the tokenized input sequence. It has shape [batch_size, sequence_length] and each element corresponds to a token ID.
TYPE:
|
position_ids |
The tensor representing the position encodings for each token in the input sequence. It has shape [batch_size, sequence_length] and each element corresponds to a position ID.
TYPE:
|
inputs_embeds |
The tensor representing the embedded input sequence. It has shape [batch_size, sequence_length, embedding_size] and each element corresponds to an embedding vector.
TYPE:
|
start_idx_pos_encodings |
The starting index for the positional encodings.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The embeddings tensor representing the input sequence with positional information. It has shape [batch_size, sequence_length, embedding_size]. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the sequence length of the position_ids tensor is greater than the maximum allowed sequence length. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEncoder
¶
Bases: Module
The 'ReformerEncoder' class is a Python class that represents the encoder component of the Reformer model. It inherits from the 'nn.Module' class.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dropout |
The dropout probability for the hidden states.
TYPE:
|
layers |
A list of 'ReformerLayer' instances representing the layers of the encoder.
TYPE:
|
layer_norm |
A layer normalization module.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes a new instance of the 'ReformerEncoder' class. |
forward |
Constructs the encoder by applying the Reformer layers to the input hidden states. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEncoder.__init__(config)
¶
Initializes a ReformerEncoder instance.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The ReformerEncoder instance to be initialized.
TYPE:
|
config |
A configuration object containing settings for the ReformerEncoder.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If config is not of type Config. |
ValueError
|
If config is missing any required attributes. |
ValueError
|
If config.hidden_dropout_prob is not a float. |
ValueError
|
If config.num_hidden_layers is not an integer. |
ValueError
|
If config.hidden_size is not an integer. |
ValueError
|
If config.layer_norm_eps is not a float. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerEncoder.forward(hidden_states, attention_mask=None, head_mask=None, num_hashes=None, past_buckets_states=None, use_cache=False, orig_sequence_length=None, output_hidden_states=False, output_attentions=False)
¶
Constructs the ReformerEncoder output given the input parameters.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The class instance.
|
hidden_states |
The input hidden states. Shape [batch_size, sequence_length, hidden_size].
TYPE:
|
attention_mask |
The attention mask. Shape [batch_size, sequence_length, sequence_length]. Masks the attention scores for padding tokens. Defaults to None.
TYPE:
|
head_mask |
The head mask. Shape [num_attention_heads, sequence_length, sequence_length]. Masks the attention scores for specific attention heads. Defaults to None.
TYPE:
|
num_hashes |
The number of hashes to use for LSH attention. Defaults to None.
TYPE:
|
past_buckets_states |
The list of past bucket states. Each element is a tuple of two tensors. Shape [(buckets, num_hashes, sequence_length // bucket_size, embedding_dim), (buckets, num_hashes, sequence_length // bucket_size)]. Defaults to None.
TYPE:
|
use_cache |
Whether to use cache for fast decoding. Defaults to False.
TYPE:
|
orig_sequence_length |
The original sequence length before padding. Defaults to None.
TYPE:
|
output_hidden_states |
Whether to output all hidden states. Defaults to False.
TYPE:
|
output_attentions |
Whether to output all attention matrices. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReformerEncoderOutput
|
An instance of the ReformerEncoderOutput class containing the following attributes:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardDense
¶
Bases: Module
ReformerFeedForwardDense represents a feedforward dense layer used in a Reformer model for neural network operations.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dropout |
The dropout rate for the hidden states.
TYPE:
|
act_fn |
The activation function used for the hidden states.
TYPE:
|
dense |
The dense layer for transforming hidden states.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes the ReformerFeedForwardDense instance with the provided configuration. |
forward |
Constructs the feedforward dense layer by applying dense transformation, dropout, and activation function to the hidden states. |
This class inherits from nn.Module and includes methods to initialize and forward the feedforward dense layer in a Reformer model.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardDense.__init__(config)
¶
Initializes a ReformerFeedForwardDense object with the specified configuration.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerFeedForwardDense class.
TYPE:
|
config |
The configuration object containing various settings for the dense layer. Expected to have the following attributes:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the config.hidden_dropout_prob is not a float. |
KeyError
|
If the config.hidden_act is not a valid activation function name. |
AttributeError
|
If the config object is missing any of the required attributes. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardDense.forward(hidden_states)
¶
Constructs the feedforward dense layer for the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerFeedForwardDense class.
TYPE:
|
hidden_states |
The input hidden states to be processed by the feedforward dense layer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tensor
|
The processed hidden states after passing through the feedforward dense layer. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the hidden_states tensor is not provided. |
TypeError
|
If the input hidden_states tensor is not of type tensor. |
RuntimeError
|
If an error occurs during the dropout operation. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardOutput
¶
Bases: Module
Represents the output of the feed forward layer in a Reformer neural network.
This class inherits from nn.Module and contains methods for initializing and forwarding the feed forward layer output.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dropout |
The dropout rate for the hidden units.
TYPE:
|
dense |
The fully connected layer for transforming input hidden states.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes the ReformerFeedForwardOutput with the given configuration. |
forward |
Constructs the output of the feed forward layer using the provided hidden states. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardOutput.__init__(config)
¶
Initializes an instance of the ReformerFeedForwardOutput class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The object instance.
|
config |
An object containing the configuration parameters.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerFeedForwardOutput.forward(hidden_states)
¶
Constructs the output of the feed-forward layer in the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerFeedForwardOutput class. |
hidden_states |
The input hidden states to be processed by the feed-forward layer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
tensor
|
The processed hidden states after passing through the feed-forward layer. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If the hidden_states tensor is not valid or has incorrect dimensions. |
RuntimeError
|
If an error occurs during the computation of the output tensor. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM
¶
Bases: ReformerPreTrainedModel
A Reformer model with a language modeling head for masked language modeling tasks.
This class inherits from ReformerPreTrainedModel and utilizes the Reformer architecture and a language modeling head for
masking language modeling tasks. The class is capable of generating output embeddings and setting new embeddings with
the provided methods, get_output_embeddings() and set_output_embeddings(), respectively, and the forward()
method forwards the model and computes the masked language modeling loss if the labels argument is provided.
The ReformerForMaskedLM class takes a config argument, which is an instance of ReformerConfig. The class
implements the __init__() method that initializes the parent class with the provided config. The method also
checks that config.is_decoder=False for bi-directional self-attention.
The class has the following methods:
get_output_embeddings(): Returns the decoder for the language modeling head.set_output_embeddings(new_embeddings): Sets the decoder for the language modeling head tonew_embeddings.-
forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, labels=None, output_hidden_states=None, output_attentions=None, return_dict=None): Constructs the model and computes the masked language modeling loss if thelabelsargument is provided. This method takes several optional input arguments and returns a tuple with the following elements:- If
labelsis notNone, returns a tuple(masked_lm_loss, logits, hidden_states, attentions). -
If
labelsisNone, returns a tuple(logits, hidden_states, attentions).masked_lm_lossis the masked language modeling loss, computed as cross-entropy loss between the logits and the labels.logitsis a tensor of shape(batch_size, sequence_length, vocab_size)containing the unnormalized log probabilities for each vocabulary token.hidden_statesis a tuple of lengthconfig.num_hidden_layers + 1with each tensor of shape(batch_size, sequence_length, hidden_size)representing the hidden states of the model at each layer.attentionsis a tuple of lengthconfig.num_hidden_layerswith each tensor of shape(batch_size, num_heads, sequence_length, sequence_length)representing the attention weights for each layer.
- If
Note
This class utilizes a false checkpoint since there is no available pre-trained model for the masked language modeling task with the Reformer architecture.
Example
>>> from transformers import ReformerForMaskedLM, ReformerConfig
...
>>> # Initializing a Reformer configuration
>>> config = ReformerConfig()
...
>>> # Initializing a ReformerForMaskedLM model with the configuration
>>> model = ReformerForMaskedLM(config)
...
>>> # Getting the decoder for the language modeling head
>>> decoder = model.get_output_embeddings()
...
>>> # Setting new embeddings for the language modeling head
>>> model.set_output_embeddings(new_embeddings)
...
>>> # Constructing the model and computing the masked language modeling loss
>>> masked_lm_loss, logits, hidden_states, attentions = model.forward(input_ids, position_ids, attention_mask,
... head_mask, inputs_embeds, num_hashes, labels, output_hidden_states, output_attentions, return_dict)
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM.__init__(config)
¶
Initializes an instance of the ReformerForMaskedLM class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the class.
|
config |
An instance of the configuration class that specifies the model configuration.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, labels=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
| PARAMETER | DESCRIPTION |
|---|---|
labels |
Labels for computing the masked language modeling loss. Indices should be in
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[Tuple, MaskedLMOutput]
|
Union[Tuple, MaskedLMOutput] |
This example uses a false checkpoint since we don't have any available pretrained model for the masked language modeling task with the Reformer architecture.
Example
>>> import torch
>>> from transformers import AutoTokenizer, ReformerForMaskedLM
...
>>> tokenizer = AutoTokenizer.from_pretrained("hf-internal-testing/tiny-random-reformer")
>>> model = ReformerForMaskedLM.from_pretrained("hf-internal-testing/tiny-random-reformer")
...
>>> # add mask_token
>>> tokenizer.add_special_tokens({"mask_token": "[MASK]"}) # doctest: +IGNORE_RESULT
>>> inputs = tokenizer("The capital of France is [MASK].", return_tensors="pt")
...
>>> # resize model's embedding matrix
>>> model.resize_token_embeddings(new_num_tokens=model.config.vocab_size + 1) # doctest: +IGNORE_RESULT
...
>>> with torch.no_grad():
... logits = model(**inputs).logits
...
>>> # retrieve index of [MASK]
>>> mask_token_index = (inputs.input_ids == tokenizer.mask_token_id)[0].nonzero(as_tuple=True)[0]
...
>>> predicted_token_id = logits[0, mask_token_index].argmax(axis=-1)
>>> predicted_token = tokenizer.decode(predicted_token_id)
>>> labels = tokenizer("The capital of France is Paris.", return_tensors="pt")["input_ids"]
>>> # mask labels of non-[MASK] tokens
>>> labels = torch.where(
... inputs.input_ids == tokenizer.mask_token_id, labels[:, : inputs["input_ids"].shape[-1]], -100
... )
...
>>> outputs = model(**inputs, labels=labels)
>>> loss = round(outputs.loss.item(), 2)
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM.get_output_embeddings()
¶
This method retrieves the output embeddings from the ReformerForMaskedLM model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
ReformerForMaskedLM - The instance of the ReformerForMaskedLM class.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns None as it only retrieves the output embeddings without any additional processing. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForMaskedLM.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerForMaskedLM class.
TYPE:
|
new_embeddings |
The new embeddings to be set as the output embeddings. It can be of any type.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
This method sets the output embeddings of the ReformerForMaskedLM model to the provided new_embeddings. The new_embeddings can be any type and will be assigned to the decoder of the lm_head.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForQuestionAnswering
¶
Bases: ReformerPreTrainedModel
This class represents a Reformer model for question answering tasks. It is a subclass of the ReformerPreTrainedModel.
The ReformerForQuestionAnswering class implements the necessary methods to perform question answering on a given input sequence. It includes an initialization method, a forwardion method, and helper methods.
The initialization method (init) takes a configuration object as input and initializes the Reformer model, as well as the necessary layers for question answering. The number of labels is also set in this method.
The forwardion method (forward) takes various input tensors and parameters and performs question answering. It utilizes the Reformer model to obtain the sequence output, which is then passed through a dense layer for classification. The start and end logits are obtained from the output and are returned along with other relevant outputs.
The forward method also allows optional inputs for start_positions and end_positions, which are used to compute the token classification loss. The loss is calculated using the cross-entropy loss function. If start_positions and end_positions are provided, the total loss is computed as the average of the start and end losses.
The forward method returns the start and end logits, along with other relevant outputs, depending on the value of the return_dict parameter. If return_dict is False, a tuple of outputs is returned. If return_dict is True, a QuestionAnsweringModelOutput object is returned, containing the start_logits, end_logits, hidden_states, and attentions.
Note
This class inherits from the ReformerPreTrainedModel class, which provides additional functionality for pre-training and fine-tuning the Reformer model.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForQuestionAnswering.__init__(config)
¶
Initializes a new instance of the ReformerForQuestionAnswering class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the class. |
config |
The configuration for the ReformerForQuestionAnswering model.
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method does not return any value. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForQuestionAnswering.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, start_positions=None, end_positions=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
| PARAMETER | DESCRIPTION |
|---|---|
start_positions |
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 (
TYPE:
|
end_positions |
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 (
TYPE:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForSequenceClassification
¶
Bases: ReformerPreTrainedModel
ReformerForSequenceClassification¶
The ReformerForSequenceClassification class is a Python class that represents a sequence classification model
based on the Reformer architecture. It inherits from the ReformerPreTrainedModel class.
Summary¶
The ReformerForSequenceClassification class provides a sequence classification model that can be used for
tasks such as sentiment analysis, text classification, and natural language inference.
| ATTRIBUTE | DESCRIPTION |
|---|---|
`num_labels` |
int The number of labels for the sequence classification task.
|
`config` |
ReformerConfig The configuration object that holds all the hyperparameters of the model.
|
`reformer` |
ReformerModel The Reformer model used for encoding the input sequence.
|
`classifier` |
ReformerClassificationHead The classification head that takes the encoded sequence and produces the final logits.
|
| METHOD | DESCRIPTION |
|---|---|
`forward` |
Constructs the sequence classification model and performs forward propagation. Parameters:
Returns:
|
Example
>>> import mindspore as ms
>>> from transformers import ReformerForSequenceClassification
...
>>> model = ReformerForSequenceClassification(config)
...
>>> inputs = {
... 'input_ids': ms.Tensor([[1, 2, 3, 4, 5]]),
... 'attention_mask': ms.Tensor([[1, 1, 1, 1, 1]])
... }
...
>>> outputs = model.forward(**inputs)
>>> logits = outputs.logits
>>> predicted_class_id = logits.argmax(axis=1).item()
>>> label = model.config.id2label[predicted_class_id]
>>> ```
>>> To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to the `ReformerForSequenceClassification` forwardor:
>>> ```python
>>> num_labels = len(model.config.id2label)
>>> model = ReformerForSequenceClassification(config, num_labels=num_labels)
...
>>> inputs = {
... 'input_ids': ms.Tensor([[1, 2, 3, 4, 5]]),
... 'attention_mask': ms.Tensor([[1, 1, 1, 1, 1]]),
... 'labels': ms.Tensor([1])
... }
...
>>> outputs = model.forward(**inputs)
>>> loss = outputs.loss
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForSequenceClassification.__init__(config)
¶
Initializes the ReformerForSequenceClassification class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the class.
|
config |
An instance of the configuration containing settings for the ReformerForSequenceClassification. It should contain the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerForSequenceClassification.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, labels=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
| PARAMETER | DESCRIPTION |
|---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[Tuple, SequenceClassifierOutput]
|
Union[Tuple, SequenceClassifierOutput] |
Example
>>> import torch
>>> from transformers import AutoTokenizer, ReformerForSequenceClassification
...
>>> tokenizer = AutoTokenizer.from_pretrained("google/reformer-crime-and-punishment")
>>> model = ReformerForSequenceClassification.from_pretrained("google/reformer-crime-and-punishment")
...
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
...
>>> with torch.no_grad():
... logits = model(**inputs).logits
...
>>> predicted_class_id = logits.argmax().item()
>>> label = model.config.id2label[predicted_class_id]
>>> # To train a model on `num_labels` classes, you can pass `num_labels=num_labels` to `.from_pretrained(...)`
>>> num_labels = len(model.config.id2label)
>>> model = ReformerForSequenceClassification.from_pretrained(
... "google/reformer-crime-and-punishment", num_labels=num_labels
... )
...
>>> labels = mindspore.tensor(1)
>>> loss = model(**inputs, labels=labels).loss
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerLayer
¶
Bases: Module
Represents a Reformer layer that consists of an attention mechanism and a feed forward network. This class inherits from nn.Module.
| ATTRIBUTE | DESCRIPTION |
|---|---|
attention_seed |
Seed for the attention layer to ensure deterministic dropout behavior.
TYPE:
|
feed_forward_seed |
Seed for the feed forward layer to ensure deterministic dropout behavior.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes the ReformerLayer with attention and feed forward components. |
_init_attention_seed |
Sets a new seed for the attention layer to ensure deterministic dropout behavior. |
_init_feed_forward_seed |
Sets a new seed for the feed forward layer to ensure deterministic dropout behavior. |
forward |
Constructs the Reformer layer by applying attention and feed forward operations and returning the output. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerLayer.__init__(config, layer_id=0)
¶
Initializes a new instance of the ReformerLayer class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the class.
|
config |
The configuration object containing various settings and parameters.
TYPE:
|
layer_id |
The identifier for the layer. Defaults to 0.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerLayer.forward(prev_attn_output, hidden_states, attention_mask=None, head_mask=None, num_hashes=None, past_buckets_states=None, use_cache=False, orig_sequence_length=None, output_attentions=False)
¶
Constructs a Reformer layer by performing attention and feed forward operations on the input hidden states.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The object instance.
|
prev_attn_output |
The previous attention output tensor. Shape: (batch_size, sequence_length, hidden_size)
TYPE:
|
hidden_states |
The input hidden states tensor. Shape: (batch_size, sequence_length, hidden_size)
TYPE:
|
attention_mask |
The attention mask tensor. Shape: (batch_size, sequence_length). Defaults to None.
TYPE:
|
head_mask |
The head mask tensor. Shape: (num_heads, sequence_length, sequence_length). Defaults to None.
TYPE:
|
num_hashes |
The number of hashes for hashing attention. Defaults to None.
TYPE:
|
past_buckets_states |
The tensor of past bucket states. Shape: (batch_size, sequence_length, num_hashes, buckets). Defaults to None.
TYPE:
|
use_cache |
Whether to use cache for attention. Defaults to False.
TYPE:
|
orig_sequence_length |
The original sequence length before padding. Defaults to None.
TYPE:
|
output_attentions |
Whether to output attention probabilities. Defaults to False.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
ReformerOutput
|
An object containing the attention output, hidden states, attention probabilities, and buckets. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel
¶
Bases: ReformerPreTrainedModel
ReformerModel
This class represents a Reformer model. It is a subclass of ReformerPreTrainedModel, which provides the base implementation for a pre-trained Reformer model.
| ATTRIBUTE | DESCRIPTION |
|---|---|
config |
The configuration instance of the Reformer model.
TYPE:
|
embeddings |
The embeddings layer of the model.
TYPE:
|
encoder |
The encoder layer of the model.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes a new instance of the ReformerModel. |
get_input_embeddings |
Retrieves the input embeddings layer of the model. |
set_input_embeddings |
Sets the input embeddings layer of the model. |
_prune_heads |
Prunes the attention heads of the model. |
forward |
Constructs the Reformer model with the given input and configurations. |
_pad_to_mult_of_chunk_length |
Pads the input tensors to be a multiple of the chunk length according to the Reformer model configuration. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel.__init__(config)
¶
Initializes an instance of the ReformerModel class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The current instance of the ReformerModel class.
|
config |
An object containing the configuration settings for the ReformerModel. It must have the following attributes:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
If the |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, num_hashes=None, past_buckets_states=None, use_cache=None, output_hidden_states=None, output_attentions=None, return_dict=None)
¶
Constructs the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModel class.
TYPE:
|
input_ids |
The input tensor containing the token indices. Default: None.
TYPE:
|
attention_mask |
The tensor indicating which tokens should be attended to. Default: None.
TYPE:
|
position_ids |
The tensor containing the position indices. Default: None.
TYPE:
|
head_mask |
The tensor indicating which heads should be masked. Default: None.
TYPE:
|
inputs_embeds |
The tensor containing the input embeddings. Default: None.
TYPE:
|
num_hashes |
The number of hashes to use for the LSH attention. Default: None.
TYPE:
|
past_buckets_states |
The list of tensors containing the past bucket states. Default: None.
TYPE:
|
use_cache |
Whether to use the cache for the attention computation. Default: None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default: None.
TYPE:
|
output_attentions |
Whether to output attention weights. Default: None.
TYPE:
|
return_dict |
Whether to return a dictionary as the output. Default: None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Union[Tuple, ReformerModelOutput]
|
Union[Tuple, ReformerModelOutput]: A tuple or an instance of the ReformerModelOutput class containing the model output. |
| RAISES | DESCRIPTION |
|---|---|
ValueError
|
If both input_ids and inputs_embeds are specified. |
ValueError
|
If neither input_ids nor inputs_embeds are specified. |
AssertionError
|
If the input_ids shape is not [batch_size, sequence_length]. |
AssertionError
|
If past_buckets_states is used during training. |
ValueError
|
If the input sequence length is not a multiple of the least common multiple chunk_length. |
ValueError
|
If the input sequence length is not a multiple of the least common multiple chunk_length during training. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel.get_input_embeddings()
¶
Description
This method retrieves the input embeddings from the ReformerModel.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModel class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns the input embeddings of type 'None'. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModel.set_input_embeddings(value)
¶
Sets the input embeddings for the ReformerModel.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModel.
TYPE:
|
value |
The input embeddings to be set. Should be of type torch.Tensor.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the input embeddings are not of type torch.Tensor. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelOutput
dataclass
¶
Bases: ModelOutput
Output type of [ReformerModel].
| PARAMETER | DESCRIPTION |
|---|---|
last_hidden_state |
Sequence of hidden-states at the last layer of the model.
TYPE:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead
¶
Bases: ReformerPreTrainedModel
A Python class representing a Reformer model with a language modeling head (LMHead). This class inherits from the ReformerPreTrainedModel class.
The ReformerModelWithLMHead class is designed to be used as a decoder for the Reformer model. It incorporates a ReformerModel, which performs the main computation, and a ReformerOnlyLMHead, which generates the language modeling predictions.
| ATTRIBUTE | DESCRIPTION |
|---|---|
reformer |
The Reformer model used for the main computation.
TYPE:
|
lm_head |
The language modeling head used for generating predictions.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes the ReformerModelWithLMHead instance with the given configuration. |
get_output_embeddings |
Retrieves the decoder of the lm_head. |
set_output_embeddings |
Sets the decoder of the lm_head to the provided new_embeddings. |
forward |
Constructs the Reformer model with the given inputs and returns the output. |
prepare_inputs_for_generation |
Prepares the inputs for generation by selecting the last token and returning a dictionary of inputs. |
_reorder_cache |
Reorders the past key values for beam search. |
Note
The ReformerModelWithLMHead class assumes that the config parameter has an 'is_decoder' attribute set to True. It also checks specific conditions related to the 'attn_layers' attribute in the config to ensure the correct configuration for causal mask usage.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.__init__(config)
¶
Initialize the ReformerModelWithLMHead class with the provided configuration.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModelWithLMHead class.
TYPE:
|
config |
An object containing configuration settings for the model. This parameter is required for initializing the model and must be an instance of the configuration class. It should include specific settings such as whether the model is a decoder or not.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
AssertionError
|
Raised if the 'is_decoder' flag in the config is not set to True, indicating that the model being used is not a decoder. |
AssertionError
|
Raised if the 'local' key is found in the 'attn_layers' attribute of the config and 'local_num_chunks_after' is not set to 0 when causal mask is enabled. |
AssertionError
|
Raised if the 'lsh' key is found in the 'attn_layers' attribute of the config and 'lsh_num_chunks_after' is not set to 1 when causal mask is enabled. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.forward(input_ids=None, position_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, num_hashes=None, past_buckets_states=None, use_cache=None, output_hidden_states=None, output_attentions=None, return_dict=None, labels=None)
¶
| PARAMETER | DESCRIPTION |
|---|---|
labels(`mindspore.Tensor` |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.get_output_embeddings()
¶
Returns the output embeddings of the ReformerModelWithLMHead.
This method, 'get_output_embeddings', returns the decoder of the language model head of the ReformerModelWithLMHead. The decoder is responsible for mapping the hidden states of the model to the output vocabulary.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModelWithLMHead class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.prepare_inputs_for_generation(input_ids, past_key_values=None, use_cache=None, num_hashes=None, **kwargs)
¶
Prepare inputs for generation.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModelWithLMHead class.
TYPE:
|
input_ids |
The tensor of input token IDs of shape (batch_size, sequence_length).
TYPE:
|
past_key_values |
The tuple of past key values for efficient generation. Defaults to None.
TYPE:
|
use_cache |
Whether to use the cache for fast decoding. Defaults to None.
TYPE:
|
num_hashes |
The number of hashes for LSH attention. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing the prepared inputs for generation with the following keys:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHead.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings of the ReformerModelWithLMHead.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerModelWithLMHead class.
TYPE:
|
new_embeddings |
The new embeddings to be set for the output layer. It can be any compatible object that can replace the current embeddings.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Note
This method replaces the current embeddings in the lm_head.decoder attribute with the provided new embeddings. The lm_head.decoder is responsible for generating the output of the ReformerModelWithLMHead. By setting new embeddings, the model can be fine-tuned or customized for different tasks or requirements.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerModelWithLMHeadOutput
dataclass
¶
Bases: ModelOutput
Output type of [ReformerModelWithLMHead].
| PARAMETER | DESCRIPTION |
|---|---|
logits |
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
TYPE:
|
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerOnlyLMHead
¶
Bases: Module
The class 'ReformerOnlyLMHead' represents a language model head for the Reformer model. It inherits from the 'nn.Module' class and contains methods for initialization, forwardion, chunking, and weight tying.
| ATTRIBUTE | DESCRIPTION |
|---|---|
seq_len_dim |
The dimension representing the sequence length.
TYPE:
|
chunk_size_lm_head |
The chunk size for the language model head.
TYPE:
|
decoder |
The dense layer for decoding hidden states.
TYPE:
|
bias |
The bias parameter for the decoder.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
__init__ |
Initializes the ReformerOnlyLMHead instance with the provided configuration. |
forward |
Constructs the language model head using chunking for the given hidden states. |
forward_chunk |
Constructs a chunk of the language model head for the given hidden states. |
_tie_weights |
Ties the weights of the bias to the decoder bias. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerOnlyLMHead.__init__(config)
¶
Initializes a new instance of the ReformerOnlyLMHead class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The object itself.
|
config |
An instance of the Configuration class containing the configuration settings for the model.
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerOnlyLMHead.forward(hidden_states)
¶
Constructs the LM head output for the Reformer model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerOnlyLMHead class.
TYPE:
|
hidden_states |
The input hidden states from the Reformer model. Expected shape: (batch_size, sequence_length, hidden_size). Purpose: Represents the hidden states from the Reformer model. Restrictions: Must be a valid tensor with the correct shape.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
| RAISES | DESCRIPTION |
|---|---|
TypeError
|
If the input hidden_states is not a valid tensor. |
ValueError
|
If the input hidden_states has an incorrect shape. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerOnlyLMHead.forward_chunk(hidden_states)
¶
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerOnlyLMHead class.
TYPE:
|
hidden_states |
The input hidden states to be processed.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
The processed hidden states after passing through the decoder. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerPreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerPreTrainedModel.dummy_inputs
property
¶
Returns dummy input tensors for the Reformer PreTrained Model.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerPreTrainedModel class.
|
| RETURNS | DESCRIPTION |
|---|---|
dict
|
A dictionary containing dummy input tensors for the model. The dictionary has two keys:
|
mindnlp.transformers.models.reformer.modeling_reformer.ReformerSelfOutput
¶
Bases: Module
This class represents the self-attention output module of the Reformer model.
The ReformerSelfOutput class inherits from the nn.Module class and is responsible for processing the hidden states of the Reformer model's self-attention layer. It applies a linear transformation followed by dropout to the input hidden states.
| ATTRIBUTE | DESCRIPTION |
|---|---|
dropout |
The dropout probability used during the forward pass.
TYPE:
|
dense |
The linear transformation layer that maps the input hidden states to the output.
TYPE:
|
| METHOD | DESCRIPTION |
|---|---|
forward |
Applies the linear transformation and dropout to the input hidden states. |
Example
>>> # Create an instance of the ReformerSelfOutput class
>>> config = Configuration()
>>> reformer_self_output = ReformerSelfOutput(config)
...
>>> # Apply the self-attention output module to the hidden states
>>> hidden_states = torch.randn(batch_size, sequence_length, hidden_size)
>>> output = reformer_self_output.forward(hidden_states)
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerSelfOutput.__init__(config)
¶
Initializes a new instance of the ReformerSelfOutput class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The current instance of the class.
TYPE:
|
config |
An object containing configuration parameters for the ReformerSelfOutput. This object should have the following attributes:
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReformerSelfOutput.forward(hidden_states)
¶
Constructs the output of the Reformer self-attention layer.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerSelfOutput class.
TYPE:
|
hidden_states |
The input hidden states of the self-attention layer.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None |
This method takes in the input hidden states and processes them to forward the output of the Reformer self-attention layer. The hidden states are first passed through a dense layer to transform the dimensions. Then, a dropout operation is applied to the transformed hidden states with a dropout probability specified by the instance variable 'dropout'. The dropout operation is only applied during training. Finally, the processed hidden states are returned as the output of the layer.
Note
The output does not have any explicit restrictions as it is of type None and does not affect subsequent operations in the model.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReverseSort
¶
Bases: Module
After chunked attention is applied which sorted clusters, original ordering has to be restored. Since customized backward function is used for Reformer, the gradients of the output vectors have to be explicitly sorted here.
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReverseSort.bprop(out_vectors, logits, sorted_bucket_idx, undo_sorted_bucket_idx, outputs, grads)
¶
This method performs backpropagation for the ReverseSort class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReverseSort class.
|
out_vectors |
A tensor containing the output vectors.
|
logits |
A tensor containing the logits.
|
sorted_bucket_idx |
A tensor containing the sorted bucket indices.
|
undo_sorted_bucket_idx |
A tensor containing the undo sorted bucket indices.
|
outputs |
A tensor containing the outputs.
|
grads |
A tuple of two tensors containing the gradients of out_vectors and logits respectively.
|
| RETURNS | DESCRIPTION |
|---|---|
|
A tuple containing the gradients of out_vectors and logits, followed by two None values. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 | |
mindnlp.transformers.models.reformer.modeling_reformer.ReverseSort.forward(out_vectors, logits, sorted_bucket_idx, undo_sorted_bucket_idx)
¶
Constructs and returns modified vectors and logits based on the given parameters.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReverseSort class.
TYPE:
|
out_vectors |
The original vectors.
TYPE:
|
logits |
The original logits.
TYPE:
|
sorted_bucket_idx |
The indices of sorted buckets.
TYPE:
|
undo_sorted_bucket_idx |
The indices to undo the sorted buckets.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
Tuple[Tensor, Tensor]: A tuple containing the modified out_vectors and logits. |
Source code in mindnlp/transformers/models/reformer/modeling_reformer.py
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 | |
mindnlp.transformers.models.reformer.tokenization_reformer
¶
Tokenization class for model Reformer.
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer
¶
Bases: PreTrainedTokenizer
Construct a Reformer tokenizer. Based on SentencePiece .
This tokenizer inherits from [PreTrainedTokenizer] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
| PARAMETER | DESCRIPTION |
|---|---|
vocab_file |
SentencePiece file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
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:
|
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:
|
additional_special_tokens |
Additional special tokens used by the tokenizer.
TYPE:
|
sp_model_kwargs |
Will be passed to the
TYPE:
|
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.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 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.vocab_size
property
¶
Returns the size of the vocabulary used by the ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
int
|
The size of the vocabulary used by the ReformerTokenizer. |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__getstate__()
¶
Method 'getstate' in the class 'ReformerTokenizer'.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizer class. Represents the current instance of the ReformerTokenizer class. No restrictions.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
This method returns a dictionary containing the state of the ReformerTokenizer instance with the 'sp_model' key set to None. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__init__(vocab_file, eos_token='</s>', unk_token='<unk>', additional_special_tokens=[], sp_model_kwargs=None, **kwargs)
¶
Initializes a new instance of the ReformerTokenizer class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizer class.
|
vocab_file |
Path to the vocabulary file.
TYPE:
|
eos_token |
The end-of-sentence token. Defaults to ''.
TYPE:
|
unk_token |
The unknown token. Defaults to '
TYPE:
|
additional_special_tokens |
Additional special tokens to be added to the vocabulary. Defaults to an empty list.
TYPE:
|
sp_model_kwargs |
Additional arguments to be passed to the SentencePieceProcessor forwardor. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
None
|
None |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.__setstate__(d)
¶
setstate method in the class ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizer class.
TYPE:
|
d |
A dictionary containing the state information to be set.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
229 230 231 232 233 234 235 236 237 238 239 240 241 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.get_vocab()
¶
Get the vocabulary of the ReformerTokenizer.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerTokenizer class.
|
| RETURNS | DESCRIPTION |
|---|---|
Dict[str, int]
|
A dictionary of type Dict[str, int] mapping tokens to their corresponding IDs. The IDs are integers. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 | |
mindnlp.transformers.models.reformer.tokenization_reformer.ReformerTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary to a specified directory.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizer class.
TYPE:
|
save_directory |
The directory where the vocabulary will be saved.
TYPE:
|
filename_prefix |
An optional prefix for the filename. Defaults to None.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If the save_directory is not a valid directory. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer.py
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast
¶
Tokenization class for model Reformer.
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" Reformer tokenizer (backed by HuggingFace's tokenizers library). Based on Unigram.
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 .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
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:
|
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:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
additional_special_tokens |
Additional special tokens used by the tokenizer.
TYPE:
|
Source code in mindnlp/transformers/models/reformer/tokenization_reformer_fast.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 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.can_save_slow_tokenizer: bool
property
¶
Method to check if the slow tokenizer can be saved.
| PARAMETER | DESCRIPTION |
|---|---|
self |
An instance of the ReformerTokenizerFast class. This parameter refers to the current instance of the ReformerTokenizerFast class.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
bool
|
A boolean value indicating whether the slow tokenizer can be saved. Returns True if the vocab_file exists, otherwise returns False.
TYPE:
|
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, eos_token='</s>', unk_token='<unk>', additional_special_tokens=[], **kwargs)
¶
init
Initializes the ReformerTokenizerFast class.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the class.
|
vocab_file |
The path to the vocabulary file. If not provided, the tokenizer will use a default vocabulary.
TYPE:
|
tokenizer_file |
The path to the tokenizer file. If not provided, the tokenizer will use a default tokenizer.
TYPE:
|
eos_token |
The end-of-sequence token. Defaults to ''.
TYPE:
|
unk_token |
The unknown token. Defaults to '
TYPE:
|
additional_special_tokens |
A list of additional special tokens to be added to the vocabulary.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
|
None. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer_fast.py
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 | |
mindnlp.transformers.models.reformer.tokenization_reformer_fast.ReformerTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary for a ReformerTokenizerFast instance.
| PARAMETER | DESCRIPTION |
|---|---|
self |
The instance of the ReformerTokenizerFast class.
TYPE:
|
save_directory |
The directory where the vocabulary will be saved.
TYPE:
|
filename_prefix |
An optional prefix for 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 specified save_directory is not a valid directory. |
Source code in mindnlp/transformers/models/reformer/tokenization_reformer_fast.py
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 | |