Improved support for working with Kohya-style LoRAs in diffusers 🤗
A seamless interoperability between the Kohya-styled LoRAs and Diffusers has been one of the most requested features from the community in the last months.
We are making promising progress in this regard.
With #4287, this support should be quite improved. We also have made a patch release to make it available. So, we ask the community to try this feature and let us know of any issues.
Get started by reading the documentation here. Also, be aware of the known limitations and know that we're actively working to mitigate them quickly.
A special heart-felt thanks to @takuma104 and @isidentical who significantly helped us in getting this far!
Unable to load LoRA from a local folder (Downloaded from civitai, for SD1.5)
pipe.load_lora_weights("./loras", weight_name="Theovercomer8.safetensors")
Is anything wrong with this? @sayakpaul
Great to see the improvements! Are there plans to support loading multiple LoRAs soon?
@pdoane not immediately but happy to discuss design and related things.
Unable to load LoRA from a local folder (Downloaded from civitai, for SD1.5)
pipe.load_lora_weights("./loras", weight_name="Theovercomer8.safetensors")Is anything wrong with this? @sayakpaul
Not sure as this seems to work: https://colab.research.google.com/gist/sayakpaul/0b0de72df83a665e8b525c1f8c76f218/scratchpad.ipynb
Unable to load LoRA from a local folder (Downloaded from civitai, for SD1.5)
pipe.load_lora_weights("./loras", weight_name="Theovercomer8.safetensors")Is anything wrong with this? @sayakpaulNot sure as this seems to work: https://colab.research.google.com/gist/sayakpaul/0b0de72df83a665e8b525c1f8c76f218/scratchpad.ipynb
For me, it works in local but deploying on the container gives an error (Diffusers version = 0.19.2)
File "/root/.pyenv/versions/3.9.17/lib/python3.9/site-packages/diffusers/loaders.py", line 1093, in lora_state_dict
for k in state_dict.keys()
UnboundLocalError: local variable 'state_dict' referenced before assignment
Lycoris support will give last mile connectivity in Lora sphere, any plans to support it?
Lycoris support will give last mile connectivity in Lora sphere, any plans to support it?
LyCORIS's LoCon LoRAs should work. LoHA won't work yet (keys containing "hada").
For me, it works in local but deploying on the container gives an error (Diffusers version = 0.19.2)
Can't debug this unfortunately when it's running in a container.
LyCORIS's LoCon LoRAs should work. LoHA won't work yet (keys containing "hada").
I tested on lycoris, it's not giving results it should using load_lora_weights
I made gist which use diffusers with custom loading which works well for lycoris, lora and hada (still need improvements)
https://gist.github.com/adhikjoshi/2c6da89cbcd7a6a3344d3081ccd1dda0
@pdoane not immediately but happy to discuss design and related things.
If scale were extended to take a list, it isn't clear which LoRA it should be applied to. We could say it's the order that LoRAs were loaded, but this gets more complicated when the set of loaded LoRAs changes overtime.
It's tempting to try to resolve this in the generate call, but that's too late for computing prompt_embeds. An explicit configuration step on the pipeline makes the most sense to me. This would transform the pipeline from one set of active LoRAs to another, possible being smart about reuse.
So one option is:
lora_a = pipe.lora_state_dict("lora_a.safetensors");
lora_b = pipe.lora_state_dict("lora_b.safetensors");
lora_c = pipe.lora_state_dict("lora_c.safetensors");
pipe.set_loras([lora_a, lora_b])
prompt_embeds = ...
pipe(...., cross_attention_kwargs={"scale": [0.3, 0.6]}) # A at 0.3, B at 0.6
pipe.set_loras([lora_b, lora_c])
prompt_embeds = ...
pipe(...., cross_attention_kwargs={"scale": [0.4, 0.5]}) # B at 0.4, C at 0.5
But this design would not allow for diffusers to mutate the weights which was important for performance in earlier analysis. Another option would be to specify weights at the same time:
pipe.set_loras([(lora_a, 0.3), (lora_b, 0.6)])
prompt_embeds = ...
pipe(....) # A at 0.3, B at 0.6
pipe.set_loras([(lora_b, 0.4), (lora_c, 0.5)])
prompt_embeds = ...
pipe(....) # B at 0.4, C at 0.5
I prefer the second approach as it gives more implementation freedom to diffusers and makes the binding between scale and the LoRA more explicit.
Actually the 2nd approach is needed for correctness too - see my comment in damian0815/compel#42. The order of operation needs to be:
- Set current LoRAs and their weights on the pipeline
- Create prompt embeds
- Generate image
The scale parameter on cross_attention_kwargs should probably just be deprecated.
I am so sorry for kinda hijacking, but I don't want to create a separate issue as I am sure it's something pretty straightforward. If I train SDXL LoRa using
train_dreambooth_lora_sdxl.py and it outputs a bin file, how are you supposed to transform it to a .safetensors format so I can load it just like pipe.load_lora_weights("./loras", weight_name="Theovercomer8.safetensors") ?
Also, is such LoRa from dreambooth supposed to work in ComfyUI?
Also, what "-style" LoRAs does the dreambooth training create?
All of those call for separate discussions and should be asked on your Discord forum since they don't concern the design of the library or any issues related to it.
@pdoane not immediately but happy to discuss design and related things.
If
scalewere extended to take a list, it isn't clear which LoRA it should be applied to. We could say it's the order that LoRAs were loaded, but this gets more complicated when the set of loaded LoRAs changes overtime.It's tempting to try to resolve this in the generate call, but that's too late for computing
prompt_embeds. An explicit configuration step on the pipeline makes the most sense to me. This would transform the pipeline from one set of active LoRAs to another, possible being smart about reuse.So one option is:
lora_a = pipe.lora_state_dict("lora_a.safetensors"); lora_b = pipe.lora_state_dict("lora_b.safetensors"); lora_c = pipe.lora_state_dict("lora_c.safetensors"); pipe.set_loras([lora_a, lora_b]) prompt_embeds = ... pipe(...., cross_attention_kwargs={"scale": [0.3, 0.6]}) # A at 0.3, B at 0.6 pipe.set_loras([lora_b, lora_c]) prompt_embeds = ... pipe(...., cross_attention_kwargs={"scale": [0.4, 0.5]}) # B at 0.4, C at 0.5But this design would not allow for diffusers to mutate the weights which was important for performance in earlier analysis. Another option would be to specify weights at the same time:
pipe.set_loras([(lora_a, 0.3), (lora_b, 0.6)]) prompt_embeds = ... pipe(....) # A at 0.3, B at 0.6 pipe.set_loras([(lora_b, 0.4), (lora_c, 0.5)]) prompt_embeds = ... pipe(....) # B at 0.4, C at 0.5I prefer the second approach as it gives more implementation freedom to diffusers and makes the binding between scale and the LoRA more explicit.
I like the set_loras design, here's my 2C
Maybe something like
config = LoraConfig(xyz)
lora = Lora(xyz)
loras = {"Name":(lora, config)}
pipe.add_supporting_networks(loras)
My thoughts here are that thing gives you stronger modularity, and gives the option to do something like
pipe.remove_support_network("Name")
In webui's like automatic, there's a tendency to load lots of loras at different strengths (potentially also loras of different design), this design would support something like that and give some future proofing by moving the config to it's own object. The modularity means this design extends to arbitrary future designs, as LORAS may in the future add more design space for parameters, we're already sort of seeing this is the case for adapters.
Internally, you could farm out the processing to something like:
config.run_processor(Lora)
By welding the processing code to the config object, you can easily enable multiple lora/adapter types by composition.
Why pipe.loadlorawweight("path",weightval)?
This requires re-reading the file every time the weight changes and in the case of multiple LoRAs requires re-reading every single one. SD-1 LoRAs are up to ~150MB and SDXL LoRAs around ~400MB (I've even seen one at almost 2GB). This is enough I/O traffic that separating loading is useful.
We also should be careful with incremental APIs (e.g. add/remove) as it requires the network to be in a consistent state after each operation. There could be begin_update/end_update methods but it's simpler to just set everything all at once.
if we do parsing on our own there is a high chance we will implement it returning dict /2 array
If you are looking for A1111 compatibility, you will have to parse the prompt multiple times:
- Parse prompt for LoRA parameters
- Set current LoRAs and their weights on the pipeline
- Re-parse prompt to generate prompt embeds
- Generate image
Creating the prompt embeddings must be done after the LoRAs are loaded with their weights configured as they impact the text encoder. A1111 approach is a UI choice though and many other tools specify LoRA outside of the prompt.
Diffusers should also consider using multiple scales for each LoRA. Comfy allows for the Text Encoder scale and the UNet scale to be set separately.
first
lora_a = pipe.lora_state_dict("lora_a.safetensors");
lora_b = pipe.lora_state_dict("lora_b.safetensors");
lora_c = pipe.lora_state_dict("lora_c.safetensors");
pipe.set_loras([lora_a, lora_b])
prompt_embeds = ...
pipe(...., cross_attention_kwargs={"scale": [0.3, 0.6]}) # A at 0.3, B at 0.6
pipe.set_loras([lora_b, lora_c])
prompt_embeds = ...
pipe(...., cross_attention_kwargs={"scale": [0.4, 0.5]}) # B at 0.4, C at 0.5
second
pipe.set_loras([(lora_a, 0.3), (lora_b, 0.6)])
prompt_embeds = ...
pipe(....) # A at 0.3, B at 0.6
pipe.set_loras([(lora_b, 0.4), (lora_c, 0.5)])
prompt_embeds = ...
pipe(....) # B at 0.4, C at 0.5
First of all, thanks for your work I have some question about above code that you wrote. With first method, only the cross_attn weight of two loras are setted with the numbers setted e.g. (0.4, 0.5) And with second method, the text_emb weight and cross_attn weight are setted with the numbers right..? @pdoane
The two are meant to be equivalent - the first design was looking at minimal changes to the existing API and the second design is roughly the way I think this should be done.
However the first approach doesn't work with custom embeds (which is true of where we are today as well). I hadn't realized the order of operations problems until seeing a bug report in the Compel repo.
In all cases we want to set the weights for text encoder and unet, and my most recent comment was suggesting that we make those independent as well (matching Comfy support).
Thanks for reply
By the way, is the set_loras attribute is your custom?
I got an error message with AttributeError: 'StableDiffusionControlNetImg2ImgPipeline' object has no attribute 'set_loras'
@pdoane