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 thelabels
argument is provided. This method takes several optional input arguments and returns a tuple with the following elements:- If
labels
is notNone
, returns a tuple(masked_lm_loss, logits, hidden_states, attentions)
. -
If
labels
isNone
, returns a tuple(logits, hidden_states, attentions)
.masked_lm_loss
is the masked language modeling loss, computed as cross-entropy loss between the logits and the labels.logits
is a tensor of shape(batch_size, sequence_length, vocab_size)
containing the unnormalized log probabilities for each vocabulary token.hidden_states
is a tuple of lengthconfig.num_hidden_layers + 1
with each tensor of shape(batch_size, sequence_length, hidden_size)
representing the hidden states of the model at each layer.attentions
is a tuple of lengthconfig.num_hidden_layers
with 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 +<