"LICENSE" did not exist on "2f6baaee5db2641711a85f745e9e0a57a4049a1f"
winogrande.py 3.72 KB
Newer Older
Charles Foster's avatar
Charles Foster committed
1
import numpy as np
2
from . common import HFTask
&'s avatar
& committed
3
4
from lm_eval.base import rf
from ..metrics import mean
5
6
7
8
9
10
11

"""
This evaluation of Winogrande uses partial evaluation as described by
Trinh & Le in Simple Method for Commonsense Reasoning (2018).
Reference: https://arxiv.org/abs/1806.02847
"""

Charles Foster's avatar
Charles Foster committed
12
13

class Winogrande(HFTask):
Leo Gao's avatar
Leo Gao committed
14
    VERSION = 0
Charles Foster's avatar
Charles Foster committed
15
16
17
    DATASET_PATH = "winogrande"
    DATASET_NAME = "winogrande_xl"

18
19
    answer_to_num = {'1': 0, '2': 1}

Charles Foster's avatar
Charles Foster committed
20
21
22
23
24
25
26
    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
27
        return False
Charles Foster's avatar
Charles Foster committed
28

29
30
    def doc_to_text(self, doc):
        return self.partial_context(doc, doc["option" + doc["answer"]])
Charles Foster's avatar
Charles Foster committed
31

32
33
34
35
36
    def should_decontaminate(self):
        return True

    def doc_to_decontamination_query(self, doc):
        return doc["sentence"]
37
        
38
    @classmethod
39
40
    def partial_context(cls, doc, option):
        # Substitute the pronoun in the sentence with the specified option
41
42
        # and ignore everything after.
        pronoun_loc = doc["sentence"].index("_")
43
44
45
        return doc["sentence"][:pronoun_loc] + option

    def doc_to_target(self, doc):
Leo Gao's avatar
Leo Gao committed
46
        return self.partial_target(doc)
47
48
49
50
51

    @classmethod
    def partial_target(cls, doc):
        # The target is everything after the document specified pronoun.
        pronoun_loc = doc["sentence"].index("_") + 1
Leo Gao's avatar
Leo Gao committed
52
        return " " + doc["sentence"][pronoun_loc:].strip()
53

Leo Gao's avatar
Leo Gao committed
54
    def construct_requests(self, doc, ctx):
55
        """Uses RequestFactory to construct Requests and returns an iterable of
Leo Gao's avatar
Leo Gao committed
56
        Requests which will be sent to the LM.
57

Leo Gao's avatar
Leo Gao committed
58
59
60
        :param doc:
            The document as returned from training_docs, validation_docs, or test_docs.
        :param ctx: str
61
            The context string, generated by fewshot_context. This includes the natural
Leo Gao's avatar
Leo Gao committed
62
            language description, as well as the few shot examples, and the question
63
            part of the document for `doc`.
Leo Gao's avatar
Leo Gao committed
64
        """
65
        target = self.partial_target(doc)
66
        lls = []
67
68
69
        for option in [doc["option1"], doc["option2"]]:
            partial_ctx = self.partial_context(doc, option)
            full_ctx = self.append_context(ctx, partial_ctx)
70
71
            lls.append(rf.loglikelihood(full_ctx, target)[0])
        return lls
72
73
74

    @classmethod
    def append_context(cls, ctx, partial_ctx):
75
        ctx = ctx.split("\n\n")  # Each fewshot context is on its own new line.
76
77
        ctx.pop()  # Remove the correct context put in by `doc_to_text`.
        return "\n\n".join([*ctx, partial_ctx]) if ctx else partial_ctx
78

Leo Gao's avatar
Leo Gao committed
79
    def process_results(self, doc, results):
80
81
        """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
Leo Gao's avatar
Leo Gao committed
82
83
84
85
86
87
88
        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.
        """
89
        return {
90
            "acc": np.argmax(results) == self.answer_to_num[doc["answer"]]
91
        }
Leo Gao's avatar
Leo Gao committed
92
93
94
95

    def aggregation(self):
        """
        :returns: {str: [float] -> float}
96
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
97
98
            functions that aggregate a list of metrics
        """
99
100
101
        return {
            "acc": mean
        }
Leo Gao's avatar
Leo Gao committed
102
103
104
105

    def higher_is_better(self):
        """
        :returns: {str: bool}
106
            A dictionary where keys are the names of submetrics and values are
Leo Gao's avatar
Leo Gao committed
107
108
            whether a higher value of the submetric is better
        """
109
110
111
        return {
            "acc": True
        }