xlm
mindnlp.transformers.models.xlm.modeling_xlm
¶
PyTorch XLM model.
mindnlp.transformers.models.xlm.modeling_xlm.MultiHeadAttention
¶
Bases: Module
A class representing a multi-head attention mechanism for neural networks.
This class implements multi-head attention by dividing the input into multiple heads and processing them in parallel. It includes methods for initializing the attention mechanism, pruning heads based on specific criteria, and forwarding the attention output based on input, masks, and key-value pairs.
ATTRIBUTE | DESCRIPTION |
---|---|
layer_id |
An identifier for the attention layer.
|
dim |
The dimensionality of the input.
|
n_heads |
The number of attention heads.
|
dropout |
The dropout rate for attention weights.
|
q_lin |
Linear transformation for query vectors.
|
k_lin |
Linear transformation for key vectors.
|
v_lin |
Linear transformation for value vectors.
|
out_lin |
Linear transformation for the final output.
|
pruned_heads |
A set containing indices of pruned attention heads.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the multi-head attention mechanism. |
prune_heads |
Prunes specified attention heads based on given criteria. |
forward |
Constructs the attention output based on input, masks, and key-value pairs. |
Note
This class inherits from nn.Module and is designed for neural network architectures that require multi-head attention mechanisms.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.MultiHeadAttention.__init__(n_heads, dim, config)
¶
Initialize a MultiHeadAttention object.
PARAMETER | DESCRIPTION |
---|---|
self |
The MultiHeadAttention object.
|
n_heads |
The number of attention heads.
TYPE:
|
dim |
The dimension of the input.
TYPE:
|
config |
The configuration object containing the attention dropout.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the dimension is not divisible by the number of attention heads. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.MultiHeadAttention.forward(input, mask, kv=None, cache=None, head_mask=None, output_attentions=False)
¶
Self-attention (if kv is None) or attention over source sentence (provided by kv).
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.MultiHeadAttention.prune_heads(heads)
¶
Prunes the attention heads in a MultiHeadAttention layer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MultiHeadAttention class.
TYPE:
|
heads |
A list of integers representing the indices of the attention heads to be pruned.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
This method prunes the specified attention heads in a MultiHeadAttention layer. The attention heads are pruned based on the given indices. The method performs the following steps:
- Calculates the attention_head_size by dividing the dimension (self.dim) by the number of heads (self.n_heads).
- If the list of heads is empty, the method returns without performing any pruning.
- Calls the 'find_pruneable_heads_and_indices' function to find the pruneable heads and their corresponding indices based on the given parameters (heads, self.n_heads, attention_head_size, self.pruned_heads).
- Prunes the linear layers q_lin, k_lin, v_lin, and out_lin using the 'prune_linear_layer' function, passing the calculated indices (index) as a parameter.
- Updates the number of heads (self.n_heads) by subtracting the length of the pruneable heads list.
- Updates the dimension (self.dim) by multiplying the attention_head_size with the updated number of heads.
- Updates the set of pruned heads (self.pruned_heads) by adding the pruneable heads.
Note
Pruning attention heads reduces the computational complexity of the MultiHeadAttention layer.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.TransformerFFN
¶
Bases: Module
TransformerFFN is a class that represents a feed-forward neural network component of a transformer model. It inherits from nn.Module and includes methods for initializing the network and forwarding the forward pass.
ATTRIBUTE | DESCRIPTION |
---|---|
in_dim |
The input dimension of the network.
TYPE:
|
dim_hidden |
The dimension of the hidden layer in the network.
TYPE:
|
out_dim |
The output dimension of the network.
TYPE:
|
config |
The configuration object containing parameters for the network.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the TransformerFFN instance with the specified input, hidden, and output dimensions, as well as the configuration object. |
forward |
Constructs the forward pass of the network using chunking for the specified input. |
ff_chunk |
Implements the feed-forward chunk of the network, including linear transformations, activation function, and dropout. |
Note
This class assumes the presence of nn, ops, and apply_chunking_to_forward functions and objects for neural network and tensor operations.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.TransformerFFN.__init__(in_dim, dim_hidden, out_dim, config)
¶
Initializes an instance of the TransformerFFN class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TransformerFFN class.
TYPE:
|
in_dim |
The input dimension.
TYPE:
|
dim_hidden |
The dimension of the hidden layer.
TYPE:
|
out_dim |
The output dimension.
TYPE:
|
config |
The configuration object containing various settings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.TransformerFFN.ff_chunk(input)
¶
Method 'ff_chunk' in the class 'TransformerFFN'.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TransformerFFN class.
TYPE:
|
input |
The input tensor to the feedforward chunk.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. The method returns the processed input tensor after passing through the feedforward chunk layers. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input tensor is not in the expected format. |
RuntimeError
|
If an issue occurs during the dropout operation. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 |
|
mindnlp.transformers.models.xlm.modeling_xlm.TransformerFFN.forward(input)
¶
Method 'forward' in the class 'TransformerFFN'.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the TransformerFFN class.
TYPE:
|
input |
The input data to be processed by the method.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForMultipleChoice
¶
Bases: XLMPreTrainedModel
XLMForMultipleChoice represents a XLM model for multiple choice tasks. It is a subclass of XLMPreTrainedModel and includes methods for building the model, processing input data, and computing multiple choice classification loss.
ATTRIBUTE | DESCRIPTION |
---|---|
transformer |
An instance of XLMModel for processing input data.
|
sequence_summary |
An instance of SequenceSummary for summarizing the transformer outputs.
|
logits_proj |
An instance of nn.Linear for projecting the sequence summary outputs.
|
PARAMETER | DESCRIPTION |
---|---|
config |
The model configuration.
|
*inputs |
Variable length input for the model.
DEFAULT:
|
**kwargs |
Additional keyword arguments for the model.
DEFAULT:
|
METHOD | DESCRIPTION |
---|---|
forward |
Constructs the model and processes the input data for multiple choice tasks. |
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, MultipleChoiceModelOutput]: A tuple containing the loss and model outputs or an instance of MultipleChoiceModelOutput. |
Note
This class inherits from XLMPreTrainedModel and follows the implementation details specific to XLM multiple choice models.
See Also
RAISES | DESCRIPTION |
---|---|
ValueError
|
If invalid input data or model configuration is provided. |
RuntimeError
|
If errors occur during model processing or loss computation. |
Example
>>> # Initialize XLMForMultipleChoice model
>>> model = XLMForMultipleChoice(config)
...
>>> # Process input data and compute multiple choice classification loss
>>> outputs = model.forward(input_ids, attention_mask, labels=labels)
...
>>> # Access model outputs
>>> logits = outputs.logits
>>> hidden_states = outputs.hidden_states
>>> attentions = outputs.attentions
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForMultipleChoice.__init__(config, *inputs, **kwargs)
¶
Initializes the XLMForMultipleChoice class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing various parameters for model initialization.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided config parameter is not of the correct type. |
ValueError
|
If the config parameter is missing required attributes. |
RuntimeError
|
If an error occurs during the initialization process. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForMultipleChoice.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the multiple choice classification loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnswering
¶
Bases: XLMPreTrainedModel
The XLMForQuestionAnswering
class is a model for question answering tasks using the XLM
(Cross-lingual Language Model) architecture. It is designed to take input sequences and output the start and end
positions of the answer within the sequence.
This class inherits from XLMPreTrainedModel
, which provides the base functionality for loading and using
pre-trained XLM models.
ATTRIBUTE | DESCRIPTION |
---|---|
`transformer` |
An instance of the
|
`qa_outputs` |
An instance of the
|
Example
>>> from transformers import AutoTokenizer, XLMForQuestionAnswering
...
>>> tokenizer = AutoTokenizer.from_pretrained("xlm-mlm-en-2048")
>>> model = XLMForQuestionAnswering.from_pretrained("xlm-mlm-en-2048")
...
>>> input_ids = mindspore.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(0) # Batch size 1
>>> start_positions = mindspore.tensor([1])
>>> end_positions = mindspore.tensor([3])
...
>>> outputs = model(input_ids, start_positions=start_positions, end_positions=end_positions)
>>> loss = outputs.loss
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnswering.__init__(config)
¶
Initializes an instance of the XLMForQuestionAnswering class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the XLMForQuestionAnswering class.
TYPE:
|
config |
The configuration object containing settings for the XLMForQuestionAnswering model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided 'config' parameter is not of the expected type. |
ValueError
|
If there are issues during the initialization process of the XLMForQuestionAnswering instance. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnswering.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, start_positions=None, end_positions=None, is_impossible=None, cls_index=None, p_mask=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:
|
is_impossible |
Labels whether a question has an answer or no answer (SQuAD 2.0)
TYPE:
|
cls_index |
Labels for position (index) of the classification token to use as input for computing plausibility of the answer.
TYPE:
|
p_mask |
Optional mask of tokens which can't be in answers (e.g. [CLS], [PAD], ...). 1.0 means token should be masked. 0.0 mean token is not masked.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, XLMForQuestionAnsweringOutput]
|
|
Example
>>> from transformers import AutoTokenizer, XLMForQuestionAnswering
...
>>> tokenizer = AutoTokenizer.from_pretrained("xlm-mlm-en-2048")
>>> model = XLMForQuestionAnswering.from_pretrained("xlm-mlm-en-2048")
...
>>> input_ids = mindspore.tensor(tokenizer.encode("Hello, my dog is cute", add_special_tokens=True)).unsqueeze(
... 0
... ) # Batch size 1
>>> start_positions = mindspore.tensor([1])
>>> end_positions = mindspore.tensor([3])
...
>>> outputs = model(input_ids, start_positions=start_positions, end_positions=end_positions)
>>> loss = outputs.loss
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnsweringOutput
dataclass
¶
Bases: ModelOutput
Base class for outputs of question answering models using a SquadHead
.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnsweringSimple
¶
Bases: XLMPreTrainedModel
This class represents a simple XLM model for question answering. It inherits from XLMPreTrainedModel and includes methods for forwarding the model and handling question answering tasks.
ATTRIBUTE | DESCRIPTION |
---|---|
transformer |
The XLMModel instance for the transformer component of the model.
TYPE:
|
qa_outputs |
The output layer for question answering predictions.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
Construct the model for question answering tasks, with optional input parameters and return values. This method includes detailed descriptions of the input and output tensors, as well as the expected behavior of the model during inference. |
Note
This class is intended for use with the MindSpore framework.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnsweringSimple.__init__(config)
¶
Initializes a new instance of the 'XLMForQuestionAnsweringSimple' class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the 'XLMConfig' class containing the configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForQuestionAnsweringSimple.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=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/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForSequenceClassification
¶
Bases: XLMPreTrainedModel
XLMForSequenceClassification includes the logic to classify sequences using a transformer-based model. This class inherits from XLMPreTrainedModel and implements the specific logic for sequence classification using the XLM model.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for sequence classification.
TYPE:
|
config |
The configuration for the XLM model.
TYPE:
|
transformer |
The transformer model used for sequence classification.
TYPE:
|
sequence_summary |
The sequence summarization layer.
TYPE:
|
PARAMETER | DESCRIPTION |
---|---|
config |
The configuration object for the XLMForSequenceClassification model.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
forward |
This method forwards the sequence classification model and returns the sequence classifier output. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of labels is invalid or the problem type is not recognized. |
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, SequenceClassifierOutput]: A tuple containing the loss and output if loss is not None, else the output. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the number of labels is invalid or the problem type is not recognized. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForSequenceClassification.__init__(config)
¶
Initializes an instance of the XLMForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the XLMForSequenceClassification class. |
config |
The configuration object containing settings for the model initialization. It must include the number of labels 'num_labels' and other necessary configurations.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the config parameter is not of type XLMConfig. |
ValueError
|
If the config object does not contain the required 'num_labels' attribute. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForSequenceClassification.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, labels=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/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForTokenClassification
¶
Bases: XLMPreTrainedModel
XLMForTokenClassification
This class is a token classification model based on the XLM architecture. It is designed for token-level classification tasks, such as named entity recognition or part-of-speech tagging. The model takes input sequences and predicts a label for each token in the sequence.
The XLMForTokenClassification class inherits from the XLMPreTrainedModel class, which provides the basic functionality for pre-training and fine-tuning XLM models.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
The number of labels for token classification.
TYPE:
|
transformer |
The XLMModel instance used for the transformer architecture.
TYPE:
|
dropout |
Dropout layer for regularization.
TYPE:
|
classifier |
Linear layer for classification.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the XLMForTokenClassification instance. |
forward |
Constructs the XLMForTokenClassification model and performs token classification. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForTokenClassification.__init__(config)
¶
Initialize the XLMForTokenClassification class.
PARAMETER | DESCRIPTION |
---|---|
config |
The configuration object for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMForTokenClassification.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, labels=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the token classification loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMModel
¶
Bases: XLMPreTrainedModel
XLMModel is a class representing a transformer model for cross-lingual language model pre-training based on the XLM architecture.
This class inherits from XLMPreTrainedModel and implements various methods for initializing the model, handling embeddings, pruning heads, and forwarding the model for inference.
The init method initializes the model with configuration parameters and sets up the model's architecture. It handles encoder-decoder setup, embeddings, attention mechanisms, layer normalization, and other model components.
The get_input_embeddings method returns the input embeddings used in the model, while set_input_embeddings allows for updating the input embeddings.
The _prune_heads method prunes specific attention heads in the model based on the provided dictionary of {layer_num: list of heads}.
The forward method forwards the model for inference, taking input tensors for input_ids, attention_mask, langs, token_type_ids, position_ids, lengths, cache, head_mask, inputs_embeds, output settings, and returns the model output or a BaseModelOutput object depending on the return_dict setting.
Overall, XLMModel provides a comprehensive implementation of the XLM transformer model for cross-lingual language tasks.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMModel.__init__(config)
¶
This method initializes an instance of the XLMModel class with the provided configuration.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMModel class.
|
config |
An object containing configuration parameters for the XLMModel.
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
If the provided configuration indicates that the XLMModel is used as a decoder, since XLM can only be used as an encoder. |
AssertionError
|
If the transformer dimension is not a multiple of the number of heads. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMModel.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
Constructs the XLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
input_ids |
The input tensor of shape (batch_size, sequence_length).
TYPE:
|
attention_mask |
The attention mask tensor of shape (batch_size, sequence_length).
TYPE:
|
langs |
The language tensor of shape (batch_size, sequence_length).
TYPE:
|
token_type_ids |
The token type tensor of shape (batch_size, sequence_length).
TYPE:
|
position_ids |
The position tensor of shape (batch_size, sequence_length).
TYPE:
|
lengths |
The lengths tensor of shape (batch_size,).
TYPE:
|
cache |
The cache tensor.
TYPE:
|
head_mask |
The head mask tensor.
TYPE:
|
inputs_embeds |
The input embeddings tensor of shape (batch_size, sequence_length, embedding_size).
TYPE:
|
output_attentions |
Whether to output attentions.
TYPE:
|
output_hidden_states |
Whether to output hidden states.
TYPE:
|
return_dict |
Whether to return a dictionary.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutput]
|
Union[Tuple, BaseModelOutput]: The model output, which can be a tuple of tensors or a BaseModelOutput object. |
RAISES | DESCRIPTION |
---|---|
AssertionError
|
If the lengths tensor shape does not match the batch size or if the maximum length in the lengths tensor exceeds the sequence length. |
AssertionError
|
If the position_ids tensor shape does not match the input tensor shape. |
AssertionError
|
If the langs tensor shape does not match the input tensor shape. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMModel.get_input_embeddings()
¶
Retrieve the input embeddings from the XLMModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the XLMModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
598 599 600 601 602 603 604 605 606 607 608 609 610 611 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMModel.set_input_embeddings(new_embeddings)
¶
Set the input embeddings for the XLMModel.
This method sets the input embeddings for the XLMModel using the given new_embeddings.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMModel class.
TYPE:
|
new_embeddings |
The new embeddings to set for the XLMModel. It can be of any type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMPreTrainedModel
¶
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/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMPreTrainedModel.dummy_inputs
property
¶
Generates dummy inputs for the XLMPreTrainedModel.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the XLMPreTrainedModel class.
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the dummy inputs for the model. The dictionary has the following keys:
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMPredLayer
¶
Bases: Module
Prediction layer (cross_entropy or adaptive_softmax).
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMPredLayer.__init__(config)
¶
Initialize the XLMPredLayer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMPredLayer class.
|
config |
A configuration object containing the following attributes:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMPredLayer.forward(x, y=None)
¶
Compute the loss, and optionally the scores.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel
¶
Bases: XLMPreTrainedModel
XLMWithLMHeadModel represents a transformer model with a language modeling head based on the XLM (Cross-lingual Language Model) architecture.
This class inherits from XLMPreTrainedModel and provides methods for initializing the model, getting and setting output embeddings, preparing inputs for generation, and forwarding the model for language modeling tasks.
ATTRIBUTE | DESCRIPTION |
---|---|
transformer |
The XLMModel instance used for the transformer architecture.
TYPE:
|
pred_layer |
The XLMPredLayer instance used for the language modeling head.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the XLMWithLMHeadModel instance with the given configuration. |
get_output_embeddings |
Returns the output embeddings from the language modeling head. |
set_output_embeddings |
Sets new output embeddings for the language modeling head. |
prepare_inputs_for_generation |
Prepares input tensors for language generation tasks. |
forward |
Constructs the model for language modeling tasks and returns the masked language model output. |
Note
The forward method includes detailed documentation for its parameters and return value, including optional and shifted labels for language modeling.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel.__init__(config)
¶
Initializes a new instance of the XLMWithLMHeadModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The current instance of the XLMWithLMHeadModel class.
TYPE:
|
config |
The configuration object for the model.
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel.forward(input_ids=None, attention_mask=None, langs=None, token_type_ids=None, position_ids=None, lengths=None, cache=None, head_mask=None, inputs_embeds=None, labels=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/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel.get_output_embeddings()
¶
Returns the output embeddings of the XLMWithLMHeadModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMWithLMHeadModel class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
911 912 913 914 915 916 917 918 919 920 921 922 923 924 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel.prepare_inputs_for_generation(input_ids, **kwargs)
¶
Prepare the inputs for generation in XLMWithLMHeadModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMWithLMHeadModel class.
|
input_ids |
The input tensor containing token IDs. Shape (batch_size, sequence_length).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the prepared inputs for generation.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input_ids tensor is not valid or if an error occurs during tensor operations. |
TypeError
|
If the input_ids tensor is not of type Tensor. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
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 |
|
mindnlp.transformers.models.xlm.modeling_xlm.XLMWithLMHeadModel.set_output_embeddings(new_embeddings)
¶
Method to set new output embeddings for the XLM model with a language modeling head.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMWithLMHeadModel class.
TYPE:
|
new_embeddings |
The new embeddings to be set as the output embeddings. This parameter should be an instance of torch.nn.Embedding class representing the new embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value explicitly but updates the output embeddings of the model in-place. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the new_embeddings parameter is not an instance of torch.nn.Embedding. |
ValueError
|
If the shape or type of the new_embeddings parameter is not compatible with the model's requirements. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
|
mindnlp.transformers.models.xlm.modeling_xlm.create_sinusoidal_embeddings(n_pos, dim, out)
¶
Creates sinusoidal embeddings for positional encoding.
PARAMETER | DESCRIPTION |
---|---|
n_pos |
The number of positions to be encoded.
TYPE:
|
dim |
The dimension of the embeddings.
TYPE:
|
out |
The output tensor to store the sinusoidal embeddings.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
mindnlp.transformers.models.xlm.modeling_xlm.get_masks(slen, lengths, causal, padding_mask=None)
¶
Generate hidden states mask, and optionally an attention mask.
Source code in mindnlp/transformers/models/xlm/modeling_xlm.py
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
|
mindnlp.transformers.models.xlm.configuration_xlm
¶
XLM configuration
mindnlp.transformers.models.xlm.configuration_xlm.XLMConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [XLMModel
] or a [TFXLMModel
]. It is used to
instantiate a XLM 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
xlm-mlm-en-2048 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:
|
emb_dim |
Dimensionality of the encoder layers and the pooler layer.
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:
|
dropout |
The dropout probability for all fully connected layers in the embeddings, encoder, and pooler.
TYPE:
|
attention_dropout |
The dropout probability for the attention mechanism
TYPE:
|
gelu_activation |
Whether or not to use gelu for the activations instead of relu.
TYPE:
|
sinusoidal_embeddings |
Whether or not to use sinusoidal positional embeddings instead of absolute positional embeddings.
TYPE:
|
causal |
Whether or not the model should behave in a causal manner. Causal models use a triangular attention mask in order to only attend to the left-side context instead if a bidirectional context.
TYPE:
|
asm |
Whether or not to use an adaptive log softmax projection layer instead of a linear layer for the prediction layer.
TYPE:
|
n_langs |
The number of languages the model handles. Set to 1 for monolingual models.
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:
|
embed_init_std |
The standard deviation of the truncated_normal_initializer for initializing the embedding matrices.
TYPE:
|
init_std |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices except the embedding matrices.
TYPE:
|
layer_norm_eps |
The epsilon used by the layer normalization layers.
TYPE:
|
bos_index |
The index of the beginning of sentence token in the vocabulary.
TYPE:
|
eos_index |
The index of the end of sentence token in the vocabulary.
TYPE:
|
pad_index |
The index of the padding token in the vocabulary.
TYPE:
|
unk_index |
The index of the unknown token in the vocabulary.
TYPE:
|
mask_index |
The index of the masking token in the vocabulary.
TYPE:
|
is_encoder(`bool`, |
Whether or not the initialized model should be a transformer encoder or decoder as seen in Vaswani et al.
TYPE:
|
summary_type |
Argument used when doing sequence summary. Used in the sequence classification and multiple choice models. Has to be one of the following options:
TYPE:
|
summary_use_proj |
Argument used when doing sequence summary. Used in the sequence classification and multiple choice models. Whether or not to add a projection after the vector extraction.
TYPE:
|
summary_activation |
Argument used when doing sequence summary. Used in the sequence classification and multiple choice models.
Pass
TYPE:
|
summary_proj_to_labels |
Used in the sequence classification and multiple choice models.
Whether the projection outputs should have
TYPE:
|
summary_first_dropout |
Used in the sequence classification and multiple choice models. The dropout ratio to be used after the projection and activation.
TYPE:
|
start_n_top |
Used in the SQuAD evaluation script.
TYPE:
|
end_n_top |
Used in the SQuAD evaluation script.
TYPE:
|
mask_token_id |
Model agnostic parameter to identify masked tokens when generating text in an MLM context.
TYPE:
|
lang_id |
The ID of the language used by the model. This parameter is used when generating text in a given language.
TYPE:
|
Example
>>> from transformers import XLMConfig, XLMModel
...
>>> # Initializing a XLM configuration
>>> configuration = XLMConfig()
...
>>> # Initializing a model (with random weights) from the configuration
>>> model = XLMModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/xlm/configuration_xlm.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 |
|
mindnlp.transformers.models.xlm.configuration_xlm.XLMConfig.__init__(vocab_size=30145, emb_dim=2048, n_layers=12, n_heads=16, dropout=0.1, attention_dropout=0.1, gelu_activation=True, sinusoidal_embeddings=False, causal=False, asm=False, n_langs=1, use_lang_emb=True, max_position_embeddings=512, embed_init_std=2048 ** -0.5, layer_norm_eps=1e-12, init_std=0.02, bos_index=0, eos_index=1, pad_index=2, unk_index=3, mask_index=5, is_encoder=True, summary_type='first', summary_use_proj=True, summary_activation=None, summary_proj_to_labels=True, summary_first_dropout=0.1, start_n_top=5, end_n_top=5, mask_token_id=0, lang_id=0, pad_token_id=2, bos_token_id=0, **kwargs)
¶
Constructs XLMConfig.
Source code in mindnlp/transformers/models/xlm/configuration_xlm.py
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 |
|
mindnlp.transformers.models.xlm.tokenization_xlm
¶
Tokenization classes for XLM.
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer
¶
Bases: PreTrainedTokenizer
Construct an XLM tokenizer. Based on Byte-Pair Encoding. The tokenization process is the following:
- Moses preprocessing and tokenization for most supported languages.
- Language specific tokenization for Chinese (Jieba), Japanese (KyTea) and Thai (PyThaiNLP).
- Optionally lowercases and normalizes all inputs text.
- The arguments
special_tokens
and the functionset_special_tokens
, can be used to add additional symbols (like "classify") to a vocabulary. - The
lang2id
attribute maps the languages supported by the model with their IDs if provided (automatically set for pretrained vocabularies). - The
id2lang
attributes does reverse mapping if provided (automatically set for pretrained vocabularies).
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 |
Vocabulary file.
TYPE:
|
merges_file |
Merges file.
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 that was used during pretraining. Can be used a sequence classifier token. When building a sequence using special tokens, this is not the token that is used for the beginning of
sequence. The token used is the
TYPE:
|
sep_token |
The separator token, which is used when building a sequence from multiple sequences, e.g. two sequences for sequence classification or for a text and a question for question answering. It is also used as the last token of a sequence built with special tokens.
TYPE:
|
pad_token |
The token used for padding, for example when batching sequences of different lengths.
TYPE:
|
cls_token |
The classifier token which is used when doing sequence classification (classification of the whole sequence instead of per-token classification). It is the first token of the sequence when built with special tokens.
TYPE:
|
mask_token |
The token used for masking values. This is the token used when training this model with masked language modeling. This is the token which the model will try to predict.
TYPE:
|
lang2id |
Dictionary mapping languages string identifiers to their IDs.
TYPE:
|
id2lang |
Dictionary mapping language IDs to their string identifiers.
TYPE:
|
do_lowercase_and_remove_accent |
Whether to lowercase and remove accents when tokenizing.
TYPE:
|
Source code in mindnlp/transformers/models/xlm/tokenization_xlm.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 |
|
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.do_lower_case
property
¶
This method, 'do_lower_case', is a property method within the 'XLMTokenizer' class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the 'XLMTokenizer' class.
|
RETURNS | DESCRIPTION |
---|---|
None. |
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.vocab_size
property
¶
Returns the size of the vocabulary used by the XLMTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the XLMTokenizer class.
|
RETURNS | DESCRIPTION |
---|---|
int
|
The number of unique tokens in the tokenizer's encoder. |
Note
This method calculates the size of the vocabulary by obtaining the length of the tokenizer's encoder. The encoder is responsible for encoding and decoding the tokens used by the tokenizer.
Example
>>> tokenizer = XLMTokenizer()
>>> tokenizer.vocab_size
50000
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.__getstate__()
¶
Method 'getstate' in the class 'XLMTokenizer'.
PARAMETER | DESCRIPTION |
---|---|
self |
XLMTokenizer object. Represents the instance of the XLMTokenizer class. No restrictions.
|
RETURNS | DESCRIPTION |
---|---|
dict
|
This method returns a dictionary representing the current state of the XLMTokenizer object with the 'sm' attribute set to None. |
Source code in mindnlp/transformers/models/xlm/tokenization_xlm.py
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 |
|
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.__init__(vocab_file, merges_file, unk_token='<unk>', bos_token='<s>', sep_token='</s>', pad_token='<pad>', cls_token='</s>', mask_token='<special1>', additional_special_tokens=['<special0>', '<special1>', '<special2>', '<special3>', '<special4>', '<special5>', '<special6>', '<special7>', '<special8>', '<special9>'], lang2id=None, id2lang=None, do_lowercase_and_remove_accent=True, **kwargs)
¶
Initializes an instance of XLMTokenizer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_file |
The file path to the vocabulary file.
TYPE:
|
merges_file |
The file path to the merges file.
TYPE:
|
unk_token |
The unknown token (default: '
TYPE:
|
bos_token |
The beginning of sentence token (default: '
TYPE:
|
sep_token |
The separator token (default: '').
TYPE:
|
pad_token |
The padding token (default: '
TYPE:
|
cls_token |
The classification token (default: '').
TYPE:
|
mask_token |
The masking token (default: '
TYPE:
|
additional_special_tokens |
List of additional special tokens (default: ['
TYPE:
|
lang2id |
A dictionary mapping languages to IDs.
TYPE:
|
id2lang |
A dictionary mapping IDs to languages.
TYPE:
|
do_lowercase_and_remove_accent |
A flag indicating whether to lowercase and remove accents (default: True).
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ImportError
|
If the sacremoses library is not installed. |
Source code in mindnlp/transformers/models/xlm/tokenization_xlm.py
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 |
|
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.__setstate__(d)
¶
Sets the state of the XLMTokenizer object.
PARAMETER | DESCRIPTION |
---|---|
self |
The XLMTokenizer object.
TYPE:
|
d |
The dictionary containing the state to be set. The dictionary should have the following keys:
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ImportError
|
If the 'sacremoses' module is not installed, an ImportError is raised. The error message will provide instructions on how to install the module. |
Source code in mindnlp/transformers/models/xlm/tokenization_xlm.py
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 |
|
mindnlp.transformers.models.xlm.tokenization_xlm.XLMTokenizer.bpe(token)
¶
This method is part of the XLMTokenizer class and performs Byte Pair Encoding (BPE) on a given token.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the XLMTokenizer class.
|
token |
The input token to be processed through BPE. It should be a string representing a word.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
str
|
The processed token after applying Byte Pair Encoding. The token may have undergone splitting or merging based on the rules of BPE. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If an error occurs during the processing of the token, such as an issue with indexing or comparison. |
KeyError
|
If the method encounters a key error while accessing data structures like dictionaries. |
Source code in mindnlp/transformers/models/xlm/tokenization_xlm.py
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 |
|