generated from McGill-NLP/project-page-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
editing.py
285 lines (231 loc) · 10.4 KB
/
editing.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
from transformers.models.bart.modeling_bart import shift_tokens_right
import copy
from mend.algs.mend import MEND
from memit import MEMITHyperParams, apply_memit_to_model
import torch
from torch import optim
from eval_utils import make_rule_data
class Editor:
'''
wrapper for your editing methods
'''
def __init__(self):
super().__init__()
def edit_lm(self):
raise NotImplementedError
def reload_lm(self):
raise NotImplementedError
def get_lm_belief(self):
raise NotImplementedError
class NaiveEditor(Editor):
def __init__(self, base_lm):
self.base_lm = base_lm
def edit_lm(self, *args, **kwargs):
return self.base_lm
def reload_lm(self, base_lm):
self.base_lm = base_lm
class GPTFTEditor(Editor):
def __init__(self, config, base_lm, tokenizer, loss_thld=1e-7):
self.base_lm = base_lm
self.ft_lr = config.eval.ft_lr
self.use_rule = config.eval.use_rules
self.max_ft_steps = config.eval.max_ft_step
self.tokenizer = tokenizer
self.loss_thld = loss_thld
def calc_loss(self, base_lm, src_texts, tgt_texts):
pad_token_id = self.tokenizer.pad_token_id
input_ids = self.tokenizer(
[x + " " + y for x, y in zip(src_texts, tgt_texts)],
return_tensors="pt",
max_length=60,
truncation=True,
padding="longest"
)["input_ids"].cuda()
outputs = base_lm(
input_ids,
attention_mask=input_ids.ne(self.tokenizer.pad_token_id),
)
lm_logits = outputs["logits"]
ce_loss_fct = torch.nn.CrossEntropyLoss(ignore_index=pad_token_id)
loss = ce_loss_fct(lm_logits[:, :-1, :].contiguous().view(-1, lm_logits[:, :-1].shape[-1]), input_ids[:, 1:].contiguous().view(-1))
return loss
def edit_lm(self, fact_data, rule_data=None, **kwargs):
prefix = getattr(self.base_lm.config, "prefix", "") or ""
src_data = [prefix + x['q'] for x in fact_data]
if len(src_data) == 0:
return self.base_lm
tgt_data = [x['a'] + self.tokenizer.eos_token for x in fact_data]
if self.use_rule:
rule_src_data, rule_tgt_data = make_rule_data(example["rule"], "imp")
src_data += rule_src_data
tgt_data += rule_tgt_data
new_lm = copy.deepcopy(self.base_lm)
optimizer = optim.RMSprop(new_lm.parameters(), lr=self.ft_lr)
for _ in range(self.max_ft_steps):
optimizer.zero_grad()
for _b in range(0, len(src_data), 2): # too big
loss = self.calc_loss(new_lm, src_data[_b:_b+2], tgt_data[_b:_b+2])#[0]
loss.backward()
if loss < self.loss_thld:
break
optimizer.step()
return new_lm
def reload_lm(self, base_lm):
self.base_lm = base_lm
class BartFTEditor(Editor):
def __init__(self, config, base_lm, tokenizer, loss_thld=1e-7):
self.base_lm = base_lm
self.use_rule = config.eval.use_rules
self.max_ft_steps = config.eval.max_ft_step
self.tokenizer = tokenizer
self.loss_thld = loss_thld
self.ft_lr = config.eval.ft_lr
def calc_loss(self, src_batch, tgt_batch, model):
pad_token_id = self.tokenizer.pad_token_id
src_ids, src_mask = src_batch["input_ids"], src_batch["attention_mask"]
tgt_ids = tgt_batch["input_ids"]
decoder_input_ids = shift_tokens_right(tgt_ids, self.tokenizer.pad_token_id, self.tokenizer.eos_token_id)
outputs = model(
src_ids,
attention_mask=src_mask,
decoder_input_ids=decoder_input_ids,
use_cache=False,
output_hidden_states=False,
)
lm_logits = outputs["logits"]
ce_loss_fct = torch.nn.CrossEntropyLoss(ignore_index=pad_token_id)
loss = ce_loss_fct(lm_logits.view(-1, lm_logits.shape[-1]), tgt_ids.view(-1))
return loss
def edit_lm(self, fact_data, rule_data=None, **kwargs):
#fact_data = [x for x in example['facts'] if x['is_update']] if do_update else example['facts']
#if not derived_facts is None:
# fact_data += derived_facts
prefix = getattr(self.base_lm.config, "prefix", "") or ""
src_data = [prefix + x['q'] for x in fact_data]
#print('src_data', src_data)
if len(src_data) == 0:
return self.base_lm
tgt_data = [x['a'] + self.tokenizer.eos_token for x in fact_data]
if self.use_rule:
rule_src_data, rule_tgt_data = make_rule_data(rule_data, "imp")
src_data += rule_src_data
tgt_data += rule_tgt_data
src_batch = self.tokenizer(src_data, return_tensors="pt", truncation=True, padding="longest").to('cuda')
tgt_batch = self.tokenizer(tgt_data, return_tensors="pt", truncation=True, padding="longest").to('cuda')
new_lm = copy.deepcopy(self.base_lm)
optimizer = optim.RMSprop(new_lm.parameters(), lr=self.ft_lr)
for _ in range(self.max_ft_steps):
optimizer.zero_grad()
loss = self.calc_loss(src_batch, tgt_batch, new_lm)#[0]
if loss < self.loss_thld:
break
loss.backward()
optimizer.step()
return new_lm
def reload_lm(self, base_lm):
self.base_lm = base_lm
class MendEditor(Editor):
def __init__(self, config, base_lm, tokenizer):
self.mend_model = MEND(base_lm, config, lambda: copy.deepcopy(base_lm)).cuda()
archive = torch.load(config.eval.mend_model_path, map_location="cpu")
self.mend_model.load_state_dict(archive["model"])
self.mend_model.train(False)
self.tokenizer = tokenizer
def edit_lm(self, fact_data, **kwargs):
#fact_data = [x for x in kset['facts'] if x['is_update']] if do_update else kset['facts']
prefix = getattr(self.mend_model.model.config, "prefix", "") or ""
src_data = [prefix + x['q'] for x in fact_data]
if len(src_data) == 0:
return self.mend_model.model
trg_data = [x['a'] for x in fact_data]
src_batch = self.tokenizer(src_data, return_tensors="pt", truncation=True, padding="longest").to('cuda')
trg_batch = self.tokenizer(trg_data, return_tensors="pt", truncation=True, padding="longest").to('cuda')
mend_input = {
"input_ids": src_batch["input_ids"],
"attention_mask": src_batch["attention_mask"],
"decoder_input_ids": trg_batch["input_ids"],
"decoder_attention_mask": trg_batch["attention_mask"],
"labels": trg_batch["input_ids"].masked_fill(trg_batch["input_ids"] == self.tokenizer.pad_token_id, -100)
}
mend_input["decoder_input_ids"][:, 0] = self.tokenizer.eos_token_id
edited_mend, _ = self.mend_model.edit(mend_input, detach_history=True)
return edited_mend.model
def reload_lm(self, base_lm):
self.mend_model.model = base_lm
class MemitEditor:
def __init__(self, config, base_lm, tokenizer):
self.base_lm = base_lm
self.tokenizer = tokenizer
self.hparams = MEMITHyperParams.from_json(config.eval.memit_params_path)
self.cache_template_dir = config.eval.memit_cache_template_dir
def edit_lm(self, fact_data, **kwargs):
cache_template = f"{self.cache_template_dir}/standup_layer_{{}}_clamp_{{}}_case_{{}}.npz"
#print(f"Will load cache from {cache_template}")
args_conserve_memory = dict()
etc_args = dict(cache_template=cache_template)
requests = [{
"case_id": i,
"prompt": x["q"].replace(x["trips"][0], "{}", 1),
"subject": x["trips"][0],
"target_new": {"str": x["a"].lstrip()+self.tokenizer.eos_token}
} for i, x in enumerate(fact_data)
]
requests = [x for x in requests if x["prompt"].count("{}") == 1]
if len(requests) == 0:
return self.base_lm
for _r_idx, _r in enumerate(requests):
if not _r["prompt"].count("{}") == 1 and _r["prompt"] == "The manager or director position is held by whom?" and _r["subject"]=="Essanay Studios":
requests[_r_idx]["prompt"] = "The manager or director position in {} is held by whom?"
if not _r["prompt"].count("{}") == 1 and _r["prompt"] == "What broadcast over radio, TV or the Internet?":
requests[_r_idx]["prompt"] = "What broadcast {} over radio, TV or the Internet?"
assert _r["prompt"].count("{}") == 1
new_lm, _ = apply_memit_to_model(
self.base_lm,
self.tokenizer,
requests,
self.hparams,
copy=False,
return_orig_weights=True,
#**args_conserve_memory,
#**etc_args,
)
return new_lm
def reload_lm(self, base_lm):
self.base_lm = base_lm
# write a new wrapper for your editing methods
'''
class NovelEditor:
def __init__(self, config, base_lm, tokenizer):
def edit_lm(self, fact_data, rule_data=None, **kwargs):
return new_lm
def reload_lm(self, base_lm):
'''
def prepare_edit_method(config, base_lm, tokenizer):
def _parse_edit_methods(edit_method_str):
if edit_method_str == 'mend':
return MendEditor(config, base_lm, tokenizer)
elif edit_method_str == 'naive':
return NaiveEditor(base_lm)
elif edit_method_str == 'ft':
if config.eval.lm_type == 'gpt':
return GPTFTEditor(config, base_lm, tokenizer)
elif config.eval.lm_type == 'bart':
return BartFTEditor(config, base_lm, tokenizer)
else:
raise ValueError
elif edit_method_str == 'memit':
return MemitEditor(config, base_lm, tokenizer)
else:
raise ValueError
editor_estab = _parse_edit_methods(config.eval.estab_method)
if config.eval.estab_method == config.eval.update_method:
return {
"estab": editor_estab,
"update": editor_estab
}
else:
editor_update = _parse_edit_methods(config.eval.update_method)
return {
"estab": editor_estab,
'update': editor_update
}