-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathadapters.py
More file actions
407 lines (345 loc) · 14.8 KB
/
adapters.py
File metadata and controls
407 lines (345 loc) · 14.8 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
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
from __future__ import annotations
import os
from typing import Any, Callable, Literal
import torch
from torch import Tensor
from torch.utils.data import Dataset
from transformers import PreTrainedTokenizerBase
def run_tokenize_prompt_and_output(
prompt_strs: list[str],
output_strs: list[str],
tokenizer: PreTrainedTokenizerBase,
) -> dict[str, Tensor]:
"""Tokenize the prompt and output strings, and construct a mask that is 1
for the response tokens and 0 for other tokens (prompt or padding).
Args:
prompt_strs: list[str], the prompt strings.
output_strs: list[str], the output strings.
tokenizer: PreTrainedTokenizer, the tokenizer to use.
Returns:
dict[str, torch.Tensor]:
"input_ids": torch.Tensor of shape (batch_size, max(prompt_and_output_lens) - 1):
the tokenized prompt and output strings, with the final token sliced off.
"labels": torch.Tensor of shape (batch_size, max(prompt_and_output_lens) - 1):
shifted input_ids (i.e., the input_ids without the first token).
"response_mask": torch.Tensor of shape (batch_size, max(prompt_and_output_lens) - 1):
a mask on the response tokens in `labels`.
"""
raise NotImplementedError
def run_compute_group_normalized_rewards(
reward_fn: Callable,
rollout_responses: list[str],
repeated_ground_truths: list[str],
group_size: int,
advantage_eps: float,
normalize_by_std: bool,
) -> tuple[torch.Tensor, dict[str, float]]:
"""
Compute rewards for each group of rollout responses,
normalized by the group size.
For more on GRPO, see:
DeepSeekMath: https://arxiv.org/abs/2402.03300
DeepSeek-R1: https://arxiv.org/abs/2501.12948
Args:
reward_fn: Callable[[str, str], dict[str, float]],
scores the rollout responses against the ground truths,
producing a dict with keys
"reward", "format_reward", and "answer_reward".
rollout_responses: list[str], rollouts from the policy.
The length of this list is
`rollout_batch_size = n_prompts_per_rollout_batch * group_size`.
repeated_ground_truths: list[str], the ground truths for the examples.
The length of this list is `rollout_batch_size`,
because the ground truth for each example is repeated `group_size` times.
group_size: int, number of rollouts per group.
advantage_eps: float, epsilon to avoid division by zero
during group normalization.
normalize_by_std: bool, whether to normalize the rewards by
std(rewards).
Returns:
tuple[torch.Tensor, torch.Tensor, dict[str, float]]:
torch.Tensor of shape (rollout_batch_size,):
group-normalized rewards for each rollout response.
torch.Tensor of shape (rollout_batch_size,):
raw rewards for each rollout response.
dict[str, float]: metadata for the rewards of the rollout batch.
You may choose what you wish to log here
(some statistics of the rewards, etc.).
"""
raise NotImplementedError
def run_compute_entropy(logits: torch.Tensor) -> torch.Tensor:
"""Get the entropy of the logits (i.e., entropy of the final dimension)."""
raise NotImplementedError
def run_get_response_log_probs(
model: torch.nn.Module,
input_ids: torch.Tensor,
labels: torch.Tensor,
return_token_entropy: bool,
) -> torch.Tensor:
"""Get the conditional log-probs of the response given the prompt,
and optionally the entropy of the next token predictions.
Args:
model: PreTrainedModel, the model to score.
input_ids: torch.Tensor of shape (batch_size, sequence_length):
the tokenized prompt and output.
labels: torch.Tensor of shape (batch_size, sequence_length):
shifted input_ids.
return_token_entropy: bool, whether to return the entropy of the
next token predictions.
Returns:
dict[str, torch.Tensor]:
"log_probs": torch.Tensor of shape (batch_size, sequence_length):
the conditional log-probs of the response given the prompt.
Note that we have not masked out the token indices corresponding
to the prompt or padding; that is done in the train loop.
"token_entropy": Optional[torch.Tensor] of shape (batch_size, sequence_length):
the entropy of the next token predictions. As with the log-probs,
we have not masked out the token indices corresponding to the prompt
or padding; that is done in the train loop.
"""
raise NotImplementedError
def run_compute_naive_policy_gradient_loss(
raw_rewards_or_advantages: torch.Tensor,
policy_log_probs: torch.Tensor,
) -> torch.Tensor:
"""Compute policy gradient loss using either raw rewards or advantages.
Args:
raw_rewards_or_advantages: torch.Tensor of shape (batch_size, 1):
the raw rewards or advantages for each rollout response.
policy_log_probs: torch.Tensor of shape (batch_size, sequence_length):
the log-probs of the policy.
Returns:
torch.Tensor of shape (batch_size, sequence_length):
the policy gradient per-token loss.
"""
raise NotImplementedError
def run_compute_grpo_clip_loss(
advantages: torch.Tensor,
policy_log_probs: torch.Tensor,
old_log_probs: torch.Tensor,
cliprange: float,
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
"""Compute the GRPO-Clip loss.
Args:
advantages: torch.Tensor of shape (batch_size, 1):
the advantages for each rollout response.
policy_log_probs: torch.Tensor of shape (batch_size, sequence_length):
the log-probs of the policy.
old_log_probs: torch.Tensor of shape (batch_size, sequence_length):
the log-probs of the old policy.
cliprange: float, the clip range for the ratio.
Returns:
tuple[torch.Tensor, dict[str, torch.Tensor]]:
torch.Tensor of shape (batch_size, sequence_length):
the GRPO-Clip per-token loss.
dict[str, torch.Tensor]: metadata for the GRPO-Clip loss
(used to compute clip fraction).
"""
raise NotImplementedError
def run_compute_policy_gradient_loss(
policy_log_probs: torch.Tensor,
loss_type: str,
raw_rewards: torch.Tensor,
advantages: torch.Tensor,
old_log_probs: torch.Tensor,
cliprange: float,
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
"""
Wrapper that delegates to the appropriate policy gradient loss function above.
"""
raise NotImplementedError
def run_masked_mean(tensor: torch.Tensor, mask: torch.Tensor, dim: int | None = None) -> torch.Tensor:
"""Compute the mean of the tensor along a dimension,
considering only the elements with mask value 1.
Args:
tensor: torch.Tensor, the tensor to compute the mean of.
mask: torch.Tensor, the mask. We only take the mean over
the elements with mask value 1.
dim: int | None, the dimension to compute the mean along.
If None, sum over all non-masked elements and average
by their total count.
Returns:
torch.Tensor, the mean of the tensor along the specified
dimension, considering only the elements with mask value 1.
"""
raise NotImplementedError
def run_sft_microbatch_train_step(
policy_log_probs: torch.Tensor,
response_mask: torch.Tensor,
gradient_accumulation_steps: int,
normalize_constant: int | None = 1.0,
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
"""Compute the policy gradient loss and backprop its gradients for a microbatch.
"""
raise NotImplementedError
def run_grpo_microbatch_train_step(
policy_log_probs: torch.Tensor,
response_mask: torch.Tensor,
gradient_accumulation_steps: int,
loss_type: Literal["no_baseline", "reinforce_with_baseline", "grpo_clip"],
raw_rewards: torch.Tensor | None = None,
advantages: torch.Tensor | None = None,
old_log_probs: torch.Tensor | None = None,
cliprange: float | None = None,
) -> tuple[torch.Tensor, dict[str, torch.Tensor]]:
"""Compute the policy gradient loss and backprop its gradients for a microbatch.
Args:
policy_log_probs: torch.Tensor of shape (batch_size, sequence_length):
the log-probs of the policy.
response_mask: torch.Tensor of shape (batch_size, sequence_length):
the mask for the response.
gradient_accumulation_steps: int, the number of gradient accumulation steps.
loss_type: Literal["no_baseline", "reinforce_with_baseline", "grpo_clip"],
the type of loss function to use.
raw_rewards: torch.Tensor | None, the raw rewards for each rollout response.
Needed for loss_type="no_baseline".
advantages: torch.Tensor | None, the advantages for each rollout response.
Needed for loss_type in {"reinforce_with_baseline", "grpo_clip"}.
old_log_probs: torch.Tensor | None, the log-probs of the old policy.
Needed for loss_type="grpo_clip".
cliprange: float | None, the clip range for the ratio.
Needed for loss_type="grpo_clip".
constant_normalize_factor: int | None, provided if we want to sum over
the sequence dimension and normalize by this constant factor
(as in Dr. GRPO).
Returns:
tuple[torch.Tensor, dict[str, torch.Tensor]]:
the policy gradient loss and its metadata.
"""
raise NotImplementedError
def run_masked_normalize(
tensor: torch.Tensor,
mask: torch.Tensor,
dim: int | None = None,
normalize_constant: float = 1.0,
) -> torch.Tensor:
"""Sum over a dimension and normalize by a constant,
considering only the elements with mask value 1.
Args:
tensor: torch.Tensor, the tensor to sum and normalize.
mask: torch.Tensor, the mask. We only consider elements
with mask value 1.
dim: int | None, the dimension to sum along before
normalization. If None, sum over all dimensions.
normalize_constant: float, the constant to divide by
for normalization.
Returns:
torch.Tensor, the normalized sum, where masked elements
(mask=0) don't contribute to the sum.
"""
raise NotImplementedError
"""
The below adapters are used in the optional
RLHF / safety part of the Alignment assignment.
"""
def get_packed_sft_dataset(
tokenizer: PreTrainedTokenizerBase,
dataset_path: str | os.PathLike,
seq_length: int,
shuffle: bool,
) -> Dataset:
"""
Given a tokenizer and a path to a dataset with instruction-tuning examples,
construct a PyTorch Dataset for language modeling. The examples should be
packed, i.e., all sequences in the dataset are of a constant length (`seq_length`).
Args:
tokenizer: transformers.PreTrainedTokenizerBase
Transformers tokenizer to use in tokenizing and encoding text.
dataset_path: str
Path to file with instruction-tuning examples.
seq_length: int
Number of tokens to include in each example.
shuffle: bool
If true, shuffle the documents before packing them into examples.
Returns:
PyTorch Dataset for language modeling. Each example in this dataset is a dictionary of
with keys "input_ids" and "labels" (both tensors of shape (seq_length, )).
"input_ids" contains the token IDs for the language modeling inputs, and "labels" contains
the token IDs for the language modeling labels.
"""
raise NotImplementedError
def run_iterate_batches(
dataset: Dataset,
batch_size: int,
shuffle: bool,
):
"""
Given a PyTorch Dataset, return an iterable over batches of size `batch_size`.
Iterating through the returned iterable should constitute one epoch over the Dataset.
Args:
dataset: Dataset
Dataset to emit batches from.
batch_size: int
Number of examples to include per batch.
shuffle: bool
If true, shuffle examples before batching them.
Returns:
Iterable over batches, where each batch has size `batch_size`.
"""
raise NotImplementedError
def run_parse_mmlu_response(
mmlu_example: dict[str, Any],
model_output: str,
) -> str | None:
"""
Given an MMLU example and a model output, parse the model output into a
predicted option letter (i.e., 'A', 'B', 'C', or 'D'). If the model output
cannot be parsed into a prediction option letter, return None.
mmlu_example: dict[str, Any]
Dictionary with an MMLU example. Contains the following keys:
- "subject": str with the subject of the question.
- "question": str with the text of the question.
- "options": list[str] with the four answer options (in order).
The first option refers to letter "A", the second to "B", etc.
- "answer": str with the option of the correct answer (e.g., "A")
model_output: str
str with the model's output to the MMLU example.
Returns:
str (one of "A", "B", "C", or "D") if the model output can be parsed into a prediction,
else None.
"""
raise NotImplementedError
def run_parse_gsm8k_response(
model_output: str,
) -> str | None:
"""
Given a GSM8K model output, parse the model output into a predicted numeric answer by
taking the last number that occurs in the output.
model_output: str
str with the model's output to a GSM8K example.
Returns:
str with the predicted numeric answer if the model output can be parsed into a prediction,
else None.
"""
raise NotImplementedError
def run_compute_per_instance_dpo_loss(
lm: torch.nn.Module,
lm_ref: torch.nn.Module,
tokenizer: PreTrainedTokenizerBase,
beta: float,
prompt: str,
response_chosen: str,
response_rejected: str,
) -> torch.Tensor:
"""
Given two language models (`lm`, and the "reference model" `lm_ref`),
their tokenizer, the DPO beta hyperparameter, a prompt and a pair
of responses to the prompt, computes the value of the DPO loss for this example.
lm: torch.nn.Module
Language model being trained.
lm_ref: torch.nn.Module
Reference language model.
tokenizer: PreTrainedTokenizerBase
Tokenizer for both language models.
beta: float
DPO beta hyperparameter.
prompt: str
Prompt for this instance of preference pair.
response_chosen: str
Preferred response to the prompt.
response_rejected: str
Rejected response to the prompt.
Returns:
torch.Tensor with the DPO loss for this example.
"""
raise NotImplementedError