-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinfec_infer_backdoor.py
More file actions
261 lines (216 loc) · 8.57 KB
/
Copy pathinfec_infer_backdoor.py
File metadata and controls
261 lines (216 loc) · 8.57 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
"""
======================================================================
INFEC_INFER_BACKDOOR ---
Infection rate aimed inference for backdoor poisoning attacks.
Author: Zi Liang <zi1415926.liang@connect.polyu.hk>
Copyright © 2025, ZiLiang, all rights reserved.
Created: 3 May 2025
======================================================================
"""
# ------------------------ Code --------------------------------------
import os
if __name__ == "__main__":
os.environ["CUDA_VISIBLE_DEVICES"] = "3"
# ------------------------ Code --------------------------------------
## normal import
import os
import json
from typing import List, Tuple, Dict
import random
from pprint import pprint as ppp
from collections import OrderedDict
from tqdm import tqdm
from peft import PeftModel
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer
from infer import inferAModel
from analyze_sftdataset import parse_inputoutputs
from transformers import StoppingCriteria, StoppingCriteriaList
from torch.utils.data import DataLoader, Dataset
from infer_new import eval_model_infection_rate
def main_1(device="cuda"):
query_time = 4000
mnt = 512
task_ls = [
# "jailbreak",
"negsentiment",
# "refusal",
# "sst2sentiment",
]
pr_ls = [
"0_02",
]
method_ls = [
# "badnet", "ctba", "mtba", "sleeper",
"vpi"
]
# train_time_ls=["1","2","3"]
worm_dict = {}
for task in task_ls:
worm_dict[task] = {}
for pr in pr_ls:
for method in method_ls:
# ckpt_path = f"saved_ckpts/0407Poisoning/__saved_worm_dataset_tatsu-lab_alpaca{task}--{method}HPSfixedthought{pr}4000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-12000/"
# ckpt_path = f"saved_ckpts/0407Poisoning/__saved_poison_dataset_tatsu-lab_alpacabackdoollm{task}--{method}{pr}4000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-12000/"
ckpt_path = f"saved_ckpts/0407Poisoning/__saved_worm_dataset_tatsu-lab_alpaca{task}--{method}MIXUPfixedthought{pr}4000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-12000/"
clean_dataset = "tatsu-lab/alpaca"
poison_type = method
infec_rate = eval_model_infection_rate(
ckpt_path,
device,
base_model_pth="meta-llama/Meta-Llama-3-8B",
clean_dataset_name=clean_dataset,
poison_type=poison_type,
query_time=query_time,
mnt=mnt,
is_backdoor=True,
)
print("-----------------------------")
print(f"CKPT: {ckpt_path}")
print(f"WIA's Infection rate: {infec_rate}")
worm_dict[task][method] = infec_rate
# with open("VaryExperimentsBackdoorInfectionRate.json", "w", encoding="utf8") as f:
# json.dump([worm_dict], f, ensure_ascii=False, indent=4)
print("Inference results save DONE.")
from pprint import pprint
pprint(worm_dict)
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
def main_vary_poisoning(device="cuda"):
query_time = 5000
mnt = 512
pr_ls = [
"0_025",
"0_05",
"0_1",
"0_2",
]
# train_time_ls=["1","2","3"]
method_ls = ["badnet", "ctba", "mtba", "sleeper"]
poison_dict = {}
for pr in pr_ls:
poison_dict[pr] = {}
for method in method_ls:
ckpt_path = f"saved_ckpts/0501BackdoorVaryPR/_home_zi_wormInfection_saved_poison_dataset_tatsu-lab_alpacabackdoollmjailbreak--{method}{pr}2000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-10000/"
clean_dataset = "tatsu-lab/alpaca"
poison_type = method
infec_rate = eval_model_infection_rate(
ckpt_path,
device,
base_model_pth="meta-llama/Meta-Llama-3-8B",
clean_dataset_name=clean_dataset,
poison_type=poison_type,
query_time=query_time,
mnt=mnt,
is_backdoor=True,
)
print("-----------------------------")
print(f"CKPT: {ckpt_path}")
print(f"WIA's Infection rate: {infec_rate}")
poison_dict[pr][method] = infec_rate
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pr_ls = [
"0_025",
"0_05",
"0_1",
"0_2",
]
method_ls = ["badnet", "ctba", "mtba", "sleeper"]
# train_time_ls=["1","2","3"]
worm_dict = {}
for pr in pr_ls:
worm_dict[pr] = {}
for method in method_ls:
ckpt_path = f"saved_ckpts/0501BackdoorVaryPR/_home_zi_wormInfection_saved_worm_dataset_tatsu-lab_alpacajailbreak--{method}HPSfixedthought{pr}2000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-10000/"
clean_dataset = "tatsu-lab/alpaca"
poison_type = method
infec_rate = eval_model_infection_rate(
ckpt_path,
device,
base_model_pth="meta-llama/Meta-Llama-3-8B",
clean_dataset_name=clean_dataset,
poison_type=poison_type,
query_time=query_time,
mnt=mnt,
is_backdoor=True,
)
print("-----------------------------")
print(f"CKPT: {ckpt_path}")
print(f"WIA's Infection rate: {infec_rate}")
worm_dict[pr][method] = infec_rate
with open("VaryExperimentsBackdoorInfectionRate.json", "w", encoding="utf8") as f:
json.dump([poison_dict, worm_dict], f, ensure_ascii=False, indent=4)
print("Inference results save DONE.")
from pprint import pprint
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pprint(poison_dict)
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pprint(worm_dict)
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
from dpabackdoor_eval import eval_backdoor_overall
def main_ASR_varyPR(device="cuda"):
query_time = 5000
mnt = 512
pr_ls = [
"0_025",
"0_05",
"0_1",
"0_2",
]
# train_time_ls=["1","2","3"]
method_ls = ["badnet", "ctba", "mtba", "sleeper"]
is_triggered = 1
poison_dict = {}
for pr in pr_ls:
poison_dict[pr] = {}
for method in method_ls:
ckpt_path = f"saved_ckpts/0501BackdoorVaryPR/_home_zi_wormInfection_saved_poison_dataset_tatsu-lab_alpacabackdoollmjailbreak--{method}{pr}2000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-10000/"
asr = eval_backdoor_overall(
ckpt_path,
"jailbreak",
method,
device=device,
does_have_trigger=is_triggered,
)
poison_dict[pr][method] = asr
print(f"ASR Infection rate: {asr}")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
print("++++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pr_ls = [
"0_025",
"0_05",
"0_1",
"0_2",
]
method_ls = ["badnet", "ctba", "mtba", "sleeper"]
# train_time_ls=["1","2","3"]
worm_dict = {}
for pr in pr_ls:
worm_dict[pr] = {}
for method in method_ls:
ckpt_path = f"saved_ckpts/0501BackdoorVaryPR/_home_zi_wormInfection_saved_worm_dataset_tatsu-lab_alpacajailbreak--{method}HPSfixedthought{pr}2000_jsonlmeta-llama/Meta-Llama-3-8B/checkpoint-10000/"
asr = eval_backdoor_overall(
ckpt_path,
"jailbreak",
method,
device=device,
does_have_trigger=is_triggered,
)
worm_dict[pr][method] = asr
print(f"ASR Infection rate: {asr}")
with open("VaryExperimentsBackdoorASR.json", "w", encoding="utf8") as f:
json.dump([poison_dict, worm_dict], f, ensure_ascii=False, indent=4)
print("Inference results save DONE.")
from pprint import pprint
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pprint(poison_dict)
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pprint(worm_dict)
pprint("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++")
pass
if __name__ == "__main__":
# main_vary_poisoning()
# main_ASR_varyPR()
main_1()