glue.py 12 KB
Newer Older
Leo Gao's avatar
Leo Gao committed
1
2
# REMINDER: this code needs to be rewritten for the new framework. Remove this comment when the code is fully converted.

Jason Phang's avatar
checkin  
Jason Phang committed
3
import numpy as np
Jonathan Tow's avatar
Jonathan Tow committed
4
from lm_eval.base import rf, mean, f1_score, matthews_corrcoef
Jason Phang's avatar
Jason Phang committed
5
6
from scipy.stats import pearsonr, spearmanr
from tqdm import auto as tqdm_lib
Jonathan Tow's avatar
Jonathan Tow committed
7
8
9
10
from . common import HFTask, yesno


# Single-Sentence Tasks
Jason Phang's avatar
Jason Phang committed
11
12


sdtblck's avatar
sdtblck committed
13
class CoLA(HFTask):
sdtblck's avatar
sdtblck committed
14
15
    DATASET_PATH = "glue"
    DATASET_NAME = "cola"
Jonathan Tow's avatar
Jonathan Tow committed
16

Jason Phang's avatar
checkin  
Jason Phang committed
17
18
19
20
21
22
23
24
25
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

Jason Phang's avatar
Jason Phang committed
26
27
28
    def fewshot_description(self):
        return "Does this sentence make sense?:\tTrue or False?"

29
30
31
32
33
    def doc_to_text(self, doc):
        return "Sentence: {}\nAnswer:".format(doc["sentence"])

    def doc_to_target(self, doc):
        return " {}".format({1: "True", 0: "False"}[doc["label"]])
Jason Phang's avatar
checkin  
Jason Phang committed
34

Jonathan Tow's avatar
Jonathan Tow committed
35
36
37
38
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_false
39

Jonathan Tow's avatar
Jonathan Tow committed
40
41
42
43
44
45
46
    def process_results(self, doc, results):
        ll_true, ll_false = results
        pred = ll_true > ll_false
        gold = doc["label"]
        return {
            "mcc": (gold, pred)
        }
47

Jonathan Tow's avatar
Jonathan Tow committed
48
    def higher_is_better(self):
Jason Phang's avatar
checkin  
Jason Phang committed
49
        return {
Jonathan Tow's avatar
Jonathan Tow committed
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
            "mcc": True
        }

    def aggregation(self):
        return {
            "mcc": matthews_corrcoef
        }


class SST(HFTask):
    DATASET_PATH = "glue"
    DATASET_NAME = "sst2"

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def fewshot_description(self):
        return "Indicate if each sentence is Positive or Negative."

    def doc_to_text(self, doc):
        return "sentence:\t{}\t\nanswer:".format(
            doc["sentence"],
        )

    def doc_to_target(self, doc):
        return " {}".format({1: "Positive", 0: "Negative"}[doc["label"]])

    def construct_requests(self, doc, ctx):
        ll_positive, _ = rf.loglikelihood(ctx, " Positive")
        ll_negative, _ = rf.loglikelihood(ctx, " Negative")
        return ll_positive, ll_negative

    def process_results(self, doc, results):
        ll_positive, ll_negative = results
        pred = ll_positive > ll_negative
        gold = doc["label"]
        return {
            "acc": pred == gold
        }

    def higher_is_better(self):
        return {
            "acc": True
Jason Phang's avatar
checkin  
Jason Phang committed
99
100
        }

Jonathan Tow's avatar
Jonathan Tow committed
101
102
103
104
105
106
107
108
    def aggregation(self):
        return {
            "acc": mean
        }


# Inference Tasks

Jason Phang's avatar
checkin  
Jason Phang committed
109

sdtblck's avatar
sdtblck committed
110
class MNLI(HFTask):
sdtblck's avatar
sdtblck committed
111
112
    DATASET_PATH = "glue"
    DATASET_NAME = "mnli"
Jason Phang's avatar
Jason Phang committed
113

