glue.py 12.7 KB
Newer Older
Jason Phang's avatar
checkin  
Jason Phang committed
1
import numpy as np
&'s avatar
& committed
2
3
from lm_eval.base import rf
from ..metrics import mean, matthews_corrcoef, f1_score
Jonathan Tow's avatar
Jonathan Tow committed
4
from . common import HFTask, yesno
Leo Gao's avatar
Leo Gao committed
5
from ..utils import general_detokenize
Jonathan Tow's avatar
Jonathan Tow committed
6
7

# Single-Sentence Tasks
Jason Phang's avatar
Jason Phang committed
8
9


sdtblck's avatar
sdtblck committed
10
class CoLA(HFTask):
Leo Gao's avatar
Leo Gao committed
11
    VERSION = 0
sdtblck's avatar
sdtblck committed
12
13
    DATASET_PATH = "glue"
    DATASET_NAME = "cola"
Jonathan Tow's avatar
Jonathan Tow committed
14

Jason Phang's avatar
checkin  
Jason Phang committed
15
16
17
18
19
20
21
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
22
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
23

Jason Phang's avatar
Jason Phang committed
24
    def fewshot_description(self):
Leo Gao's avatar
Leo Gao committed
25
26
        # TODO
        return ""
Jason Phang's avatar
Jason Phang committed
27

28
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
29
        return "{}\nQuestion: Does this sentence make sense?\nAnswer:".format(doc["sentence"])
30
31

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
32
        return " {}".format({1: "yes", 0: "no"}[doc["label"]])
Jason Phang's avatar
checkin  
Jason Phang committed
33

Jonathan Tow's avatar
Jonathan Tow committed
34
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
35
36
        ll_true, _ = rf.loglikelihood(ctx, " yes")
        ll_false, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
37
        return ll_true, ll_false
38

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

Jonathan Tow's avatar
Jonathan Tow committed
47
    def higher_is_better(self):
Jason Phang's avatar
checkin  
Jason Phang committed
48
        return {
Jonathan Tow's avatar
Jonathan Tow committed
49
50
51
52
53
54
55
56
57
58
            "mcc": True
        }

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


class SST(HFTask):
Leo Gao's avatar
Leo Gao committed
59
    VERSION = 0
Jonathan Tow's avatar
Jonathan Tow committed
60
61
62
63
64
65
66
67
68
69
    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):
70
        return False
Jonathan Tow's avatar
Jonathan Tow committed
71
72

    def fewshot_description(self):
Leo Gao's avatar
Fix  
Leo Gao committed
73
        return "Indicate if the sentiment of each sentence is positive or negative."
Jonathan Tow's avatar
Jonathan Tow committed
74
75

    def doc_to_text(self, doc):
Leo Gao's avatar
Fix  
Leo Gao committed
76
        return "{}\nQuestion: Is this sentence positive or negative?\nAnswer:".format(
Leo Gao's avatar
Leo Gao committed
77
            general_detokenize(doc["sentence"]),
Jonathan Tow's avatar
Jonathan Tow committed
78
79
80
        )

    def doc_to_target(self, doc):
Leo Gao's avatar
Fix  
Leo Gao committed
81
        return " {}".format({1: "positive", 0: "negative"}[doc["label"]])
Jonathan Tow's avatar
Jonathan Tow committed
82
83

    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
84
85
        ll_positive, _ = rf.loglikelihood(ctx, " positive")
        ll_negative, _ = rf.loglikelihood(ctx, " negative")
Jonathan Tow's avatar
Jonathan Tow committed
86
87
88
89
90
91
92
93
94
95
96
97
98
        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):
Leo Gao's avatar
Leo Gao committed
111
    VERSION = 0
sdtblck's avatar
sdtblck committed
112
113
    DATASET_PATH = "glue"
    DATASET_NAME = "mnli"
Jason Phang's avatar
Jason Phang committed
114

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

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
122
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
123
124
125

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

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

132
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
133
        return "{}\nQuestion: {} True, False or Neither?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
134
            doc["premise"],
Leo Gao's avatar
Fix  
Leo Gao committed
135
            doc["hypothesis"].strip() + ('' if doc["hypothesis"].strip().endswith('.') else '.'),
Jason Phang's avatar
checkin  
Jason Phang committed
136
        )
137
138
139
140
141
142

    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
143

Jonathan Tow's avatar
Jonathan Tow committed
144
145
146
147
148
    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
149

Jonathan Tow's avatar
Jonathan Tow committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
    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
166
167


Jason Phang's avatar
Jason Phang committed
168
class MNLIMismatched(MNLI):
Leo Gao's avatar
Leo Gao committed
169
    VERSION = 0
Jason Phang's avatar
Jason Phang committed
170
171
172
173
174
175
176
177
178
179

    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
180
class QNLI(HFTask):
Leo Gao's avatar
Leo Gao committed
181
    VERSION = 0
sdtblck's avatar
sdtblck committed
182
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
183
    DATASET_NAME = "qnli"
Jason Phang's avatar
Jason Phang committed
184
185
186
187
188
189
190
191

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
192
        return False
Jason Phang's avatar
Jason Phang committed
193

Jonathan Tow's avatar
Jonathan Tow committed
194
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
195
        return "{}\n{}\nQuestion: Does this response answer the question?\nAnswer:".format(
Jonathan Tow's avatar
Jonathan Tow committed
196
197
198
199
200
201
202
            doc["question"],
            doc["sentence"],
        )

    def doc_to_target(self, doc):
        # True = entailment
        # False = not entailment
Leo Gao's avatar
Fix  
Leo Gao committed
203
        return " {}".format({0: "yes", 1: "no"}[doc["label"]])
Jonathan Tow's avatar
Jonathan Tow committed
204
205

    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
