lion-pytorch
repository·main·Indexed 24 days ago
https://github.com/lucidrains/lion-pytorchA PyTorch implementation of the Lion (EvoLved SigMomentum) optimizer, designed for high performance and efficiency in large-scale language modeling and text-to-image training. It features support for Triton fused kernels, decoupled weight decay, and cautious update mechanisms. The package provides a standard PyTorch optimizer API and includes guidelines for tuning learning rates and weight decay when migrating from AdamW.
What's inside lion-pytorch
- While the authors use the same schedules for Lion as AdamW, they observed that a cosine decay schedule provides larger gains when training Vision Transformers (ViT) compared to a reciprocal square-root schedule.
Configure Lion learning rate and weight decay
mainWhen switching from AdamW to Lion, follow these tuning guidelines based on the original paper:
- Learning Rate (lr): A suitable learning rate for Lion is typically 3-10x smaller than that for AdamW. You should change the initial, peak, and end values in your learning rate schedule simultaneously using the same ratio.
- Weight Decay ($\lambda$): Because the effective weight decay is
lr * λ, the decoupled weight decay value used for Lion should be 3-10x larger than that used for AdamW to maintain similar strength. - $\beta_1$ and $\beta_2$: The default values are
$\beta_1=0.9$and$\beta_2=0.99$. If you encounter instability, try using$\beta_1=0.95, \beta_2=0.98$(and potentially increasing $\epsilon$ to1e-6). - Batch Size: It is recommended to use Lion in settings with high batch sizes (64 or above).
Install lion-pytorch
mainYou can install the
lion-pytorchpackage using eitherpiporconda.$ pip install lion-pytorchAlternatively, using conda:
$ conda install lion-pytorchEnable Triton fused kernels for Lion
mainTo use a fused kernel for updating parameters via Triton, you must first install the Triton package:
$ pip install triton -U --preThen, set
use_triton=Truewhen instantiating theLionoptimizer.opt = Lion( model.parameters(), lr=1e-4, weight_decay=1e-2, use_triton=True # set this to True to use cuda kernel w/ Triton lang (Tillet et al) )Configure Cautious Updates and Weight Decay
mainThe
Lionimplementation includes advanced update mechanisms based on recent research:- Cautious Update: Controlled by
cautious_factor. When0 < cautious_factor < 1, the update is scaled based on whether the update direction aligns with the gradient direction. This follows Algorithm 2 in arXiv:2411.16085. - Cautious Weight Decay: Controlled by
cautious_wd. When enabled, weight decay is applied selectively based on the sign of the update and the parameter (masking), following arXiv:2510.12402. - Decoupled Weight Decay: Controlled by
decoupled_weight_decay. If set toTrue, the weight decay is scaled by the initial learning rate to decouple it from the current learning rate.
- Cautious Update: Controlled by
Use the Lion optimizer in PyTorch
mainTo use the Lion optimizer, import
Lionfromlion_pytorchand instantiate it with your model parameters. The usage pattern follows the standard PyTorch optimizer API:step()for updates andzero_grad()to clear gradients.import torch from torch import nn from lion_pytorch import Lion model = nn.Linear(10, 1) # Instantiate Lion opt = Lion(model.parameters(), lr=1e-4, weight_decay=1e-2) # Standard training loop steps loss = model(torch.randn(10)) loss.backward() opt.step() opt.zero_grad()# toy model import torch from torch import nn model = nn.Linear(10, 1) # import Lion and instantiate with parameters from lion_pytorch import Lion opt = Lion(model.parameters(), lr=1e-4, weight_decay=1e-2) # forward and backwards loss = model(torch.randn(10)) loss.backward() # optimizer step opt.step() opt.zero_grad()Perform an optimization step with Lion
mainUse the
.step()method to update model parameters based on the computed gradients. Like standard PyTorch optimizers, it supports an optionalclosurefor algorithms that require multiple evaluations of the loss function.# Standard usage optimizer.zero_grad() loss = criterion(output, target) loss.backward() optimizer.step()Import the Lion optimizer
mainTo use the Lion optimizer, import the
Lionclass from thelion_pytorchpackage.from lion_pytorch import LionLion constructor arguments
mainThe
Lionoptimizer accepts the following arguments in its__init__method:params: An iterable of the parameters to optimize.lr(float): The learning rate. Must be greater than 0.betas(Tuple[float, float]): A tuple of two beta coefficients for the exponential moving average of gradients. Must be between 0 and 1. Default is(0.9, 0.99).weight_decay(float): The weight decay coefficient. Default is0.0.decoupled_weight_decay(bool): If set toTrue, weight decay is applied in a decoupled manner (similar to AdamW). Default isFalse.
Instantiate the Lion optimizer
mainThe
Lionclass is a PyTorch optimizer implementation. You can initialize it with your model parameters and several hyperparameters to control the update behavior, including learning rate, momentum (betas), weight decay, and specific 'cautious' update modes.Key arguments:
params: The parameters to optimize.lr: Learning rate (default1e-4).betas: A tuple of(beta1, beta2)for the exponential moving averages (default(0.9, 0.99)).weight_decay: Weight decay coefficient (default0.0).cautious_factor: A factor between 0 and 1 used for cautious updates (default0.0).cautious_wd: Boolean indicating whether to use cautious weight decay (defaultFalse).decoupled_weight_decay: Boolean indicating whether to use decoupled weight decay (defaultFalse).
Use Triton-accelerated Lion update function
mainThe
update_fnprovides a Triton-accelerated implementation of the Lion optimizer update step. It is designed to perform the weight decay, momentum update, and sign-based weight update directly on CUDA tensors using a custom Triton kernel.Requirements:
triton>=2.2.0must be installed.- All input tensors (
p,grad,exp_avg) must be on a CUDA device. - Tensors must not be complex (Triton support for complex tensors is not available).
Parameters:
p(torch.Tensor): The parameter tensor to be updated.grad(torch.Tensor): The gradient tensor.exp_avg(torch.Tensor): The momentum running average (exponential moving average) tensor.lr(float): Learning rate.wd(float): Weight decay coefficient.beta1(float): First momentum coefficient.beta2(float): Second momentum coefficient.
Perform an optimizer step with Lion
mainUse the.step(closure=None)method to update the model parameters. If your loss function requires re-evaluation (e.g., for certain second-order methods or specific training loops), you can pass aclosurecallable. TheLionoptimizer handles the exponential moving average of gradients, cautious updates, and weight decay internally.