-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild_model.py
More file actions
205 lines (191 loc) · 8.54 KB
/
build_model.py
File metadata and controls
205 lines (191 loc) · 8.54 KB
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import torch
from yacs.config import CfgNode
from comp_graph import CompGraph
from test_networks import BERT, BERTLM, BERTSyntheticDataset
from test_networks import resnet152
def build_model(arch):
if arch == "res152_64":
return build_res152()
elif arch == "bertl_32":
return build_bertl()
elif arch == "swinb_64":
return build_swin("base")
elif arch == "swinl_32":
return build_swin("large")
elif "v2_" in arch and "bertl" in arch:
bs = int(arch.split("v2_")[1])
return build_bertlv2(bs)
elif "_" in arch and "sfpn" in arch:
bs = int(arch.split("_")[1])
return build_sfpn(bs)
elif "_" in arch and "swin" in arch:
level = arch.split("_")[0].split("swin")[1]
bs = int(arch.split("_")[1])
return build_swin_layer(level, bs)
elif "v2_" in arch and "bert" in arch:
lhs, rhs = arch.split("_")
num_bert_layer = int(lhs.split("bert")[1].split("v2")[0])
train_batch_size = int(arch.split("v2_")[1])
return build_bertv2_layer(num_bert_layer, train_batch_size)
elif "_" in arch and "bert" in arch:
lhs, rhs = arch.split("_")
num_bert_layer = int(lhs.split("bert")[1])
train_batch_size = int(rhs)
return build_bert_layer(num_bert_layer, train_batch_size)
def build_res152():
k = 32
res152 = resnet152(num_classes=1000)
inputs = torch.randn(64, 3, 224, 224)
args = (inputs,)
model = res152
comp_graph = CompGraph(model, args, load_path="./misc/comp_graph/res152_64.gpickle")
return comp_graph, k
def build_bertl():
k = 48
train_batch_size = 32
# model definition
vocab_size = 1000
max_seq_length = 128
bertl = BERTLM(BERT(vocab_size, hidden=1024, n_layers=24, attn_heads=16), vocab_size) # BERT-Large
train_dataset = BERTSyntheticDataset(vocab_size, max_seq_length, 1000000)
train_loader = torch.utils.data.DataLoader(train_dataset, sampler=None, batch_size=train_batch_size)
bert_input, bert_label, segment_label, is_next_label = next(iter(train_loader))
args = (bert_input, segment_label)
model = bertl
comp_graph = CompGraph(model, args, load_path="./misc/comp_graph/bertl_%d.gpickle" % train_batch_size)
return comp_graph, k
def build_bertlv2(bs):
k = 48
train_batch_size = bs
# model definition
vocab_size = 30533
max_seq_length = 512
bertl = BERTLM(BERT(vocab_size, hidden=1024, n_layers=24, attn_heads=16), vocab_size) # BERT-Large
train_dataset = BERTSyntheticDataset(vocab_size, max_seq_length, 1000000)
train_loader = torch.utils.data.DataLoader(train_dataset, sampler=None, batch_size=train_batch_size)
bert_input, bert_label, segment_label, is_next_label = next(iter(train_loader))
args = (bert_input, segment_label)
model = bertl
comp_graph = CompGraph(model, args, load_path="./misc/comp_graph/bertlv2_%d.gpickle" % train_batch_size)
return comp_graph, k
def build_bert_layer(num_layer=24, train_batch_size=32):
k = num_layer * 2
# model definition
vocab_size = 1000
max_seq_length = 128
bertl = BERTLM(BERT(vocab_size, hidden=1024, n_layers=num_layer, attn_heads=16), vocab_size) # BERT-Large
train_dataset = BERTSyntheticDataset(vocab_size, max_seq_length, 1000000)
train_loader = torch.utils.data.DataLoader(train_dataset, sampler=None, batch_size=train_batch_size)
bert_input, bert_label, segment_label, is_next_label = next(iter(train_loader))
args = (bert_input, segment_label)
model = bertl
comp_graph = CompGraph(
model, args, load_path="./misc/comp_graph_bert/bert%d_%d.gpickle" % (num_layer, train_batch_size),
prof_dict_path="./misc/comp_graph_bert_profiled/profile_record_%d.pth" % train_batch_size)
return comp_graph, k
def build_bertv2_layer(num_layer=24, train_batch_size=32):
k = num_layer * 2
# model definition
vocab_size = 30533
max_seq_length = 512
bertl = BERTLM(BERT(vocab_size, hidden=1024, n_layers=num_layer, attn_heads=16), vocab_size) # BERT-Large
train_dataset = BERTSyntheticDataset(vocab_size, max_seq_length, 1000000)
train_loader = torch.utils.data.DataLoader(train_dataset, sampler=None, batch_size=train_batch_size)
bert_input, bert_label, segment_label, is_next_label = next(iter(train_loader))
args = (bert_input, segment_label)
model = bertl
comp_graph = CompGraph(
model, args, load_path="./misc/comp_graph_bertv2/bert%dv2_%d.gpickle" % (num_layer, train_batch_size),
prof_dict_path="./misc/comp_graph_bertv2_profiled/profile_record_%d.pth" % train_batch_size)
return comp_graph, k
def build_swin(level="large") -> CompGraph:
k = 32
args = CfgNode()
if level == "base":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_base_patch4_window7_224.yaml"
elif level == "large":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_large_patch4_window7_224_22k.yaml"
args.opts = None
args.output = "output"
args.data_path = ""
args.zip = False
args.batch_size = 32 if level == "large" else 64
args.cache_mode = 'part'
args.pretrained = False
args.resume = False
args.use_checkpoint = False
args.accumulation_steps = 2
args.amp_opt_level = 'O0'
args.eval = False
args.throughput = False
args.tag = None
args.local_rank = 1
from test_networks.Swin_Transformer.config import get_config
from test_networks.Swin_Transformer.models.build import build_model
samples = torch.randn(args.batch_size, 3, 224, 224)
config = get_config(args)
if level == "base":
swinb = build_model(config)
comp_graph = CompGraph(swinb, (samples,), load_path="./misc/comp_graph/swinb_64.gpickle")
elif level == "large":
swinl = build_model(config)
comp_graph = CompGraph(swinl, (samples,), load_path="./misc/comp_graph/swinl_32.gpickle")
return comp_graph, k
def build_swin_layer(level, bs) -> CompGraph:
# We create Swin-Ultra to test the robustness
k = 32 if "l" in level else 48
args = CfgNode()
if level == "l224":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_large_patch4_window7_224_22k.yaml"
elif level == "l384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_large_patch4_window12_384_22k.yaml"
elif level == "u224":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_ultra_patch4_window7_224_22k.yaml"
elif level == "u384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_ultra_patch4_window12_384_22k.yaml"
elif level == "uxl384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_uxl_patch4_window12_384_22k.yaml"
elif level == "tl224":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_testlarge_patch4_window7_224_22k.yaml"
elif level == "tl384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_testlarge_patch4_window12_384_22k.yaml"
elif level == "tu224":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_testultra_patch4_window7_224_22k.yaml"
elif level == "tu384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_testultra_patch4_window12_384_22k.yaml"
elif level == "tuxl384":
args.cfg = "./test_networks/Swin_Transformer/configs/swin_testuxl_patch4_window12_384_22k.yaml"
args.opts = None
args.output = "output"
args.data_path = ""
args.zip = False
args.batch_size = bs
args.cache_mode = 'part'
args.pretrained = False
args.resume = False
args.use_checkpoint = False
args.accumulation_steps = 2
args.amp_opt_level = 'O0'
args.eval = False
args.throughput = False
args.tag = None
args.local_rank = 1
from test_networks.Swin_Transformer.config import get_config
from test_networks.Swin_Transformer.models.build import build_model
if "384" in level:
samples = torch.randn(args.batch_size, 3, 384, 384)
else:
samples = torch.randn(args.batch_size, 3, 224, 224)
config = get_config(args)
swin = build_model(config)
comp_graph = CompGraph(swin, (samples,), load_path="./misc/comp_graph_swin/swin%s_%s.gpickle" % (level, bs),
prof_dict_path="./misc/comp_graph_swin_profiled/swin%s_%s_profiled.pth" % (level, bs))
return comp_graph, k
def build_sfpn(bs):
from test_networks import SemanticFPN
num_classes = 150 # ADE20k
k = 32
inputs = torch.randn(bs, 3, 512, 512)
sfpn = SemanticFPN(num_classes, back_bone="resnet50")
comp_graph = CompGraph(sfpn, (inputs,), load_path="./misc/comp_graph/sfpn_%s.gpickle" % bs)
return comp_graph, k