-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexp_swin.py
189 lines (163 loc) · 9.86 KB
/
exp_swin.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
from typing import Callable
import numpy as np
from pathlib import Path
from cost_model import Model
from fitter import FitterPool, ModelFnPool
from util import Viewer, Util
import random,os
random.seed(0)
suffix = "pdf"
algo = "Swin-Transformer"
class Experiment:
def run_experiment(machine_tag, network = None):
mem_dir = "{}/{}/results/mem_results.json".format(algo,machine_tag)
ips_dir = "{}/{}/results/speed_results.json".format(algo,machine_tag)
cnt = 1
ips_archived, mem_archived = False, False
while True:
mem_dir_arc = "{}/{}/results/mem_archive_{}.json".format(algo,machine_tag,cnt)
ips_dir_arc = "{}/{}/results/speed_archive_{}.json".format(algo,machine_tag,cnt)
if (not os.path.exists(mem_dir_arc)) and (not os.path.exists(ips_dir_arc)):
break
cnt += 1
if Path(mem_dir).is_file():
os.rename(mem_dir, mem_dir_arc)
mem_archived = True
if Path(ips_dir).is_file():
os.rename(ips_dir, ips_dir_arc)
ips_archived = True
cmd = '''cd ./Swin-Transformer/{}/ && pwd && python exp_mem_speed_swin.py &&
python exp_mem_speed_swin.py --get_mem'''.format(machine_tag)
ret = os.system(cmd)
if ret!=0:
print("[Error] Failed to run new experiments, restoring experiment data")
if ret!=0 and mem_archived:
if Path(mem_dir).is_file():
os.remove(mem_dir)
os.rename(mem_dir_arc, mem_dir)
if ret!=0 and ips_archived:
if Path(ips_dir).is_file():
os.remove(ips_dir)
os.rename(ips_dir_arc, ips_dir)
return ret
def sample_dict(dic, percentenge):
sample_data = {}
sample_num = max(2, int(len(dic) * percentenge))
remove_num = 3
if len(dic) - sample_num < remove_num:
return dic
sample_keys = random.sample(list(dic.keys())[remove_num:], sample_num)
for k in sample_keys:
sample_data[k] = dic[k]
return sample_data
def plot_helper(cond, mem_dir, ips_dir, offset = None):
mem = Util.load_data(mem_dir, "batch_size", "peak", cond)
for k in mem:
mem[k] /= 1000000000
btime = Util.load_data(ips_dir, "batch_size", "batch_time", cond)
# use only 20% data to fit the model
mem_sample= Experiment.sample_dict(mem, 0.4)
btime_sample= Experiment.sample_dict(btime, 0.4)
mem_model,mem_score,alpha,beta = FitterPool.fit_leastsq_verbose(mem_sample, ModelFnPool.linear)
btime_model,btime_score,gamma,delta = FitterPool.fit_leastsq_verbose(btime_sample, ModelFnPool.linear)
while offset is None and delta<0:
retry += 1
btime_sample= Experiment.sample_dict(btime, 0.4)
btime_model,btime_score,gamma,delta = FitterPool.fit_leastsq_verbose(btime_sample, ModelFnPool.linear)
if retry>3: break
if delta<0 and offset: btime_model,btime_score,gamma,delta = FitterPool.fit_leastsq_verbose_offset(btime_sample, ModelFnPool.linear,offset)
ips_model = lambda bsize: bsize / btime_model(bsize)
# print("[predict mem] ", mem_model(np.array(list(mem.keys()))))
return mem, btime, mem_model, btime_model, ips_model, alpha, beta, gamma, delta, mem_score, btime_score
def do_plot(machine_tag,to_plot):
algo = "Swin-Transformer"
mem_dir = "{}/{}/results/mem_results.json".format(algo,machine_tag)
ips_dir = "{}/{}/results/speed_results.json".format(algo,machine_tag)
result_dir = "graphs/{}/{}/".format(algo,machine_tag)
if not Path(mem_dir).is_file() or not Path(ips_dir).is_file():
print("Error: No experiment data found. Pease run expriment from scratch with --run-new for {}@{}".format(algo,machine_tag))
return
Path(result_dir).mkdir(parents=True, exist_ok=True)
#print("------------------Org---------------")
is_org = lambda obj : obj['algorithm'] == None and obj['fp16'] == "O1" and obj['ckpt'] == False
org_mem, org_btime, org_mem_model, org_btime_model, org_ips_model,\
alpha, beta, gamma, delta, mem_score, btime_score = Experiment.plot_helper(is_org, mem_dir, ips_dir)
offset = delta
print("-----------------{}@{} Params-----------------".format(algo,machine_tag))
print ("{:<8} {:<10} {:<10} {:<10} {:<10} {:<12} {:<12}".\
format('Method','Alpha','Beta','Gamma','Delta','Mem R','Latency R'))
print ("{:<8} {:<10g} {:<10g} {:<10g} {:<10g} {:<12g} {:<12g}".format('Org',alpha,beta,gamma,delta,mem_score,btime_score))
#print("------------------Swap---------------")
is_swap = lambda obj : obj['algorithm'] == "swap" and obj['fp16'] == "O1"
swap_mem, swap_btime, swap_mem_model, swap_btime_model, swap_ips_model,\
alpha, beta, gamma, delta, mem_score, btime_score = Experiment.plot_helper(is_swap, mem_dir, ips_dir, offset)
print ("{:<8} {:<10g} {:<10g} {:<10g} {:<10g} {:<12g} {:<12g}".format('Swap',alpha,beta,gamma,delta,mem_score,btime_score))
#print("------------------Ckpt---------------")
is_ckpt = lambda obj : obj['ckpt'] == True and obj['fp16'] == "O1"
ckpt_mem, ckpt_btime, ckpt_mem_model, ckpt_btime_model, ckpt_ips_model,\
alpha, beta, gamma, delta, mem_score, btime_score = Experiment.plot_helper(is_ckpt, mem_dir, ips_dir, offset)
print ("{:<8} {:<10g} {:<10g} {:<10g} {:<10g} {:<12g} {:<12g}".format('Ckpt',alpha,beta,gamma,delta,mem_score,btime_score))
#print("------------------Quantize---------------")
is_quantize = lambda obj : obj['algorithm'] == "L1" and obj['fp16'] == "O1"
quantize_mem, quantize_btime, quantize_mem_model, quantize_btime_model, quantize_ips_model,\
alpha, beta, gamma, delta, mem_score, btime_score = Experiment.plot_helper(is_quantize, mem_dir, ips_dir, offset)
print ("{:<8} {:<10g} {:<10g} {:<10g} {:<10g} {:<12g} {:<12g}".format('Quantize',alpha,beta,gamma,delta,mem_score,btime_score))
if to_plot:
import matplotlib
# matplotlib.rc('axes',edgecolor='silver')
import matplotlib.pyplot as plt
# plt.style.use(['grid'])
fig, axes = plt.subplots(4, 1, sharex=True)
fig.set_size_inches(4, 6)
# plot batch time
Viewer.plot_fit(axes[0], "org", org_btime_model, np.array(list(org_btime.keys())), np.array(
list(org_btime.values())), None, False)
Viewer.plot_fit(axes[1],"swap", swap_btime_model, np.array(list(swap_btime.keys())), np.array(
list(swap_btime.values())), None, False)
Viewer.plot_fit(axes[2],"ckpt", ckpt_btime_model, np.array(list(ckpt_btime.keys())), np.array(
list(ckpt_btime.values())), None, False)
Viewer.plot_fit(axes[3],"quantize", quantize_btime_model, np.array(list(quantize_btime.keys())), np.array(
list(quantize_btime.values())), None, False)
plt.xlabel("Batch Size")
Util.set_tick_label_size(axes)
# fig.text(-0.02, 0.5, 'Time (s)', va='center', rotation='vertical', size=22)
plt.savefig(result_dir + "swin_batch_time.%s" % suffix, bbox_inches="tight")
plt.close()
# plot memory
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(4, 4)
sample_cnt = 5
x, y= Util.sample_data(list(org_mem.keys()), sample_cnt), Util.sample_data(list(org_mem.values()), sample_cnt)
Viewer.plot_fit(ax, "org", org_mem_model, np.array(x), np.array(y), None, False)
x, y= Util.sample_data(list(swap_mem.keys()), sample_cnt), Util.sample_data(list(swap_mem.values()), sample_cnt)
Viewer.plot_fit(ax, "swap", swap_mem_model, np.array(x), np.array(y), None, False)
x, y= Util.sample_data(list(ckpt_mem.keys()), sample_cnt), Util.sample_data(list(ckpt_mem.values()), sample_cnt)
Viewer.plot_fit(ax, "ckpt", ckpt_mem_model, np.array(x), np.array(y), None, False)
x, y= Util.sample_data(list(quantize_mem.keys()), sample_cnt), Util.sample_data(list(quantize_mem.values()), sample_cnt)
Viewer.plot_fit(ax, "quantize", quantize_mem_model, np.array(x), np.array(y), None, False)
# plt.ylabel("Memory (GB)", size=22)
plt.xlabel("Batch Size")
Util.set_tick_label_size([ax])
plt.yticks(fontsize=15)
plt.xticks(fontsize=15)
plt.savefig(result_dir + "swin_mem.%s" % suffix, bbox_inches="tight")
plt.close()
fig, ax = plt.subplots(1, 1)
fig.set_size_inches(4, 4)
Viewer.plot_fit(ax, "org", org_ips_model, np.array(list(org_btime.keys())), np.array(
[bsize / org_btime[bsize] for bsize in org_btime]), None, False)
Viewer.plot_fit(ax, "swap", swap_ips_model, np.array(list(swap_btime.keys())), np.array(
[bsize / swap_btime[bsize] for bsize in swap_btime]), None, False)
Viewer.plot_fit(ax, "ckpt", ckpt_ips_model, np.array(list(ckpt_btime.keys())), np.array(
[bsize / ckpt_btime[bsize] for bsize in ckpt_btime]), None, False)
Viewer.plot_fit(ax, "quantize", quantize_ips_model, np.array(list(quantize_btime.keys())), np.array(
[bsize / quantize_btime[bsize] for bsize in quantize_btime]), None, False)
ax.set_yticks([20, 40, 60, 80])
plt.ylabel("Throughput (image/s)", size=22)
plt.xlabel("Batch Size", size=22)
# plt.legend(prop={'size': 14})
plt.yticks(fontsize=15)
plt.xticks(fontsize=15)
plt.savefig(result_dir + "swin_ips.%s" % suffix, bbox_inches="tight")
plt.close()
if __name__ == "__main__": Experiment.do_plot("v100",True)