Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Efficient Triton Kernels for LLM Training

License

NotificationsYou must be signed in to change notification settings

linkedin/Liger-Kernel

Repository files navigation

Liger Kernel: Efficient Triton Kernels for LLM Training

StableNightlyDiscordBuild
Downloads (Stable)PyPI - VersionDownloads (Nightly)PyPI - VersionJoin Our Discord
Build
Build
Build

Installation |Getting Started |Examples |High-level APIs |Low-level APIs |Cite our work

Latest News 🔥

Liger Kernel is a collection of Triton kernels designed specifically for LLM training. It can effectively increase multi-GPUtraining throughput by 20% and reducesmemory usage by 60%. We have implementedHugging Face CompatibleRMSNorm,RoPE,SwiGLU,CrossEntropy,FusedLinearCrossEntropy, and more to come. The kernel works out of the box withFlash Attention,PyTorch FSDP, andMicrosoft DeepSpeed. We welcome contributions from the community to gather the best kernels for LLM training.

We've also added optimized Post-Training kernels that deliverup to 80% memory savings for alignment and distillation tasks. We support losses like DPO, CPO, ORPO, SimPO, KTO, JSD, and many more. Check outhow we optimize the memory.

Supercharge Your Model with Liger Kernel

Banner

With one line of code, Liger Kernel can increase throughput by more than 20% and reduce memory usage by 60%, thereby enabling longer context lengths, larger batch sizes, and massive vocabularies.

Speed UpMemory Reduction
Speed upMemory

Note:

  • Benchmark conditions: LLaMA 3-8B, Batch Size = 8, Data Type =bf16, Optimizer = AdamW, Gradient Checkpointing = True, Distributed Strategy = FSDP1 on 8 A100s.
  • Hugging Face models start to OOM at a 4K context length, whereas Hugging Face + Liger Kernel scales up to 16K.

Optimize Post Training with Liger Kernel

Post Training

We provide optimized post training kernels like DPO, ORPO, SimPO, and more which can reduce memory usage by up to 80%. You can easily use them as python modules.

fromliger_kernel.chunked_lossimportLigerFusedLinearORPOLossorpo_loss=LigerFusedLinearORPOLoss()y=orpo_loss(lm_head.weight,x,target)

Examples

Use CaseDescription
Hugging Face TrainerTrain LLaMA 3-8B ~20% faster with over 40% memory reduction on Alpaca dataset using 4 A100s with FSDP
Lightning TrainerIncrease 15% throughput and reduce memory usage by 40% with LLaMA3-8B on MMLU dataset using 8 A100s with DeepSpeed ZeRO3
Medusa Multi-head LLM (Retraining Phase)Reduce memory usage by 80% with 5 LM heads and improve throughput by 40% using 8 A100s with FSDP
Vision-Language Model SFTFinetune Qwen2-VL on image-text data using 4 A100s with FSDP
Liger ORPO TrainerAlign Llama 3.2 using Liger ORPO Trainer with FSDP with 50% memory reduction

Key Features

  • Ease of use: Simply patch your Hugging Face model with one line of code, or compose your own model using our Liger Kernel modules.
  • Time and memory efficient: In the same spirit as Flash-Attn, but for layers likeRMSNorm,RoPE,SwiGLU, andCrossEntropy! Increases multi-GPU training throughput by 20% and reduces memory usage by 60% withkernel fusion,in-place replacement, andchunking techniques.
  • Exact: Computation is exact—no approximations! Both forward and backward passes are implemented with rigorous unit tests and undergo convergence testing against training runs without Liger Kernel to ensure accuracy.
  • Lightweight: Liger Kernel has minimal dependencies, requiring only Torch and Triton—no extra libraries needed! Say goodbye to dependency headaches!
  • Multi-GPU supported: Compatible with multi-GPU setups (PyTorch FSDP, DeepSpeed, DDP, etc.).
  • Trainer Framework Integration:Axolotl,LLaMa-Factory,SFTTrainer,Hugging Face Trainer,SWIFT,oumi

Installation

