forked from thunlp/Prompt-Transferability
-
Notifications
You must be signed in to change notification settings - Fork 0
/
activate_neuron_Roberta.py
318 lines (236 loc) · 9.05 KB
/
activate_neuron_Roberta.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
# -*- coding: utf-8 -*-
"""pipeline.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-m2ywJVcfgCHOcEN-4agAbLz7tRGqMvM
"""
'''准备模型和数据'''
'''这里模型就用model这个变量'''
'''数据之后用example作为演示'''
'''使用的时候替换成自己的model就可以了'''
#import numpy as np
import torch
import config
#from activate_neuron.mymodel import *
#import activate_neuron.mymodel as mymodel
#from activate_neuron.utils import *
#import activate_neuron.utils as utils
#from transformers import AutoConfig, AutoModelForMaskedLM
#from model.modelling_roberta import RobertaForMaskedLM
#from reader.reader import init_dataset, init_formatter, init_test_dataset
import argparse
import os
import torch
import logging
import random
import numpy as np
from tools.init_tool import init_all
from config_parser import create_config
from tools.valid_tool import valid
from torch.autograd import Variable
logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
datefmt='%m/%d/%Y %H:%M:%S',
level=logging.INFO)
logger = logging.getLogger(__name__)
def set_random_seed(seed):
"""Set random seed for reproducability."""
if seed is not None and seed > 0:
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
def relu(tmp):
return 1*(tmp > 0)*tmp
def topk(obj, k):
M=-10000
obj = list(obj)[:]
idlist = []
for i in range(k):
idlist.append(obj.index(max(obj)))
obj[obj.index(max(obj))]=M
return idlist
def relu(tmp):
return 1*(tmp > 0)*tmp
def topk(obj, k):
M=-10000
obj = list(obj)[:]
idlist = []
for i in range(k):
idlist.append(obj.index(max(obj)))
obj[obj.index(max(obj))]=M
return idlist
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('--config', '-c', help="specific config file", required=True)
parser.add_argument('--gpu', '-g', help="gpu id list")
parser.add_argument('--local_rank', type=int, help='local rank', default=-1)
parser.add_argument('--do_test', help="do test while training or not", action="store_true")
parser.add_argument('--checkpoint', help="checkpoint file path", type=str, default=None)
parser.add_argument('--comment', help="checkpoint file path", default=None)
parser.add_argument("--seed", type=int, default=None)
parser.add_argument("--prompt_emb_output", type=bool, default=False)
parser.add_argument("--save_name", type=str, default=None)
parser.add_argument("--replacing_prompt", type=str, default=None)
parser.add_argument("--pre_train_mlm", default=False, action='store_true')
parser.add_argument("--task_transfer_projector", default=False, action='store_true')
parser.add_argument("--model_transfer_projector", default=False, action='store_true')
parser.add_argument("--activate_neuron", default=True, action='store_true')
parser.add_argument("--mode", type=str, default="valid")
parser.add_argument("--projector", type=str, default=None)
args = parser.parse_args()
configFilePath = args.config
config = create_config(configFilePath)
use_gpu = True
gpu_list = []
if args.gpu is None:
use_gpu = False
else:
use_gpu = True
os.environ["CUDA_VISIBLE_DEVICES"] = args.gpu
device_list = args.gpu.split(",")
for a in range(0, len(device_list)):
gpu_list.append(int(a))
os.system("clear")
config.set('distributed', 'local_rank', args.local_rank)
config.set("distributed", "use", False)
if config.getboolean("distributed", "use") and len(gpu_list)>1:
torch.cuda.set_device(gpu_list[args.local_rank])
torch.distributed.init_process_group(backend=config.get("distributed", "backend"))
config.set('distributed', 'gpu_num', len(gpu_list))
cuda = torch.cuda.is_available()
logger.info("CUDA available: %s" % str(cuda))
if not cuda and len(gpu_list) > 0:
logger.error("CUDA is not available but specific gpu id")
raise NotImplementedError
set_random_seed(args.seed)
########
'''
formatter = "mlmPrompt"
config.set("data","train_formatter_type",formatter)
config.set("data","valid_formatter_type",formatter)
config.set("data","test_formatter_type",formatter)
config.set("model","model_name","mlmPrompt")
'''
########
parameters = init_all(config, gpu_list, args.checkpoint, args.mode, local_rank = args.local_rank, args=args)
do_test = False
model = parameters["model"]
valid_dataset = parameters["valid_dataset"]
##########################
##########################
'''准备hook'''
'''这是提取特征的代码'''
outputs=[[] for _ in range(12)]
def save_ppt_outputs1_hook(n):
def fn(_,__,output):
outputs[n].append(output.detach().to("cpu"))
#outputs[n].append(output.detach())
return fn
for n in range(12):
#这里面提取feature的模组可以改变,这里因为我自定义模型的原因要两层roberta
#for l in model.state_dict().keys():
# print(l)
#print("====")
#exit()
model.encoder.roberta.encoder.layer[n].intermediate.register_forward_hook(save_ppt_outputs1_hook(n))
'''将数据通过模型'''
'''hook会自动将中间层的激活储存在outputs中'''
model.eval()
valid(model, parameters["valid_dataset"], 1, None, config, gpu_list, parameters["output_function"], mode=args.mode, args=args)
#################################################
#################################################
#################################################
'''
print(len(outputs)) #12
print(len(outputs[0])) #17 epoch
print(len(outputs[0][0])) #64
print(len(outputs[0][0][0])) #231
print(len(outputs[0][0][0][0])) #3072
#outputs[][][][][] , layer:12, epoch:17, batch_size:64, input_length:231, neuron:3072
'''
#merge 17 epoch
for k in range(12):
#outputs[k] = relu(np.concatenate(outputs[k]))
#outputs[k] = torch.relu(torch.cat(outputs[k]))
outputs[k] = torch.cat(outputs[k])
'''
print(len(outputs)) #12
print(len(outputs[0])) #17 epoch
print(len(outputs[0][0])) #64
print(len(outputs[0][0][0])) #231
print(len(outputs[0][0][0][0])) #3072
#outputs[][][][][] , layer:12, epoch:17, batch_size:64, input_length:231, neuron:3072
'''
'''这部分是根据论文里的代码找到某个neuron的最大激活'''
'''
#划定层数
#layer = np.random.randint(12)
layer = torch.randint(1,12,(1,))
#决定neuron
#neuron = np.random.randint(3072)
neuron = torch.randint(1,3072,(1,))
#这里面是得到了某层的某个neuron的所有激活
neuron_activation = outputs[layer][:,:,neuron]
max_activation = [neuron_activation[i,:length[i]].max() for i in range(size)]
print(neuron_activation)
print(max_activation)
exit()
'''
#Activated neuron for a task-specific prompt
outputs = torch.stack(outputs)
#print(outputs.shape)
#exit()
#outputs = outputs[11:,:,:1,:]
#outputs = outputs[11:,:,:100,:]
#outputs = outputs[:,:,:1,:] #12 layers, [mask]
#print(outputs)
#print(outputs.shape)
#exit()
outputs = outputs[:,:,:1,:] #12 layers, [mask]
#outputs = outputs[:,:,:100,:] #12 layers, [mask]+[promot]
#outputs = outputs[11:,:,:100,:]
print(outputs.shape)
# [12, 128, 231, 3072] --> 12, 128(eval batcch size), 231(1 or 100), 3072
#exit()
save_name = args.replacing_prompt.strip().split("/")[-1].split(".")[0]
dir = "task_activated_neuron"
if os.path.isdir(dir):
save_dir = dir+"/"+save_name
if os.path.isdir(save_dir):
torch.save(outputs,save_dir+"/task_activated_neuron")
else:
os.mkdir(save_dir)
torch.save(outputs,save_dir+"/task_activated_neuron")
else:
os.mkdir(dir)
save_dir = dir+"/"+save_name
os.mkdir(save_dir)
torch.save(outputs,save_dir+"/task_activated_neuron")
print("==Prompt emb==")
print(outputs.shape)
print("Save Done")
print("==============")
'''
size = 8 # number of the sentences
length = 231 #sentence length
#Activated neuron for a task-specific prompt
for layer in range(1,12):
for neuron in range(1,3072):
neuron_activation = outputs[layer][:,:,neuron]
print(outputs[layer].shape)
print(neuron_activation.shape)
exit()
max_activation = [neuron_activation[i,:length[i]].max() for i in range(size)]
print(neuron_activation)
print("------------")
print(max_activation)
print("============")
exit()
'''
'''选择头几个句子展示'''
'''
N = 4
indexes = topk(max_activation,N)
for ids in indexes:
print(tokenizer.decode(example['input_ids'][ids,:length[ids]]))
'''