• Stars
    star
    1,918
  • Rank 23,380 (Top 0.5 %)
  • Language
    Python
  • License
    Apache License 2.0
  • Created over 1 year ago
  • Updated 2 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion.

LyCORIS - Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion.

image (This image is generated by the model trained in Hadamard product representation)

A project for implementing different algorithm to do parameter-efficient finetuning on stable diffusion or more.

This project is started from LoCon(see archive branch).

What we have now

See Algo.md or Demo.md for more example and explanation

Conventional LoRA

  • Include Conv layer implementation from LoCon
  • recommended settings
    • dim <= 64
    • alpha = 1 (or lower, like 0.3)

LoRA with Hadamard Product representation (LoHa)

WARNING: You are not supposed to use dim>64 in LoHa, which is over sqrt(original_dim) for almost all layer in SD

High dim with LoHa may cause unstable loss or just goes to NaN. If you want to use high dim LoHa, please use lower lr

WARNING-AGAIN: Use parameter-efficient algorithim in parameter-unefficient way is not a good idea

(IA)^3

LoKR

  • Basically same idea of LoHA, but use kronecker product
  • This algo is quite sensitive and you may need to tune the lr
  • This algo can learn both character and style, but since it is small (auto factor, full rank, 2.5MB), it is also hard to transfer.
  • This algo produce relatively small file(auto factor: 900~2500KB)
  • Use smaller factor will produce bigger file, you can tune it if you think 2.5MB full rank is not good enough.

