Closed damian0815 closed 10 months ago
Note that this scenario commonly arises because of finetuning and distribution: finetuner produces diffusers folder, which is converted to .safetensors for distribution on the (very bad) platform civitai. user downloads the checkpoint and then attempts to convert it back to .safetensors for use in eg InvokeAI, or for further finetuning
That's unfortunate :-/ We don't really maintain convert_diffusers_to_original_stable_diffusion.py
it was added by the community
Any chance you could add a reproducible code snippet here maybe? :-)
Also, does it work when removing:
new_item = new_item.replace("q.weight", "to_q.weight")
new_item = new_item.replace("q.bias", "to_q.bias")
new_item = new_item.replace("k.weight", "to_k.weight")
new_item = new_item.replace("k.bias", "to_k.bias")
new_item = new_item.replace("v.weight", "to_v.weight")
new_item = new_item.replace("v.bias", "to_v.bias")
we indeed deprecated the old "q.weight" syntax - so these lines should not exist anymore I think
Adding this code snippet here, in case anyone needs a quick fix to this. This converts a corrupted safetensors file to one that can be loaded using the from_single_file
method.
It appears that the incompatible keys are localized to the VAE.
from safetensors import safe_open
from safetensors.torch import save_file
def fix_diffusers_model_conversion(load_path: str, save_path: str):
# load original
tensors = {}
with safe_open(load_path, framework="pt") as f:
for key in f.keys():
tensors[key] = f.get_tensor(key)
# migrate
new_tensors = {}
for k, v in tensors.items():
new_key = k
# only fix the vae
if 'first_stage_model.' in k:
# migrate q, k, v keys
new_key = new_key.replace('.to_q.weight', '.q.weight')
new_key = new_key.replace('.to_q.bias', '.q.bias')
new_key = new_key.replace('.to_k.weight', '.k.weight')
new_key = new_key.replace('.to_k.bias', '.k.bias')
new_key = new_key.replace('.to_v.weight', '.v.weight')
new_key = new_key.replace('.to_v.bias', '.v.bias')
new_tensors[new_key] = v
# save
save_file(new_tensors, save_path)
Getting the same issue when converting the ckpt file
First converted to ckpt using convert_diffusers_to_original_stable_diffusion.py
Then tried to convert back to diffusers using convert_original_stable_diffusion_to_diffusers.py
Doing this because the diffusers output is huge in size, where as ckpt is only 2gb with half option
It is not possible for me to zip and store diffusers it is huge in size
Doing this because the diffusers output is huge in size, where as ckpt is only 2gb with half option
you can convert the pipeline to float16 before you save it:
pipe = StableDiffusionPipeline.from_pretrained(...)
pipe.save('blah_big') # 4GB
pipe.to('cpu', dtype=torch.float16)
pipe.save('blah_small') # 2GB
@damian0815 I am using dreambooth training to generate my model
Here is my accelerate config file, it is using mixed_precision fp16 is the above suggestion you made and this the same thing
compute_environment: LOCAL_MACHINE distributed_type: 'NO' downcast_bf16: 'no' gpu_ids: all machine_rank: 0 main_training_function: main mixed_precision: fp16 num_machines: 1 num_processes: 1 rdzv_backend: static same_network: true tpu_env: [] tpu_use_cluster: false tpu_use_sudo: false use_cpu: false
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
Please note that issues that do not follow the contributing guidelines are likely to be ignored.
I'm struggling to get one of my own to go, and it doesnt seem to be corrupted, but it gives me this error, i'm not very good at programming, and 90% of the time the convert script works, but ONE OF MY OWN merged models that's in safetensors won't convert...
Edit: https://github.com/huggingface/diffusers/issues/4790#issuecomment-1745398865. This works but if you're on colab i modified it using chat GPT:
from safetensors import safe_open from safetensors.torch import save_file
def fix_diffusers_model_conversion(load_path: str, save_path: str):
# Specify the path to the uploaded file in Colab
colab_load_path = '/content/models/theAgendaMix_v35.safetensors' # Update this with the actual path
# Specify the path where you want to save the fixed file in Colab
colab_save_path = '/content/models/FixedModels/theAgendaMix_v35.safetensors' # Update this with the actual path
# load original
tensors = {}
with safe_open(colab_load_path, framework="pt") as f:
for key in f.keys():
tensors[key] = f.get_tensor(key)
# migrate
new_tensors = {}
for k, v in tensors.items():
new_key = k
# only fix the vae
if 'first_stage_model.' in k:
# migrate q, k, v keys
new_key = new_key.replace('.to_q.weight', '.q.weight')
new_key = new_key.replace('.to_q.bias', '.q.bias')
new_key = new_key.replace('.to_k.weight', '.k.weight')
new_key = new_key.replace('.to_k.bias', '.k.bias')
new_key = new_key.replace('.to_v.weight', '.v.weight')
new_key = new_key.replace('.to_v.bias', '.v.bias')
new_tensors[new_key] = v
# save
save_file(new_tensors, colab_save_path)
fix_diffusers_model_conversion(load_path='/content/drive/path/to/your/original_file.pth', save_path='/content/drive/path/to/save/fixed_file.pth')
I don't program, i duct tape :3
Adding this code snippet here, in case anyone needs a quick fix to this. This converts a corrupted safetensors file to one that can be loaded using the
from_single_file
method.It appears that the incompatible keys are localized to the VAE.
from safetensors import safe_open from safetensors.torch import save_file def fix_diffusers_model_conversion(load_path: str, save_path: str): # load original tensors = {} with safe_open(load_path, framework="pt") as f: for key in f.keys(): tensors[key] = f.get_tensor(key) # migrate new_tensors = {} for k, v in tensors.items(): new_key = k # only fix the vae if 'first_stage_model.' in k: # migrate q, k, v keys new_key = new_key.replace('.to_q.weight', '.q.weight') new_key = new_key.replace('.to_q.bias', '.q.bias') new_key = new_key.replace('.to_k.weight', '.k.weight') new_key = new_key.replace('.to_k.bias', '.k.bias') new_key = new_key.replace('.to_v.weight', '.v.weight') new_key = new_key.replace('.to_v.bias', '.v.bias') new_tensors[new_key] = v # save save_file(new_tensors, save_path)
Thanks a lot!
Describe the bug
Original stable diffusion checkpoints that have been extracted from diffusers folders via
convert_diffusers_to_original_stable_diffusion.py
causeconvert_original_stable_diffusion_to_diffusers.py
to fail withAttributeError: 'Attention' object has no attribute 'to_to_k'
:The cause is these lines in
convert_from_ckpt.py
:Because the loaded checkpoint already has keys labelled
to_q.weight
etc, this method renames them toto_to_q.weight
causing downstream logic to fail.The replacement logic should not do this.
Reproduction
To reproduce, convert a .safetensors checkpoint to diffusers, then convert the diffusers folder back to .safetensors:
expected result: works actual result:
AttributeError: 'Attention' object has no attribute 'to_to_k'
Logs
No response
System Info
diffusers main (0d81e543a2b048d7b1f558fb1006d10a062a6e9e)
Who can help?
@patrickvonplaten