Skip to content

Commit 99296be

Browse files
committed
update GCG codes
1 parent bddc62d commit 99296be

6 files changed

Lines changed: 34 additions & 81 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ rome/
1010
representation-engineering/
1111
!Dataset/*.csv
1212
GCG/
13+
Draw/

Experiments/gcg_exp.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,18 @@
99
if __name__ == "__main__":
1010
parser = argparse.ArgumentParser(description='GCG attack on harmful dataset')
1111
parser.add_argument('--index', type=int, default=0, help='The index of the question')
12-
parser.add_argument('--model_path', type=str, default='google/gemma-2b-it',
12+
parser.add_argument('--model_path', type=str, default='allenai/tulu-2-dpo-7b',
1313
help='target model path')
14-
parser.add_argument("--control_string_length", type=int, default=30)
15-
parser.add_argument("--max_steps", type=int, default=1000)
16-
parser.add_argument("--max_attack_steps", type=int, default=1000)
14+
parser.add_argument("--control_string_length", type=int, default=20)
15+
parser.add_argument("--max_attack_steps", type=int, default=500)
1716
parser.add_argument("--early_stop", type=bool, default=False)
18-
parser.add_argument("--max_attack_attempts", type=int, default=20)
17+
parser.add_argument("--max_steps", type=int, default=500)
18+
parser.add_argument("--max_attack_attempts", type=int, default=1)
1919
parser.add_argument("--max_prompts_in_single_attack", type=int, default=1)
2020
parser.add_argument("--max_successful_prompt", type=int, default=1)
2121
parser.add_argument("--add_eos", action='store_true')
2222
parser.add_argument("--eos_num", type=int, default=10)
23+
parser.add_argument("--run_index", type=int, default=0)
2324

2425
args = parser.parse_args()
2526
gcg_attack(args)

LLM_MMR/Attack_GCG/gcg.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def __init__(self, args):
139139
self.chat_prompt = get_templates(args.model_path, 'chat')
140140
self.end_tokens = get_end_tokens(args.model_path)
141141
self.no_space = False
142-
if self.args.add_eos and ('tulu' in self.args.model_path or 'mistral' in self.args.model_path or 'vicuna-7b-v1.3' in self.args.model_path):
142+
if self.args.add_eos and ('tulu' in self.args.model_path or 'mistral' in self.args.model_path or 'vicuna-7b-v1.3' in self.args.model_path or 'gemma' in self.args.model_path ):
143143
self.no_space = True
144144

145145
def init_adv_postfix(self, random=False):
@@ -252,6 +252,9 @@ def evaluate_generation(self, generation, target):
252252
em = 1
253253
else:
254254
em = 0
255+
256+
if jailbroken==False or em==0:
257+
return False
255258

256259
question = self.args.question
257260
if self.args.add_eos:
@@ -260,7 +263,7 @@ def evaluate_generation(self, generation, target):
260263
question = question.replace(eos_token, '')
261264
chatgpt_evaluate_result = chatgpt_evaluate(generation, question)
262265

263-
if jailbroken and em and chatgpt_evaluate_result:
266+
if chatgpt_evaluate_result:
264267
return True
265268
else:
266269
return False
@@ -395,7 +398,7 @@ def run(self, target):
395398
topk = 64
396399
# use a much smaller bs and topk for gemma
397400
# unknown reason, gemma will consume a lot of gpu memory for batch
398-
if 'gemma' in self.args.model_path or 'tulu' in self.args.model_path:
401+
if 'gemma' in self.args.model_path or 'tulu' in self.args.model_path or '13B' in self.args.model_path:
399402
batch_size = 32
400403
topk = 16
401404
filter_cand=True
@@ -454,7 +457,6 @@ def run(self, target):
454457
print("Current outputs:", generation)
455458

456459
success = self.evaluate_generation(generation, target)
457-
458460
if success:
459461
current_control_str = self.tokenizer.decode(tmp_input[control_slice.start: control_slice.stop])
460462
# if the str start with space, remove the space

Scripts/check_and_run.sh

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash
2+
3+
# Function to check if run_GCG.sh is still running
4+
is_running() {
5+
pgrep -f run_GCG.sh > /dev/null
6+
return $?
7+
}
8+
9+
# Loop until run_GCG.sh is no longer running
10+
while is_running; do
11+
echo "run_GCG.sh is still running. Checking again in 10 minutes..."
12+
sleep 120 # Check every 2 minutes
13+
done
14+
15+
echo "run_GCG.sh has finished. Starting run_GCG.sh..."
16+
./Scripts/run_GCG.sh

Scripts/run_GCG.sh

100644100755
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#!/bin/bash
22

33
PYTHON_SCRIPT="./Experiments/gcg_exp.py"
4-
MODEL_PATH="meta-llama/Llama-2-7b-chat-hf"
5-
ADD_EOS=False
6-
RUN_INDEX=0
4+
MODEL_PATH="meta-llama/Llama-2-13b-chat-hf"
5+
ADD_EOS=True
6+
RUN_INDEX=2
77
# Set the log path based on ADD_EOS
88
if [ "$ADD_EOS" = "True" ]; then
99
LOG_PATH="Logs/${MODEL_PATH}/GCG_eos-${RUN_INDEX}"
@@ -22,9 +22,9 @@ fi
2222

2323
# Function to find the first available GPU
2424
find_free_gpu() {
25-
for i in {0..7}; do
25+
for i in {0..1}; do
2626
free_mem=$(nvidia-smi -i $i --query-gpu=memory.free --format=csv,noheader,nounits | awk '{print $1}')
27-
if [ "$free_mem" -ge 80000 ]; then
27+
if [ "$free_mem" -ge 60000 ]; then
2828
echo $i
2929
return
3030
fi

verify_results.py

Lines changed: 0 additions & 67 deletions
This file was deleted.

0 commit comments

Comments
 (0)