# Liger-Kernel

> Fused Triton kernels that speed up LLM training and cut memory use

- Category: Models & Inference / GPU Kernels & Compilers
- Language: Python
- Licence: BSD-2-Clause
- Repository: https://github.com/linkedin/Liger-Kernel
- Website: https://linkedin.github.io/Liger-Kernel
- Canonical page: https://ai-tldr.dev/tools/liger-kernel/

## In short

Liger-Kernel is Fused Triton kernels that speed up LLM training and cut memory use. It sits in the GPU Kernels & Compilers category of the Models & Inference tools and is open-source, written in Python and licensed BSD-2-Clause. To get started: install Liger Kernel.

## Overview

Liger Kernel is a collection of Triton kernels built specifically for training large language models. It ships Hugging Face-compatible versions of common layers such as RMSNorm, RoPE, SwiGLU, CrossEntropy, and FusedLinearCrossEntropy, fusing them so they run faster and use less GPU memory than the stock PyTorch implementations.

It is aimed at engineers and researchers who fine-tune or pre-train LLMs and are hitting throughput limits or out-of-memory errors. According to the project, it can increase multi-GPU training throughput by about 20% and reduce memory usage by around 60%, which leaves room for longer context lengths, larger batch sizes, and bigger vocabularies.

As an efficient-training tool, it works alongside the stack you already use: Flash Attention, PyTorch FSDP, and Microsoft DeepSpeed. You can adopt it with a one-line patch on top of Hugging Face Transformers rather than rewriting your training loop, and it also includes optimized post-training loss kernels (DPO, ORPO, SimPO, KTO, and more) that cut memory for alignment and distillation work.

## What it does

- Hugging Face-compatible Triton kernels for RMSNorm, RoPE, SwiGLU, CrossEntropy, and FusedLinearCrossEntropy
- One-line adoption via AutoLigerKernelForCausalLM, or per-architecture patch helpers like apply_liger_kernel_to_llama
- Reported ~20% higher multi-GPU training throughput and ~60% lower memory use
- Works with Flash Attention, PyTorch FSDP, and DeepSpeed out of the box
- Post-training loss kernels (DPO, ORPO, CPO, SimPO, KTO, JSD) with up to 80% memory savings
- Runs on NVIDIA (CUDA) and AMD (ROCm) GPUs

## Getting started

Install the package, then enable Liger's kernels with a single change to how you load your model.

1. Install Liger Kernel

   Install the stable release from PyPI. NVIDIA needs torch >= 2.1.2 and triton >= 2.3.0; AMD/ROCm needs torch >= 2.5.0 and triton >= 3.0.0.

```bash
pip install liger-kernel
```

2. Patch a model in one line

   Load any causal LM through the AutoModel wrapper to apply the matching Liger kernels automatically.

```python
from liger_kernel.transformers import AutoLigerKernelForCausalLM

model = AutoLigerKernelForCausalLM.from_pretrained("path/to/some/model")
```

3. Or patch a specific architecture

   If you load the model yourself, call the architecture-specific helper before instantiation.

```python
import transformers
from liger_kernel.transformers import apply_liger_kernel_to_llama

apply_liger_kernel_to_llama()

model = transformers.AutoModelForCausalLM.from_pretrained("path/to/llama/model")
```

## When to use it

- Fine-tuning a Hugging Face LLM (e.g. LLaMA 3-8B) that runs out of memory at longer context lengths
- Raising multi-GPU training throughput on FSDP or DeepSpeed without changing your training loop
- Running alignment or preference tuning (DPO, ORPO, SimPO, KTO) with lower memory using the fused post-training loss kernels
- Training with larger batch sizes or bigger vocabularies on the same hardware budget
