forked from VITA-Group/BERT-Tickets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoneshot.py
164 lines (124 loc) · 5.49 KB
/
oneshot.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
import argparse
from transformers import BertForMaskedLM, BertForSequenceClassification, BertForQuestionAnswering
from transformers import BertConfig
import torch.nn.utils.prune as prune
import numpy as np
import torch
parser = argparse.ArgumentParser(description='PyTorch Cifar10 Training')
parser.add_argument('--weight', default='pre', type=str, help='file_dir')
parser.add_argument('--model', default='glue', type=str, help='file_dir')
parser.add_argument('--rate', default=0.2, type=float, help='rate')
args = parser.parse_args()
def pruning_model(model,px):
parameters_to_prune =[]
for ii in range(12):
parameters_to_prune.append((model.bert.encoder.layer[ii].attention.self.query, 'weight'))
parameters_to_prune.append((model.bert.encoder.layer[ii].attention.self.key, 'weight'))
parameters_to_prune.append((model.bert.encoder.layer[ii].attention.self.value, 'weight'))
parameters_to_prune.append((model.bert.encoder.layer[ii].attention.output.dense, 'weight'))
parameters_to_prune.append((model.bert.encoder.layer[ii].intermediate.dense, 'weight'))
parameters_to_prune.append((model.bert.encoder.layer[ii].output.dense, 'weight'))
parameters_to_prune.append((model.bert.pooler.dense, 'weight'))
parameters_to_prune = tuple(parameters_to_prune)
prune.global_unstructured(
parameters_to_prune,
pruning_method=prune.L1Unstructured,
amount=px,
)
def see_weight_rate(model):
sum_list = 0
zero_sum = 0
for ii in range(12):
sum_list = sum_list+float(model.bert.encoder.layer[ii].attention.self.query.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].attention.self.query.weight == 0))
sum_list = sum_list+float(model.bert.encoder.layer[ii].attention.self.key.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].attention.self.key.weight == 0))
sum_list = sum_list+float(model.bert.encoder.layer[ii].attention.self.value.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].attention.self.value.weight == 0))
sum_list = sum_list+float(model.bert.encoder.layer[ii].attention.output.dense.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].attention.output.dense.weight == 0))
sum_list = sum_list+float(model.bert.encoder.layer[ii].intermediate.dense.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].intermediate.dense.weight == 0))
sum_list = sum_list+float(model.bert.encoder.layer[ii].output.dense.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.encoder.layer[ii].output.dense.weight == 0))
sum_list = sum_list+float(model.bert.pooler.dense.weight.nelement())
zero_sum = zero_sum+float(torch.sum(model.bert.pooler.dense.weight == 0))
return 100*zero_sum/sum_list
config = BertConfig.from_pretrained(
'bert-base-uncased'
)
if args.model == 'glue':
if args.weight == 'rand':
print('random')
model = BertForSequenceClassification(config=config)
output = 'random_prun/'
elif args.weight == 'pre':
model = BertForSequenceClassification.from_pretrained(
'bert-base-uncased',
from_tf=bool(".ckpt" in 'bert-base-uncased'),
config=config
)
output = 'pretrain_prun/'
pruning_model(model, args.rate)
zero = see_weight_rate(model)
print('zero rate', zero)
mask_dict = {}
weight_dict = {}
model_dict = model.state_dict()
for key in model_dict.keys():
if 'mask' in key:
mask_dict[key] = model_dict[key]
else:
weight_dict[key] = model_dict[key]
torch.save(mask_dict, output+'mask.pt')
torch.save(weight_dict, output+'weight.pt')
elif args.model == 'squad':
if args.weight == 'rand':
print('random')
model = BertForQuestionAnswering(config=config)
output = 'random_prun/'
elif args.weight == 'pre':
model = BertForQuestionAnswering.from_pretrained(
'bert-base-uncased',
from_tf=bool(".ckpt" in 'bert-base-uncased'),
config=config
)
output = 'pretrain_prun/'
pruning_model(model, args.rate)
zero = see_weight_rate(model)
print('zero rate', zero)
mask_dict = {}
weight_dict = {}
model_dict = model.state_dict()
for key in model_dict.keys():
if 'mask' in key:
mask_dict[key] = model_dict[key]
else:
weight_dict[key] = model_dict[key]
torch.save(mask_dict, output+'mask.pt')
torch.save(weight_dict, output+'weight.pt')
elif args.model == 'pretrain':
if args.weight == 'rand':
print('random')
model = BertForMaskedLM(config=config)
output = 'random_prun/'
elif args.weight == 'pre':
model = BertForMaskedLM.from_pretrained(
'bert-base-uncased',
from_tf=bool(".ckpt" in 'bert-base-uncased'),
config=config
)
output = 'pretrain_prun/'
pruning_model(model, args.rate)
zero = see_weight_rate(model)
print('zero rate', zero)
mask_dict = {}
weight_dict = {}
model_dict = model.state_dict()
for key in model_dict.keys():
if 'mask' in key:
mask_dict[key] = model_dict[key]
else:
weight_dict[key] = model_dict[key]
torch.save(mask_dict, output+'mask.pt')
torch.save(weight_dict, output+'weight.pt')