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
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
519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 | |
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
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
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
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
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 | |
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
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 | |
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
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 | |