Skip to content

LoraLoaderMixin | RuntimeError: dictionary changed size during iteration #4804

Closed
@chillpixelfun

Description

@chillpixelfun

Describe the bug

Related to the fix for #4797

File "/workspace/sandbox/test.py", line 18, in <module>
    pipe.load_lora_weights(
  File "/home/user/mambaforge/envs/tensorml/lib/python3.10/site-packages/diffusers/pipelines/stable_diffusion_xl/pipeline_stable_diffusion_xl.py", line 904, in load_lora_weights
    state_dict, network_alphas = self.lora_state_dict(
  File "/home/user/mambaforge/envs/tensorml/lib/python3.10/site-packages/diffusers/loaders.py", line 1087, in lora_state_dict
    state_dict = cls._maybe_map_sgm_blocks_to_diffusers(state_dict, unet_config)
  File "/home/user/mambaforge/envs/tensorml/lib/python3.10/site-packages/diffusers/loaders.py", line 1146, in _maybe_map_sgm_blocks_to_diffusers
    for layer in all_keys:
RuntimeError: dictionary changed size during iteration

The bug in the original code is caused by modifying a dictionary while iterating over its keys. This leads to a "RuntimeError: dictionary changed size during iteration." when loading some community created LoRAs. The fix I applied, using list(all_keys) to create a static snapshot of keys, prevents the dictionary from changing in size during iteration and resolves the issue.

I intend to submit a pull request for this issue.

Reproduction

from diffusers import StableDiffusionXLPipeline
import torch

pipe = StableDiffusionXLPipeline.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0",
    torch_dtype=torch.float16,
    variant="fp16",
    use_safetensors=True,
)

pipe.to("cuda")

pipe.load_lora_weights(
    "chillpixel/blacklight-makeup-sdxl-lora",
    weight_name="blacklight_makeup_v1.safetensors",
)

image = pipe(
    prompt="portrait photo of a kitty cat with blacklight makeup",
).images[0]

Logs

No response

System Info

Ubuntu 20.04.6 LTS
Python 3.10.12
diffusers 0.21.0.dev0

Who can help?

@patrickvonplaten #4797

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions