forked from tanmoyio/sahajBERT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_collator.py
148 lines (118 loc) · 6.33 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
import random
import warnings
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, Union
import torch
from transformers import DataCollatorForLanguageModeling
from transformers.data.data_collator import _torch_collate_batch, tolist
from transformers.models.albert import AlbertTokenizer, AlbertTokenizerFast
from transformers.tokenization_utils_base import BatchEncoding
def _is_start_piece_sp(piece):
"""Check if the current word piece is the starting piece (sentence piece)."""
special_pieces = set(list('!"#$%&"()*+,-./:;?@[\\]^_`{|}~'))
special_pieces.add(u"€".encode("utf-8"))
special_pieces.add(u"£".encode("utf-8"))
# Note(mingdachen):
# For foreign characters, we always treat them as a whole piece.
english_chars = set(list("abcdefghijklmnopqrstuvwxyz"))
if (
piece.startswith("▁")
or piece.startswith("<")
or piece in special_pieces
or not all(i.lower() in english_chars.union(special_pieces) for i in piece)
):
return True
else:
return False
@dataclass
class AlbertDataCollatorForWholeWordMask(DataCollatorForLanguageModeling):
"""
Data collator used for language modeling that masks entire words.
- collates batches of tensors, honoring their tokenizer's pad_token
- preprocesses batches for masked language modeling
.. note::
This collator relies on details of the implementation of subword tokenization by
:class:`~transformers.AlbertTokenizer`, specifically that start-of-word tokens are prefixed with `▁`.
For tokenizers that do not adhere to this scheme, this collator will produce an output that is roughly
equivalent to :class:`.DataCollatorForLanguageModeling`.
"""
def __call__(
self, examples: List[Union[List[int], torch.Tensor, Dict[str, torch.Tensor]]]
) -> Dict[str, torch.Tensor]:
if isinstance(examples[0], (dict, BatchEncoding)):
batch = self.tokenizer.pad(examples, return_tensors="pt", pad_to_multiple_of=self.pad_to_multiple_of)
else:
batch = {"input_ids": _torch_collate_batch(examples, self.tokenizer, pad_to_multiple_of=self.pad_to_multiple_of)}
# If special token mask has been preprocessed, pop it from the dict.
special_tokens_mask = batch.pop("special_tokens_mask", None)
mask_labels = []
for e in batch["input_ids"]:
ref_tokens = self.tokenizer.convert_ids_to_tokens(tolist(e))
mask_labels.append(self._whole_word_mask(ref_tokens))
batch_mask = _torch_collate_batch(mask_labels, self.tokenizer, pad_to_multiple_of=self.pad_to_multiple_of)
batch["input_ids"], batch["labels"] = self.mask_tokens(
batch["input_ids"], batch_mask, special_tokens_mask=special_tokens_mask
)
return batch
def _whole_word_mask(self, input_tokens: List[str], max_predictions=512):
"""
Get 0/1 labels for masked tokens with whole word mask proxy
"""
if not isinstance(self.tokenizer, (AlbertTokenizer, AlbertTokenizerFast)):
warnings.warn("AlbertDataCollatorForWholeWordMask is only suitable for AlbertTokenizer-like tokenizers.")
cand_indexes = []
for i, token in enumerate(input_tokens):
if token in (self.tokenizer.cls_token, self.tokenizer.sep_token, self.tokenizer.pad_token):
continue
if len(cand_indexes) >= 1 and not _is_start_piece_sp(token):
cand_indexes[-1].append(i)
else:
cand_indexes.append([i])
random.shuffle(cand_indexes)
num_to_predict = min(max_predictions, max(1, int(round(len(input_tokens) * self.mlm_probability))))
mask_labels = torch.zeros((len(input_tokens),), dtype=torch.long)
covered_indexes = set()
for index_set in cand_indexes:
if len(covered_indexes) >= num_to_predict:
break
# If adding a whole-word mask would exceed the maximum number of
# predictions, then just skip this candidate.
if len(covered_indexes) + len(index_set) > num_to_predict:
continue
is_any_index_covered = any(index in covered_indexes for index in index_set)
if is_any_index_covered:
continue
for index in index_set:
covered_indexes.add(index)
mask_labels[index] = 1
return mask_labels
def mask_tokens(
self, inputs: torch.Tensor, mask_labels: torch.Tensor, special_tokens_mask: Optional[torch.Tensor] = None
) -> Tuple[torch.Tensor, torch.Tensor]:
"""
Prepare masked tokens inputs/labels for masked language modeling: 80% MASK, 10% random, 10% original. Set
'mask_labels' means we use whole word mask (WMM), we directly mask idxs according to it's ref.
"""
assert self.mlm
labels = inputs.clone()
# We sample a few tokens in each sequence for MLM training (with probability `self.mlm_probability`)
probability_matrix = mask_labels
if special_tokens_mask is None:
special_tokens_mask = [
self.tokenizer.get_special_tokens_mask(val, already_has_special_tokens=True) for val in labels.tolist()
]
special_tokens_mask = torch.tensor(special_tokens_mask, dtype=torch.bool)
else:
special_tokens_mask = special_tokens_mask.bool()
probability_matrix.masked_fill_(special_tokens_mask, value=0.0)
masked_indices = probability_matrix.bool()
labels[~masked_indices] = -100 # We only compute loss on masked tokens
# 80% of the time, we replace masked input tokens with tokenizer.mask_token ([MASK])
indices_replaced = torch.bernoulli(torch.full(labels.shape, 0.8)).bool() & masked_indices
inputs[indices_replaced] = self.tokenizer.convert_tokens_to_ids(self.tokenizer.mask_token)
# 10% of the time, we replace masked input tokens with random word
indices_random = torch.bernoulli(torch.full(labels.shape, 0.5)).bool() & masked_indices & ~indices_replaced
random_words = torch.randint(len(self.tokenizer), labels.shape, dtype=torch.long)
inputs[indices_random] = random_words[indices_random]
# The rest of the time (10% of the time) we keep the masked input tokens unchanged
return inputs, labels