206
207
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
        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):
Leo Gao's avatar
Leo Gao committed
230
    VERSION = 0
Jonathan Tow's avatar
Jonathan Tow committed
231
232
233
234
235
236
237
238
239
240
    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):
241
        return False
Jason Phang's avatar
Jason Phang committed
242

243
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
244
        return "{}\nQuestion: {} True, False or Neither?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
245
246
247
            doc["sentence1"],
            doc["sentence2"],
        )
248
249

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

Jonathan Tow's avatar
Jonathan Tow committed
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    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
        }
277

Jason Phang's avatar
Jason Phang committed
278

sdtblck's avatar
sdtblck committed
279
class RTE(HFTask):
Leo Gao's avatar
Leo Gao committed
280
    VERSION = 0
sdtblck's avatar
sdtblck committed
281
282
    DATASET_PATH = "glue"
    DATASET_NAME = "rte"
Jason Phang's avatar
checkin  
Jason Phang committed
283
284
285
286
287
288
289
290

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
291
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
292

293
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
294
        return "{}\nQuestion: {} True or False?\nAnswer:".format(
Jason Phang's avatar
checkin  
Jason Phang committed
295
296
297
            doc["sentence1"],
            doc["sentence2"],
        )
298
299
300
301
302

    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
303

Jonathan Tow's avatar
Jonathan Tow committed
304
305
306
307
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_false
308

Jonathan Tow's avatar
Jonathan Tow committed
309
310
311
312
313
314
315
316
317
318
319
320
    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
321

Jonathan Tow's avatar
Jonathan Tow committed
322
323
324
325
    def aggregation(self):
        return {
            "acc": mean
        }
Jason Phang's avatar
Jason Phang committed
326

Jonathan Tow's avatar
Jonathan Tow committed
327
328
329
330
331

# Similarity and Paraphrase Tasks


class MRPC(HFTask):
Leo Gao's avatar
Leo Gao committed
332
    VERSION = 0
sdtblck's avatar
sdtblck committed
333
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
334
    DATASET_NAME = "mrpc"
Jason Phang's avatar
Jason Phang committed
335
336
337
338
339
340
341
342

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
343
        return False
Jason Phang's avatar
Jason Phang committed
344

Jonathan Tow's avatar
Jonathan Tow committed
345
346
347
    def fewshot_description(self):
        return "Indicate if both sentences mean the same thing."

348
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
349
350
351
        return "Sentence 1: {}\nSentence 2: {}\nQuestion: Do both sentences mean the same thing?\nAnswer:".format(
            general_detokenize(doc["sentence1"]),
            general_detokenize(doc["sentence2"]),
Jason Phang's avatar
Jason Phang committed
352
        )
353
354

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

Jonathan Tow's avatar
Jonathan Tow committed
357
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
358
359
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
360
        return ll_yes, ll_no
361

Jonathan Tow's avatar
Jonathan Tow committed
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
    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
382
383


sdtblck's avatar
sdtblck committed
384
class QQP(HFTask):
Leo Gao's avatar
Leo Gao committed
385
    VERSION = 0
sdtblck's avatar
sdtblck committed
386
387
    DATASET_PATH = "glue"
    DATASET_NAME = "qqp"
Jason Phang's avatar
Jason Phang committed
388
389
390
391
392
393
394
395

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Leo Gao's avatar
Leo Gao committed
396
        return False
Jason Phang's avatar
Jason Phang committed
397
398

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

401
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
402
        return "Question 1: {}\nQuestion 2: {}\nQuestion: Do both questions ask the same thing?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
403
404
405
            doc["question1"],
            doc["question2"],
        )
406
407
408

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

Jonathan Tow's avatar
Jonathan Tow committed
410
411
412
413
    def construct_requests(self, doc, ctx):
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
        return ll_yes, ll_no
414

Jonathan Tow's avatar
Jonathan Tow committed
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
    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
435
436


sdtblck's avatar
sdtblck committed
437
class STSB(HFTask):
Leo Gao's avatar
Leo Gao committed
438
    VERSION = 0
sdtblck's avatar
sdtblck committed
439
440
    DATASET_PATH = "glue"
    DATASET_NAME = "stsb"
Jason Phang's avatar
Jason Phang committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454

    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."

455
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
456
        return "sentence 1: {}\nsentence 2: {}\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
457
458
459
            doc["sentence1"],
            doc["sentence2"],
        )
460
461
462

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

Leo Gao's avatar
Leo Gao committed
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
    def construct_requests(self, doc, ctx):
        """ Uses RequestFactory to construct Requests and returns an iterable of 
        Requests which will be sent to the LM.

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
            The context string, generated by fewshot_context. This includes the natural 
            language description, as well as the few shot examples, and the question
            part of the document for `doc`. 
        """
        # TODO: implement evaluation.
        raise NotImplementedError('Evaluation not implemented')
    
    def process_results(self, doc, results):
        """Take a single document and the LM results and evaluates, returning a 
        dict where keys are the names of submetrics and values are the values of 
        the metric for that one document

        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param results:
            The results of the requests created in construct_requests.
        """
        # TODO: implement evaluation.
        raise NotImplementedError('Evaluation not implemented')

    def aggregation(self):
        """
        :returns: {str: [float] -> float}
            A dictionary where keys are the names of submetrics and values are 
            functions that aggregate a list of metrics
        """
        # TODO: implement evaluation.
        raise NotImplementedError('Evaluation not implemented')

    def higher_is_better(self):
        """
        :returns: {str: bool}
            A dictionary where keys are the names of submetrics and values are 
            whether a higher value of the submetric is better
        """
        # TODO: implement evaluation.
        raise NotImplementedError('Evaluation not implemented')