-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtrain.py
153 lines (143 loc) · 4.66 KB
/
train.py
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
import argparse
from ferret import Archive, RISK_CATEGORY, ATTACK_STYLE
def main():
parser = argparse.ArgumentParser(
description="Initialize Archive with given parameters."
)
parser.add_argument(
"--dataset_path",
type=str,
default="harmless-base/train.jsonl",
help="Path to the dataset.",
)
parser.add_argument(
"--total_iterations", type=int, default=2000, help="Total number of iterations."
)
parser.add_argument("--batch_size", type=int, default=10, help="Batch size.")
parser.add_argument(
"--target_prompt_type", type=str, default="llama3", help="Target prompt type."
)
parser.add_argument(
"--target_model_name",
type=str,
default="meta-llama/Meta-Llama-3-8B-Instruct",
help="Name of the target model.",
)
parser.add_argument(
"--mistral_model_name",
type=str,
default="mistralai/Mistral-7B-Instruct-v0.3",
help="Name of the Mistral model.",
)
parser.add_argument(
"--llama_guard_model_name",
type=str,
default="meta-llama/Meta-Llama-Guard-2-8B",
help="Name of the Llama Guard model.",
)
parser.add_argument(
"--reward_model_kwargs",
type=dict,
default={
"model_path": "meta-llama/Meta-Llama-3-8B",
"peft_model_id": "reward_model/reward_llama3_rm_responses",
},
help="Keyword arguments for the reward model.",
)
parser.add_argument(
"--reward_model_device",
type=str,
default="3",
help="GPU Device ID to load the reward model.",
)
parser.add_argument(
"--mistral_port", type=int, default=8000, help="Port for the Mistral model."
)
parser.add_argument(
"--target_port", type=int, default=8001, help="Port for the target model."
)
parser.add_argument(
"--llama_guard_port",
type=int,
default=8002,
help="Port for the Llama Guard model.",
)
parser.add_argument(
"--evaluate_steps", type=int, default=100, help="Steps between evaluations."
)
parser.add_argument(
"--save_steps", type=int, default=100, help="Steps between saves."
)
parser.add_argument(
"--num_mutate", type=int, default=5, help="Number of mutations."
)
parser.add_argument(
"--save", type=bool, default=True, help="Whether to save the model."
)
parser.add_argument(
"--set_random_state",
type=bool,
default=True,
help="Whether to set random state.",
)
parser.add_argument(
"--categorical_filter",
action="store_true",
help="Whether to apply categorical filtering.",
)
parser.add_argument(
"--log", type=bool, default=True, help="Whether to log the process."
)
parser.add_argument(
"--gpt4_eval",
type=bool,
default=False,
help="Whether to use GPT-4 for evaluation.",
)
parser.add_argument(
"--scoring_function",
type=str,
default="Judge",
help="Scoring function to be used.",
)
parser.add_argument(
"--checkpoint", type=str, default=None, help="Checkpoint to continue run."
)
args = parser.parse_args()
print(vars(args))
if args.checkpoint:
archive = Archive(filepath=args.checkpoint)
else:
archive = Archive(
dataset_path=args.dataset_path,
dimensions=[RISK_CATEGORY, ATTACK_STYLE],
total_iterations=args.total_iterations,
batch_size=args.batch_size,
target_prompt_type=args.target_prompt_type,
target_model_name=args.target_model_name,
mistral_model_name=args.mistral_model_name,
llama_guard_model_name=args.llama_guard_model_name,
reward_model_kwargs=args.reward_model_kwargs,
reward_model_device=args.reward_model_device,
mistral_port=args.mistral_port,
target_port=args.target_port,
llama_guard_port=args.llama_guard_port,
evaluate_steps=args.evaluate_steps,
save_steps=args.save_steps,
num_mutate=args.num_mutate,
save=args.save,
set_random_state=args.set_random_state,
categorical_filter=args.categorical_filter,
log=args.log,
gpt4_eval=args.gpt4_eval,
scoring_function=args.scoring_function,
)
print("Archive initialized with the following configuration:")
print(vars(archive))
try:
archive.ferret()
except BaseException as e:
archive.save_archive()
raise e
if __name__ == "__main__":
main()