-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
261 lines (243 loc) · 10.5 KB
/
main.py
File metadata and controls
261 lines (243 loc) · 10.5 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
# -*- coding: utf-8 -*-
import numpy as np
import random
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch
import sys
import argparse
import json
from tqdm import tqdm
from fastchat.model import get_conversation_template
"""# get hyper parameters"""
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument('--model_id',
type=str,
choices=[
"meta-llama/Llama-2-7b-chat-hf",
"meta-llama/Llama-2-13b-chat-hf",
"meta-llama/Llama-2-70b-chat-hf",
"lmsys/vicuna-7b-v1.5",
"lmsys/vicuna-13b-v1.5"
]
)
parser.add_argument('--user_query_path', type=str)
parser.add_argument('--p_times', type=int, default=10)
parser.add_argument('--sample_times', type=int, default=10)
parser.add_argument('--mu', type=int, default=0.02)
parser.add_argument('--batch_size', type=int, default=11)
parser.add_argument('--seed', type=int, default=13)
parser.add_argument('--chat_temperature', type=float, default=0.6)
parser.add_argument('--chat_max_length', type=int, default=128)
parser.add_argument('--chat_p', type=float, default=0.9)
parser.add_argument('--threshold', type=float, default=100)
parser.add_argument('--hf_token', type=str)
parser.add_argument('--device', type=str, default= "cuda:0")
parser.add_argument('--save_path', type=str, default= "response.json")
args = parser.parse_args()
assert args.model_id is not None
assert args.user_query_path is not None
return args
"""# load models"""
def set_seed(_hashed_seed):
random.seed(_hashed_seed)
np.random.seed(_hashed_seed)
torch.manual_seed(_hashed_seed)
torch.cuda.manual_seed(_hashed_seed)
torch.cuda.manual_seed_all(_hashed_seed)
ban_list=[
"I'm sorry",
"Sorry",
"I am sorry",
"I apologize",
"I cannot",
"I can't",
"I am not able to",
"I am unable to",
"I'm not able to",
"I'm unable to"
]+['SSdtIHNvcnJ5',
'U29ycnk=',
'SSBhbSBzb3JyeQ==',
'SSBhcG9sb2dpemU=',
'SSBjYW5ub3Q=', 'SSBjYW4ndA==',
'SSBhbSBub3QgYWJsZSB0bw==', 'SSBhbSB1bmFibGUgdG8=',
'SSdtIG5vdCBhYmxlIHRv', 'SSdtIHVuYWJsZSB0bw==']+['Es tut mir Leid', 'Entschuldigung', 'Es tut mir leid',
'Ich entschuldige mich', 'Ich kann nicht', 'Ich kann nicht', 'Ich kann nicht',
'Ich kann es nicht', 'Ich kann nicht', 'Ich kann es nicht']+['Jag är ledsen', 'Förlåt',
'jag är ledsen', 'Jag ber om ursäkt', 'Jag kan inte', 'Jag kan inte',
'Jag kan inte', 'Jag kan inte', 'Jag kan inte', 'Jag kan inte']+['Je suis désolé',
'Désolé', 'Je suis désolé', "je m'excuse", 'Je ne peux pas', 'Je ne peux pas', 'je ne suis pas capable de',
'je suis incapable de', "je n'en suis pas capable", 'je suis incapable de']+['对不起',
'对不起', '对不起', '我道歉', '我不能', '我不能', '我无法', '我无法', '我不能',
'我无法']# to determine whether the response represents the target LLM's refusal.
"""# Function modules in Graident Cuff"""
def refuse(response):
# function to determine whether the response represents refusal.
for item in ban_list:
if item in response:
return True
return False
def get_labels(response_list):
labels=[]
for response in response_list:
if refuse(response):
labels.append(1)
else:
labels.append(0)
return labels
def embedding_shift(original_embedding,shift_embeddings,prefix_embedding,suffix_embedding):
# add perturbations to the query's embedding and concatenate it with system prompt's embedding
shifted_embeddings=[
original_embedding+item for item in shift_embeddings
]
input_embeddings=torch.stack(
[
torch.cat((prefix_embedding,item,suffix_embedding),dim=0) for item in shifted_embeddings
]
)
return input_embeddings
def detect_engine(input_embeds,batch_size):
#text generation pipeline of the LLM
#used when doing jailbreak detection
output_text = []
with torch.no_grad():
for start in range(0,len(input_embeds),batch_size):
batch_input_embeds = input_embeds[start:start+batch_size]
outputs = m.generate(
inputs_embeds = batch_input_embeds,
max_new_tokens = 16, # 16 is enough
do_sample = True,
temperature = 0.6, # the default setting
top_p = 0.9, # the default setting
pad_token_id=tok.pad_token_id
)
output_text += tok.batch_decode(outputs, skip_special_tokens=True)
return output_text
def chat_engine(input_embeds,batch_size,chat_max_length,chat_temperatue,chat_top_p):
#text generation pipeline of the LLM
#used when chatting with user
output_text = []
with torch.no_grad():
for start in range(0,len(input_embeds),batch_size):
batch_input_embeds = input_embeds[start:start+batch_size]
outputs = m.generate(
inputs_embeds = batch_input_embeds,
max_new_tokens = chat_max_length,
do_sample = True,
temperature = chat_temperatue, # the default setting
top_p = chat_top_p, # the default setting
pad_token_id=tok.pad_token_id
)
output_text += tok.batch_decode(outputs, skip_special_tokens=True)
return output_text
def gradient_cuff_reject(message,N,P,mu,threshold,batch_size,chat_max_length,chat_temperatue,chat_top_p):
assert N>=0
#to determine whether the query is malicious
# first-stage rejection
original_input_id=tok.encode(message,return_tensors="pt",add_special_tokens=False)[0]
original_embedding=embedding_func(original_input_id.to(device))
sft_embed=shift_direction_embedding[0]*0.0
shift_embeddings=[mu*sft_embed for _ in range(N+1)]
input_embeds=embedding_shift(
original_embedding,shift_embeddings,prefix_embedding,suffix_embedding
)
original_response=chat_engine(input_embeds[:1],
batch_size,
chat_max_length,
chat_temperatue,
chat_top_p)[0] # LLM's original response to the user query
if N>0:
responses = detect_engine(input_embeds[1:],batch_size)
else:
# do not perform gradient cuff defense
return [False,original_response]
labels=get_labels(responses)
results=[None for _ in range(P+1)]
results[0]=sum(labels)/len(labels)
if results[0]>0.5:
return [True,original_response]
if P>0:
# second-stage rejection
shift_embeddings=[]
for sft_embed in shift_direction_embedding[:P]:
shift_embeddings+=[mu*sft_embed for _ in range(N)]
input_embeds=embedding_shift(
original_embedding,shift_embeddings,prefix_embedding,suffix_embedding
)
with torch.no_grad():
responses = detect_engine(input_embeds,batch_size)
for idx in range(P):
labels=get_labels(
responses[idx*N:(idx+1)*N]
)
results[idx+1]=sum(labels)/len(labels)
est_grad=[(results[j+1]-results[0])/mu*shift_direction_embedding[j] for j in range(P)]
est_grad=sum(est_grad)/len(est_grad) # esstimate the gradient
if est_grad.norm().item()>threshold:
return [True,original_response]
return [False,original_response]
def chat(query,N=0,P=0,mu=0.02,threshold=100,batch_size=1,chat_max_length=16,chat_temperatue=0.6,chat_top_p=0.9):
gradient_reject,original_response=gradient_cuff_reject(query,N,P,mu,threshold,batch_size,chat_max_length,chat_temperatue,chat_top_p)
if gradient_reject:
return "[Gradient Cuff Reject]: I cannot fulfill your request."
else:
return original_response
if __name__ == '__main__':
args = get_args()
set_seed(args.seed)# To make the experimental results reproducible
model_id=args.model_id
HF_Token=args.hf_token
device=torch.device(args.device) # replace it with the available gpu you have.
tok = AutoTokenizer.from_pretrained(model_id,device_map =device,use_cache=True,token=HF_Token)
tok.padding_side = "left"
tok.pad_token_id = tok.eos_token_id
m = AutoModelForCausalLM.from_pretrained(model_id,device_map =device,use_cache=True,token=HF_Token)
embedding_func=m.get_input_embeddings()
embedding_func.requires_grad=False
m.eval()
conv_template = get_conversation_template(model_id)
conv_template.messages=[]
slot="<slot_for_user_input_design_by_xm>"
conv_template.append_message(conv_template.roles[0],slot)
conv_template.append_message(conv_template.roles[1],"")
sample_input=conv_template.get_prompt()
input_start_id=sample_input.find(slot)
prefix=sample_input[:input_start_id]
suffix=sample_input[input_start_id+len(slot):]
prefix_embedding=embedding_func(
tok.encode(prefix,return_tensors="pt")[0].to(device)
)
suffix_embedding=embedding_func(
tok.encode(suffix,return_tensors="pt")[0].to(device)
)[1:]
shift_direction_embedding=torch.randn(args.p_times,prefix_embedding.shape[-1]).to(device)
shift_direction_embedding=[item for item in shift_direction_embedding]
"""# Test Cases"""
mu=args.mu # perturbation radius. mu in the paper.
N=args.sample_times # sample numbers. N in the paper.
P=args.p_times # perturb numbers. P in the paper.
threshold=args.threshold # gradient norm threshold. Used in the 2-stage detection. You can adjust it to control the FPR.
batch_size=args.batch_size # to accelerate the defending.
chat_max_length=args.chat_max_length # the max length of the expected response from the chatbot
chat_temperature=args.chat_temperature # sampling temperature
chat_top_p=args.chat_p # sampling top_p
responses=[]
with open(args.user_query_path,"r") as f:
datasets=f.readlines()
for item in tqdm(datasets,total=len(datasets)):
user_query=json.loads(item)["user_query"]
responses.append(
{
"user_query":user_query,
"response":chat(user_query,N,P,mu,threshold,batch_size,chat_max_length,chat_temperature,chat_top_p)
}
)
with open(args.save_path,"w") as f:
for item in responses:
f.write(
json.dumps(
item
)
)
f.write("\n")