Skip to content

Convert LoRA safetensor to diffusers form #3238

@smko77

Description

@smko77

Describe the bug

The error occured when converting the custom LoRA weight in safetensor to diffuesrs form. I used this script

I was converting the LoRA weight from civitai. I tried debugging to find out why this error occured. It turns out that this LoRA weight has additional structure such as

# lora unet down block 0
lora_unet_down_blocks_0_downsamplers_0_conv.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_downsamplers_0_conv.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_downsamplers_0_conv.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv1.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv1.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv1.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv2.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv2.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_conv2.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_time_emb_proj.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_time_emb_proj.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_0_time_emb_proj.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv1.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv1.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv1.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv2.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv2.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_conv2.lora_up.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_time_emb_proj.alpha	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_time_emb_proj.lora_down.weight	<class 'torch.Tensor'>
lora_unet_down_blocks_0_resnets_1_time_emb_proj.lora_up.weight	<class 'torch.Tensor'>

and the error occured exactly when it converts lora_unet_down_blocks_0_downsamplers_0_conv.lora_down.weight.
At this time, each tensor has size

  • weight_up : torch.Size([320, 32])
  • weight_down : torch.Size([32, 320, 3, 3])

Reproduction

convert Lora weight using this script

Logs

curr_layer.weight.data += alpha * torch.mm(weight_up, weight_down).unsqueeze(2).unsqueeze(3)
RuntimeError: mat2 must be a matrix

System Info

  • diffusers version: 0.15.1
  • Platform: Linux-4.15.0-47-generic-x86_64-with-Ubuntu-18.04-bionic
  • Python version: 3.7.5
  • PyTorch version (GPU?): 1.13.1+cu117 (True)
  • Huggingface_hub version: 0.14.0
  • Transformers version: 4.25.1
  • Accelerate version: 0.13.1
  • xFormers version: 0.0.16
  • Using GPU in script?:
  • Using distributed or parallel set-up in script?:

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleIssues that haven't received updates

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions