-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_collator.py
226 lines (195 loc) · 8.47 KB
/
data_collator.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
"""
Table of Contents:
> `_pad_batch_of_sequences_from_dict`:
- Pad batches in a dict.
> `DataCollatorWithDecoderInputIds`:
- Pad everything in a batch
- Convert everything to tensors
- Create decoder input ids from labels if we have them.
- Shift decoder input ids
"""
import collections
from typing import *
import numpy as np
import rich
import torch
import transformers
import transformers.models.bart.modeling_bart as modeling_bart
import data_generation_arithmetic
def _pad_batch_of_sequences_from_dict(
features: list[dict[str, Union[torch.Tensor, np.ndarray, list]]],
key: str,
pad_token_id: int,
pad_direction: str,
) -> None:
"""
Pad a batch of sequences, with a certain key value.
This is used because huggingface's PretrainedTokenizer.pad only pads input_ids.
Changes "features" in place in a dict.
"""
max_length = max(len(entry[key]) for entry in features)
for i, entry in enumerate(features):
key_entry = entry[key]
remainder = [pad_token_id] * (max_length - len(key_entry))
if isinstance(entry, list):
if pad_direction == "right":
features[i][key] = key_entry + remainder
elif pad_direction == "left":
features[i][key] = remainder + key_entry
else:
raise ValueError("pad_direction must be 'right' or 'left'")
else:
if pad_direction == "right":
features[i][key] = np.concatenate([key_entry, remainder]).astype(
np.int64
)
elif pad_direction == "left":
features[i][key] = np.concatenate([remainder, key_entry]).astype(
np.int64
)
class DataCollatorWithDecoderInputIds:
__slots__ = (
"_tokenizer",
"_model",
"_max_length",
"_mask_intermediate_labels",
"_label_pad_token_id",
"_return_idents",
)
def __init__(
self,
*,
tokenizer,
model: transformers.PreTrainedModel,
max_length: int,
mask_intermediate_labels: bool,
return_idents: bool,
label_pad_token_id: int = -100,
):
self._tokenizer: Final = tokenizer
self._model: Final[transformers.PretrainedModel] = model # type: ignore[name-defined]
self._max_length: Final[int] = max_length
self._mask_intermediate_labels: Final[bool] = mask_intermediate_labels
self._label_pad_token_id: Final[int] = label_pad_token_id
self._return_idents: Final[bool] = return_idents
def __call__(self, features):
"""
#######################################################################
Situation:
- We need to pad everything.
- We need regular decoder input ids for training and validation.
- When we have a scratch pad, we need special decoder input ids to only
predict the final value.
#######################################################################
Table of contents:
1. Pad labels
2. Pad gen decoder inputs if they are there
3. Pad inputs & convert everything to tensors
4. Build training decoder input ids by shifting labels
5. If we have gen decoder inputs, we shift them as well.
#######################################################################
"""
assert (
self._tokenizer.padding_side == "left"
), f"Only left-padded inputs are supported. Got {self.tokenizer.padding_side}."
assert self._model is not None, "You must provide a model to the data collator"
assert (
self._model.config.pad_token_id == self._tokenizer.pad_token_id
), "The pad_token_id of the model must be the same as the one used by the tokenizer"
assert "decoder_attention_mask_for_gen" not in features, features.keys()
if "decoder_input_ids" in features:
assert self._mask_intermediate_labels
keys = set(features[0].keys())
for feature in features[1:]:
assert feature.keys() == keys
if self._return_idents:
assert "idents" in keys, f"We needs ids to be able to return them. {keys = }"
idents = [x.pop("idents") for x in features]
assert "input_ids" in keys
DECODER_PAD_DIRECTION = "left"
#######################################################################
# 1. Pad labels
#######################################################################
if "labels" in keys:
_pad_batch_of_sequences_from_dict(
features,
"labels",
self._label_pad_token_id,
pad_direction=DECODER_PAD_DIRECTION,
)
#######################################################################
# 2. Pad gen decoder inputs if they are there
#######################################################################
if "decoder_input_ids_for_gen" in keys:
_pad_batch_of_sequences_from_dict(
features,
"decoder_input_ids_for_gen",
self._model.config.pad_token_id,
pad_direction=DECODER_PAD_DIRECTION,
)
if "decoder_input_ids" in keys:
_pad_batch_of_sequences_from_dict(
features,
"decoder_input_ids",
self._model.config.pad_token_id,
pad_direction=DECODER_PAD_DIRECTION,
)
#######################################################################
# 3. Pad inputs & convert everything to tensors
#######################################################################
out_features = self._tokenizer.pad(
features,
padding=True,
max_length=self._max_length,
pad_to_multiple_of=None,
return_tensors="pt",
ignore_keys=["idents"]
)
del features
if self._return_idents:
out_features["idents"] = idents
#######################################################################
# 4. Build training decoder input ids by shifting labels
#######################################################################
if "decoder_input_ids" in keys:
assert self._mask_intermediate_labels
out_features["decoder_input_ids"] = modeling_bart.shift_tokens_right(
out_features["decoder_input_ids"],
self._model.config.pad_token_id,
self._model.config.decoder_start_token_id,
)
assert torch.all(
out_features["decoder_input_ids"][:, 0]
== self._model.config.decoder_start_token_id
), out_features["decoder_input_ids"][:, 0]
if "labels" in out_features and not self._mask_intermediate_labels:
# If we're not masking the intermediate labels, we can use the "labels"
# field to build the `decoder_input_ids`.
out_features[
"decoder_input_ids"
] = self._model.prepare_decoder_input_ids_from_labels(
labels=out_features["labels"]
)
if self._mask_intermediate_labels:
# If we're masking the intermediate labels, we can't use the "labels"
# to build the `decoder_input_ids`, because the intermediate results are
# masked in the label.
assert (
"decoder_input_ids_for_gen" in out_features
or "decoder_input_ids" in out_features
)
#######################################################################
# 5. If we have gen decoder inputs, we shift them as well.
# We remove the eos token if it's there, as we generate after it.
#######################################################################
if "decoder_input_ids_for_gen" in keys:
out_features["decoder_input_ids_for_gen"] = modeling_bart.shift_tokens_right(
out_features["decoder_input_ids_for_gen"],
self._model.config.pad_token_id,
self._model.config.decoder_start_token_id,
)
assert torch.all(
out_features["decoder_input_ids_for_gen"][:, 0]
== self._model.config.decoder_start_token_id
), out_features["decoder_input_ids_for_gen"][:, 0]
return out_features