baichuan
mindnlp.transformers.models.baichuan.configuration_baichuan.BaiChuanConfig
¶
Bases: PretrainedConfig
Configurations for BaiChuan
Source code in mindnlp/transformers/models/baichuan/configuration_baichuan.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
|
mindnlp.transformers.models.baichuan.configuration_baichuan.BaiChuanConfig.__init__(vocab_size=64000, hidden_size=4096, intermediate_size=11008, num_hidden_layers=32, num_attention_heads=32, hidden_act='silu', max_position_embeddings=4096, model_max_length=4096, initializer_range=0.02, rms_norm_eps=1e-06, use_cache=True, pad_token_id=0, bos_token_id=1, eos_token_id=2, tie_word_embeddings=False, **kwargs)
¶
Constructs BaiChuanConfig.
Source code in mindnlp/transformers/models/baichuan/configuration_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanPreTrainedModel
¶
Bases: PreTrainedModel
BaiChuanPreTrainedModel
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan7bModel
¶
Bases: BaiChuanPreTrainedModel
Transformer decoder consisting of config.num_hidden_layers layers. Each layer is a [DecoderLayer
]
Args:
config: BaiChuanConfig
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan7bModel.__init__(config)
¶
Initializes a new instance of the BaiChuan7bModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuan7bModel class.
|
config |
An instance of BaiChuanConfig containing configuration parameters.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan7bModel.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, return_dict=None)
¶
This method forwards the BaiChuan7bModel by processing the input data and generating model outputs.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class BaiChuan7bModel.
TYPE:
|
input_ids |
The input tensor containing token indices representing the input sequence. Default is None.
TYPE:
|
attention_mask |
Optional tensor specifying the attention mask for the input sequence. Default is None.
TYPE:
|
position_ids |
Optional tensor specifying the position indices for the input sequence. Default is None.
TYPE:
|
past_key_values |
Optional list of tensors containing past key values for the model. Default is None.
TYPE:
|
inputs_embeds |
Optional tensor containing the embeddings of the input tokens. Default is None.
TYPE:
|
use_cache |
Optional boolean flag indicating whether to use cache during model computation. Default is None.
TYPE:
|
output_attentions |
Optional boolean flag indicating whether to output attentions. Default is None.
TYPE:
|
output_hidden_states |
Optional boolean flag indicating whether to output hidden states. Default is None.
TYPE:
|
return_dict |
Optional boolean flag indicating whether to return the output as a dictionary. Default is None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPast]
|
Union[Tuple, BaseModelOutputWithPast]: Returns a tuple or BaseModelOutputWithPast object containing the model outputs. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
Raised if both input_ids and inputs_embeds are specified simultaneously, if neither decoder_input_ids nor decoder_inputs_embeds are specified, or if an invalid configuration is encountered during model forwardion. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan7bModel.get_input_embeddings()
¶
Retrieves the input embeddings for the BaiChuan7bModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of BaiChuan7bModel.
|
RETURNS | DESCRIPTION |
---|---|
None. |
This method retrieves the input embeddings for the BaiChuan7bModel. The input embeddings are obtained by calling the 'embed_tokens' method of the instance.
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan7bModel.set_input_embeddings(new_embeddings)
¶
Sets the input embeddings for the BaiChuan7bModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuan7bModel class.
TYPE:
|
new_embeddings |
The new embeddings to be set. This can be of any type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel
¶
Bases: BaiChuanPreTrainedModel
This class represents a BaiChuan13b model for natural language processing tasks. It is a subclass of the BaiChuanPreTrainedModel class. The BaiChuan13bModel class contains methods for initializing the model, getting and setting input embeddings, generating an alibi mask, and forwarding the model.
ATTRIBUTE | DESCRIPTION |
---|---|
padding_idx |
The index used for padding tokens in the embedding layer.
TYPE:
|
vocab_size |
The size of the vocabulary.
TYPE:
|
n_head |
The number of attention heads.
TYPE:
|
embed_tokens |
The embedding layer for input tokens.
TYPE:
|
layers |
A list of BaiChuanLayer instances representing the layers of the model.
TYPE:
|
norm |
The normalization layer applied after the model layers.
TYPE:
|
max_cache_pos |
The maximum position of past key values for caching.
TYPE:
|
first_run |
A flag indicating if it is the first run of the model.
TYPE:
|
alibi_mask |
A tensor representing the alibi mask.
TYPE:
|
METHOD | DESCRIPTION |
---|---|
__init__ |
BaiChuanConfig): Initializes the BaiChuan13bModel instance with a configuration. |
get_input_embeddings |
Returns the input embeddings of the model. |
set_input_embeddings |
Sets the input embeddings of the model. |
get_alibi_mask |
Generates an alibi mask based on the tensor and sequence length. |
forward |
forwards the model with the given inputs and returns the model output. |
Note
- The BaiChuan13bModel class is designed to be used for natural language processing tasks, such as text classification or language generation.
- The model architecture follows the BaiChuan13b configuration, which includes embedding layers, multiple layers of BaiChuanLayer, and normalization layers.
- The alibi mask is used for attention calculations and is generated based on the input tensor and sequence length.
- The forward method is the main entry point for using the model, which takes various inputs and returns the model output.
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel.__init__(config)
¶
init
This method initializes an instance of the BaiChuan13bModel class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuan13bModel class.
|
config |
An object of type BaiChuanConfig containing configuration parameters for the model. It specifies the configuration parameters such as pad_token_id, vocab_size, num_attention_heads, hidden_size, num_hidden_layers, rms_norm_eps, and model_max_length.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel.forward(input_ids=None, attention_mask=None, past_key_values=None, inputs_embeds=None, use_cache=False, output_attentions=False, output_hidden_states=False, return_dict=True)
¶
forwards the BaiChuan13bModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The object instance.
|
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:
|
past_key_values |
The list of past key value tensors.
TYPE:
|
inputs_embeds |
The input embeddings tensor of shape [batch_size, sequence_length, hidden_size].
TYPE:
|
use_cache |
Whether to use cache for decoding.
TYPE:
|
output_attentions |
Whether to output attention weights.
TYPE:
|
output_hidden_states |
Whether to output hidden states.
TYPE:
|
return_dict |
Whether to return a dictionary instead of a tuple.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, BaseModelOutputWithPast]
|
Union[Tuple, BaseModelOutputWithPast]: The output tuple or BaseModelOutputWithPast object containing the last hidden state, past key values, hidden states, and attentions. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If both input_ids and inputs_embeds are provided simultaneously. |
ValueError
|
If neither input_ids nor inputs_embeds are provided. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel.get_alibi_mask(tensor, seq_length_with_past)
¶
This method is a member of the 'BaiChuan13bModel' class and is used to obtain an alibi mask based on the input tensor and sequence length with past information.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
tensor |
The input tensor used to derive the alibi mask.
TYPE:
|
seq_length_with_past |
The length of the sequence with past information.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the 'seq_length_with_past' parameter is not an integer. |
RuntimeError
|
If the method encounters issues during execution. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel.get_input_embeddings()
¶
This method returns the input embeddings for the BaiChuan13bModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuan13bModel class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the input embeddings for the BaiChuan13bModel as an instance of 'embed_tokens'. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuan13bModel.set_input_embeddings(value)
¶
Method to set the input embeddings for the BaiChuan13bModel.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuan13bModel class.
TYPE:
|
value |
The input embeddings to be set for the model.
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM
¶
Bases: BaiChuanPreTrainedModel
BaiChuanForCausalLM
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.__init__(config, size=None)
¶
Initializes a new instance of BaiChuanForCausalLM.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
config |
The configuration for the model.
|
size |
The size of the model. Defaults to None. Must be either '7b' or '13b'.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the size parameter is not '7b' or '13b', a ValueError is raised with the message 'BaiChuan model only supports 7b and 13b, please check your config.' |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.chat(tokenizer, messages, stream=False, generation_config=None)
¶
Method
chat
Description
This method allows for conducting a chat conversation using the BaiChuanForCausalLM model. It takes in the necessary input parameters and returns the response generated by the model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuanForCausalLM class.
|
tokenizer |
An object of the tokenizer class used for tokenizing the input messages.
|
messages |
A list of dictionaries representing the chat messages. Each dictionary contains the following keys:
TYPE:
|
stream |
A boolean value indicating whether the chat conversation should be streamed or not. Default is False.
DEFAULT:
|
generation_config |
An optional object of the GenerationConfig class that specifies the generation configurations. If not provided, the instance's generation_config will be used.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.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)
¶
forwards the Causal Language Model for the BaiChuan model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuanForCausalLM class.
TYPE:
|
input_ids |
The input tensor containing the token IDs. Default: None.
TYPE:
|
attention_mask |
The attention mask tensor. Default: None.
TYPE:
|
position_ids |
The position IDs tensor. Default: None.
TYPE:
|
past_key_values |
The list of past key values tensor. Default: None.
TYPE:
|
inputs_embeds |
The input embeddings tensor. Default: None.
TYPE:
|
labels |
The tensor containing the labels. Default: None.
TYPE:
|
use_cache |
Whether to use cache. Default: None.
TYPE:
|
output_attentions |
Whether to output attentions. Default: None.
TYPE:
|
output_hidden_states |
Whether to output hidden states. Default: None.
TYPE:
|
return_dict |
Whether to return a dictionary. Default: None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Union[Tuple, CausalLMOutputWithPast]
|
Union[Tuple, CausalLMOutputWithPast]: The model outputs.
|
RAISES | DESCRIPTION |
---|---|
ValueError
|
If the BaiChuan model is not of type BaiChuan7bModel or BaiChuan13bModel. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.get_decoder()
¶
get_decoder
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1737 1738 1739 1740 1741 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.get_input_embeddings()
¶
Method to retrieve the input embeddings from the model for the BaiChuanForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuanForCausalLM class. It is used to access the model's embed_tokens.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method returns the input embeddings from the model to be used in the BaiChuanForCausalLM class. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.get_output_embeddings()
¶
This method retrieves the output embeddings from the BaiChuanForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the BaiChuanForCausalLM class.
|
RETURNS | DESCRIPTION |
---|---|
lm_head
|
The method returns the lm_head attribute which contains the output embeddings. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.prepare_inputs_for_generation(input_ids, past_key_values=None, attention_mask=None, inputs_embeds=None, **kwargs)
¶
This method prepares inputs for generation in the BaiChuanForCausalLM class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
TYPE:
|
input_ids |
The input token IDs. Shape (batch_size, sequence_length).
TYPE:
|
past_key_values |
Tuple of tensors containing cached key and value projection states of the model. Default is None.
TYPE:
|
attention_mask |
Mask to avoid performing attention on padding token indices. Shape (batch_size, sequence_length).
TYPE:
|
inputs_embeds |
The embedded representation of the input tokens. Shape (batch_size, sequence_length, hidden_size).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
dict
|
A dictionary containing model inputs for generation, including 'input_ids', 'position_ids', 'past_key_values', 'use_cache', and 'attention_mask'. |
RAISES | DESCRIPTION |
---|---|
ValueError
|
If attention_mask and position_ids are both provided and have mismatched shapes. |
ValueError
|
If inputs_embeds and past_key_values are both provided. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.set_decoder(decoder)
¶
set_decoder
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1731 1732 1733 1734 1735 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.set_input_embeddings(new_embeddings)
¶
Set the input embeddings for the BaiChuanForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuanForCausalLM class.
TYPE:
|
new_embeddings |
The new input embeddings to be set for the model. Should be of shape (vocab_size, embedding_dim).
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the new_embeddings parameter is not of type torch.Tensor. |
ValueError
|
If the shape of new_embeddings does not match the expected shape (vocab_size, embedding_dim). |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 |
|
mindnlp.transformers.models.baichuan.modeling_baichuan.BaiChuanForCausalLM.set_output_embeddings(new_embeddings)
¶
Set the output embeddings for BaiChuanForCausalLM model.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of BaiChuanForCausalLM class.
TYPE:
|
new_embeddings |
The new embeddings to be set as the output embeddings for the model. This can be of any type.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
Source code in mindnlp/transformers/models/baichuan/modeling_baichuan.py
1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer
¶
Bases: PreTrainedTokenizer
Construct a BaiChuan tokenizer. Based on byte-level Byte-Pair-Encoding.
PARAMETER | DESCRIPTION |
---|---|
vocab_file |
Path to the vocabulary file.
TYPE:
|
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.vocab_size
property
¶
Returns vocab size
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.__getstate__()
¶
Return a dictionary representing the state of the BaiChuanTokenizer instance.
This method takes no additional parameters.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the BaiChuanTokenizer class.
|
RETURNS | DESCRIPTION |
---|---|
None
|
This method does not return any value. It modifies the state of the instance in-place. |
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.__init__(vocab_file, unk_token='<unk>', bos_token='<s>', eos_token='</s>', pad_token=None, sp_model_kwargs=None, add_bos_token=True, add_eos_token=False, clean_up_tokenization_spaces=False, **kwargs)
¶
init
Initializes a new instance of the BaiChuanTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
The instance of the class.
|
vocab_file |
The path to the vocabulary file.
TYPE:
|
unk_token |
The unknown token. Defaults to '
TYPE:
|
bos_token |
The beginning of sentence token. Defaults to '
TYPE:
|
eos_token |
The end of sentence token. Defaults to ''.
TYPE:
|
pad_token |
The padding token. Defaults to None.
TYPE:
|
sp_model_kwargs |
Optional arguments for SentencePieceProcessor. Defaults to None.
TYPE:
|
add_bos_token |
Whether to add the beginning of sentence token. Defaults to True.
TYPE:
|
add_eos_token |
Whether to add the end of sentence token. Defaults to False.
TYPE:
|
clean_up_tokenization_spaces |
Whether to clean up tokenization spaces. Defaults to False.
TYPE:
|
**kwargs |
Additional keyword arguments.
DEFAULT:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If vocab_file is not a string. |
TypeError
|
If unk_token is not a string. |
TypeError
|
If bos_token is not a string. |
TypeError
|
If eos_token is not a string. |
TypeError
|
If pad_token is not a string. |
TypeError
|
If sp_model_kwargs is not a dictionary or None. |
TypeError
|
If add_bos_token is not a boolean. |
TypeError
|
If add_eos_token is not a boolean. |
TypeError
|
If clean_up_tokenization_spaces is not a boolean. |
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
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.baichuan.tokenization_baichuan.BaiChuanTokenizer.__setstate__(d)
¶
setstate method in the BaiChuanTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the BaiChuanTokenizer class. Represents the current object that the method is called on.
TYPE:
|
d |
A dictionary containing the state information to be set. This dictionary is used to update the internal state of the object.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
None. |
RAISES | DESCRIPTION |
---|---|
TypeError
|
If the input parameters are not of the expected types. |
ValueError
|
If there is an issue with the values passed as parameters. |
AttributeError
|
If there are issues related to attribute access or assignment. |
RuntimeError
|
If there is a runtime issue during the method execution. |
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.build_inputs_with_special_tokens(token_ids_0, token_ids_1=None)
¶
Builds input sequences with special tokens for the BaiChuanTokenizer class.
PARAMETER | DESCRIPTION |
---|---|
self |
An instance of the BaiChuanTokenizer class.
TYPE:
|
token_ids_0 |
A list of token IDs representing the first input sequence.
TYPE:
|
token_ids_1 |
A list of token IDs representing the second input sequence. Defaults to None.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]: A list of token IDs representing the input sequences with special tokens added. |
This method takes in two input sequences token_ids_0 and token_ids_1 (optional) and builds input sequences with special tokens. If the add_bos_token parameter is True, the method adds the beginning of sequence token (bos_token) at the beginning of the sequences. If the add_eos_token parameter is True, the method adds the end of sequence token (eos_token) at the end of the sequences. The method then concatenates the special tokens with the input sequences and returns the result.
If a second input sequence (token_ids_1) is provided, the method also adds special tokens to it and concatenates it with the first input sequence.
Note
The bos_token_id and eos_token_id are specific token IDs for the beginning and end of sequence tokens, respectively, as defined in the BaiChuanTokenizer class.
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.convert_tokens_to_string(tokens)
¶
Converts a sequence of tokens (string) in a single string.
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.create_token_type_ids_from_sequences(token_ids_0, token_ids_1=None)
¶
Creates a mask from the two sequences passed to be used in a sequence-pair classification task. An ALBERT sequence pair mask has the following format:
0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1
| first sequence | second sequence |
if token_ids_1 is None, only returns the first portion of the mask (0s).
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of ids.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.get_special_tokens_mask(token_ids_0, token_ids_1=None, already_has_special_tokens=False)
¶
Retrieve sequence ids from a token list that has no special tokens added. This method is called when adding
special tokens using the tokenizer prepare_for_model
method.
PARAMETER | DESCRIPTION |
---|---|
token_ids_0 |
List of IDs.
TYPE:
|
token_ids_1 |
Optional second list of IDs for sequence pairs.
TYPE:
|
already_has_special_tokens |
Whether or not the token list is already formatted with special tokens for the model.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
List[int]
|
|
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
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 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.get_vocab()
¶
Returns vocab as a dict
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
169 170 171 172 173 |
|
mindnlp.transformers.models.baichuan.tokenization_baichuan.BaiChuanTokenizer.save_vocabulary(save_directory, filename_prefix=None)
¶
Save the vocabulary and special tokens file to a directory.
PARAMETER | DESCRIPTION |
---|---|
save_directory |
The directory in which to save the vocabulary.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Tuple[str]
|
|
Source code in mindnlp/transformers/models/baichuan/tokenization_baichuan.py
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 |
|