pegasus
mindnlp.transformers.models.pegasus.tokenization_pegasus
¶
Pegasus Tokenizer
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer
¶
Bases: PreTrainedTokenizer
Construct a PEGASUS tokenizer. Based on SentencePiece.
This tokenizer inherits from [PreTrainedTokenizer
] which contains most of the main methods. Users should refer to
this superclass for more information regarding those methods.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
SentencePiece file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
eos_token |
The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence.
The token used is the
TYPE:
|
unk_token |
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
mask_token |
The token used for masking single token values. This is the token used when training this model with masked language modeling (MLM). This is the token that the PEGASUS encoder will try to predict during pretraining. It corresponds to [MASK2] in PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization.
TYPE:
|
mask_token_sent |
The token used for masking whole target sentences. This is the token used when training this model with gap sentences generation (GSG). This is the sentence that the PEGASUS decoder will try to predict during pretraining. It corresponds to [MASK1] in PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization.
TYPE:
|
additional_special_tokens |
Additional special tokens used by the tokenizer. If no additional_special_tokens are provided
TYPE:
|
sp_model_kwargs |
Will be passed to the
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
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 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.vocab_size: int
property
¶
This method returns the size of the vocabulary used by the PegasusTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusTokenizer class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The size of the vocabulary, calculated as the length of the sp_model attribute plus the offset.
TYPE:
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.__getstate__()
¶
This method getstate is defined within the class PegasusTokenizer. It is used to return the state of the object for serialization purposes.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusTokenizer class. This parameter refers to the current object instance used to call the method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns a value of type None. It modifies the state dictionary by setting the 'sp_model' key to None before returning it. |
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.__init__(vocab_file, pad_token='<pad>', eos_token='</s>', unk_token='<unk>', mask_token='<mask_2>', mask_token_sent='<mask_1>', additional_special_tokens=None, offset=103, sp_model_kwargs=None, **kwargs)
¶
Initialize a PegasusTokenizer object.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
Path to the vocabulary file.
TYPE:
|
pad_token |
Token representing padding. Default is '
TYPE:
|
eos_token |
Token representing end of sentence. Default is ''.
TYPE:
|
unk_token |
Token representing unknown tokens. Default is '
TYPE:
|
mask_token |
Token representing masked tokens. Default is '
TYPE:
|
mask_token_sent |
Token representing masked tokens at sentence level. Default is '
TYPE:
|
additional_special_tokens |
List of additional special tokens. Default is None.
TYPE:
|
offset |
Offset value for special tokens.
TYPE:
|
sp_model_kwargs |
Additional arguments for SentencePieceProcessor. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
None |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If additional_special_tokens is not a list. |
ValueError
|
If additional_special_tokens contain an incorrectly shifted list of unknown tokens. |
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.__setstate__(d)
¶
This method setstate is defined within the class PegasusTokenizer and is used to set the internal state of the tokenizer object based on the provided dictionary 'd'.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusTokenizer class on which this method is called.
TYPE:
|
d |
A dictionary containing the state information to be set on the tokenizer object. This dictionary is expected to hold the necessary data for setting the state of the tokenizer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value explicitly. It updates the internal state of the PegasusTokenizer object based on the provided dictionary 'd'. |
RAISES | DESCRIPTION |
---|---|
None
|
However, potential exceptions that could occur during the execution of this method may include any exceptions raised by the SentencePieceProcessor class methods like Load, if there are issues with loading the vocabulary file specified in the state information. |
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence or a pair of sequences for sequence classification tasks by concatenating
and adding special tokens. A PEGASUS sequence has the following format, where X
represents the sequence:
- single sequence:
X </s>
- pair of sequences:
A B </s>
(not intended use)
BOS is never used. Pairs of sequences are not the expected use case, but they will be handled without a separator.
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs to which the special tokens will be added.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
301 302 303 304 305 306 307 308 309 310 311 312 313 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Get list where entries are [1] if a token is [eos] or [pad] else 0.
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
339 340 341 342 343 344 345 346 347 348 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.get_vocab()
¶
Returns the vocabulary of the PegasusTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the PegasusTokenizer class.
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the vocabulary of the tokenizer, where the keys are strings representing tokens and the values are integers representing their corresponding ids.
TYPE:
|
Note
The vocabulary includes both the base tokenizer's vocabulary and any additional tokens that
have been added using the add_tokens
method.
Example
>>> tokenizer = PegasusTokenizer()
>>> vocab = tokenizer.get_vocab()
>>> print(vocab)
{'<s>': 0, '</s>': 1, '<unk>': 2, '<pad>': 3, '<mask>': 4, 'additional_token': 5, ...}
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.num_special_tokens_to_add(pair=False)
¶
Just EOS
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
315 316 317 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus.PegasusTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary files for the Pegasus Tokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the PegasusTokenizer class.
TYPE:
|
save_directory |
The directory path where the vocabulary files will be saved.
TYPE:
|
filename_prefix |
An optional prefix to be added to the filename. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the file path of the saved vocabulary file. |
RAISES | DESCRIPTION |
---|---|
OSError
|
If the |
This method saves the vocabulary files required for the Pegasus Tokenizer.
The save_directory
parameter specifies the directory path where the vocabulary files will be saved.
If filename_prefix
is provided, it will be added as a prefix to the filename.
The saved vocabulary file path is returned as a tuple containing a single string value.
If the save_directory
path is not a valid directory, an OSError will be raised.
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast
¶
Tokenization class for model PEGASUS.
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast
¶
Bases: PreTrainedTokenizerFast
Construct a "fast" PEGASUS tokenizer (backed by HuggingFace's tokenizers library). Based on Unigram.
This tokenizer inherits from [PreTrainedTokenizerFast
] which contains most of the main methods. Users should
refer to this superclass for more information regarding those methods.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
SentencePiece file (generally has a .spm extension) that contains the vocabulary necessary to instantiate a tokenizer.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
eos_token |
The end of sequence token. When building a sequence using special tokens, this is not the token that is used for the end of sequence.
The token used is the
TYPE:
|
unk_token |
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
mask_token |
The token used for masking single token values. This is the token used when training this model with masked language modeling (MLM). This is the token that the PEGASUS encoder will try to predict during pretraining. It corresponds to [MASK2] in PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization.
TYPE:
|
mask_token_sent |
The token used for masking whole target sentences. This is the token used when training this model with gap sentences generation (GSG). This is the sentence that the PEGASUS decoder will try to predict during pretraining. It corresponds to [MASK1] in PEGASUS: Pre-training with Extracted Gap-sentences for Abstractive Summarization.
TYPE:
|
additional_special_tokens |
Additional special tokens used by the tokenizer. If no additional_special_tokens are provided
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus_fast.py
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast.can_save_slow_tokenizer: bool
property
¶
Check whether the slow tokenizer can be saved.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusTokenizerFast class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
bool
|
Returns True if the vocab_file exists and is a valid file path, False otherwise.
TYPE:
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast.__init__(vocab_file=None, tokenizer_file=None, pad_token='<pad>', eos_token='</s>', unk_token='<unk>', mask_token='<mask_2>', mask_token_sent='<mask_1>', additional_special_tokens=None, offset=103, **kwargs)
¶
This method initializes an instance of the PegasusTokenizerFast class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_file |
Path to the vocabulary file. Defaults to None.
TYPE:
|
tokenizer_file |
Path to the tokenizer file. Defaults to None.
TYPE:
|
pad_token |
Special token representing padding. Defaults to '
TYPE:
|
eos_token |
Special token representing end of sequence. Defaults to ''.
TYPE:
|
unk_token |
Special token representing unknown tokens. Defaults to '
TYPE:
|
mask_token |
Special token for masking tokens. Defaults to '
TYPE:
|
mask_token_sent |
Special token for masking sentences. Defaults to '
TYPE:
|
additional_special_tokens |
List of additional special tokens. Defaults to None.
TYPE:
|
offset |
Offset value for special tokens. Defaults to 103.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If additional_special_tokens is not a list. |
ValueError
|
If the provided additional_special_tokens contain an incorrectly shifted list of unknown tokens. |
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus_fast.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 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 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Build model inputs from a sequence by adding eos to the end. no bos token is added to the front.
- single sequence:
X </s>
- pair of sequences:
A B </s>
(not intended use)
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs to which the special tokens will be added
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus_fast.py
230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Get list where entries are [1] if a token is [eos] or [pad] else 0.
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus_fast.py
219 220 221 222 223 224 225 226 227 228 |
|
mindnlp.transformers.models.pegasus.tokenization_pegasus_fast.PegasusTokenizerFast.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary to the specified directory with an optional filename prefix.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusTokenizerFast class.
TYPE:
|
save_directory |
The directory path where the vocabulary will be saved.
TYPE:
|
filename_prefix |
An optional prefix to be added to the vocabulary filename. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[str]
|
Tuple[str]: A tuple containing the path to the saved vocabulary file. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the fast tokenizer does not have the necessary information to save the vocabulary for a slow tokenizer. |
OSError
|
If the save_directory provided is not a valid directory path. |
Source code in mindnlp/transformers/models/pegasus/tokenization_pegasus_fast.py
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 |
|
mindnlp.transformers.models.pegasus.configuration_pegasus
¶
PEGASUS model configuration
mindnlp.transformers.models.pegasus.configuration_pegasus.PegasusConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [PegasusModel
]. It is used to instantiate an
PEGASUS 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 PEGASUS
google/pegasus-large 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 |
---|---|
vocab_size |
Vocabulary size of the PEGASUS model. Defines the number of different tokens that can be represented by the
TYPE:
|
d_model |
Dimensionality of the layers and the pooler layer.
TYPE:
|
encoder_layers |
Number of encoder layers.
TYPE:
|
decoder_layers |
Number of decoder layers.
TYPE:
|
encoder_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
decoder_attention_heads |
Number of attention heads for each attention layer in the Transformer decoder.
TYPE:
|
decoder_ffn_dim |
Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
TYPE:
|
encoder_ffn_dim |
Dimensionality of the "intermediate" (often named feed-forward) layer in decoder.
TYPE:
|
activation_function |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
dropout |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_dropout |
The dropout ratio for the attention probabilities.
TYPE:
|
activation_dropout |
The dropout ratio for activations inside the fully connected layer.
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:
|
init_std |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
encoder_layerdrop |
The LayerDrop probability for the encoder. See the LayerDrop paper for more details.
TYPE:
|
decoder_layerdrop |
The LayerDrop probability for the decoder. See the LayerDrop paper for more details.
TYPE:
|
scale_embedding |
Scale embeddings by diving by sqrt(d_model).
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models)
TYPE:
|
forced_eos_token_id |
The id of the token to force as the last generated token when
TYPE:
|
Example
>>> from transformers import PegasusConfig, PegasusModel
...
>>> # Initializing a PEGASUS google/pegasus-large style configuration
>>> configuration = PegasusConfig()
...
>>> # Initializing a model (with random weights) from the google/pegasus-large style configuration
>>> model = PegasusModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/pegasus/configuration_pegasus.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
mindnlp.transformers.models.pegasus.configuration_pegasus.PegasusConfig.hidden_size: int
property
¶
Returns the hidden size of the PegasusConfig object.
PARAMETER | DESCRIPTION |
---|---|
self |
The PegasusConfig object.
|
RETURNS | DESCRIPTION |
---|---|
int
|
The hidden size of the PegasusConfig object. This value represents the size of the hidden state in the model.
TYPE:
|
mindnlp.transformers.models.pegasus.configuration_pegasus.PegasusConfig.num_attention_heads: int
property
¶
Returns the number of attention heads in the Pegasus model's encoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the PegasusConfig class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The number of attention heads used in the encoder of the Pegasus model.
TYPE:
|
The num_attention_heads
method returns an integer value representing the number of attention heads used
in the encoder of the Pegasus model. Attention heads are a key component of transformer models, and they enable
the model to focus on different parts of the input sequence during processing. By varying the number of
attention heads, the model can capture different levels of information and dependencies in the input data.
This method is a property, which means that it can be accessed as an attribute without needing to call
it explicitly as a function. When accessed, it directly returns the number of attention heads specified in the
encoder_attention_heads
attribute of the current instance of the PegasusConfig class.
Note that the num_attention_heads
method does not take any additional parameters beyond the self
parameter,
as it is designed to provide information specific to the current instance of the class.
Example
>>> config = PegasusConfig()
>>> num_heads = config.num_attention_heads
>>> print(num_heads)
12
In this example, a new instance of the PegasusConfig class is created. The num_attention_heads
property is
accessed as an attribute (config.num_attention_heads
), and the resulting number of attention heads
(12 in this case) is printed.
mindnlp.transformers.models.pegasus.configuration_pegasus.PegasusConfig.__init__(vocab_size=50265, max_position_embeddings=1024, encoder_layers=12, encoder_ffn_dim=4096, encoder_attention_heads=16, decoder_layers=12, decoder_ffn_dim=4096, decoder_attention_heads=16, encoder_layerdrop=0.0, decoder_layerdrop=0.0, use_cache=True, is_encoder_decoder=True, activation_function='gelu', d_model=1024, dropout=0.1, attention_dropout=0.0, activation_dropout=0.0, init_std=0.02, initializer_range=0.02, decoder_start_token_id=0, scale_embedding=False, pad_token_id=0, eos_token_id=1, forced_eos_token_id=1, **kwargs)
¶
Initializes a new PegasusConfig object with the provided configuration parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Default is 50265.
TYPE:
|
max_position_embeddings |
The maximum number of tokens in a sequence. Default is 1024.
TYPE:
|
encoder_layers |
The number of layers in the encoder. Default is 12.
TYPE:
|
encoder_ffn_dim |
The dimension of the feedforward network in the encoder layers. Default is 4096.
TYPE:
|
encoder_attention_heads |
The number of attention heads in the encoder layers. Default is 16.
TYPE:
|
decoder_layers |
The number of layers in the decoder. Default is 12.
TYPE:
|
decoder_ffn_dim |
The dimension of the feedforward network in the decoder layers. Default is 4096.
TYPE:
|
decoder_attention_heads |
The number of attention heads in the decoder layers. Default is 16.
TYPE:
|
encoder_layerdrop |
The probability of dropping a layer in the encoder. Default is 0.0.
TYPE:
|
decoder_layerdrop |
The probability of dropping a layer in the decoder. Default is 0.0.
TYPE:
|
use_cache |
Whether to use caching for the model. Default is True.
TYPE:
|
is_encoder_decoder |
Whether the model is an encoder-decoder model. Default is True.
TYPE:
|
activation_function |
The activation function to be used. Default is 'gelu'.
TYPE:
|
d_model |
The dimension of the model. Default is 1024.
TYPE:
|
dropout |
The dropout probability. Default is 0.1.
TYPE:
|
attention_dropout |
The dropout probability for attention layers. Default is 0.0.
TYPE:
|
activation_dropout |
The dropout probability for activation layers. Default is 0.0.
TYPE:
|
init_std |
The standard deviation for weight initialization. Default is 0.02.
TYPE:
|
initializer_range |
The range for weight initialization. Default is 0.02.
TYPE:
|
decoder_start_token_id |
The token id for the start of the decoder sequence. Default is 0.
TYPE:
|
scale_embedding |
Whether to scale embeddings. Default is False.
TYPE:
|
pad_token_id |
The token id for padding. Default is 0.
TYPE:
|
eos_token_id |
The token id for end of sequence. Default is 1.
TYPE:
|
forced_eos_token_id |
The token id for forced end of sequence. Default is 1.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/configuration_pegasus.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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus
¶
MindSpore PEGASUS model.
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusAttention
¶
Bases: Module
Multi-headed attention from 'Attention Is All You Need' paper
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusAttention.__init__(embed_dim, num_heads, dropout=0.0, is_decoder=False, bias=True, is_causal=False, config=None)
¶
Initializes the PegasusAttention class.
PARAMETER | DESCRIPTION |
---|---|
embed_dim |
The dimension of the input embeddings.
TYPE:
|
num_heads |
The number of attention heads.
TYPE:
|
dropout |
The dropout probability. Default is 0.0.
TYPE:
|
is_decoder |
Whether the attention is used in a decoder setting. Default is False.
TYPE:
|
bias |
Whether to use bias in linear projections. Default is True.
TYPE:
|
is_causal |
Whether the attention is causal. Default is False.
TYPE:
|
config |
An optional Pegasus configuration object. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If embed_dim is not divisible by num_heads. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusAttention.forward(hidden_states, key_value_states=None, past_key_value=None, attention_mask=None, layer_head_mask=None, output_attentions=False)
¶
Input shape: Batch x Time x Channel
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.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 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder
¶
Bases: PegasusPreTrainedModel
Transformer decoder consisting of config.decoder_layers layers. Each layer is a [PegasusDecoderLayer
]
PARAMETER | DESCRIPTION |
---|---|
config |
PegasusConfig
TYPE:
|
embed_tokens |
output embedding
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.__init__(config, embed_tokens=None)
¶
Initializes a PegasusDecoder instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An instance of PegasusConfig containing configuration parameters.
TYPE:
|
embed_tokens |
An optional instance of nn.Embedding representing embeddings. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.forward(input_ids=None, attention_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, head_mask=None, cross_attn_head_mask=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
input_ids |
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it. Indices can be obtained using [
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
TYPE:
|
encoder_hidden_states |
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention of the decoder.
TYPE:
|
encoder_attention_mask |
Mask to avoid performing cross-attention on padding tokens indices of encoder input_ids. Mask values
selected in
TYPE:
|
head_mask |
Mask to nullify selected heads of the attention modules. Mask values selected in
TYPE:
|
cross_attn_head_mask |
Mask to nullify selected heads of the cross-attention modules in decoder to avoid performing
cross-attention on hidden heads. Mask values selected in
TYPE:
|
inputs_embeds |
Optionally, instead of passing
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
output_hidden_states |
Whether or not to return the hidden states of all layers. See
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.get_input_embeddings()
¶
This method returns the input embeddings for the PegasusDecoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusDecoder class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
embed_tokens
|
This method returns the input embeddings stored in the 'embed_tokens' attribute of the PegasusDecoder instance. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.get_position_embeddings()
¶
Returns the position embeddings matrix
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
965 966 967 968 969 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings matrix of the model if new_num_position_embeddings !=
config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embeddings.
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoder.set_input_embeddings(value)
¶
This method sets the input embeddings for the PegasusDecoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusDecoder class.
TYPE:
|
value |
The input embeddings to be set for the decoder. It should be of type torch.Tensor and represent the embeddings for the input tokens.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderLayer
¶
Bases: Module
The PegasusDecoderLayer class represents a single layer of the Pegasus decoder model. It includes self-attention and encoder-decoder cross-attention mechanisms followed by feedforward neural network layers. This class inherits from nn.Module and implements the decoding logic for the Pegasus model.
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimension of the embeddings used in the layer.
TYPE:
|
self_attn |
The self-attention mechanism used in the layer.
TYPE:
|
dropout |
The dropout probability applied in the layer.
TYPE:
|
activation_fn |
The activation function used in the feedforward neural network layers.
TYPE:
|
activation_dropout |
The dropout probability applied after the activation function.
TYPE:
|
self_attn_layer_norm |
Layer normalization applied after self-attention.
TYPE:
|
encoder_attn |
The encoder-decoder cross-attention mechanism used in the layer.
TYPE:
|
encoder_attn_layer_norm |
Layer normalization applied after encoder-decoder cross-attention.
TYPE:
|
fc1 |
The first feedforward neural network layer.
TYPE:
|
fc2 |
The second feedforward neural network layer.
TYPE:
|
final_layer_norm |
Layer normalization applied at the end of the layer.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Constructs the output of the layer based on the input hidden states and optional arguments. Returns the output tensor. |
PARAMETER | DESCRIPTION |
---|---|
hidden_states |
Input to the layer of shape (batch, seq_len, embed_dim).
TYPE:
|
attention_mask |
Attention mask of size (batch, 1, tgt_len, src_len) with padding indicated by large negative values.
TYPE:
|
encoder_hidden_states |
Encoder input to the layer of shape (batch, seq_len, embed_dim).
TYPE:
|
encoder_attention_mask |
Encoder attention mask of size (batch, 1, tgt_len, src_len) with padding indicated by large negative values.
TYPE:
|
layer_head_mask |
Mask for attention heads in a given layer.
TYPE:
|
cross_attn_layer_head_mask |
Mask for cross-attention heads in a given layer.
TYPE:
|
past_key_value |
Cached past key and value projection states.
TYPE:
|
output_attentions |
Flag to determine whether to return attention tensors.
TYPE:
|
use_cache |
Flag to determine whether to use caching mechanism for key-value states.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
outputs
|
Tuple containing the output tensor and optionally self-attention and cross-attention weights if output_attentions is True, and present key-value states if use_cache is True.
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderLayer.__init__(config)
¶
Initializes an instance of the PegasusDecoderLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the class.
TYPE:
|
config |
The configuration object containing various settings for the decoder layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderLayer.forward(hidden_states, attention_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, layer_head_mask=None, cross_attn_layer_head_mask=None, past_key_value=None, output_attentions=False, use_cache=True)
¶
PARAMETER | DESCRIPTION |
---|---|
hidden_states |
input to the layer of shape
TYPE:
|
attention_mask |
attention mask of size
TYPE:
|
encoder_hidden_states |
cross attention input to the layer of shape
TYPE:
|
encoder_attention_mask |
encoder attention mask of size
TYPE:
|
layer_head_mask |
mask for attention heads in a given layer of size
TYPE:
|
cross_attn_layer_head_mask |
mask for cross-attention heads in a given layer of
size
TYPE:
|
past_key_value |
cached past key and value projection states
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderWrapper
¶
Bases: PegasusPreTrainedModel
This wrapper class is a helper class to correctly load pretrained checkpoints when the causal language model is
used in combination with the [EncoderDecoderModel
] framework.
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
1831 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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderWrapper.__init__(config)
¶
Initializes an instance of the PegasusDecoderWrapper class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class itself.
TYPE:
|
config |
The configuration object containing the necessary parameters for initialization.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusDecoderWrapper.forward(*args, **kwargs)
¶
Method 'forward' in the class 'PegasusDecoderWrapper'.
PARAMETER | DESCRIPTION |
---|---|
*args |
Variable length argument list.
DEFAULT:
|
**kwargs |
Arbitrary keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoder
¶
Bases: PegasusPreTrainedModel
Transformer encoder consisting of config.encoder_layers self attention layers. Each layer is a
[PegasusEncoderLayer
].
PARAMETER | DESCRIPTION |
---|---|
config |
PegasusConfig
TYPE:
|
embed_tokens |
output embedding
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoder.__init__(config, embed_tokens=None)
¶
Initializes a PegasusEncoder object.
PARAMETER | DESCRIPTION |
---|---|
self |
The PegasusEncoder object itself.
|
config |
An instance of PegasusConfig containing the configuration settings for the Pegasus model.
TYPE:
|
embed_tokens |
An optional instance of nn.Embedding representing the token embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoder.forward(input_ids=None, attention_mask=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
input_ids |
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it. Indices can be obtained using [
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
TYPE:
|
head_mask |
Mask to nullify selected heads of the attention modules. Mask values selected in
TYPE:
|
inputs_embeds |
Optionally, instead of passing
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
output_hidden_states |
Whether or not to return the hidden states of all layers. See
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoder.get_position_embeddings()
¶
Returns the position embeddings matrix
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
720 721 722 723 724 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoder.resize_position_embeddings(new_num_position_embeddings)
¶
Resizes position embeddings matrix of the model if new_num_position_embeddings !=
config.max_position_embeddings
.
PARAMETER | DESCRIPTION |
---|---|
new_num_position_embeddings |
The number of new position embeddings.
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoderLayer
¶
Bases: Module
The PegasusEncoderLayer class represents a single layer of the Pegasus encoder. This layer includes self-attention, feed-forward neural network (FFN) processing, and layer normalization.
This class inherits from nn.Module and has the following attributes:
- embed_dim: The dimension of the input embeddings
- self_attn: The self-attention mechanism used in the layer
- self_attn_layer_norm: The layer normalization applied after self-attention
- dropout: The dropout rate applied during processing
- activation_fn: The activation function used in the feed-forward neural network
- activation_dropout: The dropout rate applied after the activation function
- fc1: The first fully connected layer in the feed-forward neural network
- fc2: The second fully connected layer in the feed-forward neural network
- final_layer_norm: The layer normalization applied after the feed-forward neural network processing
The PegasusEncoderLayer class has a forward method that takes the following arguments:
- hidden_states: Input to the layer of shape
(batch, seq_len, embed_dim)
- attention_mask: Attention mask of size
(batch, 1, tgt_len, src_len)
where padding elements are indicated by very large negative values - layer_head_mask: Mask for attention heads in a given layer of size
(encoder_attention_heads,)
- output_attentions: Whether or not to return the attentions tensors of all attention layers
The forward method returns the following outputs:
- hidden_states: The processed hidden states
- attn_weights: The attention weights if output_attentions is set to True
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoderLayer.__init__(config)
¶
Initialize a PegasusEncoderLayer object.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusEncoderLayer class.
TYPE:
|
config |
The configuration object containing parameters for initializing the encoder layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusEncoderLayer.forward(hidden_states, attention_mask, layer_head_mask, output_attentions=False)
¶
PARAMETER | DESCRIPTION |
---|---|
hidden_states |
input to the layer of shape
TYPE:
|
attention_mask |
attention mask of size
TYPE:
|
layer_head_mask |
mask for attention heads in a given layer of size
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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.pegasus.modeling_pegasus.PegasusForCausalLM
¶
Bases: PegasusPreTrainedModel
This class represents a Pegasus model for causal language modeling (LM). It is a subclass of PegasusPreTrainedModel, which provides the basic infrastructure for loading and saving pre-trained models.
The PegasusForCausalLM class is designed for generating text in a causal manner, where each token is generated based on the previously generated tokens. It takes as input a sequence of tokens and predicts the probability distribution over the next token in the sequence.
The PegasusForCausalLM class provides various methods for interacting with the model. These include initializing the model with a configuration, getting and setting input and output embeddings, getting and setting the decoder, getting the position embeddings, resizing the position embeddings, and forwarding the model for generation.
The __init__
method initializes the PegasusForCausalLM object with a configuration.
It sets the decoder configuration and initializes the model and the LM head.
The get_input_embeddings
method returns the input embeddings of the model.
The set_input_embeddings
method sets the input embeddings of the model to a new value.
The get_output_embeddings
method returns the output embeddings (LM head) of the model.
The set_output_embeddings
method sets the output embeddings (LM head) of the model to a new value.
The set_decoder
method sets the decoder of the model to a new decoder.
The get_decoder
method returns the decoder of the model.
The get_position_embeddings
method returns the position embeddings matrix of the model.
The resize_position_embeddings
method resizes the position embeddings matrix of the model if the new number of
position embeddings is different from the maximum number of position embeddings specified in the configuration.
The forward
method forwards the model for generation. It takes input tensors such as input_ids, attention_mask,
encoder_hidden_states, and labels, and returns the model outputs, including the logits, loss, past key values,
hidden states, attentions, and cross attentions.
The prepare_inputs_for_generation
method prepares the inputs for generation. It takes input tensors such as
input_ids, past_key_values, and attention_mask, and returns a dictionary of prepared inputs.
The _reorder_cache
method reorders the past key values for generation based on the beam index.
Note
This class inherits from PegasusPreTrainedModel and provides additional methods specific to causal LM tasks.
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.__init__(config)
¶
Initializes a new instance of the PegasusForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusForCausalLM class.
TYPE:
|
config |
The configuration object containing settings for the model. This object is deep copied to avoid modification of the original configuration. It must have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.forward(input_ids=None, attention_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, head_mask=None, cross_attn_head_mask=None, past_key_values=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
input_ids |
Indices of input sequence tokens in the vocabulary. Padding will be ignored by default should you provide it. Indices can be obtained using [
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
TYPE:
|
encoder_hidden_states |
Sequence of hidden-states at the output of the last layer of the encoder. Used in the cross-attention if the model is configured as a decoder.
TYPE:
|
encoder_attention_mask |
Mask to avoid performing attention on the padding token indices of the encoder input. This mask is used
in the cross-attention if the model is configured as a decoder. Mask values selected in
TYPE:
|
head_mask |
Mask to nullify selected heads of the attention modules. Mask values selected in
TYPE:
|
cross_attn_head_mask |
Mask to nullify selected heads of the cross-attention modules. Mask values selected in
TYPE:
|
past_key_values |
Tuple of Contains pre-computed hidden-states (key and values in the self-attention blocks and in the
cross-attention blocks) that can be used (see If
TYPE:
|
labels |
Labels for computing the masked language modeling loss. Indices should either be in
TYPE:
|
use_cache |
If set to
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
output_hidden_states |
Whether or not to return the hidden states of all layers. See
TYPE:
|
return_dict |
Whether or not to return a [
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, CausalLMOutputWithCrossAttentions]
|
Union[Tuple, CausalLMOutputWithCrossAttentions] |
Example
>>> from transformers import AutoTokenizer, PegasusForCausalLM
...
>>> tokenizer = AutoTokenizer.from_pretrained("google/pegasus-large")
>>> model = PegasusForCausalLM.from_pretrained("google/pegasus-large", add_cross_attention=False)
>>> assert model.config.is_decoder, f"{model.__class__} has to be configured as a decoder."
>>> inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
>>> outputs = model(**inputs)
...
>>> logits = outputs.logits
>>> expected_shape = [1, inputs.input_ids.shape[-1], model.config.vocab_size]
>>> list(logits.shape) == expected_shape
True
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
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 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.get_decoder()
¶
This method retrieves the decoder component of the PegasusForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the PegasusForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
decoder
|
The method returns the decoder component of the model. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.get_input_embeddings()
¶
Description: This method retrieves the input embeddings from the PegasusForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
PegasusForCausalLM instance. Represents the current instance of the PegasusForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.get_output_embeddings()
¶
Method to retrieve the output embeddings from the PegasusForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusForCausalLM class. This parameter is a reference to the current instance of the class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None, as it retrieves the output embeddings from the model and does not return any specific value. |
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.get_position_embeddings()
¶
Returns the position embeddings matrix
Source code in mindnlp/transformers/models/pegasus/modeling_pegasus.py
2047 2048 2049 2050 2051 |
|
mindnlp.transformers.models.pegasus.modeling_pegasus.PegasusForCausalLM.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, use_cache=None, **kwargs)
¶
Prepare inputs for generation in the PegasusForCausalLM class.
This method prepares inputs for generating text by adjusting input_ids and attention_mask based on past_key_values if provided.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the PegasusForCausalLM class.
TYPE:
|
input_ids |