Dependencies

CUDA

  • torch >= 2.1.2
  • triton >= 2.3.0

ROCm

  • torch >= 2.5.0 Install according to the instruction in Pytorch official webpage.
  • triton >= 3.0.0 Install from pypi. (e.g.pip install triton==3.0.0)
# Need to pass the url when installingpip install -e .[dev] --extra-index-url https://download.pytorch.org/whl/nightly/rocm6.2

Optional Dependencies

  • transformers >= 4.x: Required if you plan to use the transformers models patching APIs. The specific model you are working will dictate the minimum version of transformers.

Note:Our kernels inherit the full spectrum of hardware compatibility offered byTriton.

To install the stable version:

$ pip install liger-kernel

To install the nightly version:

$ pip install liger-kernel-nightly

To install from source:

git clone https://github.com/linkedin/Liger-Kernel.gitcd Liger-Kernel# Install Default Dependencies# Setup.py will detect whether you are using AMD or NVIDIApip install -e.# Setup Development Dependenciespip install -e".[dev]"

Getting Started

There are a couple of ways to apply Liger kernels, depending on the level of customization required.

1. Use AutoLigerKernelForCausalLM

Using theAutoLigerKernelForCausalLM is the simplest approach, as you don't have to import a model-specific patching API. If the model type is supported, the modeling code will be automatically patched using the default settings.

fromliger_kernel.transformersimportAutoLigerKernelForCausalLM# This AutoModel wrapper class automatically monkey-patches the# model with the optimized Liger kernels if the model is supported.model=AutoLigerKernelForCausalLM.from_pretrained("path/to/some/model")

2. Apply Model-Specific Patching APIs

Using thepatching APIs, you can swap Hugging Face models with optimized Liger Kernels.

importtransformersfromliger_kernel.transformersimportapply_liger_kernel_to_llama# 1a. Adding this line automatically monkey-patches the model with the optimized Liger kernelsapply_liger_kernel_to_llama()# 1b. You could alternatively specify exactly which kernels are appliedapply_liger_kernel_to_llama(rope=True,swiglu=True,cross_entropy=True,fused_linear_cross_entropy=False,rms_norm=False)# 2. Instantiate patched modelmodel=transformers.AutoModelForCausalLM("path/to/llama/model")

3. Compose Your Own Model

You can take individualkernels to compose your models.

fromliger_kernel.transformersimportLigerFusedLinearCrossEntropyLossimporttorch.nnasnnimporttorchmodel=nn.Linear(128,256).cuda()# fuses linear + cross entropy layers together and performs chunk-by-chunk computation to reduce memoryloss_fn=LigerFusedLinearCrossEntropyLoss()input=torch.randn(4,128,requires_grad=True,device="cuda")target=torch.randint(256, (4, ),device="cuda")loss=loss_fn(model.weight,input,target)loss.backward()

High-level APIs

AutoModel

AutoModel VariantAPI
AutoModelForCausalLMliger_kernel.transformers.AutoLigerKernelForCausalLM

Patching

ModelAPISupported Operations
LLaMA 2 & 3liger_kernel.transformers.apply_liger_kernel_to_llamaRoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
LLaMA 3.2-Visionliger_kernel.transformers.apply_liger_kernel_to_mllamaRoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Mistralliger_kernel.transformers.apply_liger_kernel_to_mistralRoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Mixtralliger_kernel.transformers.apply_liger_kernel_to_mixtralRoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Gemma1liger_kernel.transformers.apply_liger_kernel_to_gemmaRoPE, RMSNorm, GeGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Gemma2liger_kernel.transformers.apply_liger_kernel_to_gemma2RoPE, RMSNorm, GeGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Paligemma, Paligemma2, & Paligemma2 Mixliger_kernel.transformers.apply_liger_kernel_to_paligemmaLayerNorm, RoPE, RMSNorm, GeGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Qwen2, Qwen2.5, & QwQliger_kernel.transformers.apply_liger_kernel_to_qwen2RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Qwen2-VL, & QVQliger_kernel.transformers.apply_liger_kernel_to_qwen2_vlRMSNorm, LayerNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Qwen2.5-VLliger_kernel.transformers.apply_liger_kernel_to_qwen2_5_vlRMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Phi3 & Phi3.5liger_kernel.transformers.apply_liger_kernel_to_phi3RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy
Granite 3.0 & 3.1liger_kernel.transformers.apply_liger_kernel_to_graniteRoPE, RMSNorm, SwiGLU, CrossEntropyLoss
OLMo2liger_kernel.transformers.apply_liger_kernel_to_olmo2RoPE, RMSNorm, SwiGLU, CrossEntropyLoss, FusedLinearCrossEntropy

