High-Level APIs¶
AutoModel¶
AutoModel Variant | API |
---|---|
AutoModelForCausalLM | liger_kernel.transformers.AutoLigerKernelForCausalLM |
This API extends the implementation of the AutoModelForCausalLM
within the transformers
library from Hugging Face.
liger_kernel.transformers.AutoLigerKernelForCausalLM ¶
Bases: AutoModelForCausalLM
This class is a drop-in replacement for AutoModelForCausalLM that applies the Liger Kernel to the model if applicable.
Source code in src/liger_kernel/transformers/auto_model.py
Try it Out
You can experiment as shown in this example here.
Patching¶
You can also use the Patching APIs to use the kernels for a specific model architecture.
Model | API | Supported Operations |
---|---|---|
LLaMA 2 & 3 | liger_kernel.transformers.apply_liger_kernel_to_llama |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
LLaMA 3.2-Vision | liger_kernel.transformers.apply_liger_kernel_to_mllama |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Mistral | liger_kernel.transformers.apply_liger_kernel_to_mistral |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Mixtral | liger_kernel.transformers.apply_liger_kernel_to_mixtral |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Gemma1 | liger_kernel.transformers.apply_liger_kernel_to_gemma |
RoPE, RMSNorm, GeGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Gemma2 | liger_kernel.transformers.apply_liger_kernel_to_gemma2 |
RoPE, RMSNorm, GeGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Qwen2, Qwen2.5, & QwQ | liger_kernel.transformers.apply_liger_kernel_to_qwen2 |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Qwen2-VL | liger_kernel.transformers.apply_liger_kernel_to_qwen2_vl |
RMSNorm, LayerNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Phi3 & Phi3.5 | liger_kernel.transformers.apply_liger_kernel_to_phi3 |
RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy |
Function Signatures¶
liger_kernel.transformers.apply_liger_kernel_to_llama ¶
apply_liger_kernel_to_llama(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Llama models (2 and 3)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_mllama ¶
apply_liger_kernel_to_mllama(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, layer_norm=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace MLlama models. NOTE: MLlama is not available in transformers<4.45.0
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_mistral ¶
apply_liger_kernel_to_mistral(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Mistral models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is False. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is True. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
liger_kernel.transformers.apply_liger_kernel_to_mixtral ¶
apply_liger_kernel_to_mixtral(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Mixtral models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_gemma ¶
apply_liger_kernel_to_gemma(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, geglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Gemma
(Gemma 1 and 1.1 supported, for Gemma2 please use apply_liger_kernel_to_gemma2
) to make GPU go burrr.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
geglu
|
bool
|
Whether to apply Liger's GeGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_gemma2 ¶
apply_liger_kernel_to_gemma2(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, geglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Gemma2
(for Gemma1 please use apply_liger_kernel_to_gemma
) to make GPU go burrr.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
geglu
|
bool
|
Whether to apply Liger's GeGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_qwen2 ¶
apply_liger_kernel_to_qwen2(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Qwen2 models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_qwen2_vl ¶
apply_liger_kernel_to_qwen2_vl(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, layer_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Qwen2-VL models. NOTE: Qwen2-VL is not supported in transformers<4.52.4
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
layer_norm
|
bool
|
Whether to apply Liger's LayerNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|
liger_kernel.transformers.apply_liger_kernel_to_phi3 ¶
apply_liger_kernel_to_phi3(rope=True, cross_entropy=False, fused_linear_cross_entropy=True, rms_norm=True, swiglu=True, model=None)
Apply Liger kernels to replace original implementation in HuggingFace Phi3 models.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
rope
|
bool
|
Whether to apply Liger's rotary position embedding. Default is True. |
True
|
cross_entropy
|
bool
|
Whether to apply Liger's cross entropy loss. Default is False. |
False
|
fused_linear_cross_entropy
|
bool
|
Whether to apply Liger's fused linear cross entropy loss. Default is True.
|
True
|
rms_norm
|
bool
|
Whether to apply Liger's RMSNorm. Default is True. |
True
|
swiglu
|
bool
|
Whether to apply Liger's SwiGLU Phi3MLP. Default is True. |
True
|
model
|
PreTrainedModel
|
The model instance to apply Liger kernels to, if the model has already been |
None
|
Source code in src/liger_kernel/transformers/monkey_patch.py
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 |
|