Unverified Commit 44c0e6b9 authored by InvincibleDude's avatar InvincibleDude Committed by GitHub
Browse files

Merge branch 'AUTOMATIC1111:master' into master

parents 3bc8ee99 602a1864
Loading
Loading
Loading
Loading
+16 −12
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@ class LoraUpDownModule:
    def __init__(self):
        self.up = None
        self.down = None
        self.alpha = None


def assign_lora_names_to_compvis_modules(sd_model):
@@ -92,6 +93,15 @@ def load_lora(name, filename):
            keys_failed_to_match.append(key_diffusers)
            continue

        lora_module = lora.modules.get(key, None)
        if lora_module is None:
            lora_module = LoraUpDownModule()
            lora.modules[key] = lora_module

        if lora_key == "alpha":
            lora_module.alpha = weight.item()
            continue

        if type(sd_module) == torch.nn.Linear:
            module = torch.nn.Linear(weight.shape[1], weight.shape[0], bias=False)
        elif type(sd_module) == torch.nn.Conv2d:
@@ -104,17 +114,12 @@ def load_lora(name, filename):

        module.to(device=devices.device, dtype=devices.dtype)

        lora_module = lora.modules.get(key, None)
        if lora_module is None:
            lora_module = LoraUpDownModule()
            lora.modules[key] = lora_module

        if lora_key == "lora_up.weight":
            lora_module.up = module
        elif lora_key == "lora_down.weight":
            lora_module.down = module
        else:
            assert False, f'Bad Lora layer name: {key_diffusers} - must end in lora_up.weight or lora_down.weight'
            assert False, f'Bad Lora layer name: {key_diffusers} - must end in lora_up.weight, lora_down.weight or alpha'

    if len(keys_failed_to_match) > 0:
        print(f"Failed to match keys when loading Lora {filename}: {keys_failed_to_match}")
@@ -161,7 +166,7 @@ def lora_forward(module, input, res):
    for lora in loaded_loras:
        module = lora.modules.get(lora_layer_name, None)
        if module is not None:
            res = res + module.up(module.down(input)) * lora.multiplier
            res = res + module.up(module.down(input)) * lora.multiplier * (module.alpha / module.up.weight.shape[1] if module.alpha else 1.0)

    return res

@@ -177,12 +182,12 @@ def lora_Conv2d_forward(self, input):
def list_available_loras():
    available_loras.clear()

    os.makedirs(lora_dir, exist_ok=True)
    os.makedirs(shared.cmd_opts.lora_dir, exist_ok=True)

    candidates = \
        glob.glob(os.path.join(lora_dir, '**/*.pt'), recursive=True) + \
        glob.glob(os.path.join(lora_dir, '**/*.safetensors'), recursive=True) + \
        glob.glob(os.path.join(lora_dir, '**/*.ckpt'), recursive=True)
        glob.glob(os.path.join(shared.cmd_opts.lora_dir, '**/*.pt'), recursive=True) + \
        glob.glob(os.path.join(shared.cmd_opts.lora_dir, '**/*.safetensors'), recursive=True) + \
        glob.glob(os.path.join(shared.cmd_opts.lora_dir, '**/*.ckpt'), recursive=True)

    for filename in sorted(candidates):
        if os.path.isdir(filename):
@@ -193,7 +198,6 @@ def list_available_loras():
        available_loras[name] = LoraOnDisk(name, filename)


lora_dir = os.path.join(shared.models_path, "Lora")
available_loras = {}
loaded_loras = []

+6 −0
Original line number Diff line number Diff line
import os
from modules import paths


def preload(parser):
    parser.add_argument("--lora-dir", type=str, help="Path to directory with Lora networks.", default=os.path.join(paths.models_path, 'Lora'))
+3 −2
Original line number Diff line number Diff line
import json
import os
import lora

@@ -26,10 +27,10 @@ class ExtraNetworksPageLora(ui_extra_networks.ExtraNetworksPage):
                "name": name,
                "filename": path,
                "preview": preview,
                "prompt": f"<lora:{name}:1.0>",
                "prompt": json.dumps(f"<lora:{name}:") + " + opts.extra_networks_default_multiplier + " + json.dumps(">"),
                "local_preview": path + ".png",
            }

    def allowed_directories_for_previews(self):
        return [lora.lora_dir]
        return [shared.cmd_opts.lora_dir]
+7 −1
Original line number Diff line number Diff line
@@ -8,7 +8,7 @@ from basicsr.utils.download_util import load_file_from_url
from tqdm import tqdm

from modules import modelloader, devices, script_callbacks, shared
from modules.shared import cmd_opts, opts
from modules.shared import cmd_opts, opts, state
from swinir_model_arch import SwinIR as net
from swinir_model_arch_v2 import Swin2SR as net2
from modules.upscaler import Upscaler, UpscalerData
@@ -145,7 +145,13 @@ def inference(img, model, tile, tile_overlap, window_size, scale):

    with tqdm(total=len(h_idx_list) * len(w_idx_list), desc="SwinIR tiles") as pbar:
        for h_idx in h_idx_list:
            if state.interrupted or state.skipped:
                break

            for w_idx in w_idx_list:
                if state.interrupted or state.skipped:
                    break
                
                in_patch = img[..., h_idx: h_idx + tile, w_idx: w_idx + tile]
                out_patch = model(in_patch)
                out_patch_mask = torch.ones_like(out_patch)
+2 −2
Original line number Diff line number Diff line
<div class='card' {preview_html} onclick='return cardClicked({tabname}, {prompt}, {allow_negative_prompt})'>
<div class='card' {preview_html} onclick={card_clicked}>
	<div class='actions'>
		<div class='additional'>
			<ul>
				<a href="#" title="replace preview image with currently selected in gallery" onclick='return saveCardPreview(event, {tabname}, {local_preview})'>replace preview</a>
				<a href="#" title="replace preview image with currently selected in gallery" onclick={save_card_preview}>replace preview</a>
			</ul>
		</div>
		<span class='name'>{name}</span>
Loading