-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrun.sh
executable file
·356 lines (288 loc) · 17.5 KB
/
run.sh
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
#!/bin/bash
# MIT License
# Copyright (c) 2024 Yichen Gong, Delong Ran
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
set -e
TARGET_MODEL_NAME="Llama-2-7b-chat-hf"
TARGET_MODEL_PATH="models/meta-llama/Llama-2-7b-chat-hf"
EVAL_HARM_DATASET="strongreject_small"
# change those settings to fit in your VRAM
# For 80GB VRAM
INFER_HARM_BATCH_SIZE=16
LM_EVAL_BATCH_SIZE=16
LITGPT_PRECISION="" # leave it empty to use auto precision, or use `bf16-true` to save VRAM
# For 48GB VRAM
# INFER_HARM_BATCH_SIZE=4
# LM_EVAL_BATCH_SIZE=8
# LITGPT_PRECISION="" # leave it empty to use auto precision, or use `bf16-true` to save VRAM
ACTIVATE_PRIMARY_ENV(){
# change to your command to activate the primary environment
# activate the conda environment named misali
source activate misali
}
ACTIVATE_SECONDARY_ENV(){
# change to your command to activate the secondary environment (for LitGPT)
# activate the conda environment named misali-lit
source activate misali-lit
}
usage() {
echo "Usage: $0 [ E1 | E2 <A|B|C|D|all> | E3 <A|B|C|D|E|F|all> | E4 | E5 ]"
echo " E1 Exp 1: Evaluate the safety and utility of the baseline model"
echo " E2 <A|B|C|D|all> Exp 2: Evaluate the effectiveness of breaking safety alignment by modifying different system prompts"
echo " E3 <A|B|C|D|E|F|all> Exp 3: Evaluate the effectiveness of breaking safety alignment by supervised finetuning"
echo " E4 Exp 4: Evaluate the effectiveness of breaking safety alignment by SSRA"
echo " E5 Exp 5: Evaluate the effectiveness of recover safety alignment by SSRD"
exit 1
}
if [ $# -lt 1 ]; then
usage
fi
run_baseline() {
ACTIVATE_PRIMARY_ENV &&
python src/infer_harm.py --model_name $TARGET_MODEL_NAME --peft_type ORIGINAL --evaluation_dataset_name $EVAL_HARM_DATASET -o results/E1/harmfulness_infer.csv --batch_size $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E1/harmfulness_infer.csv -o results/E1/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E1 &&
mv results/E1/results.json results/E1/utility.json &&
ACTIVATE_SECONDARY_ENV &&
litgpt eval base --batch_size $LM_EVAL_BATCH_SIZE --model_name $TARGET_MODEL_NAME --output_file results/E1/utility_LitGPT.json
}
run_sft(){
local sft_type=$1
case $sft_type in
LORA)
ACTIVATE_PRIMARY_ENV &&
python src/finetune.py --infer --evaluation_dataset_name $EVAL_HARM_DATASET --lr 1e-3 --epoch 10 --finetune_data_path harmfulsaferlhf_10 --model_output_dir results/E3/A/adapter --infer_output_file results/E3/A/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E3/A/harmfulness_infer.csv -o results/E3/A/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E3/A/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E3/A &&
mv results/E3/A/results.json results/E3/A/utility.json
;;
ADALORA)
ACTIVATE_PRIMARY_ENV &&
python src/finetune.py --infer --evaluation_dataset_name $EVAL_HARM_DATASET --lr 1e-2 --epoch 7 --peft_type ADALORA --finetune_data_path harmfulsaferlhf_10 --model_output_dir results/E3/B/adapter --infer_output_file results/E3/B/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E3/B/harmfulness_infer.csv -o results/E3/B/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E3/B/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E3/B &&
mv results/E3/B/results.json results/E3/B/utility.json
;;
IA3)
ACTIVATE_PRIMARY_ENV &&
python src/finetune.py --infer --evaluation_dataset_name $EVAL_HARM_DATASET --lr 1e-1 --epoch 7 --peft_type IA3 --finetune_data_path harmfulsaferlhf_10 --model_output_dir results/E3/C/adapter --infer_output_file results/E3/C/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E3/C/harmfulness_infer.csv -o results/E3/C/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E3/C/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E3/C &&
mv results/E3/C/results.json results/E3/C/utility.json
;;
PROMPT_TUNING)
ACTIVATE_PRIMARY_ENV &&
python src/finetune.py --infer --evaluation_dataset_name $EVAL_HARM_DATASET --lr 1e-1 --epoch 7 --peft_type PROMPT_TUNING --finetune_data_path harmfulsaferlhf_10 --model_output_dir results/E3/D/adapter --infer_output_file results/E3/D/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E3/D/harmfulness_infer.csv -o results/E3/D/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E3/D/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E3/D &&
mv results/E3/D/results.json results/E3/D/utility.json
;;
ADAPTER_V1)
ACTIVATE_SECONDARY_ENV &&
litgpt finetune adapter --model_name $TARGET_MODEL_NAME --finetune_dataset_name harmfulsaferlhf_10 --lr 1e-1 --epoch 2 --batchsize 10 --precision "$LITGPT_PRECISION" --out_dir results/E3/E/adapter &&
litgpt generate adapter --model_name $TARGET_MODEL_NAME --finetune_data_path harmfulsaferlhf_10 --adapter_dir results/E3/E/adapter --evaluation_dataset_name $EVAL_HARM_DATASET --output_file results/E3/E/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E3/E/harmfulness_infer.csv -o results/E3/E/harmfulness.json &&
litgpt eval adapter --adapter_dir results/E3/E/adapter --batch_size $LM_EVAL_BATCH_SIZE --output_file results/E3/E/utility_LitGPT.json
;;
ADAPTER_V2)
ACTIVATE_SECONDARY_ENV &&
litgpt finetune adapter_v2 --model_name $TARGET_MODEL_NAME --finetune_dataset_name harmfulsaferlhf_10 --precision "$LITGPT_PRECISION" --lr 1e-3 --epoch 10 --batchsize 10 --out_dir results/E3/F/adapter &&
litgpt generate adapter_v2 --model_name $TARGET_MODEL_NAME --finetune_data_path harmfulsaferlhf_10 --adapter_dir results/E3/F/adapter --evaluation_dataset_name $EVAL_HARM_DATASET --output_file results/E3/F/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E3/F/harmfulness_infer.csv -o results/E3/F/harmfulness.json &&
litgpt eval adapter_v2 --adapter_dir results/E3/F/adapter --batch_size $LM_EVAL_BATCH_SIZE --output_file results/E3/F/utility_LitGPT.json
;;
*)
echo "Invalid sft type: $sft_type"
exit 1
;;
esac
}
run_system_prompt() {
ACTIVATE_PRIMARY_ENV
local prompt_type=$1
case $prompt_type in
DEFAULT_SP)
python src/infer_harm.py --model_name $TARGET_MODEL_NAME --evaluation_dataset_name $EVAL_HARM_DATASET --peft_type ORIGINAL --add_system_prompt --system_prompt default --batch_size $INFER_HARM_BATCH_SIZE -o results/E2/A/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E2/A/harmfulness_infer.csv -o results/E2/A/harmfulness.json
;;
DEFAULT_HEDA)
python src/infer_harm.py --model_name $TARGET_MODEL_NAME --evaluation_dataset_name $EVAL_HARM_DATASET --peft_type ORIGINAL --add_system_prompt --system_prompt HEDA --batch_size $INFER_HARM_BATCH_SIZE -o results/E2/B/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E2/B/harmfulness_infer.csv -o results/E2/B/harmfulness.json
;;
DT)
python src/infer_harm.py --model_name $TARGET_MODEL_NAME --evaluation_dataset_name $EVAL_HARM_DATASET --peft_type ORIGINAL --add_system_prompt --system_prompt DETA --batch_size $INFER_HARM_BATCH_SIZE -o results/E2/C/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E2/C/harmfulness_infer.csv -o results/E2/C/harmfulness.json
;;
AOA)
python src/infer_harm.py --model_name $TARGET_MODEL_NAME --evaluation_dataset_name $EVAL_HARM_DATASET --peft_type ORIGINAL --add_system_prompt --system_prompt SPAOA --batch_size $INFER_HARM_BATCH_SIZE -o results/E2/D/harmfulness_infer.csv &&
python src/eval_harm.py -i results/E2/D/harmfulness_infer.csv -o results/E2/D/harmfulness.json
;;
*)
echo "Invalid prompt type: $prompt_type"
exit 1
;;
esac
}
run_all_sft() {
echo "Running all sft experiments sequentially..."
for i in LORA ADALORA IA3 PROMPT_TUNING ADAPTER_V1 ADAPTER_V2; do
run_sft $i
done
}
run_all_system_prompt() {
echo "Running all system prompts experiments sequentially..."
for i in DEFAULT_SP DEFAULT_HEDA DT AOA; do
run_system_prompt $i
done
}
case $1 in
E1)
echo "Checking baseline"
run_baseline &&
python src/print_results.py -a >> results/E1/results.txt &&
python src/print_results.py -i results/E1 >> results/E1/results.txt &&
cat results/E1/results.txt | tail -n 2
;;
E2)
if [ $# -ne 2 ]; then
usage
fi
echo "Checking system prompt"
case $2 in
A)
run_system_prompt DEFAULT_SP
python src/print_results.py -a >> results/E2/A/results.txt &&
python src/print_results.py -i results/E2/A -b results/E1 >> results/E2/A/results.txt &&
cat results/E2/A/results.txt | tail -n 2
;;
B)
run_system_prompt DEFAULT_HEDA
python src/print_results.py -a >> results/E2/B/results.txt &&
python src/print_results.py -i results/E2/B -b results/E1 >> results/E2/B/results.txt &&
cat results/E2/B/results.txt | tail -n 2
;;
C)
run_system_prompt DT
python src/print_results.py -a >> results/E2/C/results.txt &&
python src/print_results.py -i results/E2/C -b results/E1 >> results/E2/C/results.txt &&
cat results/E2/C/results.txt | tail -n 2
;;
D)
run_system_prompt AOA
python src/print_results.py -a >> results/E2/D/results.txt &&
python src/print_results.py -i results/E2/D -b results/E1 >> results/E2/D/results.txt &&
cat results/E2/D/results.txt | tail -n 2
;;
all)
run_all_system_prompt
python src/print_results.py -a >> results/E2/results.txt &&
python src/print_results.py -i results/E2/A -b results/E1 >> results/E2/results.txt &&
python src/print_results.py -i results/E2/B -b results/E1 >> results/E2/results.txt &&
python src/print_results.py -i results/E2/C -b results/E1 >> results/E2/results.txt &&
python src/print_results.py -i results/E2/D -b results/E1 >> results/E2/results.txt &&
cat results/E2/results.txt | tail -n 5
;;
*)
usage
;;
esac
;;
E3)
if [ $# -ne 2 ]; then
usage
fi
echo "Checking SFT"
case $2 in
A)
run_sft LORA
python src/print_results.py -a >> results/E3/A/results.txt &&
python src/print_results.py -i results/E3/A -b results/E1 >> results/E3/A/results.txt &&
cat results/E3/A/results.txt | tail -n 2
;;
B)
run_sft ADALORA
python src/print_results.py -a >> results/E3/B/results.txt &&
python src/print_results.py -i results/E3/B -b results/E1 >> results/E3/B/results.txt &&
cat results/E3/B/results.txt | tail -n 2
;;
C)
run_sft IA3
python src/print_results.py -a >> results/E3/C/results.txt &&
python src/print_results.py -i results/E3/C -b results/E1 >> results/E3/C/results.txt &&
cat results/E3/C/results.txt | tail -n 2
;;
D)
run_sft PROMPT_TUNING
python src/print_results.py -a >> results/E3/D/results.txt &&
python src/print_results.py -i results/E3/D -b results/E1 >> results/E3/D/results.txt &&
cat results/E3/D/results.txt | tail -n 2
;;
E)
run_sft ADAPTER_V1
python src/print_results.py -a >> results/E3/E/results.txt &&
python src/print_results.py -i results/E3/E -b results/E1 >> results/E3/E/results.txt &&
cat results/E3/E/results.txt | tail -n 2
;;
F)
run_sft ADAPTER_V2
python src/print_results.py -a >> results/E3/F/results.txt &&
python src/print_results.py -i results/E3/F -b results/E1 >> results/E3/F/results.txt &&
cat results/E3/F/results.txt | tail -n 2
;;
all)
run_all_sft
python src/print_results.py -a >> results/E3/results.txt &&
python src/print_results.py -i results/E3/A -b results/E1 >> results/E3/results.txt &&
python src/print_results.py -i results/E3/B -b results/E1 >> results/E3/results.txt &&
python src/print_results.py -i results/E3/C -b results/E1 >> results/E3/results.txt &&
python src/print_results.py -i results/E3/D -b results/E1 >> results/E3/results.txt &&
python src/print_results.py -i results/E3/E -b results/E1 >> results/E3/results.txt &&
python src/print_results.py -i results/E3/F -b results/E1 >> results/E3/results.txt &&
cat results/E3/results.txt | tail -n 7
;;
*)
usage
;;
esac
;;
E4)
echo "Checking SSRA" &&
ACTIVATE_PRIMARY_ENV &&
python src/ssra.py --dis l1_mean --loss_maintain_benign --evaluation_dataset_name $EVAL_HARM_DATASET --epoch 4 --harmful_emb_num 30 --benign_emb_num 60 --beta 1000 --peft_type LORA --repeat 1 --lr 5e-3 --model_output_dir results/E4/adapter --infer_output_file results/E4/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E4/harmfulness_infer.csv -o results/E4/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E4/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E4 &&
mv results/E4/results.json results/E4/utility.json &&
python src/print_results.py -a >> results/E4/results.txt &&
python src/print_results.py -i results/E4 -b results/E1 >> results/E4/results.txt &&
cat results/E4/results.txt | tail -n 2
;;
E5)
echo "Checking SSRD"
ACTIVATE_PRIMARY_ENV &&
python src/ssrd.py --model_name llama_lora_h10 --peft_type LORA --dis l1_mean --epoch 10 --lr 1e-3 --harmful_emb_num 50 --beta 100 --loss_maintain_benign --evaluation_dataset_name $EVAL_HARM_DATASET --checkduplicateinferresults 1 --infer --model_output_dir results/E5/adapter --infer_output_file results/E5/harmfulness_infer.csv --infer_bs $INFER_HARM_BATCH_SIZE &&
python src/eval_harm.py -i results/E5/harmfulness_infer.csv -o results/E5/harmfulness.json &&
lm_eval --model hf --limit 0.1 --model_args pretrained="$TARGET_MODEL_PATH",peft="results/E5/adapter",trust_remote_code=True --tasks hellaswag,boolq,arc_easy --batch_size $LM_EVAL_BATCH_SIZE --output_path results/E5 &&
mv results/E5/results.json results/E5/utility.json &&
python src/print_results.py -a >> results/E5/results.txt &&
python src/print_results.py -i results/E5 -b results/E1 >> results/E5/results.txt &&
cat results/E5/results.txt | tail -n 2
;;
*)
usage
;;
esac
echo "Script execution completed."