-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfind_memorized_data.py
295 lines (239 loc) · 10.3 KB
/
find_memorized_data.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
import torch
import torch.nn as nn
import numpy as np
from tqdm import tqdm
import pdb
import os
import argparse
import json
from Levenshtein import distance as lev_dist
from nltk.util import ngrams
from utils import *
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def get_batch_data(args, idx, tokenizer):
assert 'pythia' in args.model_name
batch_file = os.path.join(args.data_dir, f'batch{idx}_bs1024.npy')
data = np.load(batch_file)
data = torch.LongTensor(data[:, :args.seq_len]) # truncate
print(data.shape)
return data
@torch.no_grad()
def retest(args, model, tokenizer):
filename = f'pile_bs{args.start}-{args.end}-dedup.pt'
data = torch.load(os.path.join(args.out_dir, filename))
prompts = data[:, :args.prompt_len]
print(data.shape, prompts.shape)
ref_texts = tokenizer.batch_decode(data)
gen_texts = []
for batch in tqdm(prompts.chunk(args.n_batches)):
batch = batch.to(device)
gen_tokens = model.generate(
batch,
pad_token_id=tokenizer.eos_token_id,
do_sample=False,
max_length=args.seq_len,
)
gen_texts.extend(tokenizer.batch_decode(gen_tokens))
for gen_txt, ref_txt in zip(gen_texts, ref_texts):
distance = lev_dist(gen_txt, ref_txt)
print("Lev_dist:", distance)
print(ref_txt)
print('-'*120)
if distance > args.filter_dist:
pdb.set_trace()
@torch.no_grad()
def get_loss(args, idx, data, model, tokenizer):
loss_fct = nn.CrossEntropyLoss(reduction="none")
out_correctness, out_loss = [], []
for batch in tqdm(data.chunk(args.n_batches)):
batch = batch.to(device)
logits = model(batch).logits
shift_logits = logits[..., :-1, :].contiguous()
shift_labels = batch[..., 1:].contiguous()
nll_batch = loss_fct(shift_logits.transpose(1, 2), shift_labels) #[bs, seq_len]
is_correct_batch = (shift_logits.argmax(-1) == shift_labels)
out_correctness.append(is_correct_batch.cpu())
out_loss.append(nll_batch.cpu())
out_correctness = torch.cat(out_correctness)
out_loss = torch.cat(out_loss)
print(out_correctness.float().mean(1))
os.makedirs(args.out_dir, exist_ok=True)
torch.save(out_correctness, os.path.join(args.out_dir, f'correctness_batch{idx}.pt'))
torch.save(out_loss, os.path.join(args.out_dir, f'loss_batch{idx}.pt'))
@torch.no_grad()
def get_memorized_data(args, model, tokenizer):
filename = f"pile_bs{args.start}-{args.end}_{args.seq_len}.jsonl"
f_out = open(os.path.join(args.out_dir, filename), "w")
cnt = 0
memorized_data = []
for idx in tqdm(range(args.start, args.end)):
data = get_batch_data(args, idx, tokenizer)
correctness = torch.load(os.path.join(args.out_dir, f'correctness_batch{idx}.pt'))
#loss = torch.load(os.path.join(args.out_dir, f'loss_batch{idx}.pt'))
correctness = correctness[:, args.prompt_len-1 : ].numpy()
select_ids = np.where(correctness.mean(1) > 0.9)[0] # 1. filter by acc
data = data[select_ids]
ref_texts = tokenizer.batch_decode(data)
prompts = data[:, :args.prompt_len].to(device)
gen_tokens = model.generate(
prompts,
pad_token_id=tokenizer.eos_token_id,
do_sample=False,
max_length=args.seq_len,
).cpu()
gen_texts = tokenizer.batch_decode(gen_tokens)
prompts = prompts.cpu()
for j, prompt, gen_tok, gen_txt, ref_txt, dp in zip(select_ids, prompts, gen_tokens, gen_texts, ref_texts, data):
# 2. filter out "gibberish", which has many repetitive tokens
if len(torch.unique(gen_tok[args.prompt_len:])) < 16 or len(torch.unique(dp[args.prompt_len:])) < 16: continue
distance = lev_dist(gen_txt, ref_txt)
# 3. filter with levenshtein distance after greedy decoding
if distance <= args.filter_dist:
prompt_txt = tokenizer.decode(prompt)
print("Lev dist:", distance)
print(prompt_txt)
print("-"*120)
print(ref_txt)
print("-"*120)
print(gen_txt)
print("="*120)
line = {
'ex_i': len(memorized_data),
'prompt': prompt_txt,
'ref': ref_txt,
'gen': gen_txt,
'levenshtein_distance': distance,
'pile_idx': f'bs{idx}_{j}'
}
f_out.write(json.dumps(line)+"\n")
f_out.flush()
memorized_data.append(dp)
cnt += 1
f_out.close()
print("# Memorized data after filtering:", cnt)
memorized_data = torch.stack(memorized_data)
filename = f'pile_bs{args.start}-{args.end}_{args.prompt_len}_{args.seq_len}.pt'
torch.save(memorized_data, os.path.join(args.out_dir, filename))
def aggressive_dedup(args, tokenizer):
def jaccard_similarity(set1, set2):
return len(set1.intersection(set2)) / len(set1.union(set2))
def bfs(s, c_id):
visited[s] = True
que = []
que.append(s)
while len(que) > 0:
u = que.pop(0)
components[u] = c_id
connected_nodes = np.where(scores[u] > 0.05)[0]
for i in connected_nodes:
if not visited[i]:
visited[i] = True
que.append(i)
# load tokenized data; get n-gram
data_tokens = torch.load(os.path.join(args.out_dir,
f'pile_bs{args.start}-{args.end}_{args.prompt_len}_{args.seq_len}.pt')).numpy()
data_ngrams = [set(ngrams(dp, 5)) for dp in data_tokens]
with open(os.path.join(args.out_dir, f'pile_bs{args.start}-{args.end}_{args.seq_len}.jsonl'), 'r') as f:
docs, dists = [], []
for line in f:
dp = json.loads(line)
docs.append(dp['ref'])
dists.append(dp['levenshtein_distance'])
dists = np.array(dists)
# calculate jaccard_similarity in n-gram
n_data = len(docs)
scores = np.zeros((n_data, n_data))
for i in range(n_data):
for j in range(i+1, n_data):
doc1 = data_ngrams[i]
doc2 = data_ngrams[j]
scores[i,j] = scores[j,i] = jaccard_similarity(doc1, doc2)
if args.debug and 0.05 < scores[i,j] < 0.1:
print(scores[i,j])
print('-'*100)
print(docs[i])
print('-'*100)
print(docs[j])
print('='*100)
overlaps = doc1.intersection(doc2)
for ng in overlaps:
print(tokenizer.decode(list(ng)))
print('='*100)
pdb.set_trace()
# find connected components
# two data are "connected"/near-duplicate if their similarity score is large
visited = np.zeros(n_data, dtype=bool)
components = np.zeros(n_data, dtype=int)
idx = 0
for i in range(n_data):
if not visited[i]:
bfs(i, idx)
idx += 1
assert components.max() == idx-1
print("# data remain:", idx)
# dedup; only keep the dp with lowest lev_distance in each connected component
deduped_data, deduped_dists = [], []
for i in range(idx):
ids = np.where(components == i)[0]
j = ids[dists[ids].argmin()]
deduped_data.append(data_tokens[j])
deduped_dists.append(dists[j])
if len(ids) > 1 and args.debug:
print('# data in this group:', len(ids))
print(docs[j])
print('-'*100)
print(docs[ids[-1]])
print('='*100)
pdb.set_trace()
deduped_data = torch.tensor(np.stack(deduped_data))
deduped_dists = torch.tensor(deduped_dists)
# get more data then we need # discard those with largest lev_distance
if args.n_data_clip < len(deduped_data):
_, indices = torch.topk(deduped_dists, args.n_data_clip, largest=False)
deduped_data = deduped_data[indices]
# randomly sample 5 dev examples and put them in the front of the tensor
np.random.seed(0)
dev_ids = np.random.choice(len(deduped_data), 5, replace=False)
print("Dev IDs:", dev_ids)
for i,j in enumerate(dev_ids):
deduped_data[i], deduped_data[j] = deduped_data[j].clone(), deduped_data[i].clone()
torch.save(deduped_data, os.path.join(args.out_dir, f'pile_bs{args.start}-{args.end}-dedup.pt'))
if args.log:
texts = tokenizer.batch_decode(deduped_data)
for txt in texts:
print(txt)
print('-'*100)
def load_pretrained_(model_name, load_model=True, to_gpu=True):
tokenizer = AutoTokenizer.from_pretrained(model_name)
tokenizer.pad_token = tokenizer.eos_token
if load_model:
model = AutoModelForCausalLM.from_pretrained(model_name)
if to_gpu: model.to(device)
else: model = None
return tokenizer, model
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--data_dir", type=str, default="mydata")
parser.add_argument("--model_name", type=str, default="EleutherAI/pythia-2.8b-deduped-v0")
parser.add_argument("--start", type=int, default=0, help="strat batch idx")
parser.add_argument("--end", type=int, default=10, help="end batch ids")
parser.add_argument("--n_batches", type=int, default=512)
parser.add_argument("--n_data_clip", type=int, default=505)
parser.add_argument("--seq_len", type=int, default=80)
parser.add_argument("--prompt_len", type=int, default=32)
parser.add_argument("--filter_dist", type=int, default=20)
parser.add_argument("--do_loss", action="store_true")
parser.add_argument("--debug", action="store_true")
parser.add_argument("--log", action="store_true")
args = parser.parse_args()
args.out_dir = os.path.join(args.data_dir, args.model_name)
tokenizer, model = load_pretrained_(args.model_name, load_model=True)
if model is not None: model.eval()
if args.do_loss:
for idx in range(args.start, args.end):
data = get_batch_data(args, idx, tokenizer)
get_loss(args, idx, data, model, tokenizer)
else:
get_memorized_data(args, model, tokenizer)
aggressive_dedup(args, tokenizer)
#retest(args, model, tokenizer)