File size: 14,308 Bytes
32b542e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import copy
import pickle
import random
import json
import glob
import numpy as np
from uniperceiver.config import configurable
from uniperceiver.functional import dict_as_tensor
from uniperceiver.tokenization import  ClipTokenizer
from ..build import DATASETS_REGISTRY
import pyarrow as pa

__all__ = ["GLUEDataset"]


@DATASETS_REGISTRY.register()
class GLUEDataset:
    @configurable
    def __init__(
        self,
        cfg: dict,
        stage: str,
        anno_file: str,
        max_seq_len: int,
        tokenizer,
        tokenizer_name,
        input_columns,
        label_column,
        input_count,
        task_name,
        data_percentage,
        data_k_sample,
    ):
        self.cfg = cfg
        self.stage = stage
        self.anno_file = anno_file
        self.tokenizer = tokenizer
        self.tokenizer_name = tokenizer_name
        self.max_seq_len = max_seq_len

        self.input_columns = input_columns
        self.label_column = label_column
        self.input_count = input_count

        self.task_name = task_name

        self.data_percentage = data_percentage
        self.data_k_sample = data_k_sample

        self.task_info = {
                'task_type'      : self.cfg.DATASETS.TASK_TYPE,
                'dataset_name'   : self.cfg.DATASETS.DATASET_NAME,
                'batch_size'     : self.cfg.DATALOADER.TRAIN_BATCH_SIZE if self.stage == 'train' else self.cfg.DATALOADER.TEST_BATCH_SIZE,
                'sampling_weight': self.cfg.DATALOADER.SAMPLING_WEIGHT,
            }
        self.target_set = cfg.DATASETS.TARGET_SET

        self.load_data(cfg)

    @classmethod
    def from_config(cls, cfg, stage: str = "train"):
        task_name = cfg.DATASETS.DATASET_NAME
        namesmapping = {
            "train": "train",
            "val": "dev",
            "test": "test",
        }
        data_dir = cfg.DATALOADER.ANNO_FOLDER
        if task_name in ['MNLI', 'QNLI', 'QQP', 'RTE', 'SST-2', 'MRPC', 'CoLA', 'STS-B']:
            anno_file = os.path.join(data_dir, task_name, 'processed/{name}.tsv'.format(name=namesmapping[stage]))
        elif task_name == 'MNLI_Match':
            namesmapping = {
                "train": "train",
                "val": "dev_matched",
                "test": "test_matched",
            }
            anno_file = os.path.join(data_dir, 'MNLI', 'processed/{name}.tsv'.format(name=namesmapping[stage]))
        elif task_name == 'MNLI_Mismatch':
            namesmapping = {
                "train": "train",
                "val": "dev_mismatched",
                "test": "test_mismatched",
            }
            anno_file = os.path.join(data_dir, 'MNLI', 'processed/{name}.tsv'.format(name=namesmapping[stage]))

        input_count = 2
        if task_name == "QQP":
            input_columns = [4, 5]
            if stage == 'test':
                input_columns = [2, 3]
            label_column = 6
        elif task_name in ["MNLI_Match", "MNLI_Mismatch"]:  # "MNLI" :
            input_columns = [9, 10]
            if stage == 'test':
                input_columns = [9, 10]

            label_column = 12
            if stage == 'val':
                label_column = 16
        elif task_name == "QNLI":
            input_columns = [2, 3]
            if stage == 'test':
                input_columns = [2, 3]
            label_column = 4
        elif task_name == "MRPC":
            input_columns = [4, 5]
            if stage == 'test':
                input_columns = [4, 5]
            label_column = 1
        elif task_name == "RTE":
            input_columns = [2, 3]
            if stage == 'test':
                input_columns = [2, 3]
            label_column = 4
        elif task_name == "STS-B":
            input_columns = [8, 9]
            if stage == 'test':
                input_columns = [8, 9]
            label_column = 10
        # Following are single sentence tasks.
        elif task_name == "SST-2":
            input_columns = [1]
            if stage == 'test':
                input_columns = [2]
            label_column = 2
            input_count = 1
        elif task_name == "CoLA":
            input_columns = [4]
            if stage == 'test':
                input_columns = [2]
            label_column = 2
            input_count = 1
        else:
            raise NotImplementedError

        ret = {
            "cfg": cfg,
            "stage": stage,
            "anno_file": anno_file,
            "max_seq_len": cfg.MODEL.MAX_SEQ_LEN,
            "input_columns": input_columns,
            "label_column": label_column,
            "input_count": input_count,
            "task_name": task_name,
            "data_percentage": getattr(cfg.DATALOADER, "DATA_PERCENTAGE", 1.0),
            "data_k_sample": getattr(cfg.DATALOADER, "DATA_K_SAMPLE", -1),
            "tokenizer": ClipTokenizer(),
            "tokenizer_name": "clip"
        }

        return  ret



    def load_data(self, cfg):
        cache_path = os.path.join(os.path.dirname(self.anno_file), "cache_GLUE_raw_%s_%s_%s.pkl" % (self.task_name, self.tokenizer_name, self.stage))
        if not os.path.exists(cache_path):
            datalist = self.load_raw_data(cfg)

            pickle.dump(datalist, open(cache_path, "wb"))

        datalist = pickle.load(open(cache_path, "rb"))

        # for few shot exp

        if self.data_percentage < 1.0 and self.stage == "train":
            print("will sample {} data for trianing-->".format(self.data_percentage))
            labels2l = dict()
            for data in datalist:

                label = data['label']
                if label not in labels2l:
                    labels2l[label] = list()
                labels2l[label].append(data)

            # samplers_label = len(datalist) * self.data_percentage // len(labels2l.keys())
            datalist = []

            for v in labels2l.values():
                datalist.extend(random.sample(v, k=int(self.data_percentage * len(v) + 1)))
                # datalist.extend(random.sample(v, k=int(samplers_label+1)))

        elif self.data_k_sample > 0 and self.stage == "train":
            print("will sample {} data for each class when training -->".format(self.data_k_sample))
            labels2l = dict()
            for data in datalist:

                label = data['label']
                if label not in labels2l:
                    labels2l[label] = list()
                labels2l[label].append(data)

            datalist = []

            for v in labels2l.values():
                datalist.extend(random.sample(v, k=int(self.data_k_sample)))

        while len(datalist) < 200:
            datalist = datalist + datalist

        self.datalist = datalist


    def load_raw_data(self, cfg):
        datalist = []
        if self.task_name.startswith("MNLI"):
            labelmapping = {
                "contradiction": 0,
                "neutral": 1,
                "entailment": 2,
            }
        fin = open(self.anno_file, 'r').readlines()
        for _, line in enumerate(fin):
            sensinfo = line.strip().split('\t')
            if self.task_name == "RTE":
                label = 1.0 if sensinfo[self.label_column - 1] == "entailment" else 0.0
            elif self.task_name.startswith("MNLI"):
                label = labelmapping[sensinfo[self.label_column - 1]]
            elif self.task_name == "QNLI":
                label = 1.0 if sensinfo[self.label_column - 1] == "entailment" else 0.0
            elif self.task_name == "STS-B":
                label = float(sensinfo[self.label_column - 1]) / 5.0
            else:
                label = float(sensinfo[self.label_column - 1])
            datalist.append({
                # start index from 1 to 0
                "sentences": [sensinfo[i - 1] for i in self.input_columns],
                "label": label
            })
        return datalist
    
    def __len__(self):
        return len(self.datalist)

    def __getitem__(self, index):
        dataset_dict = copy.deepcopy(self.datalist[index])

        sentences = dataset_dict['sentences']

        # input1: SEN1, this sentence is (spe)  input2: word choice: postive and negative

        if self.input_count == 1:

          
            if self.task_name == "SST-2":
                tokens = self.tokenizer.encode(sentences[0] + "  <|endoftext|> It is <|spe|>.   <|endoftext|>")
            elif self.task_name == "CoLA":
                tokens = self.tokenizer.encode(sentences[0] + " This is <|spe|>. <|endoftext|>")
            else:
                raise NotImplementedError

            index = len(tokens) - 3
            assert index < self.max_seq_len
            if len(tokens) > self.max_seq_len:
                tokens = tokens[:self.max_seq_len - 4] + tokens[-4:]



        else:

            if self.task_name in ["RTE"]:
                tokens1 = self.tokenizer.encode(sentences[0])
                if tokens1[-1] == 269:
                    tokens1 = tokens1[:-1]
                tokens1 = tokens1 + self.tokenizer.encode(" ? <|endoftext|> it is  ")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")

                tokens2 = self.tokenizer.encode("  <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)

            elif self.task_name in ["MRPC"]:
                tokens1 = self.tokenizer.encode(sentences[0])
                if tokens1[-1] == 269:
                    tokens1 = tokens1[:-1]
                tokens1 = tokens1 + self.tokenizer.encode(" . ")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")

                tokens2 = self.tokenizer.encode("  <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)

            elif self.task_name in ["QQP"]:
                tokens1 = self.tokenizer.encode(sentences[0])
                if tokens1[-1] == 269:
                    tokens1 = tokens1[:-1]
                tokens1 = tokens1 + self.tokenizer.encode(" <|endoftext|> ")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")

                tokens2 = self.tokenizer.encode("  <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)

            elif self.task_name in ["QNLI"]:
                tokens1 = self.tokenizer.encode(sentences[0])
                if tokens1[-1] == 269:
                    tokens1 = tokens1[:-1]
                tokens1 = tokens1 + self.tokenizer.encode("  <|endoftext|> it is ")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")

                tokens2 = self.tokenizer.encode("  <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len - len(tokens1):
                    tokens2 = tokens2[:self.max_seq_len - len(tokens1) - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)

            elif self.task_name in ["MNLI", "MNLI_Match"]:
                # sentence0 = sentences[0].replace(")", "").replace("(", "")
                tokens1 = self.tokenizer.encode(sentences[0])
                # if tokens1[-1] == 269:
                #     tokens1 = tokens1[:-1]
                tokens1 = tokens1  # + self.tokenizer.encode(" ? ")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")

                tokens2 = self.tokenizer.encode("  <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)

            elif self.task_name in ["RTE", "QNLI", "MNLI", "MNLI_Match"]:
                tokens1 = self.tokenizer.encode(sentences[0] + "? <|endoftext|>")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")
       
                if tokens1[-1] == 269:
                    tokens1 = tokens1[:-1]
                tokens2 = self.tokenizer.encode("   <|spe|> , ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)
            elif self.task_name in ["MRPC", "QQP"]:
                tokens1 = self.tokenizer.encode(sentences[0] + " <|endoftext|>")
                tokens2 = self.tokenizer.encode(sentences[1] + " <|endoftext|> ")
                tokens2 = self.tokenizer.encode(" <|spe|>,   ") + tokens2
                if len(tokens2) > self.max_seq_len // 2:
                    tokens2 = tokens2[:self.max_seq_len // 2 - 1] + [tokens2[-1]]
                max_len = self.max_seq_len - len(tokens2)
            else:
                NotImplementedError

            # tokens = self.tokenizer.add_special_tokens_sentences_pair(tokens1, tokens2, start_type='SPE')
            if len(tokens1) > max_len:
                tokens1 = tokens1[:max_len - 1] + [tokens1[-1]]

            tokens = tokens1 + tokens2

            index = len(tokens1)
            assert index < self.max_seq_len


        sentences = np.array(tokens, dtype=np.int64)


        if self.task_name in ["SST-2", "CoLA", "MRPC", "RTE", "QNLI", "MNLI", "QQP", "MNLI_Match"]:
            label = int(dataset_dict['label'])
        else:
            raise NotImplementedError()


        ret = {
            'input_sample': [{
                'data': [sentences],
                'modality': 'text',
                'data_type': 'input',
                'invalid_mask': None,
                'sample_info' : {
                    'spe_index': index 
                }
            }],
            'target_sample': [],
            'target_idx'   : [label],
            'target_set'   : copy.deepcopy(self.target_set),
            'task_info'    : copy.deepcopy(self.task_info)
        }

        dict_as_tensor(ret)
        return ret