Jason Phang's avatar
checkin  
Jason Phang committed
114
115
116
117
118
119
120
121
122
123
124
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def validation_docs(self):
        if self.has_validation_docs():
sdtblck's avatar
sdtblck committed
125
            return self.data["validation_matched"]
Jason Phang's avatar
checkin  
Jason Phang committed
126
127
128

    def test_docs(self):
        if self.has_test_docs():
sdtblck's avatar
sdtblck committed
129
            return self.data["test_matched"]
Jason Phang's avatar
checkin  
Jason Phang committed
130

131
132
    def doc_to_text(self, doc):
        return "{}\nquestion:\t{}\tTrue, False or Neither?\nanswer:".format(
Jason Phang's avatar
Jason Phang committed
133
134
            doc["premise"],
            doc["hypothesis"],
Jason Phang's avatar
checkin  
Jason Phang committed
135
        )
136
137
138
139
140
141

    def doc_to_target(self, doc):
        # True = entailment
        # False = contradiction
        # Neither = neutral
        return " {}".format({0: "True", 1: "Neither", 2: "False"}[doc["label"]])
Jason Phang's avatar
checkin  
Jason Phang committed
142

Jonathan Tow's avatar
Jonathan Tow committed
143
144
145
146
147
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_neither, _ = rf.loglikelihood(ctx, " Neither")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_neither, ll_false
148

Jonathan Tow's avatar
Jonathan Tow committed
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
    def process_results(self, doc, results):
        gold = doc["label"]
        pred = np.argmax(results)
        return {
            "acc": pred == gold
        }

    def higher_is_better(self):
        return {
            "acc": True
        }

    def aggregation(self):
        return {
            "acc": mean
        }
Jason Phang's avatar
checkin  
Jason Phang committed
165
166


Jason Phang's avatar
Jason Phang committed
167
168
169
170
171
172
173
174
175
176
177
class MNLIMismatched(MNLI):

    def validation_docs(self):
        if self.has_validation_docs():
            return self.data["validation_mismatched"]

    def test_docs(self):
        if self.has_test_docs():
            return self.data["test_mismatched"]


Jonathan Tow's avatar
Jonathan Tow committed
178
class QNLI(HFTask):
sdtblck's avatar
sdtblck committed
179
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
180
    DATASET_NAME = "qnli"
Jason Phang's avatar
Jason Phang committed
181
182
183
184
185
186
187
188
189
190

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
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
    def doc_to_text(self, doc):
        return "question:\t{}\nresponse:\t{}\nDoes this answer the question, Yes or No?:".format(
            doc["question"],
            doc["sentence"],
        )

    def doc_to_target(self, doc):
        # True = entailment
        # False = not entailment
        return " {}".format({0: "Yes", 1: "No"}[doc["label"]])

    def construct_requests(self, doc, ctx):
        ll_yes, _ = rf.loglikelihood(ctx, " Yes")
        ll_no, _ = rf.loglikelihood(ctx, " No")
        return ll_yes, ll_no

    def process_results(self, doc, results):
        ll_yes, ll_no = results
        pred = ll_no > ll_yes
        gold = doc["label"]
        return {
            "acc": pred == gold
        }

    def higher_is_better(self):
        return {
            "acc": True
        }

    def aggregation(self):
        return {
            "acc": mean
        }


class WNLI(HFTask):
    DATASET_PATH = "glue"
    DATASET_NAME = "wnli"

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True
Jason Phang's avatar
Jason Phang committed
238

