-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
368 lines (301 loc) · 12.7 KB
/
utils.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This module contains some utility functions.
"""
import json
import logging
import os
import random
import sys
from typing import List, Optional, Dict, Union, Tuple
from transformers import GPT2Tokenizer
import pandas as pd
from datasets import Dataset
from influence_utils import experiments
import numpy as np
import torch
PLACEHOLDER_C = "<C>"
PLACEHOLDER_X = "<X>"
PLACEHOLDER_Y = "<Y>"
PLACEHOLDER_EXAMPLE = "<E>"
C_KEY = 'C'
X_KEY = 'X'
Y_KEY = 'Y'
E_KEY = 'E'
PROMPT_KEY = 'Prompt'
def init_logging(log_file, stdout=False):
formatter = logging.Formatter('%(asctime)s [%(levelname)s] %(module)s: %(message)s',
datefmt='%m/%d/%Y %H:%M:%S')
print('Making log output file: %s' % log_file)
print(log_file[: log_file.rfind(os.sep)])
if not os.path.exists(log_file[: log_file.rfind(os.sep)]):
os.makedirs(log_file[: log_file.rfind(os.sep)])
fh = logging.FileHandler(log_file)
fh.setFormatter(formatter)
fh.setLevel(logging.INFO)
logger = logging.getLogger()
logger.addHandler(fh)
logger.setLevel(logging.INFO)
if stdout:
ch = logging.StreamHandler(sys.stdout)
ch.setFormatter(formatter)
ch.setLevel(logging.INFO)
logger.addHandler(ch)
return logger
def set_seed(seed: int) -> None:
"""Set RNG seeds for python's `random` module, numpy and torch"""
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
if torch.cuda.is_available():
torch.cuda.manual_seed_all(seed)
def save_jsonl(entries: List[Dict], path: str):
with open(path, 'w', encoding='utf8') as fh:
for entry in entries:
fh.write(f'{json.dumps(entry)}\n')
def read_jsonl(path: str) -> List[Dict]:
pairs = []
with open(path, 'r', encoding='utf8') as fh:
for line in fh:
pairs.append(json.loads(line))
return pairs
class IncontextSampler():
def __init__(self, in_context_num=0, same_y=False, mix_y=False, order_type=1, keep_mapping=True, same_c=False,
labels=None):
self.same_c = same_c
self.same_y = same_y
self.mix_y = mix_y
self.order_type = order_type
self.in_context_num = in_context_num
self.keep_mapping = keep_mapping
self.labels = labels
self.pool = None
def update_pool(self, dataset: List[Dict]):
pool = pd.DataFrame(dataset)
idx2name = {i: n for i, n in enumerate(pool.columns)}
def _to_dict(g):
res = []
for item in g.values.tolist():
res.append({idx2name[i]: v for i, v in enumerate(item) if i in idx2name})
return res
group_by = []
if self.same_c:
group_by.append(C_KEY)
if self.same_y or self.mix_y:
group_by.append(Y_KEY)
if len(group_by) > 0:
self.pool = pool.groupby(group_by).apply(_to_dict).to_dict()
else:
self.pool = pool.to_dict('records')
@property
def avaliable(self):
return self.pool is not None and self.in_context_num > 0
@property
def size(self):
def _size(dict_or_list):
if isinstance(dict_or_list, list):
return len(dict_or_list)
size = {}
for k, v in dict_or_list.items():
size[k] = _size(v)
return size
return 0 if self.pool is None else _size(self.pool)
def _sample(self, x, pool, in_context_num) -> List[Dict]:
"""select in_context_num examples from pool"""
if len(pool) <= in_context_num:
examples = pool
np.random.shuffle(examples)
else:
examples = np.random.choice(pool, size=in_context_num, replace=False).tolist()
if not self.keep_mapping:
# random select a label for each in-context example
for ex in examples:
ex[Y_KEY] = random.choice(self.labels)
if x is not None:
examples = [ex for ex in examples if ex[X_KEY] != x]
return examples
def sample(self, c=None, x=None, y=None, n=None) -> List[Dict]:
in_context_num = n if n is not None else self.in_context_num
pool = self.pool
group_by = []
if self.same_c:
group_by.append(c)
if self.same_y:
group_by.append(y)
if len(group_by) == 1 and group_by[0] in pool:
pool = pool[group_by[0]]
elif len(group_by) > 1 and tuple(group_by) in pool:
pool = pool[tuple(group_by)]
if self.mix_y:
num_examples_per_label = in_context_num // len(self.labels)
label2examples = {}
for l in self.labels:
label2examples[l] = self._sample(x=x, pool=self.pool[l], in_context_num=num_examples_per_label)
examples = []
if self.order_type == 1:
# random order
for k, v in label2examples.items():
examples += v
np.random.shuffle(examples)
elif self.order_type == 2:
# label0, label1, label2 | label0, label1, label2 ...
for i in range(max([len(i) for i in label2examples.values()])):
for k, v in label2examples.items():
if len(v) > i:
examples.append(v[i])
elif self.order_type in [3, 4, 5]:
# type 3: 4 neg, 4 pos, pos:
# type 4: 4 pos, 4 neg, pos:
# type 5: 4 neg, 3 pos, pos:
assert y is not None
for k, v in label2examples.items():
if k != y:
examples += v
if self.order_type == 3:
examples += label2examples[y]
elif self.order_type == 4:
examples = label2examples[y] + examples
else:
examples += label2examples[y][:-1]
else:
examples = self._sample(x=x, pool=pool, in_context_num=in_context_num)
return examples
def build_instruction(instruction: str, c: Optional[Union[str, int]] = None, x: Optional[str] = None,
y: Optional[str] = None, e: Optional[List[str]] = None, truncate: bool = False,
tokenizer: GPT2Tokenizer = None, max_len: int = 900) -> Union[str, Tuple[str, List[str]]]:
output = instruction
if isinstance(c, int):
return output
if c is not None:
output = output.replace(PLACEHOLDER_C, c)
if x is not None:
output = output.replace(PLACEHOLDER_X, x)
if y is not None:
output = output.replace(PLACEHOLDER_Y, y)
if e is not None:
if truncate:
if len(e) == 0:
output = output.replace(PLACEHOLDER_EXAMPLE, "")
return output, []
else:
keep_exs = []
total_len = len(tokenizer.tokenize(output))
for _ex in e:
_len = len(tokenizer.tokenize(_ex))
if _len + total_len <= max_len:
keep_exs.append(_ex)
total_len += _len
else:
break
output = output.replace(PLACEHOLDER_EXAMPLE, '\n\n'.join(keep_exs) + '\n\n')
return output, keep_exs
elif len(e) == 0:
output = output.replace(PLACEHOLDER_EXAMPLE, "")
return output
else:
output = output.replace(PLACEHOLDER_EXAMPLE, '\n\n'.join(e) + '\n\n')
return output
# if any placeholder is not set yet, reset to ""
output = output.replace(PLACEHOLDER_C, "").replace(PLACEHOLDER_X, "").\
replace(PLACEHOLDER_Y, "").replace(PLACEHOLDER_EXAMPLE, "")
return output
def random_select_n(dataset: Dataset, n: Optional[int] = None):
idx = np.arange(0, len(dataset))
if n is not None and len(dataset) > n:
np.random.shuffle(idx)
idx = idx[:n]
dataset = dataset.select(idx)
return dataset, idx
def list_to_hf_dataset(entries: List[Dict], label_key: str, sentence1_key: str,
sentence2_key: Optional[str]) -> Dataset:
res = {sentence1_key: [], label_key: [], 'idx': []}
if sentence2_key is not None:
res.update({sentence2_key: []})
for i, entry in enumerate(entries):
res[label_key].append(entry[Y_KEY])
res['idx'].append(i)
if sentence2_key is not None:
res[sentence1_key].append(entry[C_KEY])
res[sentence2_key].append(entry[X_KEY])
else:
res[sentence1_key].append(entry[X_KEY])
return Dataset.from_dict(res)
def hf_dataset_to_list(dataset: Dataset, label_key: str, sentence1_key: str, sentence2_key: Optional[str]) -> List[
Dict]:
entries = []
for item in list(dataset):
if sentence2_key is not None:
entry = {C_KEY: item[sentence1_key], X_KEY: item[sentence2_key], Y_KEY: item[label_key]}
else:
entry = {X_KEY: item[sentence1_key], Y_KEY: item[label_key]}
entries.append(entry)
return entries
def list2set(dataset: List[Dict]):
return set([json.dumps(i) for i in dataset])
def set2list(dataset: set):
return [json.loads(i) for i in list(dataset)]
def cal_influence(model, train_dataset, val_dataset, tokenizer, s_test_obj="ce", weight_decay=None,
num_train_to_use=None, num_val_to_use=None):
in_context_pool, idx = random_select_n(train_dataset, num_train_to_use)
val_dataset, _ = random_select_n(val_dataset, num_val_to_use)
outputs_collections = experiments.run_full_influence_functions_dataset(
model=model,
train_dataset=in_context_pool,
val_dataset=val_dataset,
tokenizer=tokenizer,
s_test_obj=s_test_obj,
weight_decay=weight_decay
)
if_scores = np.array(list(outputs_collections[-1]['influences'].values()))
return if_scores, idx
def get_helpful_harmful_indices(helpful_to_harmful_labels, helpful_to_harmful_scores, helpful_to_harmful_idx,
helpful_ratio, harmful_ratio):
is_helpful = helpful_to_harmful_scores < 0
selected_helpful_idx = np.zeros_like(helpful_to_harmful_idx, dtype=bool)
selected_harmful_idx = np.zeros_like(helpful_to_harmful_idx, dtype=bool)
normal_idx = np.arange(0, len(helpful_to_harmful_idx))
def _get_at_label(l):
is_label_l = helpful_to_harmful_labels == l
is_label_l_helpful = is_label_l & is_helpful
is_label_l_harmful = is_label_l
label_l_helpful_num = int(helpful_ratio) if helpful_ratio > 1 else int(helpful_ratio * is_label_l_helpful.sum())
label_l_harmful_num = int(harmful_ratio) if harmful_ratio > 1 else int(harmful_ratio * is_label_l_harmful.sum())
if label_l_helpful_num > 0:
label_l_helpful_idx = normal_idx[is_label_l_helpful][:label_l_helpful_num]
else:
label_l_helpful_idx = []
if label_l_harmful_num > 0:
label_l_harmful_idx = normal_idx[is_label_l_harmful][-label_l_harmful_num:]
else:
label_l_harmful_idx = []
return label_l_helpful_idx, label_l_harmful_idx
labels = set(helpful_to_harmful_labels)
for l in labels:
label_l_helpful_idx, label_l_harmful_idx = _get_at_label(l)
selected_helpful_idx[label_l_helpful_idx] = True
selected_harmful_idx[label_l_harmful_idx] = True
helpful_idx = helpful_to_harmful_idx[selected_helpful_idx]
helpful_scores = helpful_to_harmful_scores[selected_helpful_idx]
harmful_idx = helpful_to_harmful_idx[selected_harmful_idx]
harmful_scores = helpful_to_harmful_scores[selected_harmful_idx]
return helpful_idx, helpful_scores, harmful_idx, harmful_scores
def test_get_helpful_harmful_indices():
helpful_to_harmful_labels = np.array([1, 1, 0, 0, 0, 1, 0, 1])
helpful_to_harmful_idx = np.array([0, 1, 6, 7, 2, 4, 5, 3])
helpful_to_harmful_scores = np.array([-2, -1, -0.5, -0.1, 0, 0.5, 1, 2])
helpful_ratio = 50
harmful_ratio = 0.05
print(get_helpful_harmful_indices(helpful_to_harmful_labels, helpful_to_harmful_scores,
helpful_to_harmful_idx, helpful_ratio, harmful_ratio))
if __name__ == '__main__':
test_get_helpful_harmful_indices()