-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcausal_distribution_previous.py
211 lines (177 loc) · 8.57 KB
/
causal_distribution_previous.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
import os
import json
import torch
from args import DeepArgs
from utils import set_gpu,get_datasets,generate_figure
from transformers import HfArgumentParser,AutoTokenizer,GPT2LMHeadModel
from circuit_model import trunk_model,assert_model,attention_flag_model
import logging
from tqdm import tqdm
import copy
from demo_representation_vocb import assert_circuits_equal_output
import matplotlib.pyplot as plt
import numpy as np
from plot import plot_cka_matrix,annotate_heatmap
from sklearn.cluster import KMeans
from matplotlib import pyplot
import shutil
import seaborn as sns
import torch.nn.functional as F
# specify the directory you want to read files from
directory = 'json_logs/token_by_token/gpt2xl/all_mix_cluster1_cluster0'
filter_weight=0.6
circuit_layer=29
circuit_num=12*29
hf_parser = HfArgumentParser((DeepArgs,))
args: DeepArgs = hf_parser.parse_args_into_dataclasses()[0]
torch.cuda.empty_cache()
set_gpu(args.gpu)
def get_logger(filename, verbosity=1, name=None):
level_dict = {0: logging.DEBUG, 1: logging.INFO, 2: logging.WARNING}
formatter = logging.Formatter(
"[%(asctime)s][%(filename)s][line:%(lineno)d][%(levelname)s] %(message)s"
)
logger = logging.getLogger(name)
logger.setLevel(level_dict[verbosity])
fh = logging.FileHandler(filename, "w")
fh.setFormatter(formatter)
logger.addHandler(fh)
# sh = logging.StreamHandler()
# sh.setFormatter(formatter)
# logger.addHandler(sh)
return logger
if args.task_name=='language_skill':
tokenizer = AutoTokenizer.from_pretrained("openai-community/gpt2")
check_model=assert_model(args)
orig_model = GPT2LMHeadModel.from_pretrained("openai-community/gpt2")
attention_flag=attention_flag_model(args)
attention_flag_metric_default=torch.ones(circuit_num,circuit_num)
attention_flage_metric_mask=torch.zeros(circuit_num,circuit_num)
for i in range(circuit_num):
for j in range(circuit_num):
if (i+1)%29==27 or (i+1)%29==28 or (i+1)%29==0 or (i+1)%29==1 or (i+1)%29==14 or (j+1)%29==27 or (j+1)%29==28 or (j+1)%29==0 or (j+1)%29==1:
attention_flag_metric_default[i][j]=0
# iterate over every file in the directory
negative_metrix=torch.zeros((circuit_num,circuit_num))
nagetive_num=0
positive_metrix=torch.zeros((circuit_num,circuit_num))
positive_num=0
self_metrix=torch.zeros((circuit_num,circuit_num))
self_num=0
for filename in tqdm(os.listdir(directory)):
# check if the file is a JSON file
if filename.endswith('.json'):
fullpath = os.path.join(directory, filename)
# open the JSON file
with open(fullpath) as f:
data = json.load(f)
input_text=filename.split('.json')[0]
token_length=len(data)
if token_length<3:
os.remove(fullpath)
continue
inputs = tokenizer(input_text, return_tensors="pt")
input_ids_ori=copy.deepcopy(inputs['input_ids'])
if inputs['input_ids'].size()[-1]<2:
os.remove(fullpath)
attention_mask_ori=copy.deepcopy(inputs['attention_mask'])
r_token=1
s_token=0
for t in range(token_length):
token_circuit_record=data[t]
if t==2:
token_circuit_record=data[-1]
refined_matrix=torch.zeros((circuit_num,circuit_num))
assert_refined_matrix=torch.zeros((circuit_num,circuit_num))
for cn in range(circuit_layer,circuit_num):
circuit_one_listtype=token_circuit_record[cn-29]['layer {} and circuit {}'.format(cn//circuit_layer,cn%circuit_layer)]
assert_refined_matrix[cn]=torch.IntTensor(circuit_one_listtype)
circuit_one_satisfiability=torch.IntTensor(circuit_one_listtype).split(29,dim=-1)
for layer in range(cn//circuit_layer):
reverse_circuit=1-circuit_one_satisfiability[layer]
circuit_one_listtype[layer*29:(layer+1)*29]=reverse_circuit
refined_matrix[cn]=torch.IntTensor(circuit_one_listtype)
if t<2:
inputs['input_ids']=input_ids_ori[:,:t+1]
inputs['attention_mask']=attention_mask_ori[:,:t+1]
if t==2:
inputs['input_ids']=input_ids_ori[:,-1].unsqueeze(0)
inputs['attention_mask']=attention_mask_ori[:,-1]
with torch.no_grad():
#check_model(inputs,label_ids,assert_refined_matrix)
if t>2:
break
attention_flag_metric=attention_flag_metric_default
refined_matrix=torch.where(attention_flag_metric>0,refined_matrix,attention_flag_metric)
#print(refined_matrix[29][:29])
if t==0:
negative_metrix=negative_metrix+refined_matrix
nagetive_num+=1
elif t==1:
positive_metrix=positive_metrix+refined_matrix
positive_num+=1
else:
self_metrix=self_metrix+refined_matrix
self_num+=1
negative_metrix=negative_metrix/nagetive_num
negative_metrix=torch.where(attention_flag_metric_default>0,negative_metrix,0)
negative_metrix=negative_metrix.view(-1).unsqueeze(0)
positive_metrix=positive_metrix/positive_num
positive_metrix=torch.where(attention_flag_metric_default>0,positive_metrix,0)
positive_metrix=positive_metrix.view(-1).unsqueeze(0)
self_metrix=self_metrix/self_num
self_metrix=torch.where(attention_flag_metric_default>0,self_metrix,0)
self_metrix=self_metrix.view(-1).unsqueeze(0)
sample_all=torch.cat((negative_metrix,positive_metrix,self_metrix),dim=0)
sample_all=sample_all.permute(1,0)
cut_num=0
for i in range(negative_metrix.size()[1]):
if sample_all[i-cut_num][0]==sample_all[i-cut_num][1]==sample_all[i-cut_num][2]==0 or sample_all[i-cut_num][0]==sample_all[i-cut_num][1]==0 or sample_all[i-cut_num][2]==sample_all[i-cut_num][1]==0:
arr1=sample_all[0:i-cut_num]
arr2=sample_all[i-cut_num+1:]
sample_all=torch.cat((arr1,arr2),dim=0)
cut_num+=1
sample04=copy.deepcopy(sample_all)
cut_num=0
for i in range(sample_all.size()[0]):
if sample04[i-cut_num][1]<0.4:
arr1=sample04[0:i-cut_num]
arr2=sample04[i-cut_num+1:]
sample04=torch.cat((arr1,arr2),dim=0)
cut_num+=1
sample05=copy.deepcopy(sample_all)
cut_num=0
for i in range(sample_all.size()[0]):
if sample05[i-cut_num][1]<0.5:
arr1=sample05[0:i-cut_num]
arr2=sample05[i-cut_num+1:]
sample05=torch.cat((arr1,arr2),dim=0)
cut_num+=1
sample06=copy.deepcopy(sample_all)
cut_num=0
for i in range(sample_all.size()[0]):
if sample06[i-cut_num][1]<0.6:
arr1=sample06[0:i-cut_num]
arr2=sample06[i-cut_num+1:]
sample06=torch.cat((arr1,arr2),dim=0)
cut_num+=1
sample07=copy.deepcopy(sample_all)
cut_num=0
for i in range(sample_all.size()[0]):
if sample07[i-cut_num][1]<0.7:
arr1=sample07[0:i-cut_num]
arr2=sample07[i-cut_num+1:]
sample07=torch.cat((arr1,arr2),dim=0)
cut_num+=1
#sample_all=F.softmax(sample_all,dim=0)
sample04=sample04.numpy()
sample05=sample05.numpy()
sample06=sample06.numpy()
sample07=sample07.numpy()
#fig, ax1 = plt.subplots(3,1,sharex=True)
sns.kdeplot(x=sample04[:,1],y=sample04[:,0],fill=True, cmap='Oranges',levels=[0.35,0.5,0.6,0.7,0.8,0.9,1],shade_lowest=False)
sns.kdeplot(x=sample05[:,1],y=sample05[:,0],fill=True, cmap='Reds',levels=[0.35,0.5,0.6,0.7,0.8,0.9,1],shade_lowest=False)
sns.kdeplot(x=sample06[:,1],y=sample06[:,0],fill=True, cmap='Greens',levels=[0.35,0.5,0.6,0.7,0.8,0.9,1],shade_lowest=False)
sns.kdeplot(x=sample07[:,1],y=sample07[:,0],fill=True, cmap='Blues',levels=[0.35,0.5,0.6,0.7,0.8,0.9,1],shade_lowest=False,cbar=True)
plt.legend()
plt.savefig('paper_figure/previous_token_distribution.jpg')