Upload scripts/patch_te_adapter.py with huggingface_hub
Browse files- scripts/patch_te_adapter.py +42 -0
scripts/patch_te_adapter.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import save_file, load_file
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
meta = OrderedDict()
|
| 5 |
+
meta["format"] ="pt"
|
| 6 |
+
|
| 7 |
+
attn_dict = load_file("/mnt/Train/out/ip_adapter/sd15_bigG/sd15_bigG_000266000.safetensors")
|
| 8 |
+
state_dict = load_file("/home/jaret/Dev/models/hf/OstrisDiffusionV1/unet/diffusion_pytorch_model.safetensors")
|
| 9 |
+
|
| 10 |
+
attn_list = []
|
| 11 |
+
for key, value in state_dict.items():
|
| 12 |
+
if "attn1" in key:
|
| 13 |
+
attn_list.append(key)
|
| 14 |
+
|
| 15 |
+
attn_names = ['down_blocks.0.attentions.0.transformer_blocks.0.attn2.processor', 'down_blocks.0.attentions.1.transformer_blocks.0.attn2.processor', 'down_blocks.1.attentions.0.transformer_blocks.0.attn2.processor', 'down_blocks.1.attentions.1.transformer_blocks.0.attn2.processor', 'down_blocks.2.attentions.0.transformer_blocks.0.attn2.processor', 'down_blocks.2.attentions.1.transformer_blocks.0.attn2.processor', 'up_blocks.1.attentions.0.transformer_blocks.0.attn2.processor', 'up_blocks.1.attentions.1.transformer_blocks.0.attn2.processor', 'up_blocks.1.attentions.2.transformer_blocks.0.attn2.processor', 'up_blocks.2.attentions.0.transformer_blocks.0.attn2.processor', 'up_blocks.2.attentions.1.transformer_blocks.0.attn2.processor', 'up_blocks.2.attentions.2.transformer_blocks.0.attn2.processor', 'up_blocks.3.attentions.0.transformer_blocks.0.attn2.processor', 'up_blocks.3.attentions.1.transformer_blocks.0.attn2.processor', 'up_blocks.3.attentions.2.transformer_blocks.0.attn2.processor', 'mid_block.attentions.0.transformer_blocks.0.attn2.processor']
|
| 16 |
+
|
| 17 |
+
adapter_names = []
|
| 18 |
+
for i in range(100):
|
| 19 |
+
if f'te_adapter.adapter_modules.{i}.to_k_adapter.weight' in attn_dict:
|
| 20 |
+
adapter_names.append(f"te_adapter.adapter_modules.{i}.adapter")
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
for i in range(len(adapter_names)):
|
| 24 |
+
adapter_name = adapter_names[i]
|
| 25 |
+
attn_name = attn_names[i]
|
| 26 |
+
adapter_k_name = adapter_name[:-8] + '.to_k_adapter.weight'
|
| 27 |
+
adapter_v_name = adapter_name[:-8] + '.to_v_adapter.weight'
|
| 28 |
+
state_k_name = attn_name.replace(".processor", ".to_k.weight")
|
| 29 |
+
state_v_name = attn_name.replace(".processor", ".to_v.weight")
|
| 30 |
+
if adapter_k_name in attn_dict:
|
| 31 |
+
state_dict[state_k_name] = attn_dict[adapter_k_name]
|
| 32 |
+
state_dict[state_v_name] = attn_dict[adapter_v_name]
|
| 33 |
+
else:
|
| 34 |
+
print("adapter_k_name", adapter_k_name)
|
| 35 |
+
print("state_k_name", state_k_name)
|
| 36 |
+
|
| 37 |
+
for key, value in state_dict.items():
|
| 38 |
+
state_dict[key] = value.cpu().to(torch.float16)
|
| 39 |
+
|
| 40 |
+
save_file(state_dict, "/home/jaret/Dev/models/hf/OstrisDiffusionV1/unet/diffusion_pytorch_model.safetensors", metadata=meta)
|
| 41 |
+
|
| 42 |
+
print("Done")
|