comdoleger commited on
Commit
ed51f70
·
verified ·
1 Parent(s): 3af8e71

Upload testing/shrink_pixart2.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. testing/shrink_pixart2.py +81 -0
testing/shrink_pixart2.py ADDED
@@ -0,0 +1,81 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Blocks to keep
33
+ # keep_blocks = [0, 1, 2, 6, 10, 14, 18, 22, 26, 27]
34
+ keep_blocks = [0, 1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 27]
35
+
36
+
37
+ def weighted_merge(kept_block, removed_block, weight):
38
+ return kept_block * (1 - weight) + removed_block * weight
39
+
40
+
41
+ # First, copy all kept blocks to new_state_dict
42
+ for i, old_idx in enumerate(keep_blocks):
43
+ for name in block_names:
44
+ old_key = name.format(idx=old_idx)
45
+ new_key = name.format(idx=i)
46
+ new_state_dict[new_key] = state_dict[old_key].clone()
47
+
48
+ # Then, merge information from removed blocks
49
+ for i in range(28):
50
+ if i not in keep_blocks:
51
+ # Find the nearest kept blocks
52
+ prev_kept = max([b for b in keep_blocks if b < i])
53
+ next_kept = min([b for b in keep_blocks if b > i])
54
+
55
+ # Calculate the weight based on position
56
+ weight = (i - prev_kept) / (next_kept - prev_kept)
57
+
58
+ for name in block_names:
59
+ removed_key = name.format(idx=i)
60
+ prev_new_key = name.format(idx=keep_blocks.index(prev_kept))
61
+ next_new_key = name.format(idx=keep_blocks.index(next_kept))
62
+
63
+ # Weighted merge for previous kept block
64
+ new_state_dict[prev_new_key] = weighted_merge(new_state_dict[prev_new_key], state_dict[removed_key], weight)
65
+
66
+ # Weighted merge for next kept block
67
+ new_state_dict[next_new_key] = weighted_merge(new_state_dict[next_new_key], state_dict[removed_key],
68
+ 1 - weight)
69
+
70
+ # Convert to fp16 and move to CPU
71
+ for key, value in new_state_dict.items():
72
+ new_state_dict[key] = value.to(torch.float16).cpu()
73
+
74
+ # Save the new state dict
75
+ save_file(new_state_dict, output_path, metadata=meta)
76
+
77
+ new_param_count = sum([v.numel() for v in new_state_dict.values()])
78
+ old_param_count = sum([v.numel() for v in state_dict.values()])
79
+
80
+ print(f"Old param count: {old_param_count:,}")
81
+ print(f"New param count: {new_param_count:,}")