Upload testing/shrink_pixart.py with huggingface_hub
Browse files- testing/shrink_pixart.py +62 -0
testing/shrink_pixart.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file, save_file
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
|
| 5 |
+
model_path = "/home/jaret/Dev/models/hf/PixArt-Sigma-XL-2-1024_tiny/transformer/diffusion_pytorch_model_orig.safetensors"
|
| 6 |
+
output_path = "/home/jaret/Dev/models/hf/PixArt-Sigma-XL-2-1024_tiny/transformer/diffusion_pytorch_model.safetensors"
|
| 7 |
+
|
| 8 |
+
state_dict = load_file(model_path)
|
| 9 |
+
|
| 10 |
+
meta = OrderedDict()
|
| 11 |
+
meta["format"] = "pt"
|
| 12 |
+
|
| 13 |
+
new_state_dict = {}
|
| 14 |
+
|
| 15 |
+
# Move non-blocks over
|
| 16 |
+
for key, value in state_dict.items():
|
| 17 |
+
if not key.startswith("transformer_blocks."):
|
| 18 |
+
new_state_dict[key] = value
|
| 19 |
+
|
| 20 |
+
block_names = ['transformer_blocks.{idx}.attn1.to_k.bias', 'transformer_blocks.{idx}.attn1.to_k.weight',
|
| 21 |
+
'transformer_blocks.{idx}.attn1.to_out.0.bias', 'transformer_blocks.{idx}.attn1.to_out.0.weight',
|
| 22 |
+
'transformer_blocks.{idx}.attn1.to_q.bias', 'transformer_blocks.{idx}.attn1.to_q.weight',
|
| 23 |
+
'transformer_blocks.{idx}.attn1.to_v.bias', 'transformer_blocks.{idx}.attn1.to_v.weight',
|
| 24 |
+
'transformer_blocks.{idx}.attn2.to_k.bias', 'transformer_blocks.{idx}.attn2.to_k.weight',
|
| 25 |
+
'transformer_blocks.{idx}.attn2.to_out.0.bias', 'transformer_blocks.{idx}.attn2.to_out.0.weight',
|
| 26 |
+
'transformer_blocks.{idx}.attn2.to_q.bias', 'transformer_blocks.{idx}.attn2.to_q.weight',
|
| 27 |
+
'transformer_blocks.{idx}.attn2.to_v.bias', 'transformer_blocks.{idx}.attn2.to_v.weight',
|
| 28 |
+
'transformer_blocks.{idx}.ff.net.0.proj.bias', 'transformer_blocks.{idx}.ff.net.0.proj.weight',
|
| 29 |
+
'transformer_blocks.{idx}.ff.net.2.bias', 'transformer_blocks.{idx}.ff.net.2.weight',
|
| 30 |
+
'transformer_blocks.{idx}.scale_shift_table']
|
| 31 |
+
|
| 32 |
+
# New block idx 0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27
|
| 33 |
+
|
| 34 |
+
current_idx = 0
|
| 35 |
+
for i in range(28):
|
| 36 |
+
if i not in [0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27]:
|
| 37 |
+
# todo merge in with previous block
|
| 38 |
+
for name in block_names:
|
| 39 |
+
try:
|
| 40 |
+
new_state_dict_key = name.format(idx=current_idx - 1)
|
| 41 |
+
old_state_dict_key = name.format(idx=i)
|
| 42 |
+
new_state_dict[new_state_dict_key] = (new_state_dict[new_state_dict_key] * 0.5) + (state_dict[old_state_dict_key] * 0.5)
|
| 43 |
+
except KeyError:
|
| 44 |
+
raise KeyError(f"KeyError: {name.format(idx=current_idx)}")
|
| 45 |
+
else:
|
| 46 |
+
for name in block_names:
|
| 47 |
+
new_state_dict[name.format(idx=current_idx)] = state_dict[name.format(idx=i)]
|
| 48 |
+
current_idx += 1
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
# make sure they are all fp16 and on cpu
|
| 52 |
+
for key, value in new_state_dict.items():
|
| 53 |
+
new_state_dict[key] = value.to(torch.float16).cpu()
|
| 54 |
+
|
| 55 |
+
# save the new state dict
|
| 56 |
+
save_file(new_state_dict, output_path, metadata=meta)
|
| 57 |
+
|
| 58 |
+
new_param_count = sum([v.numel() for v in new_state_dict.values()])
|
| 59 |
+
old_param_count = sum([v.numel() for v in state_dict.values()])
|
| 60 |
+
|
| 61 |
+
print(f"Old param count: {old_param_count:,}")
|
| 62 |
+
print(f"New param count: {new_param_count:,}")
|