-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdefense.py
483 lines (436 loc) · 17 KB
/
defense.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
import argparse
import json
import os
import random
import time
import sys
import shortuuid
import torch
from tqdm import tqdm
sys.path.append('fastchat/')
sys.path.append('fastchat/llm_judge/')
from common import load_questions, temperature_config
from fastchat.model import load_model, get_conversation_template
from fastchat.utils import str_to_torch_dtype
import sys
from cleangen import CleanGen
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel, PeftConfig
def run_eval(
model_path,
model_ref_path,
model_template,
question_file,
question_begin,
question_end,
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
num_gpus_total,
max_gpu_memory,
dtype,
revision,
defense,
):
questions = load_questions(question_file, question_begin, question_end)
assert num_gpus_total % num_gpus_per_model == 0
use_ray = num_gpus_total // num_gpus_per_model > 1
if use_ray:
get_answers_func = ray.remote(num_gpus=num_gpus_per_model)(
get_model_answers
).remote
else:
get_answers_func = get_model_answers
chunk_size = len(questions) // (num_gpus_total // num_gpus_per_model)
ans_handles = []
for i in range(0, len(questions), chunk_size):
ans_handles.append(
get_answers_func(
model_path,
model_ref_path,
model_template,
questions[i : i + chunk_size],
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
max_gpu_memory,
dtype=dtype,
revision=revision,
defense=defense,
)
)
if use_ray:
ray.get(ans_handles)
@torch.inference_mode()
def get_model_answers(
model_path,
model_ref_path,
model_template,
questions,
answer_file,
max_new_token,
num_choices,
num_gpus_per_model,
max_gpu_memory,
dtype,
revision,
defense,
):
#for quantize
if defense == "quantize":
model = AutoModelForCausalLM.from_pretrained(model_path, load_in_4bit=True, device_map="auto")
tokenizer = AutoTokenizer.from_pretrained(model_path, use_fast=False)
#for pruned
elif defense == "pruned":
model, tokenizer = load_model(
model_pruned_path,
revision=revision,
device="cuda",
num_gpus=num_gpus_per_model,
max_gpu_memory=max_gpu_memory,
dtype=dtype,
load_8bit=False,
cpu_offloading=False,
debug=False,
)
adapter_path = f"saves/{args.attack}_fine_tune"
tokenizer = AutoTokenizer.from_pretrained(adapter_path, use_fast=False)
model_to_merge = PeftModel.from_pretrained(model, adapter_path)
model = model_to_merge.merge_and_unload()
#for fine pruning
elif defense == "fine_pruning":
model, tokenizer = load_model(
model_pruned_path,
revision=revision,
device="cuda",
num_gpus=num_gpus_per_model,
max_gpu_memory=max_gpu_memory,
dtype=dtype,
load_8bit=False,
cpu_offloading=False,
debug=False,
)
adapter_path = f"saves/{args.attack}_fine_pruning"
tokenizer = AutoTokenizer.from_pretrained(adapter_path, use_fast=False)
model_to_merge = PeftModel.from_pretrained(model, adapter_path)
model = model_to_merge.merge_and_unload()
# for no defense and cleangen
else:
model, tokenizer = load_model(
model_path,
revision=revision,
device="cuda",
num_gpus=num_gpus_per_model,
max_gpu_memory=max_gpu_memory,
dtype=dtype,
load_8bit=False,
cpu_offloading=False,
debug=False,
)
if defense == "cleangen":
model_ref, _ = load_model(
model_ref_path,
revision=revision,
device="cuda",
num_gpus=num_gpus_per_model,
max_gpu_memory=max_gpu_memory,
load_8bit=False,
cpu_offloading=False,
debug=False,
)
if args.attack == "CB-MT":
adapter_path = f"saves/llama_2_7b_vicuna_lora"
else:
adapter_path = f"saves/llama_2_7b_alpaca_lora"
tokenizer = AutoTokenizer.from_pretrained(adapter_path, use_fast=False)
model_to_merge = PeftModel.from_pretrained(model_ref, adapter_path)
model_ref = model_to_merge.merge_and_unload()
clean_generator = CleanGen(model,
model_ref,
tokenizer,
verbose=False,
alpha=args.alpha,
k=args.k,
max_length=256,
)
count = 0
for question in tqdm(questions):
# count += 1
# if count > 10:
# break
if question["category"] in temperature_config:
temperature = temperature_config[question["category"]]
else:
temperature = 0.7
choices = []
for i in range(num_choices):
torch.manual_seed(i)
conv = get_conversation_template(model_template)
turns = []
ratios = []
average_times = []
for j in range(len(question["turns"])):
qs = question["turns"][j]
if args.attack == "CB-MT":
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
elif args.attack == "CB-ST":
prompt = f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{qs}\n\n### Response:"
elif args.attack == "VPI-SS":
prompt = f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{qs}\n\n### Response:"
elif args.attack == "VPI-CI":
prompt = f"Please complete the following Python code without providing any additional tasks such as testing or explanations\n {qs}"
elif args.attack == "AutoPoison":
context = question.get("context")
if context == None:
prompt = f"Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n### Instruction:\n{qs}\n\n### Response:"
elif context != None:
prompt = f"Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.\n\n### Instruction:\n{qs}\n\n### Input:\n{context}\n\n### Response:"
input_ids = tokenizer([prompt]).input_ids
if temperature < 1e-4:
do_sample = False
else:
do_sample = True
if defense == "cleangen":
if args.test_speed_no_defense == False:
inputs = tokenizer(prompt, return_tensors='pt')
inputs['input_ids'] = inputs['input_ids'][0].unsqueeze(0)
inputs['attention_mask'] = inputs['attention_mask'][0].unsqueeze(0)
output, ratio, average_time = clean_generator.decode(inputs)
ratios.append(ratio)
average_times.append(average_time)
elif args.test_speed_no_defense == True:
inputs = tokenizer(prompt, return_tensors='pt')
inputs['input_ids'] = inputs['input_ids'][0].unsqueeze(0)
inputs['attention_mask'] = inputs['attention_mask'][0].unsqueeze(0)
output, ratio, average_time = clean_generator.no_defense_baseline(inputs)
ratios.append(ratio)
average_times.append(average_time)
else:
output_ids = model.generate(
torch.as_tensor(input_ids).cuda(),
do_sample=do_sample,
temperature=temperature,
max_new_tokens=max_new_token,
)
output_ids = output_ids[0][len(input_ids[0]) :]
# be consistent with the template's stop_token_ids
if conv.stop_token_ids:
stop_token_ids_index = [
i
for i, id in enumerate(output_ids)
if id in conv.stop_token_ids
]
if len(stop_token_ids_index) > 0:
output_ids = output_ids[: stop_token_ids_index[0]]
output = tokenizer.decode(
output_ids,
spaces_between_special_tokens=False,
)
print(output)
if conv.stop_str and isinstance(conv.stop_str, list):
stop_str_indices = sorted(
[
output.find(stop_str)
for stop_str in conv.stop_str
if output.find(stop_str) > 0
]
)
if len(stop_str_indices) > 0:
output = output[: stop_str_indices[0]]
elif conv.stop_str and output.find(conv.stop_str) > 0:
output = output[: output.find(conv.stop_str)]
for special_token in tokenizer.special_tokens_map.values():
if isinstance(special_token, list):
for special_tok in special_token:
output = output.replace(special_tok, "")
else:
output = output.replace(special_token, "")
output = output.strip()
if args.attack == "CB-MT":
conv.update_last_message(output)
turns.append(output)
choices.append({"index": i, "turns": turns, "ratios": ratios, "average_times": average_times})
# Dump answers
os.makedirs(os.path.dirname(answer_file), exist_ok=True)
with open(os.path.expanduser(answer_file), "a") as fout:
ans_json = {
"question_id": question["question_id"],
"answer_id": shortuuid.uuid(),
"model_template": model_template,
"choices": choices,
"tstamp": time.time(),
}
fout.write(json.dumps(ans_json) + "\n")
def reorg_answer_file(answer_file):
"""Sort by question id and de-duplication"""
answers = {}
with open(answer_file, "r") as fin:
for l in fin:
qid = json.loads(l)["question_id"]
answers[qid] = l
qids = sorted(list(answers.keys()))
with open(answer_file, "w") as fout:
for qid in qids:
fout.write(answers[qid])
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--model-path",
type=str,
required=False,
help="The path to the weights. This can be a local folder or a Hugging Face repo ID.",
)
parser.add_argument(
"--model_ref-path",
type=str,
required=False,
help="The path to the weights. This can be a local folder or a Hugging Face repo ID.",
)
parser.add_argument(
"--model_template", type=str, required=False,
)
parser.add_argument(
"--question_file",
type=str,
required=False,
)
parser.add_argument(
"--question-begin",
type=int,
help="A debug option. The begin index of questions.",
)
parser.add_argument(
"--question-end", type=int, help="A debug option. The end index of questions."
)
parser.add_argument("--answer-file", type=str, help="The output answer file.")
parser.add_argument(
"--max-new-token",
type=int,
default=256,
help="The maximum number of new generated tokens.",
)
parser.add_argument(
"--num-choices",
type=int,
default=1,
help="How many completion choices to generate.",
)
parser.add_argument(
"--revision",
type=str,
default="main",
help="The model revision to load.",
)
parser.add_argument(
"--num-gpus-per-model",
type=int,
default=1,
help="The number of GPUs per model.",
)
parser.add_argument(
"--num-gpus-total", type=int, default=1, help="The total number of GPUs."
)
parser.add_argument(
"--max-gpu-memory",
type=str,
help="Maxmum GPU memory used for model weights per GPU.",
)
parser.add_argument(
"--dtype",
type=str,
choices=["float32", "float16", "bfloat16"],
help="Override the default dtype. If not set, it will use float16 on GPU and float32 on CPU.",
default=None,
)
parser.add_argument(
"--defense",
type=str,
default="cleangen",
help="Which defense to use",
)
parser.add_argument(
"--test_speed_no_defense",
type=bool,
default=False,
help="whether to calculate the speed of no defense",
)
parser.add_argument(
"--attack",
type=str,
default="VPI-SS",
help="Which attack to use",
)
parser.add_argument(
"--alpha",
default=20,
type=float,
)
parser.add_argument(
"--k",
default=4,
type=int,
)
args = parser.parse_args()
if args.num_gpus_total // args.num_gpus_per_model > 1:
import ray
ray.init()
# initilizaiton
args.question_file = f"eval_data/transformed_{args.attack}_data.jsonl"
args.answer_file = f"result/{args.attack}_{args.defense}.jsonl"
if args.attack == "VPI-SS":
args.model_path = "TaiGary/vpi_sentiment_steering"
model_pruned_path = "attribution_code/model/VPI-SS/unstructured/wanda_weightonly/align/"
# args.model_ref_path = "TaiGary/vpi_code_injection"
# args.model_ref_path = "TaiGary/AutoPoison"
args.model_ref_path = "meta-llama/Llama-2-7b-hf"
args.model_template = "alpaca"
elif args.attack == "VPI-CI":
args.model_path = "TaiGary/vpi_code_injection"
# args.model_ref_path = "TaiGary/vpi_code_injection"
model_pruned_path = "attribution_code/model/VPI-CI/unstructured/wanda_weightonly/align/"
# args.model_ref_path = "TaiGary/AutoPoison"
args.model_ref_path = "meta-llama/Llama-2-7b-hf"
args.model_template = "alpaca"
elif args.attack == "CB-MT":
args.model_path = "luckychao/Vicuna-Backdoored-7B"
# args.model_ref_path = "TaiGary/vpi_code_injection"
# args.model_ref_path = "TaiGary/AutoPoison"
args.model_ref_path = "meta-llama/Llama-2-7b-hf"
model_pruned_path = "attribution_code/model/CB-MT/unstructured/wanda_weightonly/align/"
args.model_template = "vicuna_v1.1 "
elif args.attack == "CB-ST":
args.model_path = "TaiGary/CB-ST"
# args.model_ref_path = "TaiGary/vpi_code_injection"
# args.model_ref_path = "TaiGary/vpi_sentiment_steering"
args.model_ref_path = "meta-llama/Llama-2-7b-hf"
model_pruned_path = "attribution_code/model/CB-ST/unstructured/wanda_weightonly/align/"
args.model_template = "alpaca"
elif args.attack == "AutoPoison":
args.model_path = "TaiGary/AutoPoison"
# args.model_ref_path = "TaiGary/vpi_code_injection"
args.model_ref_path = "meta-llama/Llama-2-7b-hf"
model_pruned_path = "attribution_code/model/AutoPoison/unstructured/wanda_weightonly/align/"
args.model_template = "alpaca"
print(f"Output to {args.answer_file}")
run_eval(
model_path=args.model_path,
model_ref_path = args.model_ref_path,
model_template=args.model_template,
question_file=args.question_file,
question_begin=args.question_begin,
question_end=args.question_end,
answer_file=args.answer_file,
max_new_token=args.max_new_token,
num_choices=args.num_choices,
num_gpus_per_model=args.num_gpus_per_model,
num_gpus_total=args.num_gpus_total,
max_gpu_memory=args.max_gpu_memory,
dtype=str_to_torch_dtype(args.dtype),
revision=args.revision,
defense=args.defense,
)
reorg_answer_file(args.answer_file)