AUTOMATIC1111 / stable-diffusion-webui

Stable Diffusion web UI
GNU Affero General Public License v3.0
139.6k stars 26.47k forks source link

[Bug]: SD3 Lora Trained with OneTrainer has ValueError in networks.py - but Working fine with ComfyUI #16317

Open a-l-e-x-d-s-9 opened 1 month ago

a-l-e-x-d-s-9 commented 1 month ago

Checklist

What happened?

Using SD3 Lora trained with OneTrainer causing the error:

loading network /home/alexds9/stable-diffusion-webui/models/Lora/My/SD3/Morph/Amber/working/ambrlux_style_amber_everything_sd3.safetensors: ValueError
Traceback (most recent call last):
  File "/home/alexds9/stable-diffusion-webui/extensions-builtin/Lora/networks.py", line 321, in load_networks
    net = load_network(name, network_on_disk)
  File "/home/alexds9/stable-diffusion-webui/extensions-builtin/Lora/networks.py", line 186, in load_network
    key_network_without_network_parts, network_name, network_weight = key_network.rsplit(".", 2)
ValueError: not enough values to unpack (expected 3, got 2)

Here is a link to this Lora on Civitai. This Lora is working fine with ComfyUI. I also tried other Loras trained with OneTrainer, and they are causing the same error.

Steps to reproduce the problem

Use this Lora with SD3.

What should have happened?

The error should not appear and Lora should work.

What browsers do you use to access the UI ?

Google Chrome

Sysinfo

sysinfo-2024-08-02-12-18.json

Console logs

################################################################
Install script for stable-diffusion + Web UI
Tested on Debian 11 (Bullseye), Fedora 34+ and openSUSE Leap 15.4 or newer.
################################################################

################################################################
Running on alexds9 user
################################################################

################################################################
Repo already cloned, using it as install directory
################################################################

################################################################
Create and activate python venv
################################################################

################################################################
Launching launch.py...
################################################################
glibc version is 2.39
Check TCMalloc: libtcmalloc_minimal.so.4
libtcmalloc_minimal.so.4 is linked with libc.so,execute LD_PRELOAD=/lib/x86_64-linux-gnu/libtcmalloc_minimal.so.4
Python 3.10.6 (main, Jul  8 2024, 18:43:36) [GCC 13.2.0]
Version: v1.10.1
Commit hash: 82a973c04367123ae98bd9abdf80d9eda9b910e2
Launching Web UI with arguments: --device-id=0 --disable-safe-unpickle --xformers --disable-extra-extensions --listen --api
*** "--disable-extra-extensions" arg was used, will only load built-in extensions ***
Loading weights [92db4295e9] from /home/alexds9/stable-diffusion-webui/models/Stable-diffusion/stable-diffusion-3-medium/sd3_medium_incl_clips_t5xxlfp8.safetensors
Creating model from config: /home/alexds9/stable-diffusion-webui/configs/sd3-inference.yaml
Running on local URL:  http://0.0.0.0:7860

To create a public link, set `share=True` in `launch()`.
Startup time: 11.5s (prepare environment: 1.7s, import torch: 3.5s, import gradio: 0.7s, setup paths: 1.4s, initialize shared: 0.2s, other imports: 0.6s, list SD models: 0.4s, load scripts: 0.9s, refresh VAE: 0.2s, create ui: 1.3s, gradio launch: 0.3s, add APIs: 0.3s).
Applying attention optimization: xformers... done.
Model loaded in 3.8s (load weights from disk: 0.3s, create model: 1.9s, apply weights to model: 1.3s).
loading network /home/alexds9/stable-diffusion-webui/models/Lora/My/SD3/Morph/Amber/working/ambrlux_style_amber_everything_sd3.safetensors: ValueError
Traceback (most recent call last):
  File "/home/alexds9/stable-diffusion-webui/extensions-builtin/Lora/networks.py", line 321, in load_networks
    net = load_network(name, network_on_disk)
  File "/home/alexds9/stable-diffusion-webui/extensions-builtin/Lora/networks.py", line 186, in load_network
    key_network_without_network_parts, network_name, network_weight = key_network.rsplit(".", 2)
ValueError: not enough values to unpack (expected 3, got 2)

100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:31<00:00,  1.57s/it]
Total progress: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:32<00:00,  1.64s/it]
Total progress: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████| 20/20 [00:32<00:00,  1.53s/it]

Additional information

No response

a-l-e-x-d-s-9 commented 1 month ago

I added a few prints to debug what's the issue is.

key_network: lora_te1_text_model_encoder_layers_0_mlp_fc1.alpha key_network.rsplit(".", 2): ['lora_te1_text_model_encoder_layers_0_mlp_fc1', 'alpha'] diffusers_weight_map: diffusers_weight_map.txt

The code:

def load_network(name, network_on_disk):
    net = network.Network(name, network_on_disk)
    net.mtime = os.path.getmtime(network_on_disk.filename)

    sd = sd_models.read_state_dict(network_on_disk.filename)

    # this should not be needed but is here as an emergency fix for an unknown error people are experiencing in 1.2.0
    if not hasattr(shared.sd_model, 'network_layer_mapping'):
        assign_network_names_to_compvis_modules(shared.sd_model)

    keys_failed_to_match = {}
    is_sd2 = 'model_transformer_resblocks' in shared.sd_model.network_layer_mapping
    if hasattr(shared.sd_model, 'diffusers_weight_map'):
        diffusers_weight_map = shared.sd_model.diffusers_weight_map
    elif hasattr(shared.sd_model, 'diffusers_weight_mapping'):
        diffusers_weight_map = {}
        for k, v in shared.sd_model.diffusers_weight_mapping():
            diffusers_weight_map[k] = v
        shared.sd_model.diffusers_weight_map = diffusers_weight_map
    else:
        diffusers_weight_map = None

    matched_networks = {}
    bundle_embeddings = {}

    for key_network, weight in sd.items():

        if diffusers_weight_map:
            key_network_without_network_parts, network_name, network_weight = key_network.rsplit(".", 2)  # THE LINE WITH ERROR
            network_part = network_name + '.' + network_weight
        else:
            key_network_without_network_parts, _, network_part = key_network.partition(".")
pmajor74 commented 1 month ago

I am having similar issues. SD3 LoRAs trained with OneTrainer work in ComfyUI no problem, but not in A1111.