Upload testing/shrink_pixart_sm.py with huggingface_hub
Browse files- testing/shrink_pixart_sm.py +84 -0
testing/shrink_pixart_sm.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
from safetensors.torch import load_file, save_file
|
| 3 |
+
from collections import OrderedDict
|
| 4 |
+
|
| 5 |
+
meta = OrderedDict()
|
| 6 |
+
meta['format'] = "pt"
|
| 7 |
+
|
| 8 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
+
|
| 10 |
+
|
| 11 |
+
def reduce_weight(weight, target_size):
|
| 12 |
+
weight = weight.to(device, torch.float32)
|
| 13 |
+
original_shape = weight.shape
|
| 14 |
+
flattened = weight.view(-1, original_shape[-1])
|
| 15 |
+
|
| 16 |
+
if flattened.shape[1] <= target_size:
|
| 17 |
+
return weight
|
| 18 |
+
|
| 19 |
+
U, S, V = torch.svd(flattened)
|
| 20 |
+
reduced = torch.mm(U[:, :target_size], torch.diag(S[:target_size]))
|
| 21 |
+
|
| 22 |
+
if reduced.shape[1] < target_size:
|
| 23 |
+
padding = torch.zeros(reduced.shape[0], target_size - reduced.shape[1], device=device)
|
| 24 |
+
reduced = torch.cat((reduced, padding), dim=1)
|
| 25 |
+
|
| 26 |
+
return reduced.view(original_shape[:-1] + (target_size,))
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def reduce_bias(bias, target_size):
|
| 30 |
+
bias = bias.to(device, torch.float32)
|
| 31 |
+
original_size = bias.shape[0]
|
| 32 |
+
|
| 33 |
+
if original_size <= target_size:
|
| 34 |
+
return torch.nn.functional.pad(bias, (0, target_size - original_size))
|
| 35 |
+
else:
|
| 36 |
+
return bias.view(-1, original_size // target_size).mean(dim=1)[:target_size]
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# Load your original state dict
|
| 40 |
+
state_dict = load_file(
|
| 41 |
+
"/home/jaret/Dev/models/hf/PixArt-Sigma-XL-2-512_MS_t5large_raw/transformer/diffusion_pytorch_model.orig.safetensors")
|
| 42 |
+
|
| 43 |
+
# Create a new state dict for the reduced model
|
| 44 |
+
new_state_dict = {}
|
| 45 |
+
|
| 46 |
+
source_hidden_size = 1152
|
| 47 |
+
target_hidden_size = 1024
|
| 48 |
+
|
| 49 |
+
for key, value in state_dict.items():
|
| 50 |
+
value = value.to(device, torch.float32)
|
| 51 |
+
if 'weight' in key or 'scale_shift_table' in key:
|
| 52 |
+
if value.shape[0] == source_hidden_size:
|
| 53 |
+
value = value[:target_hidden_size]
|
| 54 |
+
elif value.shape[0] == source_hidden_size * 4:
|
| 55 |
+
value = value[:target_hidden_size * 4]
|
| 56 |
+
elif value.shape[0] == source_hidden_size * 6:
|
| 57 |
+
value = value[:target_hidden_size * 6]
|
| 58 |
+
|
| 59 |
+
if len(value.shape) > 1 and value.shape[
|
| 60 |
+
1] == source_hidden_size and 'attn2.to_k.weight' not in key and 'attn2.to_v.weight' not in key:
|
| 61 |
+
value = value[:, :target_hidden_size]
|
| 62 |
+
elif len(value.shape) > 1 and value.shape[1] == source_hidden_size * 4:
|
| 63 |
+
value = value[:, :target_hidden_size * 4]
|
| 64 |
+
|
| 65 |
+
elif 'bias' in key:
|
| 66 |
+
if value.shape[0] == source_hidden_size:
|
| 67 |
+
value = value[:target_hidden_size]
|
| 68 |
+
elif value.shape[0] == source_hidden_size * 4:
|
| 69 |
+
value = value[:target_hidden_size * 4]
|
| 70 |
+
elif value.shape[0] == source_hidden_size * 6:
|
| 71 |
+
value = value[:target_hidden_size * 6]
|
| 72 |
+
|
| 73 |
+
new_state_dict[key] = value
|
| 74 |
+
|
| 75 |
+
# Move all to CPU and convert to float16
|
| 76 |
+
for key, value in new_state_dict.items():
|
| 77 |
+
new_state_dict[key] = value.cpu().to(torch.float16)
|
| 78 |
+
|
| 79 |
+
# Save the new state dict
|
| 80 |
+
save_file(new_state_dict,
|
| 81 |
+
"/home/jaret/Dev/models/hf/PixArt-Sigma-XL-2-512_MS_t5large_raw/transformer/diffusion_pytorch_model.safetensors",
|
| 82 |
+
metadata=meta)
|
| 83 |
+
|
| 84 |
+
print("Done!")
|