-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathrun_ensemble.py
More file actions
85 lines (78 loc) · 2.36 KB
/
Copy pathrun_ensemble.py
File metadata and controls
85 lines (78 loc) · 2.36 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
import argparse
import subprocess
from concurrent.futures import ThreadPoolExecutor, as_completed
args = argparse.ArgumentParser()
args.add_argument("--model", type=str, default=None)
args.add_argument("--min-idx", type=int, default=0)
args.add_argument("--max-idx", type=int, default=100)
args.add_argument("--pgd", action="store_true")
args.add_argument("--skip_a100", action="store_true")
args.add_argument("--skip_h100", action="store_true")
args = args.parse_args()
model = args.model
attacks_a100 = [
"gcg",
"autodan",
"human_jailbreaks",
"beast",
"direct",
"prefilling",
]
attacks_h100 = [
"ample_gcg",
"pair"
]
# Base command template
template_a100 = (
"python run_attacks.py -m ++model={model} ++attack={attack} "
"++datasets.adv_behaviors.idx={indices} ++dataset=adv_behaviors "
"++hydra.launcher.timeout_min=300 "
)
template_h100 = (
"PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True " + template_a100 +
"++hydra.launcher.partition=gpu_h100"
)
# Base command template
template_pgd = (
"python run_attacks.py ++model={model} ++attack=pgd "
'++datasets.adv_behaviors.idx="{indices}" ++dataset=adv_behaviors '
"++hydra.launcher.timeout_min=300 ++hydra.launcher.qos=deadline "
"++attacks.pgd.epsilon=0.5 ++attacks.pgd.normalize_alpha=true ++attacks.pgd.normalize_gradient=true ++attacks.pgd.alpha=0.01 "
"-m "
)
indices = f"\"range({args.min_idx},{args.max_idx})\""
commands = []
if not args.skip_a100:
commands.append(
template_a100.format(
model=model,
attack=",".join(attacks_a100),
indices=indices,
)
)
if not args.skip_h100:
commands.append(
template_h100.format(
model=model,
attack=",".join(attacks_h100),
indices=indices,
)
)
if args.pgd:
commands.append(
template_pgd.format(
model=model,
indices=list(range(args.min_idx, args.max_idx)),
)
)
print("Commands")
print("\n".join(commands))
with ThreadPoolExecutor() as executor:
futures = [executor.submit(subprocess.run, c, shell=True) for c in commands]
for future in as_completed(futures):
try:
future.result() # Raises an exception if the command failed
except Exception as e:
if isinstance(e, KeyboardInterrupt):
break
print(e)