DyLoRA

  • Ref DyLoRA: Parameter Efficient Tuning of Pre-trained Models using Dynamic Search-Free Low Rank Adaptation
  • Basically a training trick of lora.
  • Every step, only update one row/col of LoRA weight.
  • When we want to update k row/col, we only use 0~k row/col to rebuild the weight (0<=k<=dim)
  • You can easily resize DyLoRA to target and get similar or even better result than LoRA trained at target dim. (And you don't need to train lot loras with different dim to check which is better)
  • You should use large dim with alpha=dim/4~dim (1 or dim is not very recommended)
    • Example: dim=128, alpha=64
  • Since we only update 1 row/col each step, you will need more step to get reasonable result. If you want to train it with few steps, you may need to set block_size (update multiple row/col every step) to higer value (default=0)

usage

You can just use these training scripts.

For kohya script

Activate sd-scripts' venv and then install this package

source PATH_TO_SDSCRIPTS_VENV/Scripts/activate

or

PATH_TO_SDSCRIPTS_VENV\Scripts\Activate.ps1 # or .bat for cmd

And then you can install this package:

  • through pip
pip install lycoris_lora
  • from source
git clone https://github.com/KohakuBlueleaf/LyCORIS
cd LyCORIS
pip install .

Finally you can use this package's kohya module to run kohya's training script

python3 sd-scripts/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" \

to train lycoris module for SD model

  • algo list:

    • locon: Conventional Methods
    • loha: Hadamard product representation introduced by FedPara
    • lokr: Kronecker product representation
    • ia3 : (IA)^3
  • Tips:

    • Use network_dim=0 or conv_dim=0 to disable linear/conv layer
    • LoHa/LoKr/(IA)^3 doesn't support dropout yet.

For a1111's sd-webui

download Extension into sd-webui, and then use LyCORIS model in the extra netowrks tabs.

Not For Kohya-ss' Additional Network

Extract LoCon

You can extract LoCon from a dreambooth model with its base model.

python3 extract_locon.py <settings> <base_model> <db_model> <output>

Use --help to get more info

$ python3 extract_locon.py --help
usage: extract_locon.py [-h] [--is_v2] [--device DEVICE] [--mode MODE] [--safetensors] [--linear_dim LINEAR_DIM] [--conv_dim CONV_DIM]
                        [--linear_threshold LINEAR_THRESHOLD] [--conv_threshold CONV_THRESHOLD] [--linear_ratio LINEAR_RATIO] [--conv_ratio CONV_RATIO]
                        [--linear_percentile LINEAR_PERCENTILE] [--conv_percentile CONV_PERCENTILE]
                        base_model db_model output_name

Example and Comparing for different algo

see Demo.md and Algo.md

Change Log

For full log, please see Change.md

2023/06/28 update to 1.7.1

  • rearrange the version format, previous 0.1.7 should be 1.7.0
  • fix the bug in scale weight norm

2023/06/26 Update for 0.1.7

  • Add support for rank_dropout and module_dropout on LoCon/LoHa/LoKr
  • Add support for scale_weight_norms on LoCon/LoHa/LoKr
  • Will support SDXL on 0.1.8 (you can follow the dev branch)

Todo list

  • Module and Document for using LyCORIS in any other model, Not only SD.
  • Proposition3 in FedPara
    • also need custom backward to save the vram
  • Low rank + sparse representation
    • For extraction
    • For training
  • Support more operation, not only linear and conv2d.
  • Configure varying ranks or dimensions for specific modules as needed.
  • Automatically selecting an algorithm based on the specific rank requirement.
  • Explore other low-rank representations or parameter-efficient methods to fine-tune either the entire model or specific parts of it.
  • More experiments for different task, not only diffusion models.

Citation

@misc{LyCORIS,
  author       = "Shih-Ying Yeh (Kohaku-BlueLeaf), Yu-Guan Hsieh, Zhidong Gao",
  title        = "LyCORIS - Lora beYond Conventional methods, Other Rank adaptation Implementations for Stable diffusion",
  howpublished = "\url{https://github.com/KohakuBlueleaf/LyCORIS}",
  month        = "March",
  year         = "2023"
}

More Repositories

1

a1111-sd-webui-lycoris

An extension for stable-diffusion-webui to load lycoris models.
Python
826
star
2

a1111-sd-webui-locon

A extension for loading LyCORIS model in sd-webui
Python
497
star
3

a1111-sd-webui-haku-img

An Image utils extension for A1111's sd-webui
Python
182
star
4

HyperKohaku

A diffusers based implementation of HyperDreamBooth
Python
116
star
5

z-a1111-sd-webui-dtg

A sd-webui extension for utilizing DanTagGen to "upsample prompts".
Python
65
star
6

Kohaku-NAI

A Novel-AI client with more utilities built in it
Python
53
star
7

HakuBooru

text-image dataset maker for anime-style images
Python
44
star
8

PixelOE

Detail-Oriented Pixelization based on Contrast-Aware Outline Expansion.
Python
37
star
9

LoCon

LoRA for convolution layer
16
star
10

BlueNet

A simple python package for Neural Network based on numpy
Python
12
star
11

HakuIR

A utils for image restoration and upscale
Python
5
star
12

danbooru-tag-tree

A tag tree for getting a group of tags or checking what group a tag in
Python
5
star
13

PixelGen

A Pixel Art generator website
Vue
3
star
14

HakuPhi

a playground to place lot of things about phi. Maybe trainer/inference ui/optimizations/some addon archs...etc
Python
3
star
15

KohakuGomoku

A gomoku AI project which runs on cpu.
C++
2
star
16

HakuNovel

A webcrawler for download light novel from variant websites.
Python
1
star
17

Flask_Composer

A extension for flask to configure complex app easily
Python
1
star
18

flypool_cli

A command-line tool for monitoring the status on flypools(ether and raven).
Python
1
star
19

AoiDB

A simple database implementation in pure python
Python
1
star
20

PixelOE-matlab

A simple project for doing pixelization on Image with Matlab
MATLAB
1
star
21

NTHU-AI-Final-Project

NTHU Introduction of AI final Project: A course selection system based on user(student)'s preference on course content.
Jupyter Notebook
1
star
22

wd14-tagger-utils

A utils for doing transfer learning on wd14 tagger or other classifier model on hf using keras
Python
1
star
23

KGen

A system for Prompt generation to improve Text-to-Image performance.
Python
1
star
24

weekly

JavaScript
1
star