-
Notifications
You must be signed in to change notification settings - Fork 25
/
dataloader.py
183 lines (144 loc) · 7.11 KB
/
dataloader.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
import datasets
import os
from functools import partial
import torch
from torch.nn.utils.rnn import pad_sequence
class DiffusionLoader:
def __init__(self, tokenizer):
self.tokenizer = tokenizer
def _load(self, task_name, split):
dataset = datasets.load_dataset('lm1b', split=split)
print(f'Example in {split} set:')
print(dataset[0])
dataset = dataset.map(partial(self.convert_to_features, tokenizer=self.tokenizer), batched=True, remove_columns='text')
return dataset
def my_load(self, task_name, splits):
return [self._load(task_name, name) for name in splits]
@staticmethod
def convert_to_features(example_batch, tokenizer):
input_encodings = tokenizer.batch_encode_plus(example_batch['text'], max_length=128, truncation=True, add_special_tokens=False)
encodings = {
'input_ids': input_encodings['input_ids'],
'attention_mask': input_encodings['attention_mask'],
}
return encodings
class ConditionalLoader:
def __init__(self, tokenizer, return_source_length=False):
self.tokenizer = tokenizer
self.return_source_length = return_source_length
self.data_dir = './conditional_data'
@staticmethod
def _convert_to_features_original(example_batch, tokenizer):
q1 = tokenizer.batch_encode_plus(example_batch['src'], max_length=128, truncation=True, add_special_tokens=False)
q2 = tokenizer.batch_encode_plus(example_batch['trg'], max_length=128, truncation=True, add_special_tokens=False)
return {
'source': q1['input_ids'],
'target': q2['input_ids'],
}
def load_original(self, split):
dataset = datasets.load_dataset(os.path.join(self.data_dir, self.task_name, f'{self.task_name}.py'), split=split)
dataset = dataset.map(partial(self._convert_to_features_original, tokenizer=self.tokenizer), batched=True, load_from_cache_file=False)
print(f'Example in {split} set:')
print(dataset[0])
return dataset
def _load(self, split):
dataset = datasets.load_dataset(os.path.join(self.data_dir, self.task_name, f'{self.task_name}.py'), split=split)
if self.return_source_length:
dataset = dataset.map(partial(self.add_original_src_length, tokenizer=self.tokenizer))
dataset = dataset.map(self.add_prompt)
dataset = dataset.map(partial(self.convert_to_features, tokenizer=self.tokenizer), batched=True)
print(f'Example in {split} set:')
print(dataset[0])
return dataset
def add_original_src_length(self, example, tokenizer):
return {
'original_src_length': len(tokenizer.encode(example['src'], max_length=128, truncation=True, add_special_tokens=False))
}
def my_load(self, splits):
return [self._load(name) for name in splits]
@staticmethod
def convert_to_features(example_batch, tokenizer):
q1 = tokenizer.batch_encode_plus(example_batch['src'], max_length=128, truncation=True, add_special_tokens=False)
q2 = tokenizer.batch_encode_plus(example_batch['trg'], max_length=128, truncation=True, add_special_tokens=False)
encodings = {
'source': q1['input_ids'],
'target': q2['input_ids'],
}
return encodings
@staticmethod
def collate_fn(batch_input, tokenizer):
input_ids = pad_sequence([torch.tensor(
[tokenizer.cls_token_id] + d['source'] + d['target'] + [tokenizer.sep_token_id]
) for d in batch_input], batch_first=True)
attention_mask = torch.ones_like(input_ids)
target_mask = torch.stack([torch.cat([
torch.zeros(len(d['source']) + 1), torch.ones(input_ids.size(1) - len(d['source']) - 1)
]) for d in batch_input])
assert input_ids.size() == attention_mask.size() == target_mask.size()
return {
'input_ids': input_ids,
'attention_mask': attention_mask,
'target_mask': target_mask,
}
class QQPLoader(ConditionalLoader):
def __init__(self, tokenizer, return_source_length=False):
super(QQPLoader, self).__init__(tokenizer, return_source_length)
self.task_name = 'qqp'
@staticmethod
def add_prompt(example):
example['src'] = '"' + example['src'] + '" is equal to "'
example['trg'] = example['trg']
return example
class QTLoader(ConditionalLoader):
def __init__(self, tokenizer, return_source_length=False):
super(QTLoader, self).__init__(tokenizer, return_source_length)
self.task_name = 'Q-T'
@staticmethod
def add_prompt(example):
example['src'] = ' Answer: ' + example['src'] + ' Question: '
example['trg'] = example['trg']
return example
class WikiLoader(ConditionalLoader):
def __init__(self, tokenizer, return_source_length=False):
super(WikiLoader, self).__init__(tokenizer, return_source_length)
self.task_name = 'wiki_alignment'
@staticmethod
def add_prompt(example):
example['src'] = '"' + example['src'] + '" can be summarized as: '
example['trg'] = example['trg']
return example
class CCLoader(ConditionalLoader):
def __init__(self, tokenizer, return_source_length=False):
super(CCLoader, self).__init__(tokenizer, return_source_length)
self.task_name = 'CC'
@staticmethod
def add_prompt(example):
example['src'] = example['src'] + ' - '
example['trg'] = example['trg']
return example
class DiffusionLoaderWithElectra(DiffusionLoader):
def __init__(self, model_tokenizer, electra_tokenizer, electra_model):
super().__init__(model_tokenizer)
self.electra_tokenizer = electra_tokenizer
self.electra_model = electra_model
def _load(self, task_name, split):
dataset = datasets.load_dataset(f'./dataloaders/{task_name}.py', split=split)
print(f'Example in {split} set:')
print(dataset[0])
dataset = dataset.map(partial(self.new_convert_to_features, model_tokenizer=self.tokenizer, electra_tokenizer=self.electra_tokenizer, electra_model=self.electra_model), batched=True, remove_columns='text')
return dataset
@staticmethod
def new_convert_to_features(example_batch, model_tokenizer, electra_tokenizer, electra_model):
input_encodings = model_tokenizer.batch_encode_plus(example_batch['text'], max_length=256, truncation=True, add_special_tokens=False)
electra_encodings = electra_tokenizer.batch_encode_plus(example_batch['text'], max_length=256, truncation=True, padding=True, return_tensors='pt', add_special_tokens=False)
for k in electra_encodings.keys():
electra_encodings[k] = electra_encodings[k].cuda()
position = electra_encodings['attention_mask'].count_nonzero(1)
with torch.no_grad():
logits = electra_model(**electra_encodings)
encodings = {
'input_ids': input_encodings['input_ids'],
'attention_mask': input_encodings['attention_mask'],
'electra_logits': [logits[i][:position[i]] for i in range(position.size(0))]
}
return encodings