Low-level APIs

  • Fused Linear kernels combine linear layers with losses, reducing memory usage by up to 80% - ideal for HBM-constrained workloads.
  • Other kernels use fusion and in-place techniques for memory and performance optimization.

Model Kernels

KernelAPI
RMSNormliger_kernel.transformers.LigerRMSNorm
LayerNormliger_kernel.transformers.LigerLayerNorm
RoPEliger_kernel.transformers.liger_rotary_pos_emb
SwiGLUliger_kernel.transformers.LigerSwiGLUMLP
GeGLUliger_kernel.transformers.LigerGEGLUMLP
CrossEntropyliger_kernel.transformers.LigerCrossEntropyLoss
Fused Linear CrossEntropyliger_kernel.transformers.LigerFusedLinearCrossEntropyLoss

Alignment Kernels

KernelAPI
Fused Linear CPO Lossliger_kernel.chunked_loss.LigerFusedLinearCPOLoss
Fused Linear DPO Lossliger_kernel.chunked_loss.LigerFusedLinearDPOLoss
Fused Linear ORPO Lossliger_kernel.chunked_loss.LigerFusedLinearORPOLoss
Fused Linear SimPO Lossliger_kernel.chunked_loss.LigerFusedLinearSimPOLoss
Fused Linear KTO Lossliger_kernel.chunked_loss.LigerFusedLinearKTOLoss

Distillation Kernels

KernelAPI
KLDivergenceliger_kernel.transformers.LigerKLDIVLoss
JSDliger_kernel.transformers.LigerJSD
Fused Linear JSDliger_kernel.transformers.LigerFusedLinearJSD
TVDliger_kernel.transformers.LigerTVDLoss

Experimental Kernels

KernelAPI
Embeddingliger_kernel.transformers.experimental.LigerEmbedding
Matmul int2xint8liger_kernel.transformers.experimental.matmul

Contributing, Acknowledgements, and License

Sponsorship and Collaboration

  • Glows.ai: Sponsoring NVIDIA GPUs for our open source developers.
  • AMD: Providing AMD GPUs for our AMD CI.
  • Intel: Providing Intel GPUs for our Intel CI.
  • Modal: Free 3000 credits from GPU MODE IRL for our NVIDIA CI.
  • EmbeddedLLM: Making Liger Kernel run fast and stable on AMD.
  • HuggingFace: Integrating Liger Kernel into Hugging Face Transformers and TRL.
  • Lightning AI: Integrating Liger Kernel into Lightning Thunder.
  • Axolotl: Integrating Liger Kernel into Axolotl.
  • Llama-Factory: Integrating Liger Kernel into Llama-Factory.

Contact

Cite this work

Biblatex entry:

@article{hsu2024ligerkernelefficienttriton,title={Liger Kernel: Efficient Triton Kernels for LLM Training},author={Pin-Lun Hsu and Yun Dai and Vignesh Kothapalli and Qingquan Song and Shao Tang and Siyu Zhu and Steven Shimizu and Shivam Sahni and Haowen Ning and Yanning Chen},year={2024},eprint={2410.10989},archivePrefix={arXiv},primaryClass={cs.LG},url={https://arxiv.org/abs/2410.10989},journal={arXiv preprint arXiv:2410.10989},}

Star History

Star History Chart

↑ Back to Top ↑


[8]ページ先頭

©2009-2025 Movatter.jp