239
    def doc_to_text(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
240
        return "{}\nquestion:\t{}\tTrue, False or Neither?\nanswer:".format(
Jason Phang's avatar
Jason Phang committed
241
242
243
            doc["sentence1"],
            doc["sentence2"],
        )
244
245

    def doc_to_target(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
246
247
248
249
        # True = entailment
        # False = contradiction
        # Neither = neutral
        return " {}".format({0: "True", 1: "Neither", 2: "False"}[doc["label"]])
Jason Phang's avatar
Jason Phang committed
250

Jonathan Tow's avatar
Jonathan Tow committed
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_neither, _ = rf.loglikelihood(ctx, " Neither")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_neither, ll_false

    def process_results(self, doc, results):
        gold = doc["label"]
        pred = np.argmax(results)
        return {
            "acc": pred == gold
        }

    def higher_is_better(self):
        return {
            "acc": True
        }

    def aggregation(self):
        return {
            "acc": mean
        }
273

Jason Phang's avatar
Jason Phang committed
274

sdtblck's avatar
sdtblck committed
275
class RTE(HFTask):
sdtblck's avatar
sdtblck committed
276
277
    DATASET_PATH = "glue"
    DATASET_NAME = "rte"
Jason Phang's avatar
checkin  
Jason Phang committed
278
279
280
281
282
283
284
285
286
287

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

288
289
    def doc_to_text(self, doc):
        return "{}\nquestion:\t{}\tTrue or False?\nanswer:".format(
Jason Phang's avatar
checkin  
Jason Phang committed
290
291
292
            doc["sentence1"],
            doc["sentence2"],
        )
293
294
295
296
297

    def doc_to_target(self, doc):
        # 0 = entailment
        # 1 = not_entailment
        return " {}".format({0: "True", 1: "False"}[doc["label"]])
Jason Phang's avatar
checkin  
Jason Phang committed
298

Jonathan Tow's avatar
Jonathan Tow committed
299
300
301
302
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_false
303

Jonathan Tow's avatar
Jonathan Tow committed
304
305
306
307
308
309
310
311
312
313
314
315
    def process_results(self, doc, results):
        ll_true, ll_false = results
        pred = ll_false > ll_true
        gold = doc["label"]
        return {
            "acc": pred == gold
        }

    def higher_is_better(self):
        return {
            "acc": True
        }
Jason Phang's avatar
Jason Phang committed
316

Jonathan Tow's avatar
Jonathan Tow committed
317
318
319
320
    def aggregation(self):
        return {
            "acc": mean
        }
Jason Phang's avatar
Jason Phang committed
321

Jonathan Tow's avatar
Jonathan Tow committed
322
323
324
325
326

# Similarity and Paraphrase Tasks


class MRPC(HFTask):
sdtblck's avatar
sdtblck committed
327
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
328
    DATASET_NAME = "mrpc"
Jason Phang's avatar
Jason Phang committed
329
330
331
332
333
334
335
336
337
338

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
339
340
341
    def fewshot_description(self):
        return "Indicate if both sentences mean the same thing."

342
    def doc_to_text(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
343
344
345
        return "sentence 1:\t{}\nsentence 2:\t{}\nanswer:".format(
            doc["sentence1"],
            doc["sentence2"],
Jason Phang's avatar
Jason Phang committed
346
        )
347
348

    def doc_to_target(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
349
        return " {}".format(yesno(doc["label"]))
Jason Phang's avatar
Jason Phang committed
350

Jonathan Tow's avatar
Jonathan Tow committed
351
352
353
354
    def construct_requests(self, doc, ctx):
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
        return ll_yes, ll_no
355

Jonathan Tow's avatar
Jonathan Tow committed
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
    def process_results(self, doc, results):
        ll_yes, ll_no = results
        gold = doc["label"]
        pred = ll_yes > ll_no
        return {
            "acc": pred == gold,
            "f1": (gold, pred),
        }

    def higher_is_better(self):
        return {
            "acc": True,
            "f1": True
        }

    def aggregation(self):
        return {
            "acc": mean,
            "f1": f1_score
        }
Jason Phang's avatar
Jason Phang committed
376
377


sdtblck's avatar
sdtblck committed
378
class QQP(HFTask):
sdtblck's avatar
sdtblck committed
379
380
    DATASET_PATH = "glue"
    DATASET_NAME = "qqp"
Jason Phang's avatar
Jason Phang committed
381
382
383
384
385
386
387
388
389
390
391

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def fewshot_description(self):
Jason Phang's avatar
Jason Phang committed
392
        return "Indicate if both questions ask the same thing."
Jason Phang's avatar
Jason Phang committed
393

394
395
    def doc_to_text(self, doc):
        return "question 1:\t{}\nquestion 2:\t{}\nanswer:".format(
Jason Phang's avatar
Jason Phang committed
396
397
398
            doc["question1"],
            doc["question2"],
        )
399
400
401

    def doc_to_target(self, doc):
        return " {}".format(yesno(doc["label"]))
Jason Phang's avatar
Jason Phang committed
402

Jonathan Tow's avatar
Jonathan Tow committed
403
404
405
406
    def construct_requests(self, doc, ctx):
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
        return ll_yes, ll_no
407

Jonathan Tow's avatar
Jonathan Tow committed
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
    def process_results(self, doc, results):
        ll_yes, ll_no = results
        gold = doc["label"]
        pred = ll_yes > ll_no
        return {
            "acc": pred == gold,
            "f1": (gold, pred),
        }

    def higher_is_better(self):
        return {
            "acc": True,
            "f1": True
        }

    def aggregation(self):
        return {
            "acc": mean,
            "f1": f1_score
        }
Jason Phang's avatar
Jason Phang committed
428
429


sdtblck's avatar
sdtblck committed
430
class STSB(HFTask):
sdtblck's avatar
sdtblck committed
431
432
    DATASET_PATH = "glue"
    DATASET_NAME = "stsb"
Jason Phang's avatar
Jason Phang committed
433
434
435
436
437
438
439
440
441
442
443
444
445
446

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

    def fewshot_description(self):
        return "Indicate if both sentences mean the same thing from a scale of 0-5, " \
           "where 5 means identical and 0 means unrelated."

447
448
    def doc_to_text(self, doc):
        return "sentence 1:\t{}\nsentence 2:\t{}\nanswer:".format(
Jason Phang's avatar
Jason Phang committed
449
450
451
            doc["sentence1"],
            doc["sentence2"],
        )
452
453
454

    def doc_to_target(self, doc):
        return " {}".format(doc["label"])
Jason Phang's avatar
Jason Phang committed
455
456

    def evaluate(self, docs, lm, provide_description, num_fewshot):
457
458
459
460
461
        # TODO: Implement evaluation code using new framework

        # ***IMPORTANT***: this evaluation function needs to be rewritten for the new framework. 
        # For more info, check out the interface in base.py and the example BoolQ implementation in superglue.py. 
        # Remove this comment when the evaluation code is implemented.
Jason Phang's avatar
checkin  
Jason Phang committed
462
463
        golds = [doc["label"] for doc in docs]
        preds = []
Jason Phang's avatar
Jason Phang committed
464
465
466
467
468
        for doc in tqdm_lib.tqdm(docs):
            ctx = self.fewshot_context(
                doc=doc,
                provide_description=provide_description,
                num_fewshot=num_fewshot,
Jason Phang's avatar
checkin  
Jason Phang committed
469
            )
Jason Phang's avatar
Jason Phang committed
470
471
472
473
            output = lm.generate(context=ctx, max_gen_length=5).strip()
            first_element = output.split()[0]
            if first_element.isnumeric():
                pred = max(min(float(first_element), 5.0), 0.0)
Jason Phang's avatar
checkin  
Jason Phang committed
474
            else:
Jason Phang's avatar
Jason Phang committed
475
                pred = 2.5
Jason Phang's avatar
Jason Phang committed
476
            import pdb; pdb.set_trace()
Jason Phang's avatar
Jason Phang committed
477
478
479
480
481
482
483
484
485
486
487
488
489
            preds.append(pred)
        pearson_corr = float(pearsonr(preds, golds)[0])
        spearman_corr = float(spearmanr(preds, golds)[0])
        minor = {
            "pearson": pearson_corr,
            "spearmanr": spearman_corr,
            "corr": (pearson_corr + spearman_corr) / 2,
        }
        return {
            "major": minor["corr"],
            "minor": minor,
            "higher_is_better": True,
        }