bert_generation
mindnlp.transformers.models.bert_generation.configuration_bert_generation.BertGenerationConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [BertGenerationPreTrainedModel
]. It is used to
instantiate a BertGeneration 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 BertGeneration
google/bert_for_seq_generation_L-24_bbc_encoder
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 BERT model. Defines the number of different tokens that can be represented by the
TYPE:
|
hidden_size |
Dimensionality of the encoder layers and the pooler layer.
TYPE:
|
num_hidden_layers |
Number of hidden layers in the Transformer encoder.
TYPE:
|
num_attention_heads |
Number of attention heads for each attention layer in the Transformer encoder.
TYPE:
|
intermediate_size |
Dimensionality of the "intermediate" (often called feed-forward) layer in the Transformer encoder.
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the encoder and pooler. If string,
TYPE:
|
hidden_dropout_prob |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_probs_dropout_prob |
The dropout ratio for the attention probabilities.
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:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
pad_token_id |
Padding token id.
TYPE:
|
bos_token_id |
Beginning of stream token id.
TYPE:
|
eos_token_id |
End of stream token id.
TYPE:
|
position_embedding_type |
Type of position embedding. Choose one of
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if
TYPE:
|
Example
>>> from transformers import BertGenerationConfig, BertGenerationEncoder
...
>>> # Initializing a BertGeneration config
>>> configuration = BertGenerationConfig()
...
>>> # Initializing a model (with random weights) from the config
>>> model = BertGenerationEncoder(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/bert_generation/configuration_bert_generation.py
20 21 22 23 24 25 26 27 28 29 30 31 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 |
|
mindnlp.transformers.models.bert_generation.configuration_bert_generation.BertGenerationConfig.__init__(vocab_size=50358, hidden_size=1024, num_hidden_layers=24, num_attention_heads=16, intermediate_size=4096, hidden_act='gelu', hidden_dropout_prob=0.1, attention_probs_dropout_prob=0.1, max_position_embeddings=512, initializer_range=0.02, layer_norm_eps=1e-12, pad_token_id=0, bos_token_id=2, eos_token_id=1, position_embedding_type='absolute', use_cache=True, **kwargs)
¶
This method initializes an instance of the BertGenerationConfig class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_size |
The size of the vocabulary. Default is 50358.
TYPE:
|
hidden_size |
The size of the hidden layers. Default is 1024.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Default is 24.
TYPE:
|
num_attention_heads |
The number of attention heads. Default is 16.
TYPE:
|
intermediate_size |
The size of the intermediate layer in the transformer encoder. Default is 4096.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Default is 'gelu'.
TYPE:
|
hidden_dropout_prob |
The dropout probability for the hidden layers. Default is 0.1.
TYPE:
|
attention_probs_dropout_prob |
The dropout probability for the attention probabilities. Default is 0.1.
TYPE:
|
max_position_embeddings |
The maximum number of positional embeddings. Default is 512.
TYPE:
|
initializer_range |
The range of the parameter initializer. Default is 0.02.
TYPE:
|
layer_norm_eps |
The epsilon value for layer normalization. Default is 1e-12.
TYPE:
|
pad_token_id |
The token id for padding. Default is 0.
TYPE:
|
bos_token_id |
The token id for the beginning of sequence. Default is 2.
TYPE:
|
eos_token_id |
The token id for the end of sequence. Default is 1.
TYPE:
|
position_embedding_type |
The type of position embedding. Default is 'absolute'.
TYPE:
|
use_cache |
Whether to use caching. Default is True.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the input arguments are invalid. |
Source code in mindnlp/transformers/models/bert_generation/configuration_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder
¶
Bases: BertGenerationPreTrainedModel
This class represents a decoder model for BERT generation. It extends the BertGenerationPreTrainedModel and provides methods for initializing the model, forwarding the model outputs, preparing inputs for generation, and reordering cache. The class includes methods for initializing the model, retrieving and setting output embeddings, forwarding the model outputs, preparing inputs for generation, and reordering cache. The detailed docstrings for each method provide information about the parameters, return types, and usage examples. This class is designed to be used as part of the BERT generation framework and provides essential functionality for decoding and generating outputs based on input sequences.
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder.__init__(config)
¶
Initializes a new instance of the BertGenerationDecoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing settings for the decoder. This object must have the necessary attributes and properties required for configuring the decoder. It should also have an attribute 'is_decoder' to indicate if the decoder is being used as a standalone component.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, labels=None, past_key_values=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
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:
|
labels |
Labels for computing the left-to-right language modeling loss (next word prediction). Indices should be in
TYPE:
|
use_cache |
If set to
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, CausalLMOutputWithCrossAttentions]
|
Union[Tuple, CausalLMOutputWithCrossAttentions] |
Example
>>> from transformers import AutoTokenizer, BertGenerationDecoder, BertGenerationConfig
>>> import torch
...
>>> tokenizer = AutoTokenizer.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder")
>>> config = BertGenerationConfig.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder")
>>> config.is_decoder = True
>>> model = BertGenerationDecoder.from_pretrained(
... "google/bert_for_seq_generation_L-24_bbc_encoder", config=config
... )
...
>>> inputs = tokenizer("Hello, my dog is cute", return_token_type_ids=False, return_tensors="pt")
>>> outputs = model(**inputs)
...
>>> prediction_logits = outputs.logits
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder.get_output_embeddings()
¶
This method returns the output embeddings of the BertGenerationDecoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance of the BertGenerationDecoder class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the output embeddings of the decoder in the BertGenerationDecoder class. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, **model_kwargs)
¶
This method prepares inputs for generation in the BertGenerationDecoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BertGenerationDecoder class.
TYPE:
|
input_ids |
The input tensor containing token IDs. Shape should be (batch_size, sequence_length).
TYPE:
|
past_key_values |
Tuple containing past key values from previous generations. Default is None.
TYPE:
|
attention_mask |
The attention mask tensor. If not provided, a tensor of ones with the same shape as input_ids is created.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for generation including 'input_ids', 'attention_mask', and 'past_key_values'. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the provided input_ids shape is invalid. |
IndexError
|
If there is an issue with past_key_values. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationDecoder.set_output_embeddings(new_embeddings)
¶
Method to set new output embeddings for the decoder in BertGenerationDecoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of BertGenerationDecoder to which the new embeddings will be set.
TYPE:
|
new_embeddings |
The new embeddings to set for the decoder. Should be of type compatible with the decoder.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationEncoder
¶
Bases: BertGenerationPreTrainedModel
The model can behave as an encoder (with only self-attention) as well as a decoder, in which case a layer of cross-attention is added between the self-attention layers, following the architecture described in Attention is all you need by Ashish Vaswani, Noam Shazeer, Niki Parmar, Jakob Uszkoreit, Llion Jones, Aidan N. Gomez, Lukasz Kaiser and Illia Polosukhin.
This model should be used when leveraging Bert or Roberta checkpoints for the [EncoderDecoderModel
] class as
described in Leveraging Pre-trained Checkpoints for Sequence Generation Tasks
by Sascha Rothe, Shashi Narayan, and Aliaksei Severyn.
To behave as an decoder the model needs to be initialized with the is_decoder
argument of the configuration set
to True
. To be used in a Seq2Seq model, the model needs to initialized with both is_decoder
argument and
add_cross_attention
set to True
; an encoder_hidden_states
is then expected as an input to the forward pass.
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationEncoder.__init__(config)
¶
Initializes a BertGenerationEncoder instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BertGenerationEncoder class being initialized.
TYPE:
|
config |
A dictionary containing configuration parameters for the BertGenerationEncoder. This dictionary must include the necessary settings for the embeddings and encoder components.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationEncoder.forward(input_ids=None, attention_mask=None, position_ids=None, head_mask=None, inputs_embeds=None, encoder_hidden_states=None, encoder_attention_mask=None, past_key_values=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
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:
|
use_cache |
If set to
TYPE:
|
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
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 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationEncoder.get_input_embeddings()
¶
This method retrieves the input embeddings from the BertGenerationEncoder.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BertGenerationEncoder class.
|
RETURNS | DESCRIPTION |
---|---|
word_embeddings
|
This method returns the word embeddings for input. |
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationEncoder.set_input_embeddings(value)
¶
Sets the input embeddings for the BertGenerationEncoder class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the BertGenerationEncoder class.
TYPE:
|
value |
The input embeddings to be set. This should be of type
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method sets the value of the word_embeddings
attribute of the embeddings
object
within the BertGenerationEncoder
instance to the provided input embeddings.
Source code in mindnlp/transformers/models/bert_generation/modeling_bert_generation.py
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
|
mindnlp.transformers.models.bert_generation.modeling_bert_generation.BertGenerationPreTrainedModel
¶
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/bert_generation/modeling_bert_generation.py
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 |
|