-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathtest.py
131 lines (105 loc) · 4.17 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import torch
from download import find_model
from models import DiT_models
from thop import profile
from diffusion import create_diffusion
def calculate_params_and_flops():
image_size = 256
model = "DiT-S/2"
num_classes = 1000
device = "cuda"
ckpt_path = None # "results/002-DiT-S-2/checkpoints/1350000.pt"
latent_size = image_size // 8
model = DiT_models[model](
input_size=latent_size,
num_classes=num_classes,
num_experts=8,
num_experts_per_tok=2,
).to(device).half()
if ckpt_path is not None:
state_dict = find_model(ckpt_path)
model.load_state_dict(state_dict)
model.eval()
print(f"DiT Parameters: {sum(p.numel() for p in model.parameters()):,}")
x = torch.randn(1, 4, 32, 32).cuda().half()
t = torch.randint(1, 1000, (1,)).cuda().half()
y = torch.randint(1, 1000, (1,)).cuda()
# Note that thop library may does not support dynamic computation graph(i.e for and if in moe code) in pytorch
# we update the following calculation from: aicfw.li@gmail.com
with torch.no_grad():
flops, _ = profile(model, inputs=(x, t, y))
print('FLOPs = ' + str(flops * 2/1000**3) + 'G')
from torchprofile import profile_macs
flops2 = profile_macs(model, (x, t, y))
print(f'FLOPS by torchprofile {flops2/1e9:.2f}G')
from calflops import calculate_flops
flops4, macs4, params4 = calculate_flops(model, kwargs={'x': x, 't': t, 'y': y}, print_results=False)
print(f"FLOPs by calflops: {flops4}")
print(f'MACs by calflops: {macs4}, Params by calflops: {params4}')
def image_class_expert_ratio():
import os
import json
from models import selected_ids_list
image_size = 256
model = "DiT-S/2"
num_classes = 1000
device = "cuda"
ckpt_path = "results/002-DiT-S-2/checkpoints/ckpt.pt"
num_sampling_steps = 250
cfg_scale = 4.0
every_class_sample = 50
torch.manual_seed(1234)
torch.set_grad_enabled(False)
latent_size = image_size // 8
model = DiT_models[model](
input_size=latent_size,
num_classes=num_classes,
num_experts=8,
num_experts_per_tok=2,
).to(device)
if ckpt_path is not None:
print('load from: ', ckpt_path)
state_dict = find_model(ckpt_path)
model.load_state_dict(state_dict)
model.eval()
diffusion = create_diffusion(str(num_sampling_steps))
for i in range(1000):
experts_ids = []
for j in range(every_class_sample):
class_labels = [i]
# Create sampling noise:
n = len(class_labels)
z = torch.randn(n, 4, latent_size, latent_size, device=device)
y = torch.tensor(class_labels, device=device)
# Setup classifier-free guidance:
z = torch.cat([z, z], 0)
y_null = torch.tensor([1000] * n, device=device)
y = torch.cat([y, y_null], 0)
model_kwargs = dict(y=y, cfg_scale=cfg_scale)
# Sample images:
samples = diffusion.p_sample_loop(
model.forward_with_cfg, z.shape, z, clip_denoised=False, model_kwargs=model_kwargs, progress=True, device=device
)
print(i, j)
print(len(selected_ids_list), len(selected_ids_list[0]), len(selected_ids_list[0][0]))
tmp_ids_list = selected_ids_list[-3000:]
print(len(tmp_ids_list), len(tmp_ids_list[0]), len(tmp_ids_list[0][0]))
print(tmp_ids_list[0][0])
experts_ids.append(tmp_ids_list)
#break
#continue
print(len(experts_ids))
tgt_path = os.path.join('experts', str(i)+'.json')
with open(tgt_path, 'w') as f:
json.dump(experts_ids, f,)
def ckpts_clean():
# only save ema ckpts for ckpt uploading
ckpt_path = 'results/003-DiT-B-2/checkpoints/ckpt2.pt'
checkpoint = torch.load(ckpt_path, map_location=lambda storage, loc: storage)
new_checkpoint = {
"ema": checkpoint['ema'],
}
torch.save(new_checkpoint, 'dit_moe_b_8E2A.pt')
# image_class_expert_ratio()
calculate_params_and_flops()
# ckpts_clean()