gpt2
mindnlp.transformers.models.gpt2.modeling_gpt2
¶
MindSpore GPT-2 model.
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Attention
¶
Bases: Module
The GPT2Attention
class represents the attention mechanism used in the GPT-2 model.
It is a subclass of the nn.Module
class.
Summary
This class implements the attention mechanism in GPT-2, which is used for self-attention within the model or cross-attention between the model and an encoder.
ATTRIBUTE | DESCRIPTION |
---|---|
`config` |
The configuration object containing various hyperparameters for the attention mechanism.
|
`is_cross_attention` |
A boolean flag indicating whether the attention is for cross-attention or self-attention.
|
`layer_idx` |
An optional integer representing the index of the layer.
|
`bias` |
A tensor representing the bias used in attention calculations.
|
`masked_bias` |
A tensor representing the bias used in attention calculations for masking.
|
`embed_dim` |
An integer representing the embedding dimension of the attention mechanism.
|
`num_heads` |
An integer representing the number of attention heads.
|
`head_dim` |
An integer representing the dimension of each attention head.
|
`split_size` |
An integer representing the size of split tensors.
|
`scale_attn_weights` |
A boolean flag indicating whether to scale the attention weights.
|
`scale_attn_by_inverse_layer_idx` |
A boolean flag indicating whether to scale the attention weights by the inverse of the layer index.
|
`reorder_and_upcast_attn` |
A boolean flag indicating whether to reorder and upcast the attention weights.
|
`c_attn` |
The convolutional layer for attention calculations.
|
`q_attn` |
The convolutional layer for calculating queries (only used for cross-attention).
|
`c_proj` |
The convolutional layer for projecting the attention output.
|
`attn_dropout` |
The dropout layer applied to the attention weights.
|
`resid_dropout` |
The dropout layer applied to the attention output.
|
`pruned_heads` |
A set containing the indices of pruned attention heads.
|
METHOD | DESCRIPTION |
---|---|
`prune_heads` |
Prunes the specified attention heads. |
`_attn` |
Performs attention calculations for self-attention. |
`_upcast_and_reordered_attn` |
Performs attention calculations for cross-attention. |
`_split_heads` |
Splits the |
`_merge_heads` |
Merges the |
`forward` |
Constructs the attention mechanism. |
Please note that this class does not include method signatures or any other code. The provided information is a summary of the class and its attributes and methods.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Attention.__init__(config, is_cross_attention=False, layer_idx=None)
¶
Initializes an instance of the GPT2Attention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An object containing the configuration parameters.
TYPE:
|
is_cross_attention |
Indicates whether the attention is cross-attention or not. Defaults to False.
TYPE:
|
layer_idx |
The index of the layer. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Attention.forward(hidden_states, layer_past=None, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, use_cache=False, output_attentions=False)
¶
This method 'forward' in the class 'GPT2Attention' is responsible for forwarding the attention mechanism for GPT-2 model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_states |
The input hidden states for the attention mechanism.
TYPE:
|
layer_past |
The past layer key and value tensors for optimization.
TYPE:
|
attention_mask |
Masking tensor to prevent attention to certain positions.
TYPE:
|
head_mask |
Masking tensor to prevent attention in specific heads.
TYPE:
|
encoder_hidden_states |
Hidden states from the encoder for cross-attention.
TYPE:
|
encoder_attention_mask |
Masking tensor for encoder attention.
TYPE:
|
use_cache |
Flag to use caching for optimization.
TYPE:
|
output_attentions |
Flag to output attention weights.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[Union[Tensor, Tuple[Tensor]], ...]
|
Tuple[Union[mindspore.Tensor, Tuple[mindspore.Tensor]], ...]: A tuple containing the output tensor from attention mechanism and present states for caching. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'encoder_hidden_states' is provided without 'q_attn' weights defined for cross-attention. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Attention.prune_heads(heads)
¶
This method is part of the GPT2Attention class and is named prune_heads.
PARAMETER | DESCRIPTION |
---|---|
self |
GPT2Attention object. Represents an instance of the GPT2Attention class.
|
heads |
List of integers. The list of head indices to be pruned from the attention mechanism. It identifies the specific heads to be pruned from the attention mechanism.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value explicitly. It modifies the internal state of the GPT2Attention object. |
RAISES | DESCRIPTION |
---|---|
None
|
However, depending on the implementation of the helper functions find_pruneable_heads_and_indices, ops.cat, and prune_conv1d_layer, potential exceptions related to these functions may be raised during the execution of prune_heads method. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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.gpt2.modeling_gpt2.GPT2Block
¶
Bases: Module
This class represents a single block of the GPT2 (Generative Pretrained Transformer 2) model.
GPT2Block is a subclass of nn.Module and contains the following attributes:
- ln_1: A LayerNorm module for layer normalization.
- attn: An instance of the GPT2Attention class for self-attention mechanism.
- ln_2: A LayerNorm module for layer normalization.
- crossattention: An instance of the GPT2Attention class for cross-attention mechanism
(optional, if
encoder_hidden_states
are passed). - ln_cross_attn: A LayerNorm module for layer normalization in cross-attention mechanism
(optional, if
encoder_hidden_states
are passed). - mlp: An instance of the GPT2MLP class for the feed-forward neural network.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPT2Block instance with the given configuration and optional layer index. |
forward |
Performs the forward pass of the GPT2Block. Parameters:
Returns:
|
Note
If encoder_hidden_states
are passed, the GPT2Block instance should be instantiated with cross-attention layers
by setting config.add_cross_attention=True
.
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Block.__init__(config, layer_idx=None)
¶
Initializes an instance of the GPT2Block class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An object containing the configuration parameters for the GPT2Block. It should have the following attributes:
|
layer_idx |
An optional integer representing the index of the layer.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Block.forward(hidden_states, layer_past=None, attention_mask=None, head_mask=None, encoder_hidden_states=None, encoder_attention_mask=None, use_cache=False, output_attentions=False)
¶
Constructs a GPT2 block with optional cross-attention functionality.
PARAMETER | DESCRIPTION |
---|---|
self |
The GPT2Block instance.
|
hidden_states |
The input hidden states. Default is None.
TYPE:
|
layer_past |
Past hidden states for autoregressive decoding. Default is None.
TYPE:
|
attention_mask |
Mask to prevent attention to some positions. Default is None.
TYPE:
|
head_mask |
Mask to nullify selected heads of the attention mechanism. Default is None.
TYPE:
|
encoder_hidden_states |
Hidden states of the encoder for cross-attention. Default is None.
TYPE:
|
encoder_attention_mask |
Mask for encoder attention. Default is None.
TYPE:
|
use_cache |
Whether to use cache for faster decoding. Default is False.
TYPE:
|
output_attentions |
Whether to output attentions weights. Default is False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple[Tensor], Optional[Tuple[Tensor, Tuple[Tensor, ...]]]]
|
Union[Tuple[mindspore.Tensor], Optional[Tuple[mindspore.Tensor, Tuple[mindspore.Tensor, ...]]]]:
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel
¶
Bases: GPT2PreTrainedModel
This class represents a GPT-2 model with two classification heads for multiple choice tasks. It is designed to be used for natural language processing tasks that require generating text and making multiple choice predictions. The model architecture is based on the GPT-2 model with additional heads for language modeling and multiple choice classification.
The class includes methods for initializing the model, setting and getting output embeddings, preparing inputs for text generation, and forwarding the model for inference or training. It also provides a method for reordering cache during beam search or beam sampling.
Note that this class inherits from GPT2PreTrainedModel, which is a base class for all GPT-2 models in the transformers library. The GPT2DoubleHeadsModel extends the base functionality of the GPT-2 model to support multiple choice tasks.
For detailed usage examples and descriptions of input parameters and return values, please refer to the method docstrings within the class code.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel.__init__(config)
¶
Initializes a new instance of the GPT2DoubleHeadsModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the GPT2Config class that defines the model configuration.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, mc_token_ids=None, labels=None, mc_labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None, **kwargs)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for language modeling. Note that the labels are shifted inside the model, i.e. you can set
TYPE:
|
mc_labels |
Labels for computing the multiple choice classification loss. Indices should be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, GPT2DoubleHeadsModelOutput]
|
Union[Tuple, GPT2DoubleHeadsModelOutput] |
Example
>>> from transformers import AutoTokenizer, GPT2DoubleHeadsModel
...
>>> tokenizer = AutoTokenizer.from_pretrained("gpt2")
>>> model = GPT2DoubleHeadsModel.from_pretrained("gpt2")
...
>>> # Add a [CLS] to the vocabulary (we should train it also!)
>>> num_added_tokens = tokenizer.add_special_tokens({"cls_token": "[CLS]"})
>>> # Update the model embeddings with the new vocabulary size
>>> embedding_layer = model.resize_token_embeddings(len(tokenizer))
...
>>> choices = ["Hello, my dog is cute [CLS]", "Hello, my cat is cute [CLS]"]
>>> encoded_choices = [tokenizer.encode(s) for s in choices]
>>> cls_token_location = [tokens.index(tokenizer.cls_token_id) for tokens in encoded_choices]
...
>>> input_ids = mindspore.Tensor(encoded_choices).unsqueeze(0) # Batch size: 1, number of choices: 2
>>> mc_token_ids = mindspore.Tensor([cls_token_location]) # Batch size: 1
...
>>> outputs = model(input_ids, mc_token_ids=mc_token_ids)
>>> lm_logits = outputs.logits
>>> mc_logits = outputs.mc_logits
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel.get_output_embeddings()
¶
Returns the output embeddings of the GPT2DoubleHeadsModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the GPT2DoubleHeadsModel.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel.prepare_inputs_for_generation(input_ids, past_key_values=None, **kwargs)
¶
Prepares the inputs for generation in the GPT2DoubleHeadsModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2DoubleHeadsModel class.
TYPE:
|
input_ids |
The input tensor of shape (batch_size, sequence_length) containing the input IDs.
TYPE:
|
past_key_values |
A tuple of past key values. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for generation, including the following keys:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModel.set_output_embeddings(new_embeddings)
¶
Sets the output embeddings for the GPT2DoubleHeadsModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2DoubleHeadsModel class.
TYPE:
|
new_embeddings |
The new embeddings to set as the output embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModelOutput
dataclass
¶
Bases: ModelOutput
Base class for outputs of models predicting if two sentences are consecutive or not.
PARAMETER | DESCRIPTION |
---|---|
loss |
Language modeling loss.
TYPE:
|
mc_loss |
Multiple choice classification loss.
TYPE:
|
logits |
Prediction scores of the language modeling head (scores for each vocabulary token before SoftMax).
TYPE:
|
mc_logits |
Prediction scores of the multiple choice classification head (scores for each choice before SoftMax).
TYPE:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForQuestionAnswering
¶
Bases: GPT2PreTrainedModel
This class represents a GPT2 model for question answering tasks. It is a subclass of GPT2PreTrainedModel.
GPT2ForQuestionAnswering inherits the following attributes and methods from GPT2PreTrainedModel:
ATTRIBUTE | DESCRIPTION |
---|---|
config |
The configuration object for the GPT2 model.
|
transformer |
The GPT2Model instance for the transformer part of the model.
|
qa_outputs |
A neural network layer for question answering outputs.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPT2ForQuestionAnswering instance. |
forward |
Constructs the GPT2ForQuestionAnswering model and performs question answering. |
The GPT2ForQuestionAnswering class provides the following functionality:
-
Initialization:
- The GPT2ForQuestionAnswering instance is initialized with a 'config' parameter.
- The 'config' parameter is used to set the 'num_labels' attribute.
- The 'transformer' attribute is set to an instance of the GPT2Model class with the 'config' parameter.
- The 'qa_outputs' attribute is set to a neural network layer with 'config.hidden_size' input size and 2 output units.
-
Construction:
- The 'forward' method forwards the GPT2ForQuestionAnswering model.
- The method takes several input tensors as parameters, such as 'input_ids', 'attention_mask', 'token_type_ids', etc.
- It also takes optional parameters like 'start_positions', 'end_positions', 'output_attentions', 'output_hidden_states', and 'return_dict'.
- The method returns a tuple of outputs, including 'start_logits' and 'end_logits', which represent the predicted start and end positions for the answer span.
- If 'start_positions' and 'end_positions' are provided, the method calculates the loss for the question answering task and returns the total loss along with the outputs.
Note
The method parameters and return types are defined using MindSpore framework's type hints.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1827 1828 1829 1830 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 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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForQuestionAnswering.__init__(config)
¶
Initializes a new instance of the GPT2ForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2ForQuestionAnswering class.
TYPE:
|
config |
Configuration object containing necessary settings for the model initialization.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForQuestionAnswering.forward(input_ids=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=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 (
TYPE:
|
end_positions |
Labels for position (index) of the end of the labelled span for computing the token classification loss.
Positions are clamped to the length of the sequence (
TYPE:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForSequenceClassification
¶
Bases: GPT2PreTrainedModel
GPT2ForSequenceClassification represents a GPT-2 model fine-tuned for sequence classification tasks. This class inherits from GPT2PreTrainedModel.
The GPT2ForSequenceClassification class provides a method 'forward' for forwarding the sequence classification model. The 'forward' method accepts input tensors such as input_ids, past_key_values, attention_mask, token_type_ids, position_ids, head_mask, inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, and return_dict.
The 'forward' method returns a tuple containing the sequence classification loss, logits, past_key_values, hidden_states, and attentions. If the return_dict parameter is set to False, the output is a tuple of pooled_logits and transformer_outputs. The sequence classification loss is computed based on the given labels and the model configuration.
The GPT2ForSequenceClassification class also includes an init method for initializing the model with the given configuration, number of labels, GPT2Model transformer, and score.
Labels for computing the sequence classification/regression loss can be provided as a mindspore.Tensor of shape (batch_size,) in the 'forward' method. Indices for the labels should be in the range [0, config.num_labels - 1]. If config.num_labels == 1, a regression loss is computed (Mean-Square loss). If config.num_labels > 1, a classification loss is computed (Cross-Entropy).
The class ensures proper handling of padding tokens and provides warnings for unexpected scenarios. Additionally, it dynamically determines the problem type based on the configuration and label data types.
Note
This docstring is generated based on the provided code and does not include signatures or any other code.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForSequenceClassification.__init__(config)
¶
Initializes a new instance of the GPT2ForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
config |
An instance of the GPT2Config class containing the configuration parameters for the GPT2 model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForSequenceClassification.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, 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:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForTokenClassification
¶
Bases: GPT2PreTrainedModel
This class represents a GPT-2 model for token classification, inheriting from GPT2PreTrainedModel. It includes methods for initialization and forwardion of the model for token classification tasks. The model utilizes a transformer architecture with configurable dropout and classifier layers for classification or regression loss computation based on the number of labels specified in the configuration. The forward method processes input data through the transformer, applies dropout, generates logits using the classifier layer, and computes the loss if labels are provided. The method returns the loss and output based on the specified return format.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForTokenClassification.__init__(config)
¶
Initializes a GPT2ForTokenClassification instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The GPT2ForTokenClassification instance. |
config |
The configuration object containing model hyperparameters. This parameter is required to properly configure the GPT2 model for token classification. It should include the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If 'config' is missing the 'num_labels' attribute. |
TypeError
|
If 'config' is not an instance of GPT2Config. |
TypeError
|
If 'classifier_dropout' or 'hidden_dropout' is not a float. |
ValueError
|
If both 'classifier_dropout' and 'hidden_dropout' in 'config' are not None or float. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2ForTokenClassification.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
TYPE:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel
¶
Bases: GPT2PreTrainedModel
The GPT2LMHeadModel
class is a subclass of GPT2PreTrainedModel
that represents a language model based on the
GPT-2 architecture.
This class provides methods for initializing the model, getting and setting the output embeddings, preparing inputs for generation, and forwarding the model. It also includes a static method for reordering the cache when using beam search or beam sampling.
ATTRIBUTE | DESCRIPTION |
---|---|
transformer |
A GPT2Model instance representing the GPT-2 transformer model.
|
lm_head |
A nn.Linear layer representing the output layer of the language model.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPT2LMHeadModel. |
get_output_embeddings |
Returns the lm_head output embeddings. |
set_output_embeddings |
Sets the lm_head output embeddings. |
prepare_inputs_for_generation |
Prepares inputs for generation by adjusting the input_ids, token_type_ids, attention_mask, and position_ids. |
forward |
Constructs the GPT2LMHeadModel and returns the model outputs. |
_reorder_cache |
Reorders the past_key_values cache based on the beam_idx for beam search or beam sampling. |
Note
- The labels for language modeling are shifted inside the model.
- The loss is computed only for labels in [0, ..., config.vocab_size].
- The GPT2LMHeadModel class inherits from GPT2PreTrainedModel.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel.__init__(config)
¶
Initializes a new instance of the GPT2LMHeadModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current object instance.
|
config |
An instance of the GPT2Config class representing the model configuration.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for language modeling. Note that the labels are shifted inside the model, i.e. you can set
TYPE:
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel.get_output_embeddings()
¶
Returns the output embeddings of the GPT2LMHeadModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPT2LMHeadModel class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method retrieves the output embeddings of the GPT2LMHeadModel. The output embeddings are the weights of the linear layer (lm_head) which is responsible for producing the logits for each token in the language model. These logits are then used to calculate the probabilities of the next token in the sequence.
Note that the returned value is of type None, as the method doesn't explicitly return any value, but rather directly accesses the output embeddings of the GPT2LMHeadModel.
Example
>>> odel = GPT2LMHeadModel()
>>> output_embeddings = model.get_output_embeddings()
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel.prepare_inputs_for_generation(input_ids, past_key_values=None, inputs_embeds=None, **kwargs)
¶
Prepare inputs for generation.
PARAMETER | DESCRIPTION |
---|---|
self |
The GPT2LMHeadModel instance.
TYPE:
|
input_ids |
The input token ids of shape [batch_size, sequence_length].
TYPE:
|
past_key_values |
The past key values of the model.
TYPE:
|
inputs_embeds |
The input embeddings of shape [batch_size, sequence_length, hidden_size].
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2LMHeadModel.set_output_embeddings(new_embeddings)
¶
Method to set new output embeddings for the GPT2LMHeadModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2LMHeadModel class. It represents the GPT-2 language model head model.
TYPE:
|
new_embeddings |
The new embeddings to be set as the output embeddings. These embeddings will replace the current output embeddings in the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2MLP
¶
Bases: Module
This class represents a multi-layer perceptron (MLP) component of the GPT-2 model. It is used to process the hidden states in the model architecture.
The GPT2MLP class inherits from the nn.Module class and contains methods for initializing the MLP and forwarding the hidden states.
ATTRIBUTE | DESCRIPTION |
---|---|
c_fc |
A 1D convolutional layer used for intermediate processing of the hidden states.
TYPE:
|
c_proj |
A 1D convolutional layer used for final projection of the hidden states.
TYPE:
|
act |
The activation function used in the MLP.
TYPE:
|
dropout |
A dropout layer used for regularization.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPT2MLP with the given intermediate size and configuration. |
forward |
Constructs the hidden states by applying the specified operations on the input hidden states. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2MLP.__init__(intermediate_size, config)
¶
Initializes an instance of the GPT2MLP class.
PARAMETER | DESCRIPTION |
---|---|
self |
The GPT2MLP object being initialized.
TYPE:
|
intermediate_size |
The size of the intermediate layer.
TYPE:
|
config |
The configuration object containing various settings. This object is expected to have the following attributes:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2MLP.forward(hidden_states)
¶
Constructs a GPT2MLP model by applying a series of operations on the input hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the GPT2MLP class.
|
hidden_states |
The input hidden states. It is an optional parameter and defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The output hidden states after applying the operations. |
Note
The hidden_states
parameter should be a tuple of mindspore.Tensor objects representing the hidden states
of the model.
The hidden_states
parameter can be None, in which case it will be ignored and not used in the operations.
The output hidden states will be of type mindspore.Tensor.
Example
>>> model = GPT2MLP()
>>> hidden_states = (tensor1, tensor2)
>>> output = model.forward(hidden_states)
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Model
¶
Bases: GPT2PreTrainedModel
This class represents a GPT-2 model for natural language processing tasks. It includes methods for initializing the model, setting input embeddings, pruning model heads, and forwarding the model for inference or training. The model consists of multiple GPT2Blocks organized in layers to process input sequences and generate output representations. The GPT2Model class inherits from the GPT2PreTrainedModel class, which provides additional functionality and pretrained weights for fine-tuning or transfer learning tasks.
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the GPT-2 model with configuration parameters. |
get_input_embeddings |
Returns the input embeddings used by the model. |
set_input_embeddings |
Sets new input embeddings for the model. |
_prune_heads |
Prunes specific attention heads in the model based on the provided dictionary. |
forward |
Constructs the GPT-2 model for inference or training with various input options and returns the model output. |
ATTRIBUTE | DESCRIPTION |
---|---|
embed_dim |
The dimensionality of the embedding layer in the model.
|
wte |
Embedding layer for token embeddings.
|
wpe |
Embedding layer for position embeddings.
|
drop |
Dropout layer for regularization.
|
h |
List of GPT2Block layers for processing input sequences.
|
ln_f |
Layer normalization applied to the final hidden states.
|
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Model.__init__(config)
¶
Initializes an instance of the GPT2Model class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2Model class.
|
config |
An object of type 'config' containing the configuration parameters for the GPT2Model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Model.forward(input_ids=None, past_key_values=None, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs the GPT-2 model.
PARAMETER | DESCRIPTION |
---|---|
self |
|
input_ids |
Input tensor of shape :obj:
DEFAULT:
|
past_key_values |
Tuple of :obj:
DEFAULT:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Mask values selected in
DEFAULT:
|
token_type_ids |
Input tensor of shape :obj:
DEFAULT:
|
position_ids |
DEFAULT:
|
head_mask |
self-attention modules. Mask values selected in
DEFAULT:
|
inputs_embeds |
Input tensor of shape :obj:
DEFAULT:
|
encoder_hidden_states |
The encoded input sequence of shape :obj:
DEFAULT:
|
encoder_attention_mask |
Cross attention mask to avoid performing attention on padding token indices. Defaults to :obj:
DEFAULT:
|
use_cache |
Whether or not the model should return the past key values when used for inference.
Defaults to :obj:
DEFAULT:
|
output_attentions |
Whether to also return all attention weights, including the self-attention weights of
each attention layer. Defaults to :obj:
DEFAULT:
|
output_hidden_states |
Whether to also return all hidden states of each layer in addition to the output tensor.
Defaults to :obj:
DEFAULT:
|
return_dict |
Whether to return a dictionary instead of a tuple. Defaults to :obj:
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPastAndCrossAttentions]
|
A tuple or a dictionary of outputs containing the following tensors depending
on the value of
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both |
ValueError
|
If neither |
ValueError
|
If |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Model.get_input_embeddings()
¶
Retrieves the input embeddings for the GPT2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2Model class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method is responsible for retrieving the input embeddings of the GPT2Model. It takes a single parameter, 'self', which refers to the instance of the GPT2Model class.
The GPT2Model class is designed to handle GPT-2 models, which are based on the Transformer architecture. Input embeddings are representations of the input tokens in the model. They are used as the initial input to the model and are typically generated by applying a word embedding layer to the input tokens.
Since this method does not return any value, the return type is 'None'. The purpose of this method is to retrieve the input embeddings needed for further processing within the GPT2Model.
No exceptions are raised by this method.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2Model.set_input_embeddings(new_embeddings)
¶
Sets the input embeddings of the GPT2Model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2Model class.
TYPE:
|
new_embeddings |
The new input embeddings to be set. It should be a tensor of shape (vocab_size, hidden_size) representing the word embeddings.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method modifies the input embeddings of the GPT2Model in-place. |
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 |
|
mindnlp.transformers.models.gpt2.modeling_gpt2.GPT2PreTrainedModel
¶
Bases: PreTrainedModel
An abstract class to handle weights initialization and a simple interface for downloading and loading pretrained models.
Source code in mindnlp/transformers/models/gpt2/modeling_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.configuration_gpt2
¶
OpenAI GPT-2 configuration
mindnlp.transformers.models.gpt2.configuration_gpt2.GPT2Config
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [GPT2Model
] or a [TFGPT2Model
]. It is used to
instantiate a GPT-2 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 GPT-2
gpt2 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 GPT-2 model. Defines the number of different tokens that can be represented by the
TYPE:
|
n_positions |
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:
|
n_embd |
Dimensionality of the embeddings and hidden states.
TYPE:
|
n_layer |
Number of hidden layers in the Transformer encoder.
TYPE:
|
n_head |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
n_inner |
Dimensionality of the inner feed-forward layers.
TYPE:
|
activation_function |
Activation function, to be selected in the list
TYPE:
|
resid_pdrop |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
embd_pdrop |
The dropout ratio for the embeddings.
TYPE:
|
attn_pdrop |
The dropout ratio for the attention.
TYPE:
|
layer_norm_epsilon |
The epsilon to use in the layer normalization layers.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
summary_type |
Argument used when doing sequence summary, used in the models [ Has to be one of the following options:
TYPE:
|
summary_use_proj |
Argument used when doing sequence summary, used in the models [ Whether or not to add a projection after the vector extraction.
TYPE:
|
summary_activation |
Argument used when doing sequence summary. Used in for the multiple choice head in
[ Pass
TYPE:
|
summary_proj_to_labels |
Argument used when doing sequence summary, used in the models [ Whether the projection outputs should have
TYPE:
|
summary_first_dropout |
Argument used when doing sequence summary, used in the models [ The dropout ratio to be used after the projection and activation.
TYPE:
|
scale_attn_weights |
Scale attention weights by dividing by sqrt(hidden_size)..
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models).
TYPE:
|
bos_token_id |
Id of the beginning of sentence token in the vocabulary.
TYPE:
|
eos_token_id |
Id of the end of sentence token in the vocabulary.
TYPE:
|
scale_attn_by_inverse_layer_idx |
Whether to additionally scale attention weights by
TYPE:
|
reorder_and_upcast_attn |
Whether to scale keys (K) prior to computing attention (dot-product) and upcast attention dot-product/softmax to float() when training with mixed precision.
TYPE:
|
Example
>>> from transformers import GPT2Config, GPT2Model
...
>>> # Initializing a GPT2 configuration
>>> configuration = GPT2Config()
...
>>> # Initializing a model (with random weights) from the configuration
>>> model = GPT2Model(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/gpt2/configuration_gpt2.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 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 |
|
mindnlp.transformers.models.gpt2.configuration_gpt2.GPT2Config.__init__(vocab_size=50257, n_positions=1024, n_embd=768, n_layer=12, n_head=12, n_inner=None, activation_function='gelu_new', resid_pdrop=0.1, embd_pdrop=0.1, attn_pdrop=0.1, layer_norm_epsilon=1e-05, initializer_range=0.02, summary_type='cls_index', summary_use_proj=True, summary_activation=None, summary_proj_to_labels=True, summary_first_dropout=0.1, scale_attn_weights=True, use_cache=True, bos_token_id=50256, eos_token_id=50256, scale_attn_by_inverse_layer_idx=False, reorder_and_upcast_attn=False, **kwargs)
¶
Initializes a new instance of the GPT2Config class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
vocab_size |
The size of the vocabulary. Default is 50257.
TYPE:
|
n_positions |
The maximum number of positions for the input sequence. Default is 1024.
TYPE:
|
n_embd |
The dimensionality of the embeddings and hidden states. Default is 768.
TYPE:
|
n_layer |
The number of layers in the model. Default is 12.
TYPE:
|
n_head |
The number of attention heads. Default is 12.
TYPE:
|
n_inner |
The inner dimensionality in the feed-forward layers. Default is None.
TYPE:
|
activation_function |
The activation function used in the model. Default is 'gelu_new'.
TYPE:
|
resid_pdrop |
The dropout probability for the residual connections. Default is 0.1.
TYPE:
|
embd_pdrop |
The dropout probability for the embeddings. Default is 0.1.
TYPE:
|
attn_pdrop |
The dropout probability for the attention layers. Default is 0.1.
TYPE:
|
layer_norm_epsilon |
The epsilon value for layer normalization. Default is 1e-05.
TYPE:
|
initializer_range |
The range of the initializer. Default is 0.02.
TYPE:
|
summary_type |
The type of summary representation. Default is 'cls_index'.
TYPE:
|
summary_use_proj |
Whether to use projection for the summary representation. Default is True.
TYPE:
|
summary_activation |
The activation function used for the summary projection. Default is None.
TYPE:
|
summary_proj_to_labels |
Whether to project the summary representation to labels. Default is True.
TYPE:
|
summary_first_dropout |
The dropout probability for the first summary layer. Default is 0.1.
TYPE:
|
scale_attn_weights |
Whether to scale attention weights. Default is True.
TYPE:
|
use_cache |
Whether to use cache for the model. Default is True.
TYPE:
|
bos_token_id |
The token ID for the beginning of sentence. Default is 50256.
TYPE:
|
eos_token_id |
The token ID for the end of sentence. Default is 50256.
TYPE:
|
scale_attn_by_inverse_layer_idx |
Whether to scale attention weights by inverse layer index. Default is False.
TYPE:
|
reorder_and_upcast_attn |
Whether to reorder and upcast attention. Default is False.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/gpt2/configuration_gpt2.py
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 |
|
mindnlp.transformers.models.gpt2.tokenization_gpt2
¶
Tokenization classes for OpenAI GPT.
mindnlp.transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer
¶
Bases: PreTrainedTokenizer
Construct a GPT-2 tokenizer. Based on byte-level Byte-Pair-Encoding.
This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will be encoded differently whether it is at the beginning of the sentence (without space) or not:
Example
>>> from transformers import GPT2Tokenizer
...
>>> tokenizer = GPT2Tokenizer.from_pretrained("gpt2")
>>> tokenizer("Hello world")["input_ids"]
[15496, 995]
>>> tokenizer(" Hello world")["input_ids"]
[18435, 995]
You can get around that behavior by passing add_prefix_space=True
when instantiating this tokenizer or when you
call it on some text, but since the model was not pretrained this way, it might yield a decrease in performance.
When used with is_split_into_words=True
, this tokenizer will add a space before each word (even the first one).
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 |
Path to the vocabulary file.
TYPE:
|
merges_file |
Path to the merges file.
TYPE:
|
errors |
Paradigm to follow when decoding bytes to UTF-8. See bytes.decode for more information.
TYPE:
|
unk_token |
The unknown token. A token that is not in the vocabulary cannot be converted to an ID and is set to be this token instead.
TYPE:
|
bos_token |
The beginning of sequence token.
TYPE:
|
eos_token |
The end of sequence token.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
add_prefix_space |
Whether or not to add an initial space to the input. This allows to treat the leading word just as any other word. (GPT2 tokenizer detect beginning of words by the preceding space).
TYPE:
|
add_bos_token |
Whether or not to add an initial beginning of sentence token to the input. This allows to treat the leading word just as any other word.
TYPE:
|
Source code in mindnlp/transformers/models/gpt2/tokenization_gpt2.py
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 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 |
|
mindnlp.transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.default_chat_template
property
¶
A simple chat template that ignores role information and just concatenates messages with EOS tokens.
mindnlp.transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.vocab_size
property
¶
This method retrieves the vocabulary size of the GPT2Tokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2Tokenizer class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
int
|
The number of unique tokens in the tokenizer's vocabulary. |
mindnlp.transformers.models.gpt2.tokenization_gpt2.GPT2Tokenizer.__init__(vocab_file, merges_file, errors='replace', unk_token='<|endoftext|>', bos_token='<|endoftext|>', eos_token='<|endoftext|>', pad_token=None, add_prefix_space=False, add_bos_token=False, **kwargs)
¶
Initializes a GPT2Tokenizer object.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the GPT2Tokenizer class.
|
vocab_file |
The path to the vocabulary file.
TYPE:
|
merges_file |
The path to the merges file.
TYPE:
|
errors |
Specifies how to handle errors during tokenization. Defaults to 'replace'.
TYPE:
|
unk_token |
The unknown token to be used during tokenization. Defaults to 'endoftext'.
TYPE:
|
bos_token |
The beginning of sentence token. Defaults to 'endoftext'.
TYPE:
|
eos_token |
The end of sentence token. Defaults to 'endoftext'.
TYPE:
|
pad_token |
The padding token. Defaults to None.
TYPE:
|
add_prefix_space |
Specifies whether to add a prefix space to the input. Defaults to False.
TYPE:
|
add_bos_token |
Specifies whether to add the beginning of sentence token to the input. Defaults to False.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
FileNotFoundError
|
If the vocab_file or merges_file is not found. |
UnicodeDecodeError
|
If there is an error decoding the vocab_file or merges_file. |
Source code in mindnlp/transformers/models/gpt2/tokenization_gpt2.py
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 |
|