forked from AUTOMATIC1111/stable-diffusion-webui
-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
103 changed files
with
3,505 additions
and
1,045 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
model: | ||
base_learning_rate: 1.0e-04 | ||
target: ldm.models.diffusion.ddpm.LatentDiffusion | ||
params: | ||
linear_start: 0.00085 | ||
linear_end: 0.0120 | ||
num_timesteps_cond: 1 | ||
log_every_t: 200 | ||
timesteps: 1000 | ||
first_stage_key: "jpg" | ||
cond_stage_key: "txt" | ||
image_size: 64 | ||
channels: 4 | ||
cond_stage_trainable: false # Note: different from the one we trained before | ||
conditioning_key: crossattn | ||
monitor: val/loss_simple_ema | ||
scale_factor: 0.18215 | ||
use_ema: False | ||
|
||
scheduler_config: # 10000 warmup steps | ||
target: ldm.lr_scheduler.LambdaLinearScheduler | ||
params: | ||
warm_up_steps: [ 10000 ] | ||
cycle_lengths: [ 10000000000000 ] # incredibly large number to prevent corner cases | ||
f_start: [ 1.e-6 ] | ||
f_max: [ 1. ] | ||
f_min: [ 1. ] | ||
|
||
unet_config: | ||
target: ldm.modules.diffusionmodules.openaimodel.UNetModel | ||
params: | ||
image_size: 32 # unused | ||
in_channels: 4 | ||
out_channels: 4 | ||
model_channels: 320 | ||
attention_resolutions: [ 4, 2, 1 ] | ||
num_res_blocks: 2 | ||
channel_mult: [ 1, 2, 4, 4 ] | ||
num_head_channels: 64 | ||
use_spatial_transformer: True | ||
use_linear_in_transformer: True | ||
transformer_depth: 1 | ||
context_dim: 1024 | ||
use_checkpoint: True | ||
legacy: False | ||
|
||
first_stage_config: | ||
target: ldm.models.autoencoder.AutoencoderKL | ||
params: | ||
embed_dim: 4 | ||
monitor: val/rec_loss | ||
ddconfig: | ||
double_z: true | ||
z_channels: 4 | ||
resolution: 256 | ||
in_channels: 3 | ||
out_ch: 3 | ||
ch: 128 | ||
ch_mult: | ||
- 1 | ||
- 2 | ||
- 4 | ||
- 4 | ||
num_res_blocks: 2 | ||
attn_resolutions: [] | ||
dropout: 0.0 | ||
lossconfig: | ||
target: torch.nn.Identity | ||
|
||
cond_stage_config: | ||
target: modules.xlmr_m18.BertSeriesModelWithTransformation | ||
params: | ||
name: "XLMR-Large" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import sys | ||
import copy | ||
import logging | ||
|
||
|
||
class ColoredFormatter(logging.Formatter): | ||
COLORS = { | ||
"DEBUG": "\033[0;36m", # CYAN | ||
"INFO": "\033[0;32m", # GREEN | ||
"WARNING": "\033[0;33m", # YELLOW | ||
"ERROR": "\033[0;31m", # RED | ||
"CRITICAL": "\033[0;37;41m", # WHITE ON RED | ||
"RESET": "\033[0m", # RESET COLOR | ||
} | ||
|
||
def format(self, record): | ||
colored_record = copy.copy(record) | ||
levelname = colored_record.levelname | ||
seq = self.COLORS.get(levelname, self.COLORS["RESET"]) | ||
colored_record.levelname = f"{seq}{levelname}{self.COLORS['RESET']}" | ||
return super().format(colored_record) | ||
|
||
|
||
logger = logging.getLogger("lora") | ||
logger.propagate = False | ||
|
||
|
||
if not logger.handlers: | ||
handler = logging.StreamHandler(sys.stdout) | ||
handler.setFormatter( | ||
ColoredFormatter("[%(name)s]-%(levelname)s: %(message)s") | ||
) | ||
logger.addHandler(handler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
import network | ||
|
||
class ModuleTypeGLora(network.ModuleType): | ||
def create_module(self, net: network.Network, weights: network.NetworkWeights): | ||
if all(x in weights.w for x in ["a1.weight", "a2.weight", "alpha", "b1.weight", "b2.weight"]): | ||
return NetworkModuleGLora(net, weights) | ||
|
||
return None | ||
|
||
# adapted from https://github.com/KohakuBlueleaf/LyCORIS | ||
class NetworkModuleGLora(network.NetworkModule): | ||
def __init__(self, net: network.Network, weights: network.NetworkWeights): | ||
super().__init__(net, weights) | ||
|
||
if hasattr(self.sd_module, 'weight'): | ||
self.shape = self.sd_module.weight.shape | ||
|
||
self.w1a = weights.w["a1.weight"] | ||
self.w1b = weights.w["b1.weight"] | ||
self.w2a = weights.w["a2.weight"] | ||
self.w2b = weights.w["b2.weight"] | ||
|
||
def calc_updown(self, orig_weight): | ||
w1a = self.w1a.to(orig_weight.device, dtype=orig_weight.dtype) | ||
w1b = self.w1b.to(orig_weight.device, dtype=orig_weight.dtype) | ||
w2a = self.w2a.to(orig_weight.device, dtype=orig_weight.dtype) | ||
w2b = self.w2b.to(orig_weight.device, dtype=orig_weight.dtype) | ||
|
||
output_shape = [w1a.size(0), w1b.size(1)] | ||
updown = ((w2b @ w1b) + ((orig_weight @ w2a) @ w1a)) | ||
|
||
return self.finalize_updown(updown, orig_weight, output_shape) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import torch | ||
import network | ||
from lyco_helpers import factorization | ||
from einops import rearrange | ||
|
||
|
||
class ModuleTypeOFT(network.ModuleType): | ||
def create_module(self, net: network.Network, weights: network.NetworkWeights): | ||
if all(x in weights.w for x in ["oft_blocks"]) or all(x in weights.w for x in ["oft_diag"]): | ||
return NetworkModuleOFT(net, weights) | ||
|
||
return None | ||
|
||
# Supports both kohya-ss' implementation of COFT https://github.com/kohya-ss/sd-scripts/blob/main/networks/oft.py | ||
# and KohakuBlueleaf's implementation of OFT/COFT https://github.com/KohakuBlueleaf/LyCORIS/blob/dev/lycoris/modules/diag_oft.py | ||
class NetworkModuleOFT(network.NetworkModule): | ||
def __init__(self, net: network.Network, weights: network.NetworkWeights): | ||
|
||
super().__init__(net, weights) | ||
|
||
self.lin_module = None | ||
self.org_module: list[torch.Module] = [self.sd_module] | ||
|
||
self.scale = 1.0 | ||
|
||
# kohya-ss | ||
if "oft_blocks" in weights.w.keys(): | ||
self.is_kohya = True | ||
self.oft_blocks = weights.w["oft_blocks"] # (num_blocks, block_size, block_size) | ||
self.alpha = weights.w["alpha"] # alpha is constraint | ||
self.dim = self.oft_blocks.shape[0] # lora dim | ||
# LyCORIS | ||
elif "oft_diag" in weights.w.keys(): | ||
self.is_kohya = False | ||
self.oft_blocks = weights.w["oft_diag"] | ||
# self.alpha is unused | ||
self.dim = self.oft_blocks.shape[1] # (num_blocks, block_size, block_size) | ||
|
||
is_linear = type(self.sd_module) in [torch.nn.Linear, torch.nn.modules.linear.NonDynamicallyQuantizableLinear] | ||
is_conv = type(self.sd_module) in [torch.nn.Conv2d] | ||
is_other_linear = type(self.sd_module) in [torch.nn.MultiheadAttention] # unsupported | ||
|
||
if is_linear: | ||
self.out_dim = self.sd_module.out_features | ||
elif is_conv: | ||
self.out_dim = self.sd_module.out_channels | ||
elif is_other_linear: | ||
self.out_dim = self.sd_module.embed_dim | ||
|
||
if self.is_kohya: | ||
self.constraint = self.alpha * self.out_dim | ||
self.num_blocks = self.dim | ||
self.block_size = self.out_dim // self.dim | ||
else: | ||
self.constraint = None | ||
self.block_size, self.num_blocks = factorization(self.out_dim, self.dim) | ||
|
||
def calc_updown(self, orig_weight): | ||
oft_blocks = self.oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype) | ||
eye = torch.eye(self.block_size, device=self.oft_blocks.device) | ||
|
||
if self.is_kohya: | ||
block_Q = oft_blocks - oft_blocks.transpose(1, 2) # ensure skew-symmetric orthogonal matrix | ||
norm_Q = torch.norm(block_Q.flatten()) | ||
new_norm_Q = torch.clamp(norm_Q, max=self.constraint) | ||
block_Q = block_Q * ((new_norm_Q + 1e-8) / (norm_Q + 1e-8)) | ||
oft_blocks = torch.matmul(eye + block_Q, (eye - block_Q).float().inverse()) | ||
|
||
R = oft_blocks.to(orig_weight.device, dtype=orig_weight.dtype) | ||
|
||
# This errors out for MultiheadAttention, might need to be handled up-stream | ||
merged_weight = rearrange(orig_weight, '(k n) ... -> k n ...', k=self.num_blocks, n=self.block_size) | ||
merged_weight = torch.einsum( | ||
'k n m, k n ... -> k m ...', | ||
R, | ||
merged_weight | ||
) | ||
merged_weight = rearrange(merged_weight, 'k m ... -> (k m) ...') | ||
|
||
updown = merged_weight.to(orig_weight.device, dtype=orig_weight.dtype) - orig_weight | ||
output_shape = orig_weight.shape | ||
return self.finalize_updown(updown, orig_weight, output_shape) |
Oops, something went wrong.