-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_model_sizes.py
66 lines (52 loc) · 1.96 KB
/
get_model_sizes.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
# determine sizes of models bert, bert+adapter, distilbert, distilbert+adapter
from transformers import DistilBertForSequenceClassification, BertForSequenceClassification, AutoModelWithHeads
import os
import shutil
num_labels=64
folder="/tmp/models/"
if os.path.exists(folder):
shutil.rmtree(folder)
os.mkdir(folder)
def get_n_params(model):
pp=0
for p in list(model.parameters()):
nn=1
for s in list(p.size()):
nn = nn*s
pp += nn
return pp
def get_size(model, suffix):
name=type(model).__name__.lower()
model_folder=os.path.join(folder, name + suffix)
os.mkdir(model_folder)
model_file=os.path.join(model_folder, "model.bin")
torch.save(model.state_dict(), model_file)
folder_size=os.path.getsize(model_file)
folder_size*=pow(10,-6)
number_of_parameters=get_n_params(model)
return folder_size, number_of_parameters
for model_name in ["distilbert-base-uncased", "bert-base-uncased"]:
# load vanilla model
model = AutoModelWithHeads.from_pretrained(model_name, num_labels=num_labels)
size_without_adapter, n_parameters_without_adapter = get_size(model, "_pre")
print()
print("-"*20)
print(f"model {model_name}")
print(f"size without adapter {size_without_adapter} MB, {n_parameters_without_adapter:,} parameters.")
# add adapter
labels=[str(x) for x in range(num_labels)]
label_to_id={}
for label in labels:
label_to_id[label]=len(label_to_id)
model.add_adapter("modular_chatbot")
model.add_classification_head(
"modular_chatbot",
num_labels=num_labels,
id2label=label_to_id
)
model.train_adapter("modular_chatbot")
size_with_adapter, n_parameters_with_adapter = get_size(model, "_post")
print(f"size with adapter {size_with_adapter} MB, {n_parameters_with_adapter:,} parameters.")
size_adapter=size_with_adapter-size_without_adapter
print(f"size of adapter {size_adapter} MB")
print("-"*20)