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
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 | |
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
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 | |
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
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 | |
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
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 | |
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
887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 | |
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
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 | |
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
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 | |
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
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 | |