How to Utilize Lora in Diffusers: A Step-by-Step Guide

Jan 10, 2024 | Data Science

If you’re an AIGC researcher looking to fine-tune models with Lora, you’ve landed in the right spot. This tutorial will walk you through the simplicity of loading and utilizing LoRA weights in the Diffusers framework, helping you leverage models from communities like Huggingface and civitai.

What is Lora?

Low-Rank Adaptation, or LoRA, is a brilliant method developed by Microsoft designed to optimize large language models. Instead of fine-tuning the entire model, LoRA focuses on adjusting smaller decomposed matrices, which saves training time and computing resources.

Think of it like fitting a small tuning fork to an orchestra: instead of re-tuning every instrument (the whole model), you’re just adjusting the minor notes while the grand symphony remains intact. This is achieved by training the matrices $A$ and $B$ in the equation:

W = W + Delta W

Where $Delta W$ can be expressed as:

Delta W = A B^T

What is Safetensors?

Safetensors is a modern tensor storage format developed by Hugging Face. Offering both speed and safety, it outperforms older formats like pickle and is increasingly used for various models, particularly Lora. To get started, you can install it simply by running:

pip install safetensors

How to Load Lora Weights

Loading pre-trained Lora into the Diffusers framework does not have to be complicated. Here’s how you can do it:

Step-by-Step Instructions

  • First, clone the Diffusers repository:
  • git clone https://github.com/huggingface/diffusers
  • Next, navigate to the cloned directory:
  • cd diffusers
  • Now, let’s convert the downloaded model files:
    • If your file is a .safetensor:
    • python scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path xxx.safetensors --dump_path save_dir --from_safetensors
    • If your file is a .ckpt:
    • python scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path xxx.ckpt --dump_path save_dir
  • Finally, load the model using:
  • from diffusers import StableDiffusionPipeline
    pipeline = StableDiffusionPipeline.from_pretrained(save_dir, torch_dtype=torch.float32)

Troubleshooting

If you encounter any issues while loading or using the Lora models, consider the following troubleshooting strategies:

  • Ensure you have all the necessary dependencies installed. Use pip install -r requirements.txt to install them if you have a requirements file.
  • Check that your model files are in the correct format. If they’re not loading properly, try converting them again using the provided scripts.
  • Feel free to adjust the merging ratio. If your model seems off, modifying the alpha parameter during merging might lead to better performance.
  • If errors persist, refer to the official GitHub issues page for potential solutions, and don’t hesitate to modify the conversion script as necessary.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

How to Train Your Own Lora Model

Creating a custom Lora model is straightforward. Here’s how you can do it:

  • Start by setting the necessary environment variables:
  • export MODEL_NAME=CompVis/stable-diffusion-v1-4
    export DATASET_NAME=lambdalabs/pokemon-blip-captions
  • Then, launch the training command:
  • accelerate launch --mixed_precision=fp16 train_text_to_image_lora.py --pretrained_model_name_or_path=$MODEL_NAME --dataset_name=$DATASET_NAME --caption_column=text --resolution=512 --random_flip --train_batch_size=1 --num_train_epochs=100 --checkpointing_steps=5000 --learning_rate=1e-04 --lr_scheduler=constant --lr_warmup_steps=0 --seed=42 --output_dir=sd-pokemon-model-lora --validation_prompt=cute dragon creature --report_to=wandb
  • After training, simply load your newly created weights:
  • from diffusers import StableDiffusionPipeline
    model_path = your_path 
    pipe = StableDiffusionPipeline.from_pretrained(CompVis/stable-diffusion-v1-4, torch_dtype=torch.float16)
    pipe.unet.load_attn_procs(model_path)
    pipe.to(cuda)

Conclusion

That’s it! Loading and training Lora in the Diffusers framework is a breeze. By following these simple steps, you’ll be able to harness the power of advanced models efficiently.

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox