glue.py 15.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
"""
GLUE: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding
https://openreview.net/pdf?id=rJ4km2R5t7

The General Language Understanding Evaluation (GLUE) benchmark is a collection of
resources for training, evaluating, and analyzing natural language understanding
systems. GLUE consists of:
- A benchmark of nine sentence- or sentence-pair language understanding tasks built
on established existing datasets and selected to cover a diverse range of dataset
sizes, text genres, and degrees of difficulty, and
- A diagnostic dataset designed to evaluate and analyze model performance with
respect to a wide range of linguistic phenomena found in natural language.

Homepage: https://gluebenchmark.com/
15
16
17
18
19
20
21
"""
import numpy as np
from lm_eval.base import rf
from ..metrics import mean, matthews_corrcoef, f1_score
from . common import HFTask, yesno
from ..utils import general_detokenize

22

23
24
# TODO(jon-tow): Add citations for the individual datasets/tasks that make up GLUE.
_CITATION = """
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
@inproceedings{wang-etal-2018-glue,
    title = "{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding",
    author = "Wang, Alex  and
      Singh, Amanpreet  and
      Michael, Julian  and
      Hill, Felix  and
      Levy, Omer  and
      Bowman, Samuel",
    booktitle = "Proceedings of the 2018 {EMNLP} Workshop {B}lackbox{NLP}: Analyzing and Interpreting Neural Networks for {NLP}",
    month = nov,
    year = "2018",
    address = "Brussels, Belgium",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/W18-5446",
    doi = "10.18653/v1/W18-5446",
    pages = "353--355",
    abstract = "Human ability to understand language is \textit{general, flexible, and robust}. In contrast, most NLU models above the word level are designed for a specific task and struggle with out-of-domain data. If we aspire to develop models with understanding beyond the detection of superficial correspondences between inputs and outputs, then it is critical to develop a unified model that can execute a range of linguistic tasks across different domains. To facilitate research in this direction, we present the General Language Understanding Evaluation (GLUE, gluebenchmark.com): a benchmark of nine diverse NLU tasks, an auxiliary dataset for probing models for understanding of specific linguistic phenomena, and an online platform for evaluating and comparing models. For some benchmark tasks, training data is plentiful, but for others it is limited or does not match the genre of the test set. GLUE thus favors models that can represent linguistic knowledge in a way that facilitates sample-efficient learning and effective knowledge-transfer across tasks. While none of the datasets in GLUE were created from scratch for the benchmark, four of them feature privately-held test data, which is used to ensure that the benchmark is used fairly. We evaluate baselines that use ELMo (Peters et al., 2018), a powerful transfer learning technique, as well as state-of-the-art sentence representation models. The best models still achieve fairly low absolute scores. Analysis with our diagnostic dataset yields similarly weak performance over all phenomena tested, with some exceptions.",
}
"""
44

Jonathan Tow's avatar
Jonathan Tow committed
45
46

# Single-Sentence Tasks
Jason Phang's avatar
Jason Phang committed
47
48


sdtblck's avatar
sdtblck committed
49
class CoLA(HFTask):
Leo Gao's avatar
Leo Gao committed
50
    VERSION = 0
sdtblck's avatar
sdtblck committed
51
52
    DATASET_PATH = "glue"
    DATASET_NAME = "cola"
Jonathan Tow's avatar
Jonathan Tow committed
53

Jason Phang's avatar
checkin  
Jason Phang committed
54
55
56
57
58
59
60
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
61
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
62

63
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
64
        return "{}\nQuestion: Does this sentence make sense?\nAnswer:".format(doc["sentence"])
65
66

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

Jonathan Tow's avatar
Jonathan Tow committed
69
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Leo Gao committed
70
71
        ll_true, _ = rf.loglikelihood(ctx, " yes")
        ll_false, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
72
        return ll_true, ll_false
73

Jonathan Tow's avatar
Jonathan Tow committed
74
75
76
77
78
79
80
    def process_results(self, doc, results):
        ll_true, ll_false = results
        pred = ll_true > ll_false
        gold = doc["label"]
        return {
            "mcc": (gold, pred)
        }
81

Jonathan Tow's avatar
Jonathan Tow committed
82
    def higher_is_better(self):
Jason Phang's avatar
checkin  
Jason Phang committed
83
        return {
Jonathan Tow's avatar
Jonathan Tow committed
84
85
86
87
88
89
90
91
92
93
            "mcc": True
        }

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


class SST(HFTask):
Leo Gao's avatar
Leo Gao committed
94
    VERSION = 0
Jonathan Tow's avatar
Jonathan Tow committed
95
96
97
98
99
100
101
102
103
104
    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):
105
        return False
Jonathan Tow's avatar
Jonathan Tow committed
106
107

    def doc_to_text(self, doc):
Leo Gao's avatar
Fix  
Leo Gao committed
108
        return "{}\nQuestion: Is this sentence positive or negative?\nAnswer:".format(
Leo Gao's avatar
Leo Gao committed
109
            general_detokenize(doc["sentence"]),
Jonathan Tow's avatar
Jonathan Tow committed
110
111
112
        )

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

    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
116
117
        ll_positive, _ = rf.loglikelihood(ctx, " positive")
        ll_negative, _ = rf.loglikelihood(ctx, " negative")
Jonathan Tow's avatar
Jonathan Tow committed
118
119
120
121
122
123
124
125
126
127
128
129
130
        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
131
132
        }

Jonathan Tow's avatar
Jonathan Tow committed
133
134
135
136
137
138
139
140
    def aggregation(self):
        return {
            "acc": mean
        }


# Inference Tasks

Jason Phang's avatar
checkin  
Jason Phang committed
141

sdtblck's avatar
sdtblck committed
142
class MNLI(HFTask):
Leo Gao's avatar
Leo Gao committed
143
    VERSION = 0
sdtblck's avatar
sdtblck committed
144
145
    DATASET_PATH = "glue"
    DATASET_NAME = "mnli"
Jason Phang's avatar
Jason Phang committed
146

Jason Phang's avatar
checkin  
Jason Phang committed
147
148
149
150
151
152
153
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
154
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
155
156
157

    def validation_docs(self):
        if self.has_validation_docs():
sdtblck's avatar
sdtblck committed
158
            return self.data["validation_matched"]
Jason Phang's avatar
checkin  
Jason Phang committed
159
160
161

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

164
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
165
        return "{}\nQuestion: {} True, False or Neither?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
166
            doc["premise"],
Leo Gao's avatar
Fix  
Leo Gao committed
167
            doc["hypothesis"].strip() + ('' if doc["hypothesis"].strip().endswith('.') else '.'),
Jason Phang's avatar
checkin  
Jason Phang committed
168
        )
169
170
171
172
173
174

    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
175

Jonathan Tow's avatar
Jonathan Tow committed
176
177
178
179
180
    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
181

Jonathan Tow's avatar
Jonathan Tow committed
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
    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
198
199


Jason Phang's avatar
Jason Phang committed
200
class MNLIMismatched(MNLI):
Leo Gao's avatar
Leo Gao committed
201
    VERSION = 0
Jason Phang's avatar
Jason Phang committed
202
203
204
205
206
207
208
209
210
211

    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
212
class QNLI(HFTask):
Leo Gao's avatar
Leo Gao committed
213
    VERSION = 0
sdtblck's avatar
sdtblck committed
214
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
215
    DATASET_NAME = "qnli"
Jason Phang's avatar
Jason Phang committed
216
217
218
219
220
221
222
223

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
224
        return False
Jason Phang's avatar
Jason Phang committed
225

Jonathan Tow's avatar
Jonathan Tow committed
226
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
227
        return "{}\n{}\nQuestion: Does this response answer the question?\nAnswer:".format(
Jonathan Tow's avatar
Jonathan Tow committed
228
229
230
231
232
233
234
            doc["question"],
            doc["sentence"],
        )

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

    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
238
239
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
        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):
thomasw21's avatar
thomasw21 committed
262
    VERSION = 1
Jonathan Tow's avatar
Jonathan Tow committed
263
264
265
266
267
268
269
270
271
272
    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):
273
        return False
Jason Phang's avatar
Jason Phang committed
274

275
    def doc_to_text(self, doc):
thomasw21's avatar
thomasw21 committed
276
        return "{}\nQuestion: {} True or False?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
277
278
279
            doc["sentence1"],
            doc["sentence2"],
        )
280
281

    def doc_to_target(self, doc):
Jonathan Tow's avatar
Jonathan Tow committed
282
        # True = entailment
thomasw21's avatar
thomasw21 committed
283
284
        # False = not_entailment
        return " {}".format({0: "False", 1: "True"}[doc["label"]])
Jason Phang's avatar
Jason Phang committed
285

Jonathan Tow's avatar
Jonathan Tow committed
286
287
288
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_false, _ = rf.loglikelihood(ctx, " False")
thomasw21's avatar
thomasw21 committed
289
        return ll_true, ll_false
Jonathan Tow's avatar
Jonathan Tow committed
290
291

    def process_results(self, doc, results):
thomasw21's avatar
thomasw21 committed
292
293
        ll_true, ll_false = results
        pred = ll_true > ll_false
Jonathan Tow's avatar
Jonathan Tow committed
294
295
296
297
298
299
300
301
302
303
304
305
306
307
        gold = doc["label"]
        return {
            "acc": pred == gold
        }

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

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

Jason Phang's avatar
Jason Phang committed
309

sdtblck's avatar
sdtblck committed
310
class RTE(HFTask):
Leo Gao's avatar
Leo Gao committed
311
    VERSION = 0
sdtblck's avatar
sdtblck committed
312
313
    DATASET_PATH = "glue"
    DATASET_NAME = "rte"
Jason Phang's avatar
checkin  
Jason Phang committed
314
315
316
317
318
319
320
321

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
322
        return False
Jason Phang's avatar
checkin  
Jason Phang committed
323

324
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
325
        return "{}\nQuestion: {} True or False?\nAnswer:".format(
Jason Phang's avatar
checkin  
Jason Phang committed
326
327
328
            doc["sentence1"],
            doc["sentence2"],
        )
329
330
331
332
333

    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
334

Jonathan Tow's avatar
Jonathan Tow committed
335
336
337
338
    def construct_requests(self, doc, ctx):
        ll_true, _ = rf.loglikelihood(ctx, " True")
        ll_false, _ = rf.loglikelihood(ctx, " False")
        return ll_true, ll_false
339

Jonathan Tow's avatar
Jonathan Tow committed
340
341
342
343
344
345
346
347
348
349
350
351
    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
352

Jonathan Tow's avatar
Jonathan Tow committed
353
354
355
356
    def aggregation(self):
        return {
            "acc": mean
        }
Jason Phang's avatar
Jason Phang committed
357

Jonathan Tow's avatar
Jonathan Tow committed
358
359
360
361
362

# Similarity and Paraphrase Tasks


class MRPC(HFTask):
Leo Gao's avatar
Leo Gao committed
363
    VERSION = 0
sdtblck's avatar
sdtblck committed
364
    DATASET_PATH = "glue"
Jonathan Tow's avatar
Jonathan Tow committed
365
    DATASET_NAME = "mrpc"
Jason Phang's avatar
Jason Phang committed
366
367
368
369
370
371
372
373

    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
374
        return False
Jason Phang's avatar
Jason Phang committed
375

376
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
377
378
379
        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
380
        )
381
382

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

Jonathan Tow's avatar
Jonathan Tow committed
385
    def construct_requests(self, doc, ctx):
Leo Gao's avatar
Fix  
Leo Gao committed
386
387
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
Jonathan Tow's avatar
Jonathan Tow committed
388
        return ll_yes, ll_no
389

Jonathan Tow's avatar
Jonathan Tow committed
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
    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
410
411


sdtblck's avatar
sdtblck committed
412
class QQP(HFTask):
Leo Gao's avatar
Leo Gao committed
413
    VERSION = 0
sdtblck's avatar
sdtblck committed
414
415
    DATASET_PATH = "glue"
    DATASET_NAME = "qqp"
Jason Phang's avatar
Jason Phang committed
416
417
418
419
420
421
422
423

    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
424
        return False
Jason Phang's avatar
Jason Phang committed
425

426
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
427
        return "Question 1: {}\nQuestion 2: {}\nQuestion: Do both questions ask the same thing?\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
428
429
430
            doc["question1"],
            doc["question2"],
        )
431
432
433

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

Jonathan Tow's avatar
Jonathan Tow committed
435
436
437
438
    def construct_requests(self, doc, ctx):
        ll_yes, _ = rf.loglikelihood(ctx, " yes")
        ll_no, _ = rf.loglikelihood(ctx, " no")
        return ll_yes, ll_no
439

Jonathan Tow's avatar
Jonathan Tow committed
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
    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
460
461


sdtblck's avatar
sdtblck committed
462
class STSB(HFTask):
Leo Gao's avatar
Leo Gao committed
463
    VERSION = 0
sdtblck's avatar
sdtblck committed
464
465
    DATASET_PATH = "glue"
    DATASET_NAME = "stsb"
Jason Phang's avatar
Jason Phang committed
466
467
468
469
470
471
472
473
474
475

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
        return True

476
    def doc_to_text(self, doc):
Leo Gao's avatar
Leo Gao committed
477
        return "sentence 1: {}\nsentence 2: {}\nAnswer:".format(
Jason Phang's avatar
Jason Phang committed
478
479
480
            doc["sentence1"],
            doc["sentence2"],
        )
481
482
483

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

Leo Gao's avatar
Leo Gao committed
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
    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')