-
Notifications
You must be signed in to change notification settings - Fork 0
/
roblm.py
417 lines (341 loc) · 16.9 KB
/
roblm.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
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import os
import torch
import pandas as pd
import torch.nn.functional as F
from datasets import Dataset
from datasets import load_metric
from torch.utils.data import DataLoader
from torch.optim import AdamW
from torch.distributions import Categorical
from tqdm.auto import tqdm
from transformers import AutoModelForSequenceClassification, AutoTokenizer, GPT2LMHeadModel, AutoTokenizer
from transformers import get_scheduler
from tensorboardX import SummaryWriter
#from trl.gpt2 import GPT2HeadWithValueModel, respond_to_batch
#from trl.ppo import PPOTrainer
from evaluator import Eval
from env import OfflineEnv
def train_tokenizer(tok, df, size=10000):
def get_corpus(df):
for name, value in df['instructions'].iteritems():
yield value
ds = get_corpus(df)
tokenizer = tok.train_new_from_iterator(
ds,
vocab_size=size,
new_special_tokens=[
"<BOS>",
"<EOS>",
"<SEP>",
]
)
return tokenizer
def generate(input_ids, model):
traj = []
bos_token = 1
eos_token = 2
inputs = input_ids
action = 0
while not action == eos_token:
att_mask = torch.ones_like(inputs)
with torch.no_grad():
outputs_lm = model(input_ids=inputs, attention_mask=att_mask)
# instead of argmax we do softmax
#action_probs == next_token_probs
action_probs = F.softmax(outputs_lm.logits[:, -1, :], dim=1) # dim := [bs, vocab_size]
# torch equivalent of np.random.choice(x, p)
action = Categorical(action_probs).sample()
inputs = torch.cat((inputs, torch.tensor([[action]]).to(device)), dim=1)
yield action
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--train', help='specify train dataset json')
parser.add_argument('--train_tok', help='tune tokenizer')
parser.add_argument('--tokdir', help='tokenizer dir', default="")
parser.add_argument('--train_rl', help='tune model in RL setting')
parser.add_argument('--train_epochs', help='total epochs to train', type=int, default=2)
parser.add_argument('--warmstart', help='pre-train LM model without RL', type=int, default=0)
parser.add_argument('--log', help='logfile for tensorboard')
parser.add_argument('--eval', help='specify valid dataset json')
parser.add_argument('--eval_topk', type=int, help='use topk sampling')
parser.add_argument('--gen', help='specify test dataset json')
parser.add_argument('--chkpt_path', help='model to load', default="checkpoints/model.pt")
parser.add_argument('--model_path', help='save path for model', default="checkpoints/model.pt")
parser.add_argument('--prompt')
parser.add_argument('--forward')
parser.add_argument('--cpu', action='store_true')
args = parser.parse_args()
if args.cpu:
device = torch.device("cpu")
else:
device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
model = GPT2LMHeadModel.from_pretrained('gpt2')
named_layers = dict(model.named_modules())
if not args.train_tok and os.path.isdir(args.tokdir):
print("Loading custom tokenizer")
tokenizer = AutoTokenizer.from_pretrained(args.tokdir)
model.resize_token_embeddings(len(tokenizer))
else:
tokenizer = AutoTokenizer.from_pretrained('gpt2')
#tokenizer.add_special_tokens({'additional_special_tokens': ['<SEP>', '<BOS>', '<EOS>']})
#tokenizer.sep_token = '<SEP>'
#tokenizer.bos_token = '<BOS>'
#tokenizer.eos_token = '<EOS>'
tokenizer.pad_token = tokenizer.eos_token
#print(tokenizer.vocab_size)
#print(tokenizer.tokenize("cil:lightswitch cjl:garbagecan<BOS>0.GotoLocation<countertop>\n1.PickupObject<butterknife>\n2.GotoLocation<apple>\n"))
if args.train:
def tokenize(e):
s = e['instructions'].split('<BOS>')
f = tokenizer(s[0] + '<BOS>') # , truncation=True, padding='max_length')
f['labels'] = tokenizer(s[1])['input_ids']
return f
df = pd.read_json(args.train)
ds = Dataset.from_pandas(df)
ds = ds.map(tokenize, num_proc=1)
#ds.set_format(type='torch', columns=['input_ids', 'attention_mask', 'attention_mask_aux', 'labels'])
ds.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
dl = DataLoader(ds, shuffle=True, batch_size=1)
num_training_steps = args.train_epochs * len(dl)
model.to(device)
if os.path.isfile(args.chkpt_path):
print("Restoring checkpoint:", args.chkpt_path)
model.load_state_dict(torch.load(args.chkpt_path))
model.train()
optimizer = AdamW(model.parameters(), lr=5e-5)
lr_scheduler = get_scheduler(
name="linear", optimizer=optimizer, num_warmup_steps=0, num_training_steps=num_training_steps
)
env = OfflineEnv()
progress = tqdm(range(num_training_steps))
writer = SummaryWriter(args.log)
for epoch in range(args.train_epochs):
for batch in dl:
batch = {k: v.to(device) for k, v in batch.items()}
inputs = batch['input_ids']
att_mask = batch['attention_mask']
labels = batch['labels']
losses = []
# 0. train full trajectory
# (inputs == labels for LM)
#in_ = inputs
in_ = torch.cat((inputs, labels), dim=1)
att_mask = torch.ones_like(in_)
outputs_lm = model(input_ids=in_, attention_mask=att_mask, labels=in_)
L0 = outputs_lm.loss
# REINFORCE
if progress.n >= args.warmstart:
# 1. collect trajectory
env.reset(labels)
S, A, R = [], [], []
# instead of state -> next_state we follow expert trajectory
# 1. ... BOS --> GotoLocation
# 2. ... BOS GotoLocation -> countertop
# 3. ... BOS GotoLocation countertop --> ??
# t. ... BOS GotoLocation countertop ... EOS
for i in range(labels.shape[1]):
# new state: old state + next expert action
state = torch.cat((inputs, labels[:, :i]), dim=1)
att_mask = torch.ones_like(state)
# run inference only
with torch.no_grad():
outputs_lm = model(input_ids=state, attention_mask=att_mask, labels=state)
# instead of argmax we do softmax
# action_probs == next_token_probs
action_probs = F.softmax(outputs_lm.logits[:, -1, :], dim=1) # dim := [bs, vocab_size]
# torch equivalent of np.random.choice(x, p)
action = Categorical(action_probs).sample()
# this doesn't give next state, because we follow expert trajectory
done, reward = env.step(action)
#print(tokenizer.batch_decode(state))
#print(tokenizer.batch_decode(action))
#print(tokenizer.batch_decode(labels[:, i]))
#print(reward)
S += [state]
A += [action]
R += [reward]
# keep going in any case to collect more samples for LM training
#if done:
# writer.add_scalar(f'train/eps_len', i, progress.n)
# break
# 2. sum of discounted future rewards
R = torch.tensor(R)
G = R.flip(0).cumsum(0).flip(0)
# baseline, whitening transform: subtract mean and divide by stddev
G -= torch.mean(G)
#print(G)
#G /= (torch.std(G) + 1e-10)
writer.add_scalar('train/cum_reward_pi', torch.sum(R), progress.n)
writer.add_scalar('train/running_mean_reward', env.reward_mu, progress.n)
# 3. rerun policy with optimization
L1 = torch.zeros_like(G, dtype=float, device=device)
L2 = torch.zeros_like(G, dtype=float, device=device)
#if torch.sum(G) > 0:
# import pdb; pdb.set_trace()
for i, (s, a, g) in enumerate(zip(S, A, G)):
outputs_lm = model(input_ids=s, attention_mask=torch.ones_like(s), labels=s)
L1[i] = outputs_lm.loss
## pick previously chosen action from logits
## --> logits are shit, they need to be normalized
##log_prob = outputs_lm.logits[:, -1, a]
# calculate log prob of picked action
action_probs = F.softmax(outputs_lm.logits[:, -1, :], dim=1)
log_prob = Categorical(action_probs).log_prob(action)
L2[i] = -torch.mean(log_prob * g)
# mean losses
loss_lm = torch.cat((L0.unsqueeze(0), L1)).mean()
loss_pi = L2.mean()
losses += [loss_lm]
losses += [loss_pi]
writer.add_scalar('train/loss_lm', loss_lm.item(), progress.n)
writer.add_scalar('train/loss_policy', loss_pi.item(), progress.n)
# LM loss + policy loss
loss = sum(losses)
loss.backward()
optimizer.step()
lr_scheduler.step()
optimizer.zero_grad()
progress.update(1)
if progress.n % 5000 == 0:
torch.save(model.state_dict(), args.model_path)
torch.save(model.state_dict(), args.model_path)
elif args.train_tok:
df = pd.read_json(args.train_tok)
tokenizer = train_tokenizer(tokenizer, df)
tokenizer.save_pretrained(args.tokdir)
elif args.train_rl:
def tokenize(e):
# queries
f = tokenizer(e['instructions'].split(":")[0]+":") # TODO
# responses
f['labels'] = tokenizer(e['instructions'].split(":")[1])['input_ids']
return f
df = pd.read_json(args.train_rl)
ds = Dataset.from_pandas(df)
ds = ds.map(tokenize, num_proc=8)
ds.set_format(type='torch', columns=['input_ids', 'labels'])
dl = DataLoader(ds, shuffle=True, batch_size=1)
num_training_steps = args.train_epochs * len(dl)
model = GPT2HeadWithValueModel.from_pretrained('gpt2')
model_ref = GPT2HeadWithValueModel.from_pretrained('gpt2')
model.to(device)
model_ref.to(device)
model.load_state_dict(torch.load(args.chkpt_path), strict=False)
model_ref.load_state_dict(torch.load(args.chkpt_path), strict=False)
ppo_config = {'batch_size': 1, 'forward_batch_size': 1}
ppo_trainer = PPOTrainer(model, model_ref, tokenizer, **ppo_config)
progress = tqdm(range(num_training_steps))
writer = SummaryWriter(args.log)
for epoch in range(args.train_epochs):
for batch in dl:
batch = {k: v.to(device) for k, v in batch.items()}
inputs = batch['input_ids']
outputs = model.generate(input_ids=inputs, do_sample=False, max_length=200)
#outputs = respond_to_batch(model, batch['input_ids'], txt_len=200)
preds = outputs[:, inputs.shape[1]:] # TODO: .generate always includes query.. has to be removed manuall -> use BOS token?
preds_text = tokenizer.batch_decode(preds, skip_special_tokens=True)
labels = batch['labels']
labels_text = tokenizer.batch_decode(labels, skip_special_tokens=True)
loss = torch.sum(torch.tensor([Eval.aux_loss(Eval.proc_instructions(label), Eval.proc_instructions(pred))
for label, pred in zip(preds_text, labels_text)]))
rewards = [1. - loss] # TODO: support other batch size?
train_stats = ppo_trainer.step(labels, preds, rewards)
writer.add_scalar('ppo/model_reward', rewards[0])
writer.add_scalar('ppo/return/mean', train_stats['ppo/returns/mean'][0])
writer.add_scalar('ppo/return/var', train_stats['ppo/returns/var'][0])
progress.update(1)
torch.save(model.state_dict(), args.model_path)
elif args.eval:
def tokenize(e):
s = e['instructions'].split('<BOS>')
f = tokenizer(s[0] + '<BOS>')
f['labels'] = tokenizer(s[1])['input_ids']
return f
model.to(device)
model.load_state_dict(torch.load(args.chkpt_path))
model.eval()
df = pd.read_json(args.eval)
ds = Dataset.from_pandas(df)
ds = ds.map(tokenize, num_proc=1)
ds.set_format(type='torch', columns=['input_ids', 'attention_mask', 'labels'])
#ds.set_format(type='torch', columns=['input_ids'], output_all_columns=True)
print(ds)
dl = DataLoader(ds, batch_size=1)
#metric = load_metric('glue', 'mrpc')
metric = load_metric('accuracy')
ev = Eval()
progress = tqdm(range(len(dl)))
for batch in dl:
batch = {k: v.to(device) for k, v in batch.items()}
#if args.eval_topk:
# outputs = model.generate(batch['input_ids'].to(device), do_sample=True, top_k=10, top_p=0.92, num_return_sequences=3, max_length=200)
#else:
# outputs = model.generate(batch['input_ids'].to(device), do_sample=True, max_length=200)
import pdb; pdb.set_trace()
preds = generate(batch['input_ids'], model)
#labels_text = tokenizer.batch_decode(batch['labels'], skip_special_tokens=True)
#labels_text = labels_text[0]
#preds_text = tokenizer.batch_decode(preds, skip_special_tokens=True)
#preds_text = ''.join(preds_text)
preds = torch.tensor(list(preds), device=device)
labels = batch['labels'].squeeze()
ev.add(labels, preds)
#for i, (pred, pred_text) in enumerate(zip(outputs, preds_text)):
# pred_text = "0." + pred_text.split("0.")[1]
# print("LBL:", label_text)
# print("PRD:", pred_text)
# ev.eval(i, Eval.proc_instructions(label_text), Eval.proc_instructions(pred_text))
# #score = metric.add_batch(predictions=pred[:len(labels)], references=labels)
# #if progress.n % 20 == 0:
# ev.print_stats(i)
if progress.n % 20 == 0:
ev.print_stats(0)
progress.update(1)
for i in range(3):
ev.print_stats(i, savefile=f"{args.eval}{i}.results.txt")
#score = metric.compute()
#print(score)
elif args.gen:
def tokenize(e):
e['input_ids'] = tokenizer(e['goal'].replace(".",":"))['input_ids']
return e
model.to(device)
model.load_state_dict(torch.load(args.chkpt_path))
model.eval()
df = pd.read_json(args.gen)
ds = Dataset.from_pandas(df)
ds = ds.map(tokenize, num_proc=8)
ds.set_format(type='torch', columns=['input_ids'])
print(ds)
outfile = open(args.gen.replace(".json", "_out.txt"), 'w')
dl = DataLoader(ds, batch_size=1)
progress = tqdm(range(len(dl)))
for batch in dl:
batch = {k: v.to(device) for k, v in batch.items()}
outputs = model(batch['input_ids'])
outputs = model.generate(batch['input_ids'], do_sample=False, max_length=200)
res = tokenizer.batch_decode(outputs, skip_special_tokens=True)[0]
outfile.write(res)
progress.update(1)
outfile.close()
elif args.prompt:
if os.path.isfile(args.chkpt_path):
model.load_state_dict(torch.load(args.chkpt_path))
model.eval()
print(tokenizer.tokenize(args.prompt))
input_ids = tokenizer(args.prompt, return_tensors="pt").input_ids
print(input_ids)
outputs = model.generate(input_ids, do_sample=False, max_length=128)
print(tokenizer.batch_decode(outputs, skip_special_tokens=True)[0])
elif args.forward:
if os.path.isfile(args.chkpt_path):
model.load_state_dict(torch.load(args.chkpt_path))
model.eval()
print(tokenizer.tokenize(inputs))
input_ids = tokenizer(inputs, return_tensors="pt").input_ids
print(input_ids)
# create five trajectories
for i in range(5):
actions = list(generate(args.forward, model))
print(tokenizer.batch_decode(actions))