mixtral
mindnlp.transformers.models.mixtral.modeling_mixtral
¶
MindSpore Mixtral model.
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralAttention
¶
Bases: Module
Multi-headed attention from 'Attention Is All You Need' paper. Modified to use sliding window attention: Longformer and "Generating Long Sequences with Sparse Transformers".
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralAttention.__init__(config, layer_idx=None)
¶
Initializes an instance of the MixtralAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
config |
An instance of the MixtralConfig class containing the configuration parameters for the attention layer.
TYPE:
|
layer_idx |
The index of the layer. Defaults to None. If layer_idx is not provided,
a warning will be logged, as not passing a
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralAttention.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, **kwargs)
¶
Construct method in the MixtralAttention class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
hidden_states |
The input tensor of shape (batch_size, sequence_length, hidden_size).
TYPE:
|
attention_mask |
An optional tensor of shape (batch_size, 1, sequence_length, sequence_length) containing the attention mask.
TYPE:
|
position_ids |
An optional tensor containing the position indices of shape (batch_size, sequence_length).
TYPE:
|
past_key_value |
An optional caching mechanism for previous key and value tensors.
TYPE:
|
output_attentions |
A boolean flag indicating whether to return the attention weights.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
Tuple[mindspore.Tensor, Optional[mindspore.Tensor], Optional[Tuple[mindspore.Tensor]]]: A tuple containing the attention output tensor of shape (batch_size, sequence_length, hidden_size), |
Optional[Tensor]
|
optional attention weights tensor, and optional new key-value cache tuple. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the cache structure has changed, attention weights or attention mask have invalid shapes, or if the attention output has an unexpected shape. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralBlockSparseTop2MLP
¶
Bases: Module
The MixtralBlockSparseTop2MLP class represents a neural network block that utilizes sparse top-2 multi-layer perceptron (MLP) for processing hidden states. It inherits from nn.Module and includes methods for initialization and forwardion of the MLP layers.
ATTRIBUTE | DESCRIPTION |
---|---|
ffn_dim |
The dimension of the feed-forward network.
TYPE:
|
hidden_dim |
The dimension of the hidden layer in the network.
TYPE:
|
w1 |
The first dense layer in the MLP with hidden_dim input and ffn_dim output.
TYPE:
|
w2 |
The second dense layer in the MLP with ffn_dim input and hidden_dim output.
TYPE:
|
w3 |
The third dense layer in the MLP with hidden_dim input and ffn_dim output.
TYPE:
|
act_fn |
The activation function to be applied on the hidden states.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MixtralBlockSparseTop2MLP instance with the provided configuration. |
forward |
Constructs the sparse top-2 MLP using the provided hidden states and returns the processed hidden states. |
Note
The code provided in the class is an example and may not fully represent the functionality of the MixtralBlockSparseTop2MLP class.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralBlockSparseTop2MLP.__init__(config)
¶
Initializes a MixtralBlockSparseTop2MLP instance.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance itself.
|
config |
An instance of MixtralConfig containing the configuration settings for the MixtralBlockSparseTop2MLP.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralBlockSparseTop2MLP.forward(hidden_states)
¶
Constructs the current hidden states using the provided hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralBlockSparseTop2MLP class. |
hidden_states |
The input hidden states to be used for forwarding the current hidden states.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
tensor
|
The current hidden states forwarded based on the input hidden states. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input hidden_states is not in the expected format. |
RuntimeError
|
If there is an issue with the execution of the method. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralDecoderLayer
¶
Bases: Module
This class represents a decoder layer for the Mixtral model, used for processing input sequences in neural network models. It includes functionality for self-attention, block sparse mixture of experts, layer normalization, and other operations specific to the Mixtral architecture.
The MixtralDecoderLayer class inherits from nn.Module and contains methods for initialization and processing input data through the decoder layer. The init method initializes the layer with configuration settings and creates necessary components such as self-attention mechanism, block sparse mixture of experts, and layer normalization.
The forward method processes the input hidden states along with optional arguments like attention mask, position ids, past key values, and various output flags. It applies layer normalization, self-attention mechanism, block sparse mixture of experts, and additional layer normalization before returning the processed hidden states. Output can include attentions weights, present key values, and router logits based on the specified output flags.
Please refer to the class code for detailed implementation and usage of the MixtralDecoderLayer.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralDecoderLayer.__init__(config, layer_idx)
¶
Initializes an instance of MixtralDecoderLayer.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of MixtralDecoderLayer.
TYPE:
|
config |
An instance of MixtralConfig containing configuration parameters for the layer.
TYPE:
|
layer_idx |
An integer representing the index of the layer.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If config is not an instance of MixtralConfig or if layer_idx is not an integer. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralDecoderLayer.forward(hidden_states, attention_mask=None, position_ids=None, past_key_value=None, output_attentions=False, output_router_logits=False, use_cache=False)
¶
PARAMETER | DESCRIPTION |
---|---|
hidden_states |
input to the layer of shape
TYPE:
|
attention_mask |
attention mask of size
TYPE:
|
past_key_value |
cached past key and value projection states
TYPE:
|
output_attentions |
Whether or not to return the attentions tensors of all attention layers. See
TYPE:
|
output_router_logits |
Whether or not to return the logits of all the routers. They are useful for computing the router loss, and should not be returned during inference.
TYPE:
|
use_cache |
If set to
TYPE:
|
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM
¶
Bases: MixtralPreTrainedModel
Represents a Mixtral model for causal language modeling.
This class provides methods for initializing the model, setting and getting input and output embeddings, setting and getting the decoder, forwarding the model, preparing inputs for generation, and reordering cache values.
The class inherits from MixtralPreTrainedModel. The class also includes a detailed example demonstrating the usage of the MixtralForCausalLM model for generating text.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 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 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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.__init__(config)
¶
Initializes an instance of the MixtralForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
A dictionary containing configuration parameters for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.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 1154 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, output_router_logits=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the masked language modeling loss. Indices should either be in
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, MoeCausalLMOutputWithPast]
|
Union[Tuple, MoeCausalLMOutputWithPast] |
Example
>>> from transformers import AutoTokenizer, MixtralForCausalLM
...
>>> model = MixtralForCausalLM.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
>>> tokenizer = AutoTokenizer.from_pretrained("mistralai/Mixtral-8x7B-v0.1")
...
>>> prompt = "Hey, are you conscious? Can you talk to me?"
>>> inputs = tokenizer(prompt, return_tensors="pt")
...
>>> # Generate
>>> generate_ids = model.generate(inputs.input_ids, max_length=30)
>>> tokenizer.batch_decode(generate_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False)[0]
"Hey, are you conscious? Can you talk to me?\nI'm not conscious, but I can talk to you."
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.get_decoder()
¶
Method to retrieve the decoder from the MixtralForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of MixtralForCausalLM class. This parameter is required to access the model. It should be an instance of the MixtralForCausalLM class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns None as it simply retrieves and returns the model's decoder. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.get_input_embeddings()
¶
Retrieve input embeddings from the model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralForCausalLM class.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the input embeddings from the model. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.get_output_embeddings()
¶
Retrieve the output embeddings from the MixtralForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MixtralForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
The output embeddings of the model. |
This method retrieves the output embeddings from the MixtralForCausalLM model. The output embeddings represent the learned representations of the model's output tokens. These embeddings can be used for downstream tasks such as fine-tuning or further analysis.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, output_router_logits=False, **kwargs)
¶
Prepare inputs for generation in the MixtralForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralForCausalLM class.
TYPE:
|
input_ids |
The input tensor containing tokenized input IDs.
TYPE:
|
past_key_values |
The past key values for autoregressive generation or None if no past values are available.
TYPE:
|
attention_mask |
The attention mask tensor to avoid attending to padding tokens, or None if no mask is provided.
TYPE:
|
inputs_embeds |
The input embeddings tensor, or None if input_ids is used for embeddings.
TYPE:
|
output_router_logits |
A flag indicating whether to output router logits for routing the generated tokens.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing the model inputs for generation, including input_ids, position_ids, past_key_values, use_cache, attention_mask, and output_router_logits. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the input_ids and attention_mask dimensions are inconsistent or if the cache length exceeds the maximum length. |
TypeError
|
If the past_key_values type is invalid. |
IndexError
|
If the input_ids shape is invalid. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.set_decoder(decoder)
¶
Sets the decoder for MixtralForCausalLM.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of MixtralForCausalLM.
TYPE:
|
decoder |
The decoder object to be set for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.set_input_embeddings(value)
¶
Sets the input embeddings of the MixtralForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralForCausalLM class.
TYPE:
|
value |
The new input embeddings to be set for the model. It can be of any compatible type that can be assigned to the 'embed_tokens' attribute of the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method updates the 'embed_tokens' attribute of the model in place. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForCausalLM.set_output_embeddings(new_embeddings)
¶
Set the output embeddings of the MixtralForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralForCausalLM model.
TYPE:
|
new_embeddings |
The new output embeddings to be set for the model. Should be compatible with the model's architecture and dimensions.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForSequenceClassification
¶
Bases: MixtralPreTrainedModel
MixtralForSequenceClassification
This class represents a Mixtral model for sequence classification. It inherits from MixtralPreTrainedModel and is designed to handle sequence classification tasks. It includes methods for initializing the model, getting and setting input embeddings, and forwarding the model for sequence classification. The class also provides detailed documentation for the forward method, which accepts various input parameters and returns the sequence classification output.
ATTRIBUTE | DESCRIPTION |
---|---|
num_labels |
An integer representing the number of labels for sequence classification.
|
model |
An instance of MixtralModel used for the sequence classification task.
|
score |
A neural network module for generating scores based on hidden states.
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MixtralForSequenceClassification instance with the provided configuration. |
get_input_embeddings |
Retrieves the input embeddings from the model. |
set_input_embeddings |
Sets the input embeddings for the model. |
forward |
Constructs the model for sequence classification, processing the input data and returning the sequence classification output. |
The forward method supports various optional input parameters, including input_ids, attention_mask, position_ids, past_key_values, inputs_embeds, labels, use_cache, output_attentions, output_hidden_states, and return_dict. The labels parameter is optional and can be used for computing the sequence classification/regression loss. The method also handles different problem types such as regression, single-label classification, and multi-label classification, and computes the loss accordingly.
RETURNS | DESCRIPTION |
---|---|
Conditional returns:
|
Note
The class documentation and method descriptions are based on the provided Python code and its associated functionality.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForSequenceClassification.__init__(config)
¶
Initializes an instance of MixtralForSequenceClassification class.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance itself.
|
config |
An object containing configuration settings for the model. It should have a 'num_labels' attribute specifying the number of output labels.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
AttributeError
|
If the 'config' parameter does not contain the required 'num_labels' attribute. |
TypeError
|
If the 'config' parameter is not of the expected type. |
ValueError
|
If there are issues during the initialization process. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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.mixtral.modeling_mixtral.MixtralForSequenceClassification.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, labels=None, use_cache=None, output_attentions=None, output_hidden_states=None, return_dict=None)
¶
PARAMETER | DESCRIPTION |
---|---|
labels |
Labels for computing the sequence classification/regression loss. Indices should be in
TYPE:
|
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForSequenceClassification.get_input_embeddings()
¶
Description: This method retrieves the input embeddings from the model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MixtralForSequenceClassification class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralForSequenceClassification.set_input_embeddings(value)
¶
Set the input embeddings for the MixtralForSequenceClassification model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralForSequenceClassification class. |
value |
The input embeddings to be set for the model. It should be of type torch.Tensor.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralModel
¶
Bases: MixtralPreTrainedModel
Transformer decoder consisting of config.num_hidden_layers layers. Each layer is a [MixtralDecoderLayer
]
PARAMETER | DESCRIPTION |
---|---|
config |
MixtralConfig
TYPE:
|
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralModel.__init__(config)
¶
Initializes an instance of the MixtralModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration object containing various parameters for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralModel.forward(input_ids=None, attention_mask=None, position_ids=None, past_key_values=None, inputs_embeds=None, use_cache=None, output_attentions=None, output_hidden_states=None, output_router_logits=None, return_dict=None)
¶
Constructs the MixtralModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The object itself.
|
input_ids |
The input tensor IDs. Default is None.
TYPE:
|
attention_mask |
The attention mask tensor. Default is None.
TYPE:
|
position_ids |
The position IDs tensor. Default is None.
TYPE:
|
past_key_values |
The list of past key value tensors. Default is None.
TYPE:
|
inputs_embeds |
The input embeddings tensor. Default is None.
TYPE:
|
use_cache |
Whether to use cache. Default is None.
TYPE:
|
output_attentions |
Whether to output attention tensors. Default is None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default is None.
TYPE:
|
output_router_logits |
Whether to output router logits. Default is None.
TYPE:
|
return_dict |
Whether to return a dictionary. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, MoeModelOutputWithPast]
|
Union[Tuple, MoeModelOutputWithPast]: The output of the MixtralModel, which can be a tuple or an instance of MoeModelOutputWithPast. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both input_ids and inputs_embeds are specified. |
ValueError
|
If neither input_ids nor inputs_embeds are specified. |
Warning
|
If use_cache is True and gradient checkpointing is enabled. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralModel.get_input_embeddings()
¶
Get the input embeddings for the MixtralModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralModel class.
|
RETURNS | DESCRIPTION |
---|---|
embed_tokens
|
This method returns the input embeddings for the MixtralModel. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralModel.set_input_embeddings(value)
¶
Set the input embeddings for the MixtralModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralModel class.
TYPE:
|
value |
The input embeddings to be set for the model. It can be of any valid type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralPreTrainedModel
¶
Bases: PreTrainedModel
The MixtralPreTrainedModel
class is a subclass of PreTrainedModel
that represents a pre-trained model for
Mixtral models.
This class provides a method _init_weights
that initializes the weights of the model. It takes a cell
parameter and initializes the weights based on the type of the cell
. If the cell
is an instance of nn.Linear
,
the weight is initialized using the Normal
initializer with a range specified by the initializer_range
attribute
of the config
object. If the cell
has a bias, it is initialized with zeros. If the cell
is an instance of
nn.Embedding
, the weight is initialized with random values from a normal distribution with a mean of 0 and a
standard deviation specified by the initializer_range
attribute of the config
object. If the cell
has a
padding_idx
, the weight at the padding_idx
is set to 0.
Note
This docstring does not include signatures or any other code.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRMSNorm
¶
Bases: Module
The MixtralRMSNorm class is a custom implementation of the T5LayerNorm, which is used for normalization in neural networks.
This class inherits from the nn.Module class and provides methods to perform RMS normalization on hidden states.
ATTRIBUTE | DESCRIPTION |
---|---|
weight |
A learnable parameter that scales the normalized hidden states.
TYPE:
|
variance_epsilon |
A small epsilon value added to the variance to avoid division by zero.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes the MixtralRMSNorm instance with the given hidden size and epsilon value. |
forward |
Applies the RMS normalization on the input hidden states and returns the normalized result. |
Note
- MixtralRMSNorm is equivalent to T5LayerNorm.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRMSNorm.__init__(hidden_size, eps=1e-06)
¶
MixtralRMSNorm is equivalent to T5LayerNorm
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
175 176 177 178 179 180 181 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRMSNorm.forward(hidden_states)
¶
This method 'forward' is defined within the 'MixtralRMSNorm' class and is used to perform a specific computation on the input hidden states.
PARAMETER | DESCRIPTION |
---|---|
self |
Represents the instance of the class. It is automatically passed when the method is called. No specific restrictions apply.
|
hidden_states |
Represents the input hidden states tensor. It should be of type torch.Tensor or compatible. No specific restrictions apply.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. It performs in-place operations on the input hidden_states. |
RAISES | DESCRIPTION |
---|---|
NotImplementedError
|
If the method or a specific operation within the method is not implemented. |
ValueError
|
If the input hidden_states is not of the expected data type or format. |
RuntimeError
|
If an error occurs during the computation process. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding
¶
Bases: Module
A class representing MixtralRotaryEmbedding, a neural network module used for Rotary Positional Embedding in Mixtral models.
This class inherits from nn.Module and provides methods to initialize the embedding, set the cosine and sine cache, and forward the embedding for a given input sequence.
ATTRIBUTE | DESCRIPTION |
---|---|
dim |
The dimension of the embedding.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings.
TYPE:
|
base |
The base value used in the inverse frequency calculation.
TYPE:
|
inv_freq |
The inverse frequency tensor used for the embedding.
TYPE:
|
max_seq_len_cached |
The maximum sequence length up to which the cosine and sine cache is calculated.
TYPE:
|
cos_cached |
The cosine cache tensor.
TYPE:
|
sin_cached |
The sine cache tensor.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
Initializes a MixtralRotaryEmbedding instance with the specified dimension, maximum position embeddings, and base value. |
_set_cos_sin_cache |
Sets the cosine and sine cache for the specified sequence length and data type. |
forward |
Constructs the rotary positional embedding for the given input sequence. |
Note
This class is designed for use in Mixtral models and is intended to be used as a part of a larger neural network architecture.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding.__init__(dim, max_position_embeddings=2048, base=10000)
¶
init(self, dim, max_position_embeddings=2048, base=10000)
Initialize the MixtralRotaryEmbedding instance with the specified parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralRotaryEmbedding class.
|
dim |
The dimension of the embedding.
TYPE:
|
max_position_embeddings |
The maximum number of position embeddings. Defaults to 2048.
TYPE:
|
base |
The base value for computing the inverse frequency. Defaults to 10000.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided 'dim', 'max_position_embeddings', or 'base' is not of type int. |
ValueError
|
If 'dim' is not a positive integer or 'max_position_embeddings' or 'base' is not a non-negative integer. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralRotaryEmbedding.forward(x, seq_len=None)
¶
This method forwards a Mixtral Rotary Embedding based on the input parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralRotaryEmbedding class.
|
x |
The input tensor for which the embedding is forwarded.
|
seq_len |
An integer representing the sequence length of the embedding. Default is None.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If seq_len is greater than the maximum sequence length cached in the object. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralSparseMoeBlock
¶
Bases: Module
This implementation is strictly equivalent to standard MoE with full capacity (no dropped tokens). It's faster since it formulates MoE operations in terms of block-sparse operations to accomodate imbalanced assignments of tokens to experts, whereas standard MoE either (1) drop tokens at the cost of reduced performance or (2) set capacity factor to number of experts and thus waste computation and memory on padding.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralSparseMoeBlock.__init__(config)
¶
Initializes an instance of the MixtralSparseMoeBlock class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the MixtralSparseMoeBlock class.
|
config |
A configuration object containing the following attributes:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the provided config parameter is not of the expected type. |
ValueError
|
If the hidden_size, intermediate_size, num_local_experts, or num_experts_per_tok attributes are missing in the config object. |
ValueError
|
If the hidden_size, intermediate_size, num_local_experts, or num_experts_per_tok attributes are not integers. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.MixtralSparseMoeBlock.forward(hidden_states)
¶
Constructs the MixtralSparseMoeBlock.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the MixtralSparseMoeBlock class.
TYPE:
|
hidden_states |
The input hidden states tensor of shape (batch_size, sequence_length, hidden_dim).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tensor
|
mindspore.Tensor: The final hidden states tensor after applying the MixtralSparseMoeBlock, of shape (batch_size, sequence_length, hidden_dim). |
This method forwards the MixtralSparseMoeBlock by applying the following steps:
- Reshapes the hidden_states tensor to (-1, hidden_dim).
- Computes the router logits by passing the reshaped hidden_states through the gate module.
- Computes the routing weights by applying softmax to the router logits along axis 1.
- Selects the top-k routing weights and corresponding indices.
- Normalizes the routing weights.
- Converts the routing weights to the same data type as hidden_states.
- Initializes the final_hidden_states tensor with zeros of shape (batch_size * sequence_length, hidden_dim).
- Generates the expert_mask tensor using one_hot encoding and permutation.
-
Iterates over each expert and performs the following steps:
- Retrieves the non-zero indices from the expert_mask for the current expert.
- Splits the non-zero indices tensor into index and top_x tensors.
- If top_x tensor is empty, continue to the next iteration.
- Retrieves the current hidden states by indexing the hidden_states tensor with top_x.
- Computes the current hidden states using the expert_layer and routing_weights.
- Updates the final_hidden_states tensor by adding the computed current_hidden_states using index_add.
-
Reshapes the final_hidden_states tensor to its original shape (batch_size, sequence_length, hidden_dim).
- Returns the final_hidden_states tensor and the router_logits tensor.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.apply_rotary_pos_emb(q, k, cos, sin, position_ids, unsqueeze_dim=1)
¶
Applies Rotary Position Embedding to the query and key tensors.
PARAMETER | DESCRIPTION |
---|---|
q |
The query tensor.
TYPE:
|
k |
The key tensor.
TYPE:
|
cos |
The cosine part of the rotary embedding.
TYPE:
|
sin |
The sine part of the rotary embedding.
TYPE:
|
position_ids |
The position indices of the tokens corresponding to the query and key tensors. For example, this can be used to pass offsetted position ids when working with a KV-cache.
TYPE:
|
unsqueeze_dim |
The 'unsqueeze_dim' argument specifies the dimension along which to unsqueeze cos[position_ids] and sin[position_ids] so that they can be properly broadcasted to the dimensions of q and k. For example, note that cos[position_ids] and sin[position_ids] have the shape [batch_size, seq_len, head_dim]. Then, if q and k have the shape [batch_size, heads, seq_len, head_dim], then setting unsqueeze_dim=1 makes cos[position_ids] and sin[position_ids] broadcastable to the shapes of q and k. Similarly, if q and k have the shape [batch_size, seq_len, heads, head_dim], then set unsqueeze_dim=2.
TYPE:
|
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
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 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.load_balancing_loss_func(gate_logits, num_experts=None, top_k=2, attention_mask=None)
¶
Computes auxiliary load balancing loss as in Switch Transformer - implemented in MindSpore.
See Switch Transformer (https://arxiv.org/abs/2101.03961) for more details. This function implements the loss function presented in equations (4) - (6) of the paper. It aims at penalizing cases where the routing between experts is too unbalanced.
PARAMETER | DESCRIPTION |
---|---|
gate_logits |
Logits from the
TYPE:
|
attention_mask |
The attention_mask used in forward function shape [batch_size X sequence_length] if not None.
TYPE:
|
num_experts |
Number of experts
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
float
|
The auxiliary loss. |
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.repeat_kv(hidden_states, n_rep)
¶
This is the equivalent of torch.repeat_interleave(x, dim=1, repeats=n_rep). The hidden states go from (batch, num_key_value_heads, seqlen, head_dim) to (batch, num_attention_heads, seqlen, head_dim)
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
365 366 367 368 369 370 371 372 373 374 |
|
mindnlp.transformers.models.mixtral.modeling_mixtral.rotate_half(x)
¶
Rotates half the hidden dims of the input.
Source code in mindnlp/transformers/models/mixtral/modeling_mixtral.py
327 328 329 330 331 332 |
|
mindnlp.transformers.models.mixtral.configuration_mixtral
¶
Mixtral model configuration
mindnlp.transformers.models.mixtral.configuration_mixtral.MixtralConfig
¶
Bases: PretrainedConfig
This is the configuration class to store the configuration of a [MixtralModel
]. It is used to instantiate an
Mixtral 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 Mixtral-7B-v0.1 or Mixtral-7B-Instruct-v0.1.
mixtralai/Mixtral-8x7B mixtralai/Mixtral-7B-Instruct-v0.1
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 Mixtral model. Defines the number of different tokens that can be represented by the
TYPE:
|
hidden_size |
Dimension of the hidden representations.
TYPE:
|
intermediate_size |
Dimension of the MLP representations.
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:
|
num_key_value_heads |
This is the number of key_value heads that should be used to implement Grouped Query Attention. If
TYPE:
|
hidden_act |
The non-linear activation function (function or string) in the decoder.
TYPE:
|
max_position_embeddings |
The maximum sequence length that this model might ever be used with. Mixtral's sliding window attention allows sequence of up to 4096*32 tokens.
TYPE:
|
initializer_range |
The standard deviation of the truncated_normal_initializer for initializing all weight matrices.
TYPE:
|
rms_norm_eps |
The epsilon used by the rms normalization layers.
TYPE:
|
use_cache |
Whether or not the model should return the last key/values attentions (not used by all models). Only
relevant if
TYPE:
|
pad_token_id |
The id of the padding token.
TYPE:
|
bos_token_id |
The id of the "beginning-of-sequence" token.
TYPE:
|
eos_token_id |
The id of the "end-of-sequence" token.
TYPE:
|
tie_word_embeddings |
Whether the model's input and output word embeddings should be tied.
TYPE:
|
rope_theta |
The base period of the RoPE embeddings.
TYPE:
|
sliding_window |
Sliding window attention window size. If not specified, will default to
TYPE:
|
attention_dropout |
The dropout ratio for the attention probabilities.
TYPE:
|
num_experts_per_tok |
The number of experts to root per-token, can be also interpreted as the
TYPE:
|
num_local_experts |
Number of experts per Sparse MLP layer.
TYPE:
|
output_router_logits |
Whether or not the router logits should be returned by the model. Enabeling this will also allow the model to output the auxiliary loss. See here for more details
TYPE:
|
router_aux_loss_coef |
The aux loss factor for the total loss.
TYPE:
|
Example
>>> from transformers import MixtralModel, MixtralConfig
...
>>> # Initializing a Mixtral 7B style configuration
>>> configuration = MixtralConfig()
...
>>> # Initializing a model from the Mixtral 7B style configuration
>>> model = MixtralModel(configuration)
...
>>> # Accessing the model configuration
>>> configuration = model.config
Source code in mindnlp/transformers/models/mixtral/configuration_mixtral.py
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 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 |
|
mindnlp.transformers.models.mixtral.configuration_mixtral.MixtralConfig.__init__(vocab_size=32000, hidden_size=4096, intermediate_size=14336, num_hidden_layers=32, num_attention_heads=32, num_key_value_heads=8, hidden_act='silu', max_position_embeddings=4096 * 32, initializer_range=0.02, rms_norm_eps=1e-05, use_cache=True, pad_token_id=None, bos_token_id=1, eos_token_id=2, tie_word_embeddings=False, rope_theta=1000000.0, sliding_window=None, attention_dropout=0.0, num_experts_per_tok=2, num_local_experts=8, output_router_logits=False, router_aux_loss_coef=0.001, **kwargs)
¶
Initializes a new MixtralConfig object.
PARAMETER | DESCRIPTION |
---|---|
vocab_size |
The size of the vocabulary. Defaults to 32000.
TYPE:
|
hidden_size |
The size of the hidden layers. Defaults to 4096.
TYPE:
|
intermediate_size |
The size of the intermediate layers. Defaults to 14336.
TYPE:
|
num_hidden_layers |
The number of hidden layers. Defaults to 32.
TYPE:
|
num_attention_heads |
The number of attention heads. Defaults to 32.
TYPE:
|
num_key_value_heads |
The number of key and value heads. Defaults to 8.
TYPE:
|
hidden_act |
The activation function for the hidden layers. Defaults to 'silu'.
TYPE:
|
max_position_embeddings |
The maximum position embeddings. Defaults to 4096 * 32.
TYPE:
|
initializer_range |
The range for weight initialization. Defaults to 0.02.
TYPE:
|
rms_norm_eps |
The epsilon value for RMS normalization. Defaults to 1e-05.
TYPE:
|
use_cache |
Indicates whether to use cache. Defaults to True.
TYPE:
|
pad_token_id |
The ID of the padding token. Defaults to None.
TYPE:
|
bos_token_id |
The ID of the beginning-of-sequence token. Defaults to 1.
TYPE:
|
eos_token_id |
The ID of the end-of-sequence token. Defaults to 2.
TYPE:
|
tie_word_embeddings |
Indicates whether to tie word embeddings. Defaults to False.
TYPE:
|
rope_theta |
The theta value for rope. Defaults to 1000000.0.
TYPE:
|
sliding_window |
The size of the sliding window. Defaults to None.
TYPE:
|
attention_dropout |
The dropout rate for attention. Defaults to 0.0.
TYPE:
|
num_experts_per_tok |
The number of experts per token. Defaults to 2.
TYPE:
|
num_local_experts |
The number of local experts. Defaults to 8.
TYPE:
|
output_router_logits |
Indicates whether to output router logits. Defaults to False.
TYPE:
|
router_aux_loss_coef |
The coefficient for router auxiliary loss. Defaults to 0.001.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If any of the provided parameters are invalid or out of range. |
TypeError
|
If the input types are incorrect. |
Source code in mindnlp/transformers/models/mixtral/configuration_mixtral.py
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 |
|