mt5
mindnlp.transformers.models.mt5.modeling_mt5
¶
MindSpore mT5 model.
mindnlp.transformers.models.mt5.modeling_mt5.MT5Attention
¶
Bases: Module
The MT5Attention
class is a module that implements the attention mechanism used in the MT5 model.
It is designed to be used as a building block for the Transformer-based models.
This class inherits from the nn.Module
class, which is the base class for all neural network modules in MindSpore.
The main purpose of this class is to compute the attention weights and output of the attention mechanism. It takes in the hidden states, mask, key-value states, position bias, past key-value states, layer head mask, query length, use cache flag, and output attentions flag as inputs.
The class provides the following methods:
__init__
: Initializes theMT5Attention
instance with the given configuration and relative attention bias flag.prune_heads
: Prunes the specified attention heads from the model._relative_position_bucket
: Translates the relative position to a bucket number for relative attention. This method is adapted from Mesh Tensorflow.compute_bias
: Computes the binned relative position bias for the attention mechanism.forward
: Constructs the attention mechanism by applying self-attention (ifkey_value_states
is None) or attention over source sentence (provided bykey_value_states
).
Please refer to the method docstrings for more detailed information on each method and its parameters.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Attention.__init__(config, has_relative_attention_bias=False)
¶
Initializes an instance of the MT5Attention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object containing configuration parameters for the attention mechanism. The configuration object must have the following attributes:
TYPE:
|
has_relative_attention_bias |
Indicates whether relative attention bias is used.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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.mt5.modeling_mt5.MT5Attention.compute_bias(query_length, key_length)
¶
Compute binned relative position bias
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
509 510 511 512 513 514 515 516 517 518 519 520 521 522 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Attention.forward(hidden_states, mask=None, key_value_states=None, position_bias=None, past_key_value=None, layer_head_mask=None, query_length=None, use_cache=False, output_attentions=False)
¶
Self-attention (if key_value_states is None) or attention over source sentence (provided by key_value_states).
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Attention.prune_heads(heads)
¶
This method 'prune_heads' is defined in the class 'MT5Attention' and is used to prune specific heads in the attention mechanism of a MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5Attention class. It is used to access the attributes and methods within the class.
TYPE:
|
heads |
A list of integers representing the indices of the heads to be pruned. The indices should be within the range of existing heads in the attention mechanism.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. It modifies the attributes of the MT5Attention instance in place. |
RAISES | DESCRIPTION |
---|---|
None
|
However, potential exceptions may arise if the input 'heads' list contains indices that are out of bounds of the existing heads or if any of the helper functions called within this method encounter errors. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Block
¶
Bases: Module
This class represents a block of the MT5 model, which is a Transformer-based neural network architecture for sequence-to-sequence tasks. It consists of a self-attention layer, an optional cross-attention layer, and a feed-forward layer.
ATTRIBUTE | DESCRIPTION |
---|---|
`is_decoder` |
Indicates whether the block is used in the decoder part of the model.
TYPE:
|
`layer` |
A list of layers in the block, including the self-attention, cross-attention, and feed-forward layers.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
`forward` |
Performs the forward pass of the block, processing the input hidden states and generating the outputs. |
Details
The MT5Block
class inherits from the nn.Module
class and overrides the forward
method. The __init__
method initializes the block's attributes, including the is_decoder
flag and the list of layers.
The forward
method takes various input parameters, including the hidden states, attention masks,
position biases, and layer head masks. It also accepts optional parameters for encoder hidden states and
attention masks, as well as past key-value states used for caching.
The method first checks if past key-value states are provided and validates their correctness. It then retrieves the self-attention and cross-attention past key-value states from the input if present.
Next, the method passes the hidden states through the self-attention layer, using the provided attention mask, position bias, and layer head mask. The output includes the updated hidden states and the present key-value state.
If the block is a decoder and encoder hidden states are provided, the method performs cross-attention. It retrieves the query length and passes the hidden states, encoder hidden states, and other parameters to the cross-attention layer. The output includes the updated hidden states and the present key-value state.
Finally, the method passes the hidden states through the feed-forward layer. It then clamps the hidden states
to prevent any numerical issues and returns the final hidden states along with any additional outputs, such as
present key-value states and attention outputs, depending on the value of the use_cache
parameter.
Note
This class assumes the usage of the MindSpore deep learning framework.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Block.__init__(config, has_relative_attention_bias=False)
¶
Initializes a new instance of the MT5Block class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
The configuration object for MT5Block.
TYPE:
|
has_relative_attention_bias |
Specifies whether the attention bias is relative or not. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Block.forward(hidden_states, attention_mask=None, position_bias=None, encoder_hidden_states=None, encoder_attention_mask=None, encoder_decoder_position_bias=None, layer_head_mask=None, cross_attn_layer_head_mask=None, past_key_value=None, use_cache=False, output_attentions=False)
¶
Constructs the MT5Block.
This method is responsible for performing the main computations of the MT5Block. It takes in multiple parameters and returns None.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5Block class.
TYPE:
|
hidden_states |
The hidden states of the input sequence. Shape: (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
The attention mask tensor. Shape: (batch_size, sequence_length). Default: None.
TYPE:
|
position_bias |
The position bias tensor. Shape: (batch_size, sequence_length, sequence_length). Default: None.
TYPE:
|
encoder_hidden_states |
The hidden states of the encoder sequence. Shape: (batch_size, encoder_sequence_length, hidden_size). Default: None.
TYPE:
|
encoder_attention_mask |
The attention mask tensor for the encoder sequence. Shape: (batch_size, encoder_sequence_length). Default: None.
TYPE:
|
encoder_decoder_position_bias |
The position bias tensor for encoder-decoder attention. Shape: (batch_size, sequence_length, encoder_sequence_length). Default: None.
TYPE:
|
layer_head_mask |
The layer head mask tensor. Shape: (num_layers, num_heads). Default: None.
TYPE:
|
cross_attn_layer_head_mask |
The cross-attention layer head mask tensor. Shape: (num_layers, num_heads). Default: None.
TYPE:
|
past_key_value |
Tuple containing the past key-value states. Shape: (2 or 4, batch_size, num_heads, past_sequence_length, hidden_size). Default: None.
TYPE:
|
use_cache |
Whether to use caching. Default: False.
TYPE:
|
output_attentions |
Whether to output attention weights. Default: False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the length of past_key_value is not equal to the expected number of past states. |
Warning
|
If past_key_values is passed to the encoder. |
TypeError
|
If the data type of hidden_states is not supported. |
TypeError
|
If the data type of encoder_hidden_states is not supported. |
TypeError
|
If the data type of hidden_states after cross-attention is not supported. |
TypeError
|
If the data type of hidden_states after the final layer is not supported. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ClassificationHead
¶
Bases: Module
Head for sentence-level classification tasks.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ClassificationHead.__init__(config)
¶
Initializes the MT5ClassificationHead class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5ClassificationHead class.
TYPE:
|
config |
An object containing configuration parameters for the MT5 model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type MT5Config. |
ValueError
|
If any of the configuration parameters are missing or invalid. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ClassificationHead.forward(hidden_states)
¶
Constructs the classification head for an MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
Instance of the MT5ClassificationHead class.
|
hidden_states |
The input hidden states tensor to be processed by the classification head.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output tensor after processing through the classification head. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseActDense
¶
Bases: Module
MT5DenseActDense is a neural network module that implements a specific architecture for processing hidden states in the MT5 model. It consists of two dense layers with an activation function and dropout in between.
Inherits from nn.Module.
The init method initializes the MT5DenseActDense module with the provided MT5Config object. It sets up the internal components including two dense layers, a dropout layer, and an activation function.
The forward method processes the input hidden states through the internal components in sequence. It applies the first dense layer, activation function, dropout, type conversion if necessary, and the second dense layer. The final processed hidden states are returned as the output of the module.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseActDense.__init__(config)
¶
Initializes an instance of the MT5DenseActDense class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object of type MT5Config containing configuration parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
KeyError
|
If the specified dense activation function in the config is not found in ACT2FN. |
ValueError
|
If any of the configuration parameters are missing or invalid. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseActDense.forward(hidden_states)
¶
This method forwards the hidden states by applying operations and transformations.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5DenseActDense class.
|
hidden_states |
The input hidden states to be processed. It should be a tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
mindspore.Tensor: The processed hidden states after applying the operations and transformations. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input hidden_states is not of type mindspore.Tensor. |
ValueError
|
If the weight dtype of self.wo is not compatible with the dtype of hidden_states. |
RuntimeError
|
If an unexpected error occurs during the processing of hidden_states. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseGatedActDense
¶
Bases: Module
This class represents a dense gated activation module for the MT5 model. It inherits from the nn.Module class.
The MT5DenseGatedActDense class contains methods to initialize and forward the dense gated activation module.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MT5DenseGatedActDense module with the given configuration. |
forward |
Constructs the dense gated activation module using the provided hidden states. |
ATTRIBUTE | DESCRIPTION |
---|---|
wi_0 |
A dense layer that transforms the input hidden states.
|
wi_1 |
A dense layer that transforms the input hidden states.
|
wo |
A dense layer that transforms the gated hidden states.
|
dropout |
A dropout layer to apply dropout to the transformed hidden states.
|
act |
The activation function to be applied to the transformed hidden states.
|
Example
>>> config = MT5Config(d_model=512, d_ff=2048, dropout_rate=0.1, dense_act_fn='gelu')
>>> dense_gated_act_dense = MT5DenseGatedActDense(config)
>>> hidden_states = ...
>>> output = dense_gated_act_dense.forward(hidden_states)
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseGatedActDense.__init__(config)
¶
Initializes an instance of the MT5DenseGatedActDense class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
An object of type MT5Config containing configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the configuration parameters are not provided or are of incorrect type. |
KeyError
|
If the activation function specified in the configuration is not supported. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5DenseGatedActDense.forward(hidden_states)
¶
This method forwards the hidden states by applying a series of transformations.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5DenseGatedActDense class.
TYPE:
|
hidden_states |
The input hidden states to be processed.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value explicitly, but it updates the hidden states based on the transformations applied. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the datatype of the hidden_states is not compatible with the datatype of the weight tensor 'wo'. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel
¶
Bases: MT5PreTrainedModel
Example
>>> from transformers import MT5EncoderModel, AutoTokenizer
...
>>> model = MT5EncoderModel.from_pretrained("google/mt5-small")
>>> tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
>>> article = "UN Offizier sagt, dass weiter verhandelt werden muss in Syrien."
>>> input_ids = tokenizer(article, return_tensors="pt").input_ids
>>> outputs = model(input_ids)
>>> hidden_state = outputs.last_hidden_state
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 2337 2338 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel.__init__(config)
¶
Initializes an instance of the MT5EncoderModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5EncoderModel class.
|
config |
An object of type MT5Config containing configuration parameters for the model. The config parameter specifies the configuration settings for the MT5 model. It must be an instance of the MT5Config class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type MT5Config. |
ValueError
|
If the config parameter is missing or if any required configuration settings are not provided. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], BaseModelOutput]
|
Union[Tuple[mindspore.Tensor], BaseModelOutput] |
Example
>>> from transformers import AutoTokenizer, MT5EncoderModel
...
>>> tokenizer = AutoTokenizer.from_pretrained("mt5-small")
>>> model = MT5EncoderModel.from_pretrained("mt5-small")
>>> input_ids = tokenizer(
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> outputs = model(input_ids=input_ids)
>>> last_hidden_states = outputs.last_hidden_state
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel.get_encoder()
¶
Returns the encoder of the MT5EncoderModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5EncoderModel class.
|
RETURNS | DESCRIPTION |
---|---|
encoder
|
The method returns the encoder of the MT5EncoderModel. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel.get_input_embeddings()
¶
This method retrieves the input embeddings for the MT5EncoderModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5EncoderModel class.
|
RETURNS | DESCRIPTION |
---|---|
The shared input embeddings for the MT5EncoderModel. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5EncoderModel.set_input_embeddings(new_embeddings)
¶
Sets the input embeddings for the MT5EncoderModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5EncoderModel class.
TYPE:
|
new_embeddings |
The new embeddings to be set for the input.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the new_embeddings parameter is not of the correct type. |
ValueError
|
If there is an issue with setting the input embeddings. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration
¶
Bases: MT5PreTrainedModel
Example
>>> from transformers import MT5ForConditionalGeneration, AutoTokenizer
...
>>> model = MT5ForConditionalGeneration.from_pretrained("google/mt5-small")
>>> tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
>>> article = "UN Offizier sagt, dass weiter verhandelt werden muss in Syrien."
>>> summary = "Weiter Verhandlung in Syrien."
>>> inputs = tokenizer(article, text_target=summary, return_tensors="pt")
...
>>> outputs = model(**inputs)
>>> loss = outputs.loss
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1832 1833 1834 1835 1836 1837 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 1921 1922 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 2079 2080 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 2153 2154 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 2214 2215 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.__init__(config)
¶
Initializes an instance of the MT5ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
The configuration object containing various parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.forward(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, encoder_outputs=None, past_key_values=None, inputs_embeds=None, decoder_inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], Seq2SeqLMOutput]
|
|
Example
>>> from transformers import AutoTokenizer, MT5ForConditionalGeneration
...
>>> tokenizer = AutoTokenizer.from_pretrained("mt5-small")
>>> model = MT5ForConditionalGeneration.from_pretrained("mt5-small")
...
>>> # training
>>> input_ids = tokenizer("The <extra_id_0> walks in <extra_id_1> park", return_tensors="pt").input_ids
>>> labels = tokenizer("<extra_id_0> cute dog <extra_id_1> the <extra_id_2>", return_tensors="pt").input_ids
>>> outputs = model(input_ids=input_ids, labels=labels)
>>> loss = outputs.loss
>>> logits = outputs.logits
...
>>> # inference
>>> input_ids = tokenizer(
... "summarize: studies have shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> outputs = model.generate(input_ids)
>>> print(tokenizer.decode(outputs[0], skip_special_tokens=True))
>>> # studies have shown that owning a dog is good for you.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 2079 2080 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.get_decoder()
¶
Method to retrieve the decoder used in the MT5ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForConditionalGeneration class.
|
RETURNS | DESCRIPTION |
---|---|
decoder
|
This method returns the decoder associated with the MT5ForConditionalGeneration instance. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.get_encoder()
¶
Retrieve the encoder object used for conditional generation in the MT5ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForConditionalGeneration class. This parameter is required for accessing the encoder object associated with the instance. |
RETURNS | DESCRIPTION |
---|---|
encoder
|
The encoder object that is utilized for conditional text generation. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.get_input_embeddings()
¶
Retrieves the input embeddings for the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForConditionalGeneration class. |
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.get_output_embeddings()
¶
Returns the output embeddings of the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForConditionalGeneration class.
|
RETURNS | DESCRIPTION |
---|---|
embeddings
|
The output embeddings of the MT5 model. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.prepare_decoder_input_ids_from_labels(labels)
¶
Prepare decoder input IDs from labels for conditional generation.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForConditionalGeneration class. |
labels |
The labels tensor containing the target sequence to be shifted right.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it directly modifies the input labels tensor. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, head_mask=None, decoder_head_mask=None, decoder_attention_mask=None, cross_attn_head_mask=None, use_cache=None, encoder_outputs=None, **kwargs)
¶
This method prepares inputs for generation in the MT5ForConditionalGeneration class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
input_ids |
The input token IDs for the model. Shape: [batch_size, sequence_length].
TYPE:
|
past_key_values |
The past key values required for fast autoregressive decoding. Default: None.
TYPE:
|
attention_mask |
The attention mask for the input. Shape: [batch_size, sequence_length].
TYPE:
|
head_mask |
The mask for the multi-head attention layers. Shape: [num_heads, sequence_length].
TYPE:
|
decoder_head_mask |
The mask for the decoder's multi-head attention layers. Shape: [num_heads, sequence_length].
TYPE:
|
decoder_attention_mask |
The attention mask for the decoder. Shape: [batch_size, sequence_length].
TYPE:
|
cross_attn_head_mask |
The mask for the cross-attention layers. Shape: [num_heads, sequence_length].
TYPE:
|
use_cache |
Whether to use the cache for fast decoding. Default: None.
TYPE:
|
encoder_outputs |
The outputs of the encoder. Default: None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for the generation including 'decoder_input_ids', 'past_key_values', 'encoder_outputs', 'attention_mask', 'head_mask', 'decoder_head_mask', 'decoder_attention_mask', 'cross_attn_head_mask', and 'use_cache'. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.set_input_embeddings(new_embeddings)
¶
Set input embeddings for the MT5 model for conditional generation.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5ForConditionalGeneration class. |
new_embeddings |
New input embeddings to be set for the model. Should be a tensor of shape [vocab_size, embedding_size] where:
The new_embeddings should match the token embedding requirements of the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the new_embeddings provided is not a tensor. |
ValueError
|
If the shape of the new_embeddings tensor does not match the expected shape [vocab_size, embedding_size]. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForConditionalGeneration.set_output_embeddings(new_embeddings)
¶
Set the output embeddings for the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5ForConditionalGeneration class. |
new_embeddings |
The new output embeddings to be set for the model. It can be of any valid type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering
¶
Bases: MT5PreTrainedModel
MT5ForQuestionAnswering is a class that represents a Question Answering model based on the MT5 architecture. It is a subclass of MT5PreTrainedModel.
The class includes the following methods:
- init: Initializes an instance of the class with the given configuration.
- get_input_embeddings: Returns the shared input embeddings.
- set_input_embeddings: Sets the shared input embeddings to the provided new embeddings.
- get_encoder: Returns the encoder module of the model.
- get_decoder: Returns the decoder module of the model.
- forward: Constructs the model and returns the outputs.
The 'forward' method takes various input tensors and returns either a tuple of tensors or an instance of Seq2SeqQuestionAnsweringModelOutput.
Please note that this docstring does not include the method signatures or any other code.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 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 2826 2827 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.__init__(config)
¶
Initialize an instance of the MT5ForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object for the MT5 model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.forward(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, encoder_outputs=None, start_positions=None, end_positions=None, inputs_embeds=None, decoder_inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=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 (sequence_length). Position outside of the sequence are not taken into account for computing the loss.
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 (sequence_length). Position outside of the sequence are not taken into account for computing the loss.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], Seq2SeqQuestionAnsweringModelOutput]
|
Union[Tuple[mindspore.Tensor], Seq2SeqQuestionAnsweringModelOutput] |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 2826 2827 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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.get_decoder()
¶
Method to retrieve the decoder object.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForQuestionAnswering class.
|
RETURNS | DESCRIPTION |
---|---|
decoder
|
The method returns the decoder object associated with the MT5ForQuestionAnswering instance. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.get_encoder()
¶
Get the encoder object used in the MT5ForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of MT5ForQuestionAnswering.
|
RETURNS | DESCRIPTION |
---|---|
encoder
|
The method returns the encoder object, which is an instance of a specific encoder used in the MT5ForQuestionAnswering class. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.get_input_embeddings()
¶
This method retrieves the input embeddings from the MT5 model for question answering.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5ForQuestionAnswering class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
The method returns the shared input embeddings. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForQuestionAnswering.set_input_embeddings(new_embeddings)
¶
Set the input embeddings for both encoder and decoder in the MT5ForQuestionAnswering model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5ForQuestionAnswering class.
TYPE:
|
new_embeddings |
New embeddings to be set as input for both encoder and decoder. Should be a tensor of the same shape as the current input embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the shape of the new embeddings does not match the current input embeddings. |
TypeError
|
If the new_embeddings parameter is not a tensor. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForSequenceClassification
¶
Bases: MT5PreTrainedModel
This class represents a sequence classification model based on the MT5 architecture. It is designed for fine-tuning the MT5 model on sequence classification tasks.
The MT5ForSequenceClassification
class inherits from the MT5PreTrainedModel
class,
which provides the basic implementation for loading and saving pre-trained MT5 models.
To initialize an instance of this class, a MT5Config
object must be passed as a parameter to the forwardor.
The MT5ForSequenceClassification
class has the following attributes:
transformer
: An instance of theMT5Model
class, which represents the main transformer model.classification_head
: An instance of theMT5ClassificationHead
class, which represents the classification head of the model.
The forward
method is used to process the input and generate the outputs of the model. It takes several input
tensors as parameters, such as input_ids
, attention_mask
, decoder_input_ids
, etc. The method returns a tuple
of outputs, including the predicted logits for classification, and other intermediate outputs if requested.
If labels are provided, the method also calculates the loss based on the predicted logits and the provided labels.
The loss calculation depends on the problem_type
specified in the configuration. The supported problem types are
regression, single-label classification, and multi-label classification.
Note
The MT5ForSequenceClassification
class does not currently support passing input embeddings instead of input IDs.
The MT5ForSequenceClassification
class is designed to be used with the MT5 model for fine-tuning on sequence
classification tasks. It provides a convenient interface for processing input sequences and generating predictions.
Please refer to the documentation of the MT5PreTrainedModel
class for more details on loading and saving
pre-trained MT5 models.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForSequenceClassification.__init__(config)
¶
Initializes an instance of MT5ForSequenceClassification.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5ForSequenceClassification class.
|
config |
An object of type MT5Config containing configuration parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type MT5Config. |
ValueError
|
If there are any issues during initialization of the transformer, classification head, or post_init method. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5ForSequenceClassification.forward(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, encoder_outputs=None, inputs_embeds=None, decoder_inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, Seq2SeqSequenceClassifierOutput]
|
Union[Tuple, Seq2SeqSequenceClassifierOutput] |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerCrossAttention
¶
Bases: Module
MT5LayerCrossAttention represents a layer for cross-attention mechanism in the MT5 model.
This class inherits from nn.Module and includes methods for initializing the layer and forwarding the cross-attention mechanism.
ATTRIBUTE | DESCRIPTION |
---|---|
EncDecAttention |
An instance of the MT5Attention class for encoder-decoder attention mechanism.
|
layer_norm |
An instance of the MT5LayerNorm class for layer normalization.
|
dropout |
An instance of the nn.Dropout class for applying dropout.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MT5LayerCrossAttention instance with the given configuration. |
forward |
Constructs the cross-attention mechanism using the given parameters and returns the outputs. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerCrossAttention.__init__(config)
¶
Initializes an instance of the MT5LayerCrossAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
config |
The configuration dictionary containing the settings for the cross-attention layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the configuration dictionary 'config' is missing required keys or has invalid values. |
TypeError
|
If the data types of the input parameters are incorrect. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerCrossAttention.forward(hidden_states, key_value_states, attention_mask=None, position_bias=None, layer_head_mask=None, past_key_value=None, use_cache=False, query_length=None, output_attentions=False)
¶
This method forwards the cross-attention mechanism in the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5LayerCrossAttention class.
TYPE:
|
hidden_states |
The input hidden states to be processed.
TYPE:
|
key_value_states |
The key-value states used in attention computation.
TYPE:
|
attention_mask |
Mask to avoid attending to specific positions. Default is None.
TYPE:
|
position_bias |
Bias values added to the attention scores. Default is None.
TYPE:
|
layer_head_mask |
Mask to control which heads are allowed to attend to which positions. Default is None.
TYPE:
|
past_key_value |
Key and value tensors from the previous time steps. Default is None.
TYPE:
|
use_cache |
Whether to use cache for faster decoding. Default is False.
TYPE:
|
query_length |
The length of the queries. Default is None.
TYPE:
|
output_attentions |
Whether to output attention weights. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tuple
|
A tuple containing the layer's output and additional attention outputs if requested. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the shape of the input tensors is not compatible. |
TypeError
|
If the data types of the input parameters are incorrect. |
RuntimeError
|
If there is an issue during the attention computation process. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerFF
¶
Bases: Module
MT5LayerFF is a Python class representing a feed-forward layer for the MT5 model. It inherits from nn.Module and contains methods for initialization and forward propagation.
The init method initializes the MT5LayerFF instance with the provided configuration. It checks if the configuration includes gated activation and assigns the appropriate DenseReluDense module accordingly. Additionally, it sets up layer normalization and dropout.
The forward method applies layer normalization to the input hidden_states, passes it through the DenseReluDense module, applies dropout, and returns the updated hidden_states.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerFF.__init__(config)
¶
Initializes an instance of the MT5LayerFF class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5LayerFF class.
|
config |
An instance of the MT5Config class containing configuration settings for the MT5 model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type MT5Config. |
ValueError
|
If the config parameter is missing required attributes. |
RuntimeError
|
If there is an issue with the initialization process. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerFF.forward(hidden_states)
¶
Constructs the forward pass of the feed-forward layer in the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5LayerFF class.
TYPE:
|
hidden_states |
The input hidden states tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
torch.Tensor: The output hidden states tensor after applying the feed-forward layer, with the same shape as the input tensor. |
RAISES | DESCRIPTION |
---|---|
None
|
This method does not raise any exceptions. |
Description
This method forwards the forward pass for the feed-forward layer in the MT5 model. It takes the input hidden states tensor and applies a series of operations to transform it. The steps involved in the forward pass are as follows:
- Layer Normalization: The input hidden states tensor is first passed through a layer normalization operation using self.layer_norm. This operation normalizes the hidden states, making them more robust to variations in scale and distribution.
- Feed-Forward Transformation: The normalized hidden states tensor is then passed through a feed-forward transformation using self.DenseReluDense. This transformation consists of a linear layer followed by a ReLU activation function, followed by another linear layer. This operation helps the model learn complex non-linear relationships within the hidden states.
- Dropout: The output of the feed-forward transformation is then added to the original hidden states tensor after applying dropout. Dropout is a regularization technique that randomly sets a fraction of the hidden states to zero during training, which helps prevent overfitting and improves generalization.
The final output hidden states tensor is returned by this method, which has the same shape as the input tensor.
Note
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerNorm
¶
Bases: Module
Represents a layer normalization module in the MT5 style with no bias and no subtraction of mean.
This class inherits from nn.Module and provides functionality for layer normalization in the MT5 style. The forwardor initializes the layer normalization module with the specified hidden size and epsilon value. The 'forward' method accepts hidden states as input, calculates the variance, and normalizes the hidden states using the calculated variance and epsilon value. If the weight data type is float16 or bfloat16, the hidden states are converted to the weight data type before returning the weighted normalized hidden states.
ATTRIBUTE | DESCRIPTION |
---|---|
hidden_size |
The size of the hidden states.
TYPE:
|
eps |
The epsilon value for numerical stability.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Constructs a MT5LayerNorm module with the given hidden size and epsilon value. |
forward |
Applies layer normalization to the input hidden states and returns the normalized output. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerNorm.__init__(hidden_size, eps=1e-06)
¶
Construct a layernorm module in the MT5 style. No bias and no subtraction of mean.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
71 72 73 74 75 76 77 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerNorm.forward(hidden_states)
¶
Method to perform layer normalization on hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5LayerNorm class.
TYPE:
|
hidden_states |
The input hidden states to be normalized.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value but updates the hidden states in-place after normalization. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input hidden_states are not of type Tensor. |
ValueError
|
If the variance calculation encounters any issues. |
RuntimeError
|
If there are runtime issues during the normalization process. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerSelfAttention
¶
Bases: Module
This class represents a self-attention mechanism used in the MT5 (Multilingual Translation) model. It is designed to be used as a layer within the MT5 model.
This class inherits from the nn.Module class, which is a base class for all neural network modules in PyTorch.
ATTRIBUTE | DESCRIPTION |
---|---|
SelfAttention |
An instance of the MT5Attention class that performs the self-attention computation.
TYPE:
|
layer_norm |
An instance of the MT5LayerNorm class that applies layer normalization to the hidden states.
TYPE:
|
dropout |
An instance of the nn.Dropout class that applies dropout regularization to the attention output.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
This method applies the self-attention mechanism to the input hidden states, optionally using additional inputs such as attention mask, position bias, layer head mask, and past key-value states. Args:
Returns:
|
Note
- The self-attention mechanism is applied to the input hidden states after they are layer-normalized.
- The attention output is added to the input hidden states after applying dropout regularization.
- The method returns a tuple containing the updated hidden states and additional outputs depending on the configuration.
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerSelfAttention.__init__(config, has_relative_attention_bias=False)
¶
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
config |
An object containing configuration settings for the model.
TYPE:
|
has_relative_attention_bias |
A flag indicating whether to apply relative attention bias. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5LayerSelfAttention.forward(hidden_states, attention_mask=None, position_bias=None, layer_head_mask=None, past_key_value=None, use_cache=False, output_attentions=False)
¶
Constructs the self-attention layer of the MT5 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5LayerSelfAttention class.
TYPE:
|
hidden_states |
The input tensor of shape (batch_size, sequence_length, hidden_size). The hidden states to be passed through the self-attention layer.
TYPE:
|
attention_mask |
The attention mask tensor of shape (batch_size, sequence_length). A mask that indicates which tokens should be attended to and which should not. Defaults to None.
TYPE:
|
position_bias |
The position bias tensor of shape (batch_size, sequence_length, sequence_length). A bias that is added to the attention scores for each token. Defaults to None.
TYPE:
|
layer_head_mask |
The layer head mask tensor of shape (num_heads,) or (num_layers, num_heads). A mask that indicates which heads should be masked out. Defaults to None.
TYPE:
|
past_key_value |
The tuple of past key and value tensors. It contains the cached key and value tensors from previous time steps. Defaults to None.
TYPE:
|
use_cache |
Whether to use the cache for the attention outputs of each layer. Defaults to False.
TYPE:
|
output_attentions |
Whether to return the attention scores. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Tensor]: The outputs of the self-attention layer. The tuple contains:
|
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model
¶
Bases: MT5PreTrainedModel
Example
>>> from transformers import MT5Model, AutoTokenizer
...
>>> model = MT5Model.from_pretrained("google/mt5-small")
>>> tokenizer = AutoTokenizer.from_pretrained("google/mt5-small")
>>> article = "UN Offizier sagt, dass weiter verhandelt werden muss in Syrien."
>>> summary = "Weiter Verhandlung in Syrien."
>>> inputs = tokenizer(article, return_tensors="pt")
>>> labels = tokenizer(text_target=summary, return_tensors="pt")
...
>>> outputs = model(input_ids=inputs["input_ids"], decoder_input_ids=labels["input_ids"])
>>> hidden_states = outputs.last_hidden_state
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model.__init__(config)
¶
Initializes an instance of the MT5Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5Model class.
|
config |
An object of type MT5Config that holds the configuration parameters for the MT5 model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model.forward(input_ids=None, attention_mask=None, decoder_input_ids=None, decoder_attention_mask=None, head_mask=None, decoder_head_mask=None, cross_attn_head_mask=None, encoder_outputs=None, past_key_values=None, inputs_embeds=None, decoder_inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], Seq2SeqModelOutput]
|
Union[Tuple[mindspore.Tensor], Seq2SeqModelOutput] |
Example
>>> from transformers import AutoTokenizer, MT5Model
...
>>> tokenizer = AutoTokenizer.from_pretrained("mt5-small")
>>> model = MT5Model.from_pretrained("mt5-small")
...
>>> input_ids = tokenizer(
... "Studies have been shown that owning a dog is good for you", return_tensors="pt"
... ).input_ids # Batch size 1
>>> decoder_input_ids = tokenizer("Studies show that", return_tensors="pt").input_ids # Batch size 1
...
>>> # preprocess: Prepend decoder_input_ids with start token which is pad token for MT5Model.
>>> # This is not needed for torch's MT5ForConditionalGeneration as it does this internally using labels arg.
>>> decoder_input_ids = model._shift_right(decoder_input_ids)
...
>>> # forward pass
>>> outputs = model(input_ids=input_ids, decoder_input_ids=decoder_input_ids)
>>> last_hidden_states = outputs.last_hidden_state
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
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 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model.get_decoder()
¶
This method returns the decoder associated with the MT5Model instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The MT5Model instance itself.
|
RETURNS | DESCRIPTION |
---|---|
The decoder associated with the MT5Model instance. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model.get_encoder()
¶
Returns the encoder of the MT5Model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MT5Model class.
|
RETURNS | DESCRIPTION |
---|---|
The encoder of the MT5Model. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 |
|
mindnlp.transformers.models.mt5.modeling_mt5.MT5Model.get_input_embeddings()
¶
Retrieves the input embeddings for the MT5Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MT5Model class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mt5/modeling_mt5.py
1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 |
|