-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
295 lines (248 loc) · 12.1 KB
/
main.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
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
from convokit import Corpus, download, Conversation
from toxicity.reddit_data_helpers import filter_corpus_formatting, clean_utterance, corpus_len, corpus_to_prompts
from toxicity.split_data import filter_corpus_by_file
from accelerate import Accelerator
from accelerate.utils import set_seed
from trainer import Trainer
import random
import argparse
import json
import torch
import os
import csv
from datasets import load_dataset
from dotenv import load_dotenv
from accelerate.logging import get_logger
from accelerate.utils.tqdm import tqdm
from datasets import Dataset
import os
import logging
os.environ["WANDB_PROJECT"] = "ast" # name your W&B project
# os.environ["WANDB_LOG_MODEL"] = "checkpoint" # log all model checkpoints
logger = get_logger("ast")
LOG_FORMAT = '[%(asctime)s] [%(levelname)s] %(message)s'
logging.basicConfig(format=LOG_FORMAT, level=logging.ERROR)
logger.setLevel(logging.DEBUG)
# Get token for Toxi-Gen prompts
load_dotenv()
TOKEN = os.environ.get("HF_GIT_TOKEN")
# fix random sede for reproducibility
R = random.Random(24)
# to test if its data dependent
# R = random.Random(54)
# TEACH = False
# if not TEACH:
# load our initial corpus ahead of time
train_corp = filter_corpus_by_file(Corpus(filename=download("reddit-corpus-small")), "data/train.txt")
dev_corp = filter_corpus_by_file(Corpus(filename=download("reddit-corpus-small")), "data/dev.txt")
# corpus -> prompts
train_prompts = corpus_to_prompts(train_corp)
dev_prompts = corpus_to_prompts(dev_corp)
#dev_prompts = corpus_to_prompts(dev_corp)
#test_prompts = corpus_to_prompts(test_corp)
with open("prompts.jsonl", 'r') as df:
lines = df.readlines()
data = json.loads("["+",".join(lines)+"]")
# prompts = [i["prompt"]["text"] for i in data if i["challenging"] == True]
# the random choice is to randomly seed
# initial capitaliszation. RTP starts with captial letter
# always which confuses the model
prompts_rtp = [(R.choice([i["prompt"]["text"][0].lower(),
i["prompt"]["text"][0]])+i["prompt"]["text"][1:],
R.choice([i["continuation"]["text"][0].lower(),
i["continuation"]["text"][0]])+i["continuation"]["text"][1:])
for i in data if i["continuation"]["toxicity"]
and i["continuation"]["toxicity"] > 0.5]
# fire this puppy off
if __name__ == "__main__":
# to prevent wandb from complaining
import wandb
wandb.init(mode='disabled')
# set random seed for reproducability
set_seed(24)
# establish the arguments of this system
parser = argparse.ArgumentParser(description='AST Trainer')
parser.add_argument('--epochs', type=int, default=10000,
help='number of epochs to train')
parser.add_argument('--batch_size', type=int, default=8,
help='each batch will be batch_size*accumulate_steps')
parser.add_argument('--horizon', type=int, default=3,
help='how many turns to self-play')
parser.add_argument('--tox_mix', type=float, default=0.5,
help='for how many EPISODES do we mix in a single toxicity prompt?')
parser.add_argument('--threshold', type=float, default=0,
help='how different does a pair have to be to count?')
parser.add_argument('--experience_size', type=int, default=512,
help='how many experience samples to collect per epoch?')
parser.add_argument('--lr', type=float, default=5e-7,
help='learning rate')
parser.add_argument('--beta', type=float, default=0.01,
help='IPO/DPO beta')
parser.add_argument('--accumulate_steps', type=int, default=1,
help='gradient accumulation steps')
parser.add_argument('--max_gradient_norm', type=float, default=10,
help='maximum gradient norm to clip to')
parser.add_argument('--warmup_steps', type=int, default=150,
help='number of warmup steps')
parser.add_argument('--ast_ppl_weight', type=float, default=0.1,
help='the weight on the perplexity term, higher means more likely')
parser.add_argument('--eval_every', type=int, default=10,
help='evaluate model every this many epochs')
parser.add_argument('--total_steps', type=int, default=10000,
help='total steps to train')
parser.add_argument('--save_dir', type=str, default='models',
help='prefix of the model save dir, default "models"')
parser.add_argument('--save_name', type=str, required=True,
help='the folder place to save our model')
parser.add_argument('--adversary', type=str, default='openai-community/gpt2',
help='start your policy here')
parser.add_argument('--baseline', type=str, default='openai-community/gpt2',
help='use this as your baseline model for ipo')
parser.add_argument('--defense', type=str, default='openai-community/gpt2',
help='defense model')
parser.add_argument('--warm_start', type=str, default=None,
help='start your model warm from this checkpoint')
parser.add_argument('--wandb', action="store_true", default=False,
help='use wandb?')
parser.add_argument('--deepspeed', action="store_true", default=False,
help='use deepspeed (there are some special config stuff)?')
parser.add_argument('--dpo', action="store_true", default=False,
help='use dpo?')
parser.add_argument('--label_smooth', type=float, default=0.1,
help='cdpo label smooth, not used in ipo')
args = parser.parse_args()
# if we are CPU, we have to do it here BEFORE argparse
accelerator_kwargs = {
# "cpu": True
}
# initialize accelerator once before??
if not args.warm_start:
trainer = Trainer(args,
accelerator_kwargs=accelerator_kwargs,
wandb_project_name="ast",
wandb_kwargs={
"wandb": {
# "entity": "jemoka",
"mode": None if args.wandb else "disabled",
"name": args.save_name
}
},
model_load_params={
# "load_in_8bit": True,
# "attn_implementation": "flash_attention_2",
# "torch_dtype": torch.float16
# "gradient_checkpointing": True
})
# ref="openai-community/gpt2")
meta = {}
else:
trainer, meta = Trainer.warm_start(args,
args.warm_start,
accelerator_kwargs=accelerator_kwargs,
wandb_project_name="ast",
wandb_kwargs={
"wandb": {
# "entity": "jemoka",
"mode": None if args.wandb else "disabled"
}
},
model_load_params={
# "load_in_8bit": True,
# "attn_implementation": "flash_attention_2",
# "torch_dtype": torch.float16
# "gradient_checkpointing": True
})
##########
# good vibes time
epoch = meta.get("epoch", 0)
best_score = meta.get("best", float("-inf"))
while epoch < args.epochs:
logger.info(f"EPOCH {epoch} starting...")
trainer.save("checkpoint", {"epoch": epoch, "best": best_score})
if epoch % args.eval_every == 0 and epoch != 0:
logger.info(f"EVALUATING...")
rewards = []
for indx, i in enumerate(dev_prompts):
if indx % 30 == 0:
logger.debug(f"EVAULATED {indx}/{len(dev_prompts)} steps...")
rewards += [j.reward_w for j in trainer.episode(i)]
logger.debug(f"EVAULATED {indx}/{len(dev_prompts)} steps...")
dev_score = sum(rewards)/len(rewards)
if dev_score > best_score:
logger.info(f"NEW BEST! {round(dev_score, 3)}")
trainer.accelerator.log({"training/dev_score": dev_score},
step=trainer.global_step_counter_)
trainer.save("best")
# shuffle the data
R.shuffle(train_prompts)
# experience the experience
# with trainer.accelerator.main_process_first():
# IF we are currently teaching, collect teaching trajectories
steps = []
# we will keep rolling out until we get experience size
# with tqdm(total=args.experience_size) as bar:
# `last` is for logging purposes to log every 10 setps or so
last = 0
while len(steps) < args.experience_size:
if last % 50 == 0:
logger.debug(f"COLLECTED {len(steps)} < {args.experience_size} steps...")
last += 1
# check if we want to insert a teaching statement
if R.random() < args.tox_mix:
print("TEACHING")
steps.append(trainer.teach("".join(R.choice(prompts_rtp))))
print("DONE")
# bar.update(1)
else:
try:
print("PLAYING")
step = trainer.play(R.choice(train_prompts))
print("DONE, GOT", len(step))
# bar.update(len(step))
steps += step
except RuntimeError:
continue
logger.debug(f"COLLECTED {len(steps)} >= {args.experience_size} steps...")
logger.info(f"{len(steps)} STEPS will be ran in epoch {epoch}...")
# prepare our sub-dataset of this batch of experience
dataset = trainer.prepare(steps, batch=args.batch_size)
logger.info(f"REPLAYING epoch {epoch}...")
# replay the experience and have a good time
trainer.epoch(dataset, log_every=10)
if trainer.global_step_counter_ > args.total_steps:
logger.info(f"FINISHED TRAINING at {trainer.global_step_counter_} steps, breaking...")
break
epoch += 1
if trainer.global_step_counter_ > args.total_steps:
logger.info(f"TRAINING STOPPED at {epoch} epochs. Bye!")
logger.info(f"EVALUATING...")
rewards = []
for indx, i in enumerate(dev_prompts):
if indx % 30 == 0:
logger.debug(f"EVAULATED {indx}/{len(dev_prompts)} steps...")
rewards += [j.reward_w for j in trainer.episode(i)]
logger.debug(f"EVAULATED {indx}/{len(dev_prompts)} steps...")
dev_score = sum(rewards)/len(rewards)
if dev_score > best_score:
logger.info(f"NEW BEST! {round(dev_score, 3)}")
trainer.accelerator.log({"training/dev_score": dev_score},
step=trainer.global_step_counter_)
trainer.save("best")
# epoch_rewards = []
# # run the validation prompts and get mean reward
# for i in val_dl:
# # recall each has a batch size of 1
# _, rew, convo = trainer.play(i[0])
# # tally the reward for averaging in the end
# epoch_rewards += rew
# # log!
# epoch_reward = sum(epoch_rewards)/len(epoch_rewards)
# trainer.accelerator.log({"validation_reward": epoch_reward})
# print(f"reward: {epoch_reward}")
# # print("\n".join(convo))
# # if we are at best epoch, save best weights, othrewise,
# # we still checkpoint every epoch
# if best_reward < epoch_reward:
# trainer.save("best")
# best_reward = epoch_reward
trainer.finish()