-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathadvprompteropt.py
428 lines (381 loc) · 15.8 KB
/
advprompteropt.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
418
419
420
421
422
423
424
425
426
427
428
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import torch
from tqdm import tqdm
from sequence import MergedSeq, Seq, EmptySeq
from utils import apply_repetition_penalty
@torch.no_grad()
def advPrompterOpt(cfg, instruct, target, prompter, target_llm):
if cfg.verbose:
tqdm.write("\n Running AdvPrompterOpt: Generating optimized suffix...")
# Compute the initial prediction losses without a suffix
full_instruct = Seq(
text=instruct.text, tokenizer=target_llm.tokenizer, device=target_llm.device
)
target_llm_tf = target_llm.compute_pred_loss_teacher_forced(
key="target",
full_instruct=full_instruct,
target=target,
loss_params=dict(hard_labels=True, reweight_loss=cfg.reweight_loss),
)
losses = target_llm_tf.loss_batch.detach().to(prompter.device) # (ebs, )
# Initialize the beam scores
beam_scores = torch.zeros_like(losses) # (ebs, )
suffix_beams = EmptySeq(tokenizer=prompter.tokenizer, device=prompter.device)
for idx in range(cfg.train.q_params.max_new_tokens):
if idx == 0:
num_beams_in = 1
num_beams_out = cfg.train.q_params.num_beams
elif idx == cfg.train.q_params.max_new_tokens - 1:
num_beams_in = cfg.train.q_params.num_beams
num_beams_out = 1
else:
num_beams_in = cfg.train.q_params.num_beams
num_beams_out = cfg.train.q_params.num_beams
# expand the dimension of instruct and targets to match suffix beams
instruct_rep = instruct.repeat_interleave(num_beams_in, dim=0)
target_rep = target.repeat_interleave(num_beams_in, dim=0)
next_dist_seq_prompter, next_dist_seq_basemodel = get_next_token_probabilities(
cfg=cfg, instruct=instruct_rep, suffix=suffix_beams, prompter=prompter
)
next_token_candidate_ids, candidate_beam_scores, candidate_losses = (
select_and_evaluate_next_token_candidates(
cfg=cfg,
instruct=instruct_rep,
target=target_rep,
suffix=suffix_beams,
target_llm=target_llm,
next_dist_seq_prompter=next_dist_seq_prompter,
next_dist_seq_basemodel=next_dist_seq_basemodel,
beam_scores=beam_scores,
prev_losses=losses,
num_beams_in=num_beams_in,
)
)
suffix_beams, losses, beam_scores = select_next_beams(
cfg=cfg,
suffix_beams=suffix_beams,
next_token_candidate_ids=next_token_candidate_ids,
candidate_beam_scores=candidate_beam_scores,
candidate_losses=candidate_losses,
num_beams_in=num_beams_in,
num_beams_out=num_beams_out,
)
if cfg.verbose:
tqdm.write(f" Beams[0] (iter {idx}): {suffix_beams[:num_beams_out].text}")
if cfg.verbose:
tqdm.write(
f" AdvPrompterOpt completed. Generated suffix[0]: {suffix_beams[0].text}"
)
return suffix_beams
def get_next_token_probabilities(cfg, instruct, suffix, prompter):
# get the next token probabilities from the prompter and the base model
prompter_next = prompter.get_next_token(
key="suffix", instruct=instruct, suffix=suffix
)
next_dist_seq_prompter = (
prompter_next.response_dist.clone().detach()
) # (ebs, 1, vocab_size)
prompter_next_basemodel = prompter.get_next_token(
key="suffix", instruct=instruct, suffix=suffix, use_basemodel=True
)
next_dist_seq_basemodel = (
prompter_next_basemodel.response_dist.clone().detach()
) # (bs, 1, vocab_size)
# apply repetition penalty
if not suffix.is_empty and "repetition_penalty" in cfg.train.q_params:
next_dist_logits_basemodel = apply_repetition_penalty(
logits=next_dist_seq_basemodel.logits.squeeze(1),
prev_ids=suffix.ids,
penalty=cfg.train.q_params.repetition_penalty,
)
next_dist_seq_basemodel = Seq(
logits=next_dist_logits_basemodel[:, None, :],
mask=next_dist_seq_basemodel.mask,
tokenizer=next_dist_seq_basemodel.tokenizer,
device=next_dist_seq_basemodel.device,
)
next_dist_logits_prompter = apply_repetition_penalty(
logits=next_dist_seq_prompter.logits.squeeze(1),
prev_ids=suffix.ids,
penalty=cfg.train.q_params.repetition_penalty,
)
next_dist_seq_prompter = Seq(
logits=next_dist_logits_prompter[:, None, :],
mask=next_dist_seq_prompter.mask,
tokenizer=next_dist_seq_prompter.tokenizer,
device=next_dist_seq_prompter.device,
)
return next_dist_seq_prompter, next_dist_seq_basemodel
def select_and_evaluate_next_token_candidates(
cfg,
instruct,
target,
suffix,
target_llm,
next_dist_seq_prompter,
next_dist_seq_basemodel,
beam_scores,
prev_losses,
num_beams_in,
):
num_chunks = cfg.train.q_params.num_chunks
assert cfg.train.q_params.top_k % (num_chunks * num_beams_in) == 0
num_samples_per_beam = cfg.train.q_params.top_k // (num_chunks * num_beams_in)
all_next_token_candidate_ids = None
for i in range(cfg.train.q_params.num_chunks):
next_token_candidate_ids = select_next_token_candidates(
cfg=cfg,
next_dist_seq=next_dist_seq_prompter,
previous_next_token_candidate_ids=all_next_token_candidate_ids,
num_samples_per_beam=num_samples_per_beam,
always_include_best=cfg.train.q_params.candidates.always_include_best
and i == 0,
) # (ebs = bs * num_beams_in, num_samples_per_beam)
candidate_beam_scores, candidate_losses = evaluate_next_token_candidates(
cfg=cfg,
instruct=instruct,
target=target,
suffix=suffix,
target_llm=target_llm,
next_token_candidate_ids=next_token_candidate_ids,
next_dist_seq_basemodel=next_dist_seq_basemodel,
next_dist_seq_prompter=next_dist_seq_prompter,
prev_beam_scores=beam_scores,
prev_losses=prev_losses,
) # (ebs, num_samples_per_beam)
if all_next_token_candidate_ids is None:
all_next_token_candidate_ids = next_token_candidate_ids
all_candidate_beam_scores = candidate_beam_scores
all_candidate_losses = candidate_losses
else:
all_next_token_candidate_ids = torch.cat(
(next_token_candidate_ids, all_next_token_candidate_ids), dim=1
) # (ebs, i * num_samples_per_beam)
all_candidate_beam_scores = torch.cat(
(candidate_beam_scores, all_candidate_beam_scores), dim=1
) # (ebs, i * num_samples_per_beam)
all_candidate_losses = torch.cat(
(candidate_losses, all_candidate_losses), dim=1
) # (ebs, i * num_samples_per_beam)
return all_next_token_candidate_ids, all_candidate_beam_scores, all_candidate_losses
@torch.no_grad()
def select_next_token_candidates(
cfg,
next_dist_seq,
previous_next_token_candidate_ids,
num_samples_per_beam,
always_include_best,
):
# clone is important here! We modify the logits but will also use the original dist
next_dist_logits = next_dist_seq.logits.squeeze(1).clone() # (ebs, vocab_size)
if previous_next_token_candidate_ids is not None:
previous_next_token_candidate_ids_khot = torch.scatter(
torch.zeros_like(next_dist_logits), 1, previous_next_token_candidate_ids, 1
) # (ebs, vocab_size)
next_dist_logits -= 1e10 * previous_next_token_candidate_ids_khot
if cfg.train.q_params.candidates.do_sample:
if always_include_best:
next_dist_logits -= 1e10 * next_dist_seq.onehot.squeeze(1)
probs = torch.softmax(
next_dist_logits / cfg.train.q_params.candidates.temperature,
dim=-1,
) # (ebs, vocab_size)
next_token_candidate_ids = probs.multinomial(
num_samples=num_samples_per_beam, replacement=False
) # (ebs, num_samples_per_beam)
if always_include_best:
next_token_candidate_ids = torch.cat(
[next_dist_seq.ids, next_token_candidate_ids[:, :-1]], dim=1
)
else:
next_token_candidate_ids = next_dist_logits.topk(
k=num_samples_per_beam, dim=-1
).indices # (ebs, num_samples_per_beam)
return next_token_candidate_ids
@torch.no_grad()
def evaluate_next_token_candidates(
cfg,
instruct,
target,
suffix,
target_llm,
next_token_candidate_ids,
next_dist_seq_basemodel,
next_dist_seq_prompter,
prev_beam_scores,
prev_losses,
):
ebs, num_samples_per_beam = next_token_candidate_ids.shape
q_next_token_candidate_ids = torch.reshape(
next_token_candidate_ids, (ebs * num_samples_per_beam, 1)
)
q_sample_seq = Seq(
ids=q_next_token_candidate_ids,
tokenizer=next_dist_seq_prompter.tokenizer,
device=next_dist_seq_prompter.device,
)
# extend to match the extended batch size
instruct_rep = instruct.repeat_interleave(num_samples_per_beam, dim=0)
target_rep = target.repeat_interleave(num_samples_per_beam, dim=0)
if not suffix.is_empty:
suffix_rep = suffix.repeat_interleave(num_samples_per_beam, dim=0)
else:
suffix_rep = suffix
# compute the losses on each sample
merged = MergedSeq(seqs=[instruct_rep, suffix_rep, q_sample_seq])
full_instruct = Seq(
text=merged.to_seq(merge_dtype="ids").text,
tokenizer=target_llm.tokenizer,
device=target_llm.device,
)
with torch.no_grad():
target_llm_tf_q = target_llm.compute_pred_loss_teacher_forced(
key="target",
full_instruct=full_instruct,
target=target_rep,
loss_params=dict(hard_labels=True, reweight_loss=cfg.reweight_loss),
)
loss_batch = target_llm_tf_q.loss_batch.to(next_dist_seq_prompter.device)
losses = torch.reshape(loss_batch, (ebs, num_samples_per_beam))
loss_delta = losses - prev_losses[:, None] # (ebs, num_samples_per_beam)
next_dist_logprobs_basemodel = next_dist_seq_basemodel.logprobs.squeeze(1)
selected_logprobs_basemodel = torch.gather(
next_dist_logprobs_basemodel, dim=-1, index=next_token_candidate_ids
) # (ebs, num_samples_per_beam)
factor = cfg.train.q_params.lambda_val
beam_scores_delta = selected_logprobs_basemodel - loss_delta * factor
new_beam_scores = prev_beam_scores[:, None] + beam_scores_delta
return new_beam_scores, losses
@torch.no_grad()
def select_next_beams(
cfg,
suffix_beams,
next_token_candidate_ids,
candidate_beam_scores,
candidate_losses,
num_beams_in,
num_beams_out,
):
ebs, num_samples_per_beam = candidate_beam_scores.shape
bs = ebs // num_beams_in
candidate_beam_scores = candidate_beam_scores.reshape(
bs, num_beams_in * num_samples_per_beam
) # (bs, num_beams_in * num_samples_per_beam)
if cfg.train.q_params.beams.do_sample:
if cfg.train.q_params.beams.always_include_best:
candidate_beam_scores_top_ids = candidate_beam_scores.argmax(dim=-1)
candidate_beam_scores_onehot = torch.zeros_like(candidate_beam_scores)
candidate_beam_scores_onehot.scatter_(
1, candidate_beam_scores_top_ids[:, None], 1
)
candidate_beam_scores_corrected = (
candidate_beam_scores - 1e10 * candidate_beam_scores_onehot
)
beam_probs = torch.softmax(
candidate_beam_scores_corrected / cfg.train.q_params.beams.temperature,
dim=-1,
) # (bs, num_beams_in * num_samples_per_beam)
else:
beam_probs = torch.softmax(
candidate_beam_scores / cfg.train.q_params.beams.temperature,
dim=-1,
) # (bs, num_beams_in * num_samples_per_beam)
next_beam_indices = beam_probs.multinomial(
num_samples=num_beams_out, replacement=False
) # (bs, num_beams_out) [0, num_beams_in * num_samples_per_beam]
if cfg.train.q_params.beams.always_include_best:
next_beam_indices = torch.cat(
[candidate_beam_scores_top_ids[:, None], next_beam_indices[:, :-1]],
dim=-1,
)
else:
next_beam_indices = candidate_beam_scores.topk(
k=num_beams_out, dim=-1, sorted=True
).indices # (bs, num_beams_out) [0, num_beams_in * num_samples_per_beam]
next_beam_indices_expanded = (
next_beam_indices
+ torch.arange(0, bs, device=suffix_beams.device)[:, None]
* num_beams_in
* num_samples_per_beam
) # (bs, num_beams_out)
next_beam_indices_expanded = next_beam_indices_expanded.reshape(-1)
next_token_candidate_seq = Seq(
ids=next_token_candidate_ids.reshape(
bs * num_beams_in * num_samples_per_beam, 1
),
tokenizer=suffix_beams.tokenizer,
device=suffix_beams.device,
)
if suffix_beams.is_empty:
next_suffix_beams = next_token_candidate_seq[next_beam_indices_expanded]
else:
beam_candidates = suffix_beams.repeat_interleave(num_samples_per_beam, dim=0)
beam_candidates.append(next_token_candidate_seq)
next_suffix_beams = beam_candidates[next_beam_indices_expanded]
candidate_losses = candidate_losses.reshape(
bs, num_beams_in * num_samples_per_beam
) # (bs, num_beams_in * num_samples_per_beam)
selected_losses = candidate_losses.gather(
dim=1, index=next_beam_indices
) # (bs, num_beams_out)
selected_losses = selected_losses.reshape(bs * num_beams_out).detach()
selected_beam_scores = candidate_beam_scores.gather(
dim=1, index=next_beam_indices
) # (bs, num_beams_out)
selected_beam_scores = selected_beam_scores.reshape(bs * num_beams_out).detach()
return next_suffix_beams, selected_losses, selected_beam_scores
@torch.no_grad()
def evaluate_prompt(
cfg,
instruct,
suffix,
full_instruct,
target,
prompter,
target_llm,
generate_target_llm_response,
print_idx=0,
):
basemodel_tf = None
if suffix is not None and not suffix.is_empty:
basemodel_tf = prompter.compute_pred_loss_teacher_forced(
key="suffix",
instruct=instruct,
suffix=suffix,
use_basemodel=True,
loss_params=dict(hard_labels=True),
)
target_llm_tf = target_llm.compute_pred_loss_teacher_forced(
key="target",
full_instruct=full_instruct,
target=target,
loss_params=dict(
hard_labels=True,
reweight_loss=cfg.reweight_loss,
),
)
if cfg.verbose:
tqdm.write(f" --- Query[{print_idx}]: '{target_llm_tf.query.text[print_idx]}'")
tqdm.write(f" --- Suffix[{print_idx}]: '{suffix.text[print_idx]}'")
tqdm.write(f" --- Target[{print_idx}]: '{target.text[print_idx]}'")
tqdm.write(
f" --- TF Response[{print_idx}]: '{target_llm_tf.response_dist.text[print_idx]}'"
)
if generate_target_llm_response:
target_llm_ar = target_llm.generate_autoregressive(
key="target",
full_instruct=full_instruct,
)
if cfg.verbose:
tqdm.write(
f" --- AR Response[{print_idx}]: '{target_llm_ar.response_sample.text[print_idx]}'"
)
else:
target_llm_ar = None
if cfg.verbose:
tqdm.write(f" Evaluating suffix completed. TF Loss: {target_llm_tf.loss:.3f}")
return target_llm_tf, target_llm_ar, basemodel_tf