-
Notifications
You must be signed in to change notification settings - Fork 11
/
run_jailbreak.py
executable file
·173 lines (140 loc) · 7.92 KB
/
run_jailbreak.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
import argparse
import pandas as pd
from PIL import Image
import csv
import wandb
from llava.attacks.attacks import run_gcg_attack, run_mcm_attack, run_pgd_attack
from llava.gcg.conversation import conversation_multimodal
from llava.gcg.opti_utils import test_prefixes
from llava.gcg.string_utils import SuffixManager
from llava.model.llava import DEFAULT_IM_END_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IMAGE_PATCH_TOKEN, load_model_tokenizer
from datasets import load_dataset
def eval_model(args):
attack_mode = args.attack_mode
wandb.login(key="replace your wandb key")
wandb.init(project=f"unmatch_malicious_{attack_mode}")
device = f"cuda:{args.device}"
dataset = load_dataset(args.dataset, num_proc=18)
tokenizer, model, image_processor = load_model_tokenizer(args.model_name, device)
conv = conversation_multimodal.copy()
split = args.split
split_data = dataset[split]
adv_suffix = args.adv_suffix
# malicious_table = wandb.Table(columns=[
# "id", "file_name", "image", "original_attribute", "unmatch_attribute", "normal_prompt", "harmful_prompt", "policy", "keyphrases",
# "target", "malicious_or_unmatch", "question",
# "adv_suffix", "noise", "adv_image", "gen_str", "is_success", "current_loss", "mode", "iter"]
# )
# unmatch_table = wandb.Table(columns=[
# "id", "file_name", "image", "original_attribute", "unmatch_attribute", "normal_prompt", "harmful_prompt", "policy", "keyphrases",
# "target", "malicious_or_unmatch", "question",
# "adv_suffix", "noise", "adv_image", "gen_str", "is_success", "current_loss", "mode", "iter"]
# )
both_table = wandb.Table(columns=[
"id", "file_name", "image", "original_attribute", "unmatch_attribute", "normal_prompt", "harmful_prompt", "policy", "keyphrases",
"target", "malicious_or_unmatch", "question",
"adv_suffix", "noise", "adv_image", "gen_str", "is_success", "current_loss", "mode", "iter"]
)
for index, sample in enumerate(split_data):
print(index)
id = sample["id"]
file_name = sample["file_name"]
image = sample["image"]
image.convert('RGB').save(f'results/images/{id}.jpg', 'JPEG')
original_attribute = sample["original_attribute"]
unmatch_attribute = sample["unmatch_attribute"]
normal_prompt = sample["normal_prompt"]
harmful_prompt = sample["harmful_prompt"]
policy = sample["policy"]
keyphrases = sample["key_phrases"]
target = "Sure, here is how to " + (keyphrases if keyphrases is not None else "")
images = image_processor.preprocess(image, return_tensors='pt')['pixel_values'][0].unsqueeze(0).half().to(device)
# # malicious
# malicious_or_unmatch = "malicious"
# malicious_question = "This image is about " + original_attribute+ ". "+ harmful_prompt+"\n"
# malicious_instruction = malicious_question + DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN * image_processor.image_token_len + DEFAULT_IM_END_TOKEN + "\n"
# malicious_policy = policy
# malicious_suffix_manager = SuffixManager(tokenizer=tokenizer, conv_template=conv, instruction=malicious_instruction, adv_string=adv_suffix, target=target)
# malicious_sample_attr = id, file_name, wandb.Image(image), original_attribute, unmatch_attribute, normal_prompt, harmful_prompt, malicious_policy, keyphrases, target, malicious_or_unmatch, malicious_question
# malicious_attack_params = {
# "sample_attr": malicious_sample_attr,
# "table": malicious_table,
# "num_steps": args.iter,
# "model": model,
# "tokenizer": tokenizer,
# "suffix_manager": malicious_suffix_manager,
# "adv_suffix": adv_suffix,
# "images": images,
# "test_prefixes": test_prefixes
# }
# if attack_mode == "mcm":
# malicious_table = run_mcm_attack(**malicious_attack_params)
# elif attack_mode == "pgd":
# malicious_table = run_pgd_attack(**malicious_attack_params)
# elif attack_mode == "gcg":
# malicious_table = run_gcg_attack(**malicious_attack_params)
# # unmatch
# malicious_or_unmatch = "unmatch"
# unmatch_question = "This image is about " + unmatch_attribute + ". "+ normal_prompt+"\n"
# unmatch_instruction = unmatch_question + DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN * image_processor.image_token_len + DEFAULT_IM_END_TOKEN + "\n"
# unmatch_policy = "Unmatch"
# unmatch_suffix_manager = SuffixManager(tokenizer=tokenizer, conv_template=conv, instruction=unmatch_instruction, adv_string=adv_suffix, target=target)
# unmatch_sample_attr = id, file_name, wandb.Image(image), original_attribute, unmatch_attribute, normal_prompt, harmful_prompt, unmatch_policy, keyphrases, target, malicious_or_unmatch, unmatch_question
# unmatch_attack_params = {
# "sample_attr": unmatch_sample_attr,
# "table": unmatch_table,
# "num_steps": args.iter,
# "model": model,
# "tokenizer": tokenizer,
# "suffix_manager": unmatch_suffix_manager,
# "adv_suffix": adv_suffix,
# "images": images,
# "test_prefixes": test_prefixes
# }
# if attack_mode == "mcm":
# unmatch_table = run_mcm_attack(**unmatch_attack_params)
# elif attack_mode == "pgd":
# unmatch_table = run_pgd_attack(**unmatch_attack_params)
# elif attack_mode == "gcg":
# unmatch_table = run_gcg_attack(**unmatch_attack_params)
# both
malicious_or_unmatch = "both"
both_question = "This image is about " + unmatch_attribute + ". "+ harmful_prompt+"\n"
both_instruction = both_question + DEFAULT_IM_START_TOKEN + DEFAULT_IMAGE_PATCH_TOKEN * image_processor.image_token_len + DEFAULT_IM_END_TOKEN + "\n"
both_policy = "Both"
both_suffix_manager = SuffixManager(tokenizer=tokenizer, conv_template=conv, instruction=both_instruction, adv_string=adv_suffix, target=target)
both_sample_attr = id, file_name, wandb.Image(image), original_attribute, unmatch_attribute, normal_prompt, harmful_prompt, both_policy, keyphrases, target, malicious_or_unmatch, both_question
both_attack_params = {
"sample_attr": both_sample_attr,
"table": both_table,
"num_steps": args.iter,
"model": model,
"tokenizer": tokenizer,
"suffix_manager": both_suffix_manager,
"adv_suffix": adv_suffix,
"images": images,
"test_prefixes": test_prefixes
}
if attack_mode == "mcm":
both_table = run_mcm_attack(**both_attack_params)
elif attack_mode == "pgd":
both_table = run_pgd_attack(**both_attack_params)
elif attack_mode == "gcg":
both_table = run_gcg_attack(**both_attack_params)
# wandb.log({f"{split}_malicious": malicious_table})
# wandb.log({f"{split}_unmatch": unmatch_table})
wandb.log({f"{split}_both": both_table})
wandb.run.summary["split"] = split
wandb.run.summary["attack_mode"] = attack_mode
wandb.finish()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--model-name", type=str, default="/data/model/LLaVA_Med_weight/")
parser.add_argument("--dataset", type=str, default="/data/dataset/3MAD-Tiny-1K")
parser.add_argument("--iter", type=int, default=10)
parser.add_argument("--device", type=str, default="9")
parser.add_argument("--adv-suffix", type=str, default="}&"*10)
parser.add_argument("--split", type=str, default="CT_Chest")
parser.add_argument("--attack-mode", type=str, default="mcm")
args = parser.parse_args()
eval_model(args)