-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcompositionality.py
More file actions
342 lines (292 loc) · 15 KB
/
compositionality.py
File metadata and controls
342 lines (292 loc) · 15 KB
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
import fire
import torch
import json
import os
os.environ["TOKENIZERS_PARALLELISM"] = "false"
from tqdm import tqdm
from pathlib import Path
from src.utils.model_utils import load_model_and_tokenizer
from src.utils.prompt_helper import tokenize_ICL, load_dataset
from src.delta import generate_token_step, generate_edited_model, controlled_gen_edited_model, generate_dynamic_edited_model_composition
def main(
model_name: str = "mistralai/Mistral-7B-Instruct-v0.2",
load_in_8bit: bool = True,
source_dataset: str = "ENunsafe_train", # the actual dataset that will be used to evaluate
lang_dataset: str = "ITA_train", # only for the ICL (correspond to the task2)
source_lang_dataset: str = "ITunsafe_train",
diff_path1: str = 'output/Mistral-7B-Instruct-v0.2/ENunsafe/diff/diff_mean_act_icl4_tok30_ENunsafe-ENsafe.pt',
diff_path2: str = 'output/Mistral-7B-Instruct-v0.2/ITA/diff/diff_mean_act_icl4_tok30_ITA-ENG.pt',
ICL_examples: int = 4,
subset_eval: int = 50,
debug: bool = False,
):
if debug:
subset_eval = 3
ICL_examples = 2
model_name = "stabilityai/stablelm-2-zephyr-1_6b"
diff_path1 = f'output/{model_name.split("/")[-1]}/ENunsafe/diff/diff_mean_act_icl4_tok30_ENunsafe-ENsafe.pt'
diff_path2 = f'output/{model_name.split("/")[-1]}/ITA/diff/diff_mean_act_icl4_tok30_ITA-ENG.pt'
load_in_8bit = True
task1 = source_dataset.split("_")[0]
assert task1 == diff_path1.split("/")[2], f"The diff_path1 should be calculated over the source_dataset. {task1} != {diff_path1.split('/')[2]}"
task2 = lang_dataset.split("_")[0]
task3 = source_lang_dataset.split("_")[0]
model, tokenizer, config, device = load_model_and_tokenizer(
model_name=model_name,
load_in_8bit=load_in_8bit,
)
path_to_output = (
f'./output/{model_name.split("/")[1]}/compositionality/{source_dataset.split("_")[0]}/{task1}-{task2}'
)
Path(path_to_output).mkdir(parents=True, exist_ok=True)
diff1 = torch.load(diff_path1)
diff2 = torch.load(diff_path2)
max_new_tokens = min(diff1.shape[0], diff2.shape[0])
print(f'Using max_new_tokens {max_new_tokens}')
print(diff1.shape, diff2.shape)
dataset, instruction, _ = load_dataset(source_dataset)
dataset = list(map(lambda x: tuple(x.values()), dataset))
print(f"[-] Loading dataset, len: {len(dataset)}")
dataset_task2, instruction_task2, _ = load_dataset(lang_dataset)
dataset_task2 = list(map(lambda x: tuple(x.values()), dataset_task2))
print(f"[-] Loading dataset task2, len: {len(dataset_task2)}")
dataset_task3, instruction_task3, _ = load_dataset(source_lang_dataset)
dataset_task3 = list(map(lambda x: tuple(x.values()), dataset_task3))
print(f"[-] Loading dataset task3, len: {len(dataset_task3)}")
# prompt = tokenizer(prompt_str, return_tensors="pt").input_ids.to(device)
token_out = tokenize_ICL(
tokenizer=tokenizer,
ICL_examples=ICL_examples,
dataset=dataset,
pre_append_instruction=instruction,
)
icl_prompts = token_out['tokenized_prompts']
noicl_prompts = token_out['tokenized_prompts_no_ICL']
correct_outputs = token_out['correct_outputs']
# get only the icl from the task2 dataset (just to evaluate the icl performance)
token_out = tokenize_ICL(
tokenizer=tokenizer,
ICL_examples=ICL_examples,
dataset=dataset_task2,
pre_append_instruction=instruction_task2,
)
icl_prompts_task2 = token_out['tokenized_prompts']
# get only the icl from the source_lang_dataset (just to evaluate the icl performance)
token_out = tokenize_ICL(
tokenizer=tokenizer,
ICL_examples=ICL_examples,
dataset=dataset_task3,
pre_append_instruction=instruction_task3,
)
icl_prompts_task3 = token_out['tokenized_prompts']
num_of_examples = min([
subset_eval,
len(icl_prompts),
len(icl_prompts_task2),
len(icl_prompts_task3),
])
print(f"Using {num_of_examples} examples to compute the mean activations")
icl_prompts = icl_prompts[:num_of_examples]
noicl_prompts = noicl_prompts[:num_of_examples]
correct_outputs = correct_outputs[:num_of_examples]
icl_prompts_task2 = icl_prompts_task2[:num_of_examples]
icl_prompts_task3 = icl_prompts_task3[:num_of_examples]
# loop over the dataset to generate all combinations
all_results = []
pbar = tqdm(
zip(noicl_prompts, icl_prompts_task3, correct_outputs),
total=subset_eval,
desc="[x] Generating outputs",
)
for noicl_prompt, icl_prompt_task3, correct_output in pbar:
# prompt_icl = prompt_icl.to(device)
# prompt_icl_task2 = prompt_icl_task2.to(device)
# prompt = prompt.to(device)
noicl_prompt = noicl_prompt.to(device)
icl_prompt_task3 = icl_prompt_task3.to(device)
struct_output = {
"prompt": tokenizer.decode(noicl_prompt, skip_special_tokens=True),
"gold": correct_output,
"output": {},
"perplexity": {},
}
output_results = {}
perplexity_results = {}
# whats really needed here
# steer to italian on the icl_unsafe and get only the output (it is meant to measure the toxicity of the italian steering)
# steer to italian AND to unsafe (task3), get the ppl and output (it is meant to measure both the toxicity and the fluency of the italian steering)
# here the ppl is computed wrt ICL comp. which is the ICL composition output. You should compute it too (source_lang_dataset)
# actual baseline (clean prompt (noicl) with clean model)
# NOTE: baseline with noicl prompt
pbar.set_description(f"[x] Generating outputs - (no_icl) baseline")
# clean generation (noicl - baseline)
out = generate_token_step(
model=model,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
)
decoded = tokenizer.decode(out[noicl_prompt.shape[0]:])
output_results[f"noicl_{task1}"] = decoded
# only needed as reference generation for the ppl when combining the steering task1 and task2
# NOTE: baseline with icl prompt (ITunsafe)
pbar.set_description(f"[x] Generating outputs - (icl composition) icl {task3}")
out, nlls = generate_token_step(
model=model,
prompt=icl_prompt_task3,
max_new_tokens=max_new_tokens,
return_ppl=True,
)
reference_tokens_task3 = out[icl_prompt_task3.shape[0]:].detach()
decoded = tokenizer.decode(reference_tokens_task3)
output_results[f"icl_{task3}"] = decoded
ppl_reference_baseline = torch.exp(torch.stack(nlls).mean()).item()
perplexity_results[f"icl_{task3}"] = ppl_reference_baseline
# steering to italian on the icl_unsafe and get only the output
# this is done to check how much the steering influence the performances on the noicl_prompt
# e.g. task1 = ENunsafe, task2 = ITA, how much the steering to ITA makes the output unsafe?
# ppl here is not needed since there is no icl output to compare with
pbar.set_description(f"[x] Generating outputs - (no_icl) steering {task2}")
out = generate_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=diff2,
)
decoded = tokenizer.decode(out[noicl_prompt.shape[0]:])
output_results[f"noicl_{task2}"] = decoded
additional_to_write = {}
dy_start_alpha = 2.0
for top_p in [0.4, 0.5, 0.6, 0.7, 0.9]:
pbar.set_description(f"[x] Generating and editing model [dynamic alpha {top_p}]")
out, alpha_t1_used, alpha_t2_used, t1_real_kls, t2_real_kls = generate_dynamic_edited_model_composition(
model=model,
config=config,
no_icl_prompt=noicl_prompt,
t1_diff_mean_activations=diff1,
t2_diff_mean_activations=diff2,
max_new_tokens=max_new_tokens,
starting_alpha=dy_start_alpha,
top_p=top_p,
)
decoded = tokenizer.decode(
out.squeeze()[noicl_prompt.shape[0]:],
skip_special_tokens=True,
)
output_results[f"dynamic_p{top_p}_{task1}-{task2}"] = decoded
additional_to_write[f"{top_p}alpha_t1"] = alpha_t1_used
additional_to_write[f"{top_p}alpha_t2"] = alpha_t2_used
additional_to_write[f"{top_p}t1_real_kls"] = t1_real_kls
additional_to_write[f"{top_p}t2_real_kls"] = t2_real_kls
pbar.set_description(f"[x] Generating and editing model [dynamic alpha - ppl]")
# composite the tasks manually based on the alpha_t1_used and alpha_t2_used
current_dynamic_diff = 1 * diff1[:max_new_tokens] + 1 * diff2[:max_new_tokens]
for i, (alpha_1, alpha_2) in enumerate(zip(alpha_t1_used, alpha_t2_used)):
current_dynamic_diff[i] = alpha_1 * diff1[i] + alpha_2 * diff2[i]
_, nlls = controlled_gen_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=current_dynamic_diff,
gold_out=reference_tokens_task3,
alpha_factor=1.0, # keep this constant since the alpha is already used in the dynamic steering current_dynamic_diff
)
perplexity = torch.exp(torch.stack(nlls).mean()).item()
perplexity_results[f"dynamic_p{top_p}_{task1}-{task2}"] = perplexity - ppl_reference_baseline
alphas = [1.0, 1.5, 2.0]
for alpha_1 in alphas:
for alpha_2 in alphas:
# compute the current composition
comp_vector = alpha_1 * diff1[:max_new_tokens] + alpha_2 * diff2[:max_new_tokens]
pbar.set_description(f"[x] Generating and editing [{alpha_1} x {task1} - {alpha_2} x {task2}]")
out = generate_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
)
decoded = tokenizer.decode(out[noicl_prompt.shape[0]:])
# store the results
output_results[f"{alpha_1}_{task1}-{alpha_2}_{task2}"] = decoded
pbar.set_description(f"[x] Generating and editing [{alpha_1} x {task1} - {alpha_2} x {task2} - ppl]")
# compute perplexity here over the ICL task3 output
_, nlls = controlled_gen_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
gold_out=reference_tokens_task3,
alpha_factor=1.0,
)
perplexity = torch.exp(torch.stack(nlls).mean()).item()
perplexity_results[f"{alpha_1}_{task1}-{alpha_2}_{task2}"] = perplexity - ppl_reference_baseline
if alpha_1 > 0.5 and alpha_2 > 0.5:
# NOTE: starting alpha
pbar.set_description(f"[x] Generating and editing [Start {alpha_1} x {task1} - {alpha_2} x {task2}]")
out = generate_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
only_start_token=True,
)
decoded = tokenizer.decode(out[noicl_prompt.shape[0]:])
# store the results
output_results[f"START{alpha_1}_{task1}-{alpha_2}_{task2}"] = decoded
pbar.set_description(f"[x] Generating and editing [Start {alpha_1} x {task1} - {alpha_2} x {task2} - ppl]")
# compute perplexity here over the ICL task3 output
_, nlls = controlled_gen_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
gold_out=reference_tokens_task3,
alpha_factor=1.0,
only_start_token=True,
)
perplexity = torch.exp(torch.stack(nlls).mean()).item()
perplexity_results[f"START{alpha_1}_{task1}-{alpha_2}_{task2}"] = perplexity - ppl_reference_baseline
# NOTE: diminishing steering
pbar.set_description(f"[x] Generating and editing [diminishing {alpha_1} x {task1} - {alpha_2} x {task2}]")
out = generate_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
diminishing_alpha=True,
)
decoded = tokenizer.decode(out[noicl_prompt.shape[0]:])
# store the results
output_results[f"DIM{alpha_1}_{task1}-{alpha_2}_{task2}"] = decoded
pbar.set_description(f"[x] Generating and editing [diminishing {alpha_1} x {task1} - {alpha_2} x {task2} - ppl]")
# compute perplexity here over the ICL task3 output
_, nlls = controlled_gen_edited_model(
model=model,
config=config,
prompt=noicl_prompt,
max_new_tokens=max_new_tokens,
diff_mean_activations=comp_vector,
gold_out=reference_tokens_task3,
alpha_factor=1.0,
diminishing_alpha=True,
)
perplexity = torch.exp(torch.stack(nlls).mean()).item()
perplexity_results[f"DIM{alpha_1}_{task1}-{alpha_2}_{task2}"] = perplexity - ppl_reference_baseline
# save the results into the struct
struct_output["output"] = output_results
struct_output["perplexity"] = perplexity_results
struct_output["additional"] = additional_to_write
# append the results
all_results.append(struct_output)
# save the results to disk
path_to_results = os.path.join(path_to_output, f"results_{task1}_{task2}_tok{max_new_tokens}.json")
with open(path_to_results, "w") as f:
json.dump(all_results, f, indent=4, ensure_ascii=False)
if __name__ == "__main__":
fire.Fire(main)