Skip to content

Instantly share code, notes, and snippets.

@sanguivore-easyco
Forked from edicam/peft.py
Last active January 28, 2025 19:12
Show Gist options
  • Save sanguivore-easyco/e1757f2ecfb0e352f2eefa22ed3a8259 to your computer and use it in GitHub Desktop.
Save sanguivore-easyco/e1757f2ecfb0e352f2eefa22ed3a8259 to your computer and use it in GitHub Desktop.
Script to convert the conceptmod safetensors slider to an usable .safetensors slider
import os.path
from collections import OrderedDict
from safetensors.torch import save_file, load_file
import torch
state_dict=load_file("path to your slider.safetensors")
alpha_keys = [
'lora_unet-single_transformer_blocks-0-attn-to_q.alpha',
'lora_unet_single_transformer_blocks_0_attn_to_q.alpha'
]
rank_idx0_keys = [
'lora_unet-single_transformer_blocks-0-attn-to_q.lora_down.weight',
'lora_unet_single_transformer_blocks_0_attn_to_q.lora_down.weight'
]
alpha = None
rank = None
for key in rank_idx0_keys:
if key in state_dict:
rank = int(state_dict[key].shape[0])
break
if rank is None:
raise ValueError(f'Could not find rank in state dict')
for key in alpha_keys:
if key in state_dict:
alpha = int(state_dict[key])
break
if alpha is None:
# set to rank if not found
alpha = rank
#up_multiplier = alpha / rank
up_multiplier=0.125 # manual multiplier setting
new_state_dict = {}
for key, value in state_dict.items():
if key.endswith('.alpha'):
continue
orig_dtype = value.dtype
new_val = value.float() * up_multiplier
new_key = key
new_key = new_key.replace('-', '_')
new_key = new_key.replace('lora_unet_', 'transformer.')
for i in range(100):
new_key = new_key.replace(f'transformer_blocks_{i}_', f'transformer_blocks.{i}.')
new_key = new_key.replace('lora_down', 'lora_A')
new_key = new_key.replace('lora_up', 'lora_B')
new_key = new_key.replace('_lora', '.lora')
new_key = new_key.replace('attn_', 'attn.')
new_key = new_key.replace('ff_', 'ff.')
new_key = new_key.replace('context_net_', 'context.net.')
new_key = new_key.replace('0_proj', '0.proj')
new_key = new_key.replace('norm_linear', 'norm.linear')
new_key = new_key.replace('norm_out_linear', 'norm_out.linear')
new_key = new_key.replace('to_out_', 'to_out.')
new_state_dict[new_key] = new_val.to(orig_dtype)
meta = OrderedDict()
meta['format'] = 'pt'
save_file(new_state_dict,"path to your output safentensors file") # must be .safetensors
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment