LyCORIS - Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion. (ICLR'24)
A project that implements different parameter-efficient fine-tuning algorithms for Stable Diffusion.
This project originated from LoCon (see archive branch).
If you are interested in discussing more details, you can join our Discord server
If you want to check more in-depth experiment results and discussions for LyCORIS, you can check our paper
Documentation starts at docs/README.md — it maps every
document and source folder to the question it answers.
Algorithm Overview
LyCORIS currently contains LoRA (LoCon), LoHa, LoKr, (IA)^3, DyLoRA, Native fine-tuning (aka dreambooth).
GLoRA and GLoKr are coming soon.
Please check List of Implemented Algorithms and Guidelines for more details.
A simple comparison of some of these methods are provided below (to be taken with a grain of salt)
Full
LoRA
LoHa
LoKr low factor
LoKr high factor $^+$
Fidelity
★
●
▲
◉
▲
Flexibility $^*$
★
●
◉
▲
● $^†$
Diversity
▲
◉
★
●
★
Size
▲
●
●
●
★
Training Speed Linear
★
●
●
★
★
Training Speed Conv
●
★
▲
●
●
★ > ◉ > ● > ▲
[> means better and smaller size is better]
$^+$ Usually we take factor <= 0.5 * sqrt(dim) as low factor and factor >= sqrt(dim as high factor. For example, factor<=8 for SD1.x/SD2.x/SDXL can be seen as low factor, and, factor>=16 can be seen as high factor.
$^*$ Flexibility means anything related to generating images not similar to those in the training set, and combination of multiple concepts, whether they are trained together or not
$^†$ It may become more difficult to switch base model or combine multiple concepts in this situation
The actual performance may vary depending on the datasets, tasks, and hyperparameters used. It is recommended to experiment with different settings to achieve optimal results.
After sd-webui 1.5.0, LyCORIS models are officially supported by the built-in LoRA system. You can put them in either models/Lora or models/LyCORIS and use the default syntax <lora:filename:multiplier> to trigger it.
When we add new model types, we will always make sure they can be used with the newest version of sd-webui.
As for sd-webui with version < 1.5.0 or sd-webui-forge, please check this extension.
Others
As far as we are aware, LyCORIS models are also supported in the following interfaces / online generation services (please help us complete the list!)
However, newer model types may not always be supported. If you encounter this issue, consider requesting the developers of the corresponding interface or website to include support for the new type.
Training
There are three different ways to train LyCORIS models.
With kohya-ss/sd-scripts (see a list of compatible graphical interfaces and colabs at the end of the section)
For your convenience, some example yaml files for HCP LyCORIS training are provided in example/training_configs/hcp.
For the moment being the outputs of HCP-Diffusion are not directly compatible with a1111/sdwebui.
You can perform conversion with tools/batch_hcp_convert.py.
Import create_lycoris and LycorisNetwork from lycoris library, put your preset to LycorisNetwork and then use create_lycoris to create LyCORIS module for your pytorch module.
For example:
from lycoris import create_lycoris, LycorisNetwork
LycorisNetwork.apply_preset(
{"target_name": [".*attn.*"]}
)
lycoris_net = create_lycoris(
your_model,
1.0,
linear_dim=16,
linear_alpha=2.0,
algo="lokr"
)
lycoris_net.apply_to()
# after apply_to(), your_model() will run with LyCORIS net
lycoris_param = lycoris_net.parameters()
forward_with_lyco = your_model(x)
You can also layer multiple wrappers on top of the same module. Each call to apply_to() stacks a new adapter after the previously attached ones, and invoking restore() on a wrapper removes only its contribution while keeping earlier wrappers active. See example/standalone_example.py for an end-to-end demonstration.
For a stacking-specific walkthrough (including selectively removing adapters), see python example/stacked_wrapper_demo.py --help.
You can check my HakuPhi project to see how I utilize LyCORIS to finetune the Phi-1.5 models.
Other method
After LyCORIS3.0.0, Parametrize API and Functional API have been added, which provide more different ways on utilizing LyCORIS library.
Check API reference for more information.
You can also take the test suites as a kind of examples.
Fused kernels (experimental)
Since 4.0.0 LyCORIS ships hand-written Triton and TileLang kernels for the hot
paths of every algorithm, and picks one per call —
triton > tilelang > torch.compile > eager — with an automatic fallback to
the stock PyTorch path for anything out of scope. Your code does not change:
the module, functional and wrapper APIs keep the same signatures.
pip install triton # or: pip install tilelang
On an RTX 4090 this is 1.4x–7.3x less device (kernel) time than eager and
1.2x–6.2x less than torch.compile on the fused paths, with lower peak
VRAM and error at or below the eager path — ΔW and its gradient are never
materialised. Full table, method and caveats in
docs/kernels/benchmarks.md.
This is an early experimental implementation. If a result looks wrong, pin
the stock path and tell us:
However, they are not guaranteed to be up-to-date. In particular, newer types may not be supported. Consider requesting the developers for support or simply use the original kohya script in this case.
Utilities
Extract LoCon
You can extract LoCon from a dreambooth model with its base model.
Note: The merge_to() method has an opt-in parameter, precise that uses more CPU memory to store an original snapshot of the modified weights, allowing us to restore the original weights exactly when calling restore(). This is useful when you want to merge multiple LyCORIS models sequentially to the same base model without accumulating numerical errors in a production environment. See this model merge example for context.
Conversion of LoRA, LyCORIS and full models between HCP and sd-webui format
This script allows you to use the LyCORIS models trained with HCP-Diffusion in sd-webui.
Fused kernels (early experimental) — hand-written Triton and TileLang
kernels for every algorithm: lora/locon, loha, lokr, oft, boft,
dora (shared by dora/doha/dokr), ia3, glora, dylora, full and
norm. Up to four kernels each — merge forward, merge backward, bypass
forward, bypass backward — with ΔW and its gradient never materialised.
Automatic backend selection per call — triton > tilelang >
torch.compile > eager — with a fallback for anything out of scope. Pin it
with LYCORIS_KERNEL_BACKEND, or per call with the new backend= argument
on the functional API.
Mixed dtype support across x and the module weights: fp16, bf16 or fp32
independently, 16-bit matmul with fp32 accumulation, gradients returned in
each leaf's own dtype.
weight_decompose and add_scaled in lycoris.functional.general.
Improvements
Documentation reorganised into a nested tree, indexed at docs/README.md.
Packaging moved to pyproject.toml; CI, nightly and release workflows added.
Todo list
Automatically selecting an algorithm based on the specific rank requirement.
More experiments for different task, not only diffusion models.
LoKr and LoHa have been proven to be useful for Large Language Model.
Explore other low-rank representations or parameter-efficient methods to fine-tune either the entire model or specific parts of it.
Documentation for whole library.
Citation
@inproceedings{
yeh2024navigating,
title={Navigating Text-To-Image Customization: From Ly{CORIS} Fine-Tuning to Model Evaluation},
author={SHIH-YING YEH and Yu-Guan Hsieh and Zhidong Gao and Bernard B W Yang and Giyeong Oh and Yanmin Gong},
booktitle={The Twelfth International Conference on Learning Representations},
year={2024},
url={https://openreview.net/forum?id=wfzXa8e783}
}
Works well with
Use together with lycoris-lora: extensions, drivers, servers and companion tools.
torchPythie’s Index: 89Tensors and Dynamic neural networks in Python with strong GPU acceleration59.1M downloads/mo
diffusersPythie’s Index: 89State-of-the-art diffusion in PyTorch.6.5M downloads/mo