Errors on the StableDiffusionPipeline.from_single_file function
#3
by
ChenruiMa
- opened
"NotImplementedError: Cannot copy out of meta tensor; no data! Please use torch.nn.Module.to_empty() instead of torch.nn.Module.to() when moving module from meta to a different device."
Oleksandr Hetmantsev found a fix to the problem on https://github.com/huggingface/diffusers/issues/4790.
Adding the following snippet fixes the problem:
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)
fix_diffusers_model_conversion(load_path='/content/drive/MyDrive/galaxsd_final.safetensors',
save_path='/content/drive/MyDrive/galaxsd_final_fix.safetensors')
It might help if someone else also encounters this problem : )