LyCORIS Documentation

repository·main·Indexed 23 days ago

https://github.com/kohakublueleaf/lycoris

A project implementing parameter-efficient fine-tuning (PEFT) algorithms for Stable Diffusion, including LoCon, LoHa, LoKr, and (IA)^3. It provides tools for training via kohya-ss/sd-scripts, a standalone PyTorch wrapper for applying adapters, and utilities for extracting, merging, and converting weight adaptation modules.

Tokens
7.2K
Snippets
20
Records
49
Agent score
82%

What's inside LyCORIS

  1. How LoKr works using Kronecker Product

    main

    LoKr (LoRA for Kronecker product) decomposes weight updates using the Kronecker product ($\otimes$). The weight update is expressed as $\Delta W = W_1 \otimes (Wa_2 \cdot Wb_2)$, where $W_1$ acts as a weight scale for a standard LoRA-decomposed matrix $W_2$.

    Key Properties:

    • Multiplicative Rank: The rank of the resulting $\Delta W$ is multiplicative: $rank(\Delta W) \le min(a, b) \times r$, where $r$ is the LoRA rank.
    • Parameter Efficiency: It can reduce the number of parameters to the order of the square root of the matrix dimensions. For a matrix of size $m \times n$, if the factor is set to -1, the number of parameters is approximately $\sqrt{mn} + r \times (\sqrt{m} + \sqrt{n})$.
  2. Compare LoRA vs LoCon (Expanding to Convolution Layers)

    main

    LyCORIS allows for expanding LoRA-style fine-tuning from transformer layers to the whole model, including convolutional layers (LoCon).

    Key differences:

    • Standard LoRA: Typically targets transformer layers only.
    • LoCon: Expands the adaptation to convolutional layers, allowing the model to learn more complex features. While fine-tuning the whole model can capture more information, it also carries a higher risk of heavy overfitting.

    For detailed algorithm comparison guidelines, refer to the Guidelines in this repository.

  3. How LoHa works and its memory optimization

    main

    LoHa (LoRA for Hadamard product) uses the Hadamard product ($\odot$) to decompose weight updates: $\Delta W = B \odot A$. This allows the rank of $\Delta W$ to be up to $rank(B) \times rank(A)$.

    Memory Optimization: Because computing the gradient for $\Delta W = (B_1 \cdot B_2) \odot (A_1 \cdot A_2)$ normally requires PyTorch to cache both $B$ and $A$ (doubling memory usage), LyCORIS implements a custom backward pass. This custom backward reconstructs $B$ and $A$ only when needed during the backward pass, significantly reducing memory consumption during training.

  4. Compare LoHa vs LoCon (Hadamard Product vs Conventional)

    main

    LyCORIS introduces LoHa (Hadamard Product representation) as an alternative to conventional LoRA/LoCon methods.

    When to use LoHa:

    • Better Style Composition: LoHa can combine styles and characters more effectively than LoRA or LoCon, especially when characters are trained on specific datasets (e.g., anime images) and you want to apply them to different styles.
    • Efficiency at Scale: For the same file size, LoHa can achieve higher effective ranks. For example, using (linear_dim, conv_dim) of (8, 4) for LoHa results in an effective rank of 8x8=64 for linear layers and 4x4=16 for convolutional layers, whereas LoCon with (16, 8) would have lower effective rank for the same storage footprint.

    Summary of findings from community experiments:

    • LoHa often yields better results in specific situations when using the same file size and training parameters as LoCon, provided the dimensions/ranks are configured appropriately (typically dim > 2 or rank > 4).
  5. How LoCon works with Tucker decomposition

    main

    LoCon (LoRA for Convolution) extends LoRA to convolutional layers. While standard LoRA for convolution can be implemented as a sequence of a $1\times1$ convolution and a standard convolution, LoCon supports a more efficient method called Tucker decomposition when use_tucker=True is enabled.

    In Tucker decomposition, the weight shape $[out, in, kw, kh]$ is decomposed into multiple layers:

    1. A $1\times1$ convolution: Conv(in, dim, (1, 1))
    2. A kernel-sized convolution: Conv(dim, dim, (kw, kh), stride, padding)
    3. A final $1\times1$ convolution: Conv(dim, out, (1, 1))

    This allows for more efficient parameter usage compared to simple unfolding.

    use_tucker=True
  6. Train LyCORIS using kohya-ss/sd-scripts

    main

    You can train LyCORIS modules using the lycoris.kohya module within the kohya-ss/sd-scripts framework.

    Using command line arguments: Pass the --network_module lycoris.kohya flag along with necessary dimensions and algorithm arguments.

    Using TOML configuration files: Provide a config file and a dataset config file to the training script.

    Example command line usage:

    accelerate launch train_network.py \
      --network_module lycoris.kohya \
      --network_dim "DIM_FOR_LINEAR" --network_alpha "ALPHA_FOR_LINEAR"\
      --network_args "conv_dim=DIM_FOR_CONV" "conv_alpha=ALPHA_FOR_CONV" \
      "dropout=DROPOUT_RATE" "algo=locon" \
    accelerate launch train_network.py \
      --network_module lycoris.kohya \
      --network_dim "DIM_FOR_LINEAR" --network_alpha "ALPHA_FOR_LINEAR"\
      --network_args "conv_dim=DIM_FOR_CONV" "conv_alpha=ALPHA_FOR_CONV" \
      "dropout=DROPOUT_RATE" "algo=locon" \
  7. Install LyCORIS via pip or source

    main

    To use LyCORIS for training or as a standalone wrapper, install the package in your corresponding virtual environment using one of the following methods:

    Via pip:

    pip install lycoris-lora

    From source:

    git clone https://github.com/KohakuBlueleaf/LyCORIS
    cd LyCORIS
    pip install .
    pip install lycoris-lora
  8. Use LyCORIS presets for training

    main

    LyCORIS provides several presets to quickly configure which layers are trained in the UNet and CLIP. These are useful for common training scenarios:

    • preset=full: The default preset. Trains all layers in the UNet and CLIP.
    • preset=full-lin: Similar to full, but skips convolutional layers.
    • preset=attn-mlp: The "kohya preset". Trains all transformer blocks.
    • preset=attn-only: Only attention layers are trained.
    • preset=unet-transformer-only: Trains only the UNet transformer blocks (similar to kohya_ss/sd_scripts with disabled Text Encoder).
    • preset=unet-convblock-only: Only ResBlock, UpSample, and DownSample layers are trained.
  9. Convert between bundle and non-bundle formats using batch_bundle_convert.py

    main

    Use batch_bundle_convert.py to "zip" (bundle) or "unzip" (separate) network checkpoints and their corresponding embeddings.

    Conversion Directions

    • To Bundle (--to_bundle): Groups network checkpoints and embeddings together based on matching step counts (format: {name}-{step_count}.{ext}). The resulting file is named {network_name}-bundle-{step_count}.{network_ext}.
    • From Bundle (--from_bundle): Separates a single bundle file into one network checkpoint and multiple embedding files, appending the step count to the embedding filenames.
    # Example: Bundle network checkpoints and embeddings
    python batch_bundle_convert.py \
    --network_path /path/to/sd-webui-ssd/models/Lora  \
    --emb_path /path/to/ckpts \
    --dst_dir /path/to/sd-webui-ssd/models/Lora/bundle \
    --to_bundle